- Fragment navigation library added

- on back pressed fixed
This commit is contained in:
2023-09-10 18:19:26 +01:00
parent cb3cf4014d
commit 3a3a14e95f
5 changed files with 82 additions and 94 deletions

1
.gitignore vendored
View File

@@ -89,6 +89,7 @@ gen-external-apklibs
.idea/assetWizardSettings.xml .idea/assetWizardSettings.xml
.idea/gradle.xml .idea/gradle.xml
.idea/jarRepositories.xml .idea/jarRepositories.xml
.idea/navEditor.xml
# Gem/fastlane # Gem/fastlane
Gemfile.lock Gemfile.lock

View File

@@ -1,6 +1,7 @@
package com.appttude.h_mal.farmr.ui package com.appttude.h_mal.farmr.ui
import android.os.Bundle import android.os.Bundle
import android.util.Log
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Button import android.widget.Button
@@ -10,21 +11,19 @@ import android.widget.RadioButton
import android.widget.RadioGroup import android.widget.RadioGroup
import android.widget.ScrollView import android.widget.ScrollView
import android.widget.TextView import android.widget.TextView
import androidx.activity.OnBackPressedCallback
import androidx.core.widget.doAfterTextChanged import androidx.core.widget.doAfterTextChanged
import com.appttude.h_mal.farmr.R import com.appttude.h_mal.farmr.R
import com.appttude.h_mal.farmr.base.BackPressedListener
import com.appttude.h_mal.farmr.base.FormFragment import com.appttude.h_mal.farmr.base.FormFragment
import com.appttude.h_mal.farmr.model.ShiftType import com.appttude.h_mal.farmr.model.ShiftType
import com.appttude.h_mal.farmr.model.Success import com.appttude.h_mal.farmr.model.Success
import com.appttude.h_mal.farmr.utils.ID import com.appttude.h_mal.farmr.utils.ID
import com.appttude.h_mal.farmr.utils.SHIFT_ID
import com.appttude.h_mal.farmr.utils.createDialog import com.appttude.h_mal.farmr.utils.createDialog
import com.appttude.h_mal.farmr.utils.displayToast import com.appttude.h_mal.farmr.utils.displayToast
import com.appttude.h_mal.farmr.utils.formatAsCurrencyString import com.appttude.h_mal.farmr.utils.formatAsCurrencyString
import com.appttude.h_mal.farmr.utils.formatToTwoDpString import com.appttude.h_mal.farmr.utils.formatToTwoDpString
import com.appttude.h_mal.farmr.utils.goBack import com.appttude.h_mal.farmr.utils.goBack
import com.appttude.h_mal.farmr.utils.hide import com.appttude.h_mal.farmr.utils.hide
import com.appttude.h_mal.farmr.utils.popBackStack
import com.appttude.h_mal.farmr.utils.setDatePicker import com.appttude.h_mal.farmr.utils.setDatePicker
import com.appttude.h_mal.farmr.utils.setTimePicker import com.appttude.h_mal.farmr.utils.setTimePicker
import com.appttude.h_mal.farmr.utils.show import com.appttude.h_mal.farmr.utils.show
@@ -32,7 +31,9 @@ import com.appttude.h_mal.farmr.utils.validateField
import com.appttude.h_mal.farmr.viewmodel.SubmissionViewModel import com.appttude.h_mal.farmr.viewmodel.SubmissionViewModel
class FragmentAddItem : FormFragment<SubmissionViewModel>(R.layout.fragment_add_item), class FragmentAddItem : FormFragment<SubmissionViewModel>(R.layout.fragment_add_item),
RadioGroup.OnCheckedChangeListener, BackPressedListener { RadioGroup.OnCheckedChangeListener {
private lateinit var onBackPressed: OnBackPressedCallback
private lateinit var mHourlyRadioButton: RadioButton private lateinit var mHourlyRadioButton: RadioButton
private lateinit var mPieceRadioButton: RadioButton private lateinit var mPieceRadioButton: RadioButton
@@ -123,6 +124,13 @@ class FragmentAddItem : FormFragment<SubmissionViewModel>(R.layout.fragment_add_
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setHasOptionsMenu(false) setHasOptionsMenu(false)
// This callback is only called when MyFragment is at least started
onBackPressed = object : OnBackPressedCallback(false) {
override fun handleOnBackPressed() {
onBackPressed()
}
}
requireActivity().onBackPressedDispatcher.addCallback(this, onBackPressed)
} }
override fun onResume() { override fun onResume() {
@@ -132,56 +140,65 @@ class FragmentAddItem : FormFragment<SubmissionViewModel>(R.layout.fragment_add_
else -> getString(R.string.add_item_title) else -> getString(R.string.add_item_title)
} }
setTitle(title) setTitle(title)
onBackPressed.isEnabled = true
}
override fun onPause() {
super.onPause()
onBackPressed.isEnabled = false
} }
private fun setupViewAfterViewCreated() { private fun setupViewAfterViewCreated() {
val id = arguments?.takeIf { it.containsKey(SHIFT_ID) } val id = try {
?.let { FragmentAddItemArgs.fromBundle(it).shiftId } FragmentAddItemArgs.fromBundle(requireArguments()).shiftId
} catch (e: Exception) {
Log.i("Nav Args", "Failed to retrieve args from navigation")
null
}
wholeView.hide() wholeView.hide()
if (id != null) { // Since we are editing a shift lets load the shift data into the views
// Since we are editing a shift lets load the shift data into the views id?.let { viewModel.getCurrentShift(id) }?.run {
viewModel.getCurrentShift(arguments!!.getLong(ID))?.run { mLocationEditText.setText(description)
mLocationEditText.setText(description) mDateEditText.setText(date)
mDateEditText.setText(date)
// Set types // Set types
mType = ShiftType.getEnumByType(type) mType = ShiftType.getEnumByType(type)
mDescription = description mDescription = description
mDate = date mDate = date
mPayRate = rateOfPay mPayRate = rateOfPay
when (ShiftType.getEnumByType(type)) { when (ShiftType.getEnumByType(type)) {
ShiftType.HOURLY -> { ShiftType.HOURLY -> {
mHourlyRadioButton.isChecked = true mHourlyRadioButton.isChecked = true
mPieceRadioButton.isChecked = false mPieceRadioButton.isChecked = false
mTimeInEditText.setText(timeIn) mTimeInEditText.setText(timeIn)
mTimeOutEditText.setText(timeOut) mTimeOutEditText.setText(timeOut)
mBreakEditText.setText(breakMins.toString()) mBreakEditText.setText(breakMins.toString())
val durationText = "${duration.formatToTwoDpString()} Hours" val durationText = "${duration.formatToTwoDpString()} Hours"
mDurationTextView.text = durationText mDurationTextView.text = durationText
// Set fields // Set fields
mTimeIn = timeIn mTimeIn = timeIn
mTimeOut = timeOut mTimeOut = timeOut
mBreaks = breakMins mBreaks = breakMins
}
ShiftType.PIECE -> {
mHourlyRadioButton.isChecked = false
mPieceRadioButton.isChecked = true
mUnitEditText.setText(units.formatToTwoDpString())
// Set piece rate units
mUnits = units
}
} }
mPayRateEditText.setText(rateOfPay.formatAsCurrencyString())
mTotalPayTextView.text = totalPay.formatAsCurrencyString()
calculateTotalPay() ShiftType.PIECE -> {
mHourlyRadioButton.isChecked = false
mPieceRadioButton.isChecked = true
mUnitEditText.setText(units.formatToTwoDpString())
// Set piece rate units
mUnits = units
}
} }
mPayRateEditText.setText(rateOfPay.formatAsCurrencyString())
mTotalPayTextView.text = totalPay.formatAsCurrencyString()
calculateTotalPay()
} }
applyFormListener(view = view as ViewGroup) applyFormListener(view = view as ViewGroup)
@@ -286,7 +303,7 @@ class FragmentAddItem : FormFragment<SubmissionViewModel>(R.layout.fragment_add_
} }
} }
override fun onBackPressed(): Boolean { fun onBackPressed() {
if (didFormChange()) { if (didFormChange()) {
requireContext().createDialog( requireContext().createDialog(
title = "Discard Changes?", title = "Discard Changes?",
@@ -299,7 +316,6 @@ class FragmentAddItem : FormFragment<SubmissionViewModel>(R.layout.fragment_add_
} else { } else {
goBack() goBack()
} }
return true
} }
override fun onSuccess(data: Any?) { override fun onSuccess(data: Any?) {

View File

@@ -7,11 +7,10 @@ import android.view.Menu
import android.view.MenuInflater import android.view.MenuInflater
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import androidx.activity.OnBackPressedCallback
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.AdapterDataObserver
import com.appttude.h_mal.farmr.R import com.appttude.h_mal.farmr.R
import com.appttude.h_mal.farmr.base.BackPressedListener
import com.appttude.h_mal.farmr.base.BaseFragment import com.appttude.h_mal.farmr.base.BaseFragment
import com.appttude.h_mal.farmr.data.legacydb.ShiftObject import com.appttude.h_mal.farmr.data.legacydb.ShiftObject
import com.appttude.h_mal.farmr.model.Order import com.appttude.h_mal.farmr.model.Order
@@ -19,30 +18,43 @@ import com.appttude.h_mal.farmr.model.Sortable
import com.appttude.h_mal.farmr.model.Success import com.appttude.h_mal.farmr.model.Success
import com.appttude.h_mal.farmr.utils.createDialog import com.appttude.h_mal.farmr.utils.createDialog
import com.appttude.h_mal.farmr.utils.displayToast import com.appttude.h_mal.farmr.utils.displayToast
import com.appttude.h_mal.farmr.utils.hide
import com.appttude.h_mal.farmr.utils.navigateTo import com.appttude.h_mal.farmr.utils.navigateTo
import com.appttude.h_mal.farmr.utils.navigateToFragment
import com.appttude.h_mal.farmr.utils.show
import com.appttude.h_mal.farmr.viewmodel.MainViewModel import com.appttude.h_mal.farmr.viewmodel.MainViewModel
import com.google.android.material.floatingactionbutton.FloatingActionButton import com.google.android.material.floatingactionbutton.FloatingActionButton
import java.io.File import java.io.File
import kotlin.system.exitProcess import kotlin.system.exitProcess
class FragmentMain : BaseFragment<MainViewModel>(R.layout.fragment_main), BackPressedListener { class FragmentMain : BaseFragment<MainViewModel>(R.layout.fragment_main) {
private lateinit var productListView: RecyclerView private lateinit var productListView: RecyclerView
private lateinit var emptyView: View private lateinit var emptyView: View
private lateinit var mAdapter: ShiftListAdapter private lateinit var mAdapter: ShiftListAdapter
private lateinit var onBackPressed: OnBackPressedCallback
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
// Inflate the layout for this fragment // Inflate the layout for this fragment
setHasOptionsMenu(true) setHasOptionsMenu(true)
// This callback is only called when MyFragment is at least started
onBackPressed = object : OnBackPressedCallback(false) {
override fun handleOnBackPressed() {
onBackPressed()
}
}
requireActivity().onBackPressedDispatcher.addCallback(this, onBackPressed)
} }
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
setTitle("Shift List") setTitle("Shift List")
onBackPressed.isEnabled = true
}
override fun onPause() {
super.onPause()
onBackPressed.isEnabled = false
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@@ -182,21 +194,13 @@ class FragmentMain : BaseFragment<MainViewModel>(R.layout.fragment_main), BackPr
file file
) )
intent.setDataAndType(excelUri, "application/vnd.ms-excel") intent.setDataAndType(excelUri, "application/vnd.ms-excel")
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivity(intent) startActivity(intent)
} }
} }
private fun exportDialog() { fun onBackPressed() {
AlertDialog.Builder(context)
.setTitle("Export?")
.setMessage("Exporting current filtered data. Continue?")
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(android.R.string.ok) { _, _ -> exportData() }.create().show()
}
override fun onBackPressed(): Boolean {
requireContext().createDialog( requireContext().createDialog(
title = "Leave?", title = "Leave?",
message = "Are you sure you want to exit Farmr?", message = "Are you sure you want to exit Farmr?",
@@ -210,6 +214,5 @@ class FragmentMain : BaseFragment<MainViewModel>(R.layout.fragment_main), BackPr
exitProcess(0) exitProcess(0)
} }
) )
return true
} }
} }

View File

@@ -1,16 +1,11 @@
package com.appttude.h_mal.farmr.ui package com.appttude.h_mal.farmr.ui
import android.os.Bundle import android.os.Bundle
import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import androidx.appcompat.widget.Toolbar import androidx.appcompat.widget.Toolbar
import androidx.core.app.ActivityCompat
import androidx.navigation.fragment.NavHostFragment import androidx.navigation.fragment.NavHostFragment
import com.appttude.h_mal.farmr.R import com.appttude.h_mal.farmr.R
import com.appttude.h_mal.farmr.base.BackPressedListener
import com.appttude.h_mal.farmr.base.BaseActivity import com.appttude.h_mal.farmr.base.BaseActivity
import com.appttude.h_mal.farmr.utils.goBack
import com.appttude.h_mal.farmr.utils.popBackStack
class MainActivity : BaseActivity() { class MainActivity : BaseActivity() {
private lateinit var toolbar: Toolbar private lateinit var toolbar: Toolbar
@@ -28,33 +23,6 @@ class MainActivity : BaseActivity() {
navController.setGraph(R.navigation.shift_navigation) navController.setGraph(R.navigation.shift_navigation)
} }
override fun onSupportNavigateUp(): Boolean {
val currentFragment = navHost.parentFragment
return if (currentFragment is BackPressedListener) {
currentFragment.onBackPressed()
} else {
if (supportFragmentManager.backStackEntryCount > 1) {
navHost.goBack()
} else {
super.onSupportNavigateUp()
}
}
}
override fun onBackPressed() {
val currentFragment = supportFragmentManager.findFragmentById(R.id.container)
if (currentFragment is BackPressedListener) {
currentFragment.onBackPressed()
} else {
if (supportFragmentManager.backStackEntryCount > 1) {
navHost.goBack()
} else {
super.onBackPressed()
}
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will // Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long // automatically handle clicks on the Home/Up button, so long

View File

@@ -24,7 +24,7 @@
android:id="@+id/fragmentAddItem" android:id="@+id/fragmentAddItem"
android:name="com.appttude.h_mal.farmr.ui.FragmentAddItem" android:name="com.appttude.h_mal.farmr.ui.FragmentAddItem"
android:label="fragment_add_item" android:label="fragment_add_item"
tools:layout="@layout/fragment_add_item" > tools:layout="@layout/fragment_add_item">
<argument <argument
android:name="shiftId" android:name="shiftId"
app:argType="long" /> app:argType="long" />