diff --git a/.gitignore b/.gitignore index ae7f176..be47ae6 100644 --- a/.gitignore +++ b/.gitignore @@ -89,6 +89,7 @@ gen-external-apklibs .idea/assetWizardSettings.xml .idea/gradle.xml .idea/jarRepositories.xml +.idea/navEditor.xml # Gem/fastlane Gemfile.lock diff --git a/app/src/main/java/com/appttude/h_mal/farmr/ui/FragmentAddItem.kt b/app/src/main/java/com/appttude/h_mal/farmr/ui/FragmentAddItem.kt index d78f045..a3ed587 100644 --- a/app/src/main/java/com/appttude/h_mal/farmr/ui/FragmentAddItem.kt +++ b/app/src/main/java/com/appttude/h_mal/farmr/ui/FragmentAddItem.kt @@ -1,6 +1,7 @@ package com.appttude.h_mal.farmr.ui import android.os.Bundle +import android.util.Log import android.view.View import android.view.ViewGroup import android.widget.Button @@ -10,21 +11,19 @@ import android.widget.RadioButton import android.widget.RadioGroup import android.widget.ScrollView import android.widget.TextView +import androidx.activity.OnBackPressedCallback import androidx.core.widget.doAfterTextChanged 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.model.ShiftType import com.appttude.h_mal.farmr.model.Success 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.displayToast import com.appttude.h_mal.farmr.utils.formatAsCurrencyString import com.appttude.h_mal.farmr.utils.formatToTwoDpString import com.appttude.h_mal.farmr.utils.goBack 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.setTimePicker 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 class FragmentAddItem : FormFragment(R.layout.fragment_add_item), - RadioGroup.OnCheckedChangeListener, BackPressedListener { + RadioGroup.OnCheckedChangeListener { + + private lateinit var onBackPressed: OnBackPressedCallback private lateinit var mHourlyRadioButton: RadioButton private lateinit var mPieceRadioButton: RadioButton @@ -123,6 +124,13 @@ class FragmentAddItem : FormFragment(R.layout.fragment_add_ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) 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() { @@ -132,56 +140,65 @@ class FragmentAddItem : FormFragment(R.layout.fragment_add_ else -> getString(R.string.add_item_title) } setTitle(title) + + onBackPressed.isEnabled = true + } + + override fun onPause() { + super.onPause() + onBackPressed.isEnabled = false } private fun setupViewAfterViewCreated() { - val id = arguments?.takeIf { it.containsKey(SHIFT_ID) } - ?.let { FragmentAddItemArgs.fromBundle(it).shiftId } + val id = try { + FragmentAddItemArgs.fromBundle(requireArguments()).shiftId + } catch (e: Exception) { + Log.i("Nav Args", "Failed to retrieve args from navigation") + null + } wholeView.hide() - if (id != null) { - // Since we are editing a shift lets load the shift data into the views - viewModel.getCurrentShift(arguments!!.getLong(ID))?.run { - mLocationEditText.setText(description) - mDateEditText.setText(date) + // Since we are editing a shift lets load the shift data into the views + id?.let { viewModel.getCurrentShift(id) }?.run { + mLocationEditText.setText(description) + mDateEditText.setText(date) - // Set types - mType = ShiftType.getEnumByType(type) - mDescription = description - mDate = date - mPayRate = rateOfPay + // Set types + mType = ShiftType.getEnumByType(type) + mDescription = description + mDate = date + mPayRate = rateOfPay - when (ShiftType.getEnumByType(type)) { - ShiftType.HOURLY -> { - mHourlyRadioButton.isChecked = true - mPieceRadioButton.isChecked = false - mTimeInEditText.setText(timeIn) - mTimeOutEditText.setText(timeOut) - mBreakEditText.setText(breakMins.toString()) - val durationText = "${duration.formatToTwoDpString()} Hours" - mDurationTextView.text = durationText + when (ShiftType.getEnumByType(type)) { + ShiftType.HOURLY -> { + mHourlyRadioButton.isChecked = true + mPieceRadioButton.isChecked = false + mTimeInEditText.setText(timeIn) + mTimeOutEditText.setText(timeOut) + mBreakEditText.setText(breakMins.toString()) + val durationText = "${duration.formatToTwoDpString()} Hours" + mDurationTextView.text = durationText - // Set fields - mTimeIn = timeIn - mTimeOut = timeOut - mBreaks = breakMins - } - - ShiftType.PIECE -> { - mHourlyRadioButton.isChecked = false - mPieceRadioButton.isChecked = true - mUnitEditText.setText(units.formatToTwoDpString()) - - // Set piece rate units - mUnits = units - } + // Set fields + mTimeIn = timeIn + mTimeOut = timeOut + mBreaks = breakMins } - 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) @@ -286,7 +303,7 @@ class FragmentAddItem : FormFragment(R.layout.fragment_add_ } } - override fun onBackPressed(): Boolean { + fun onBackPressed() { if (didFormChange()) { requireContext().createDialog( title = "Discard Changes?", @@ -299,7 +316,6 @@ class FragmentAddItem : FormFragment(R.layout.fragment_add_ } else { goBack() } - return true } override fun onSuccess(data: Any?) { diff --git a/app/src/main/java/com/appttude/h_mal/farmr/ui/FragmentMain.kt b/app/src/main/java/com/appttude/h_mal/farmr/ui/FragmentMain.kt index 698ebb7..1fbe573 100644 --- a/app/src/main/java/com/appttude/h_mal/farmr/ui/FragmentMain.kt +++ b/app/src/main/java/com/appttude/h_mal/farmr/ui/FragmentMain.kt @@ -7,11 +7,10 @@ import android.view.Menu import android.view.MenuInflater import android.view.MenuItem import android.view.View +import androidx.activity.OnBackPressedCallback import androidx.core.content.FileProvider import androidx.recyclerview.widget.RecyclerView -import androidx.recyclerview.widget.RecyclerView.AdapterDataObserver 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.data.legacydb.ShiftObject 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.utils.createDialog 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.navigateToFragment -import com.appttude.h_mal.farmr.utils.show import com.appttude.h_mal.farmr.viewmodel.MainViewModel import com.google.android.material.floatingactionbutton.FloatingActionButton import java.io.File import kotlin.system.exitProcess -class FragmentMain : BaseFragment(R.layout.fragment_main), BackPressedListener { +class FragmentMain : BaseFragment(R.layout.fragment_main) { private lateinit var productListView: RecyclerView private lateinit var emptyView: View private lateinit var mAdapter: ShiftListAdapter + private lateinit var onBackPressed: OnBackPressedCallback + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Inflate the layout for this fragment 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() { super.onResume() setTitle("Shift List") + + onBackPressed.isEnabled = true + } + + override fun onPause() { + super.onPause() + onBackPressed.isEnabled = false } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { @@ -182,21 +194,13 @@ class FragmentMain : BaseFragment(R.layout.fragment_main), BackPr file ) 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) } } - private fun exportDialog() { - 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 { + fun onBackPressed() { requireContext().createDialog( title = "Leave?", message = "Are you sure you want to exit Farmr?", @@ -210,6 +214,5 @@ class FragmentMain : BaseFragment(R.layout.fragment_main), BackPr exitProcess(0) } ) - return true } } \ No newline at end of file diff --git a/app/src/main/java/com/appttude/h_mal/farmr/ui/MainActivity.kt b/app/src/main/java/com/appttude/h_mal/farmr/ui/MainActivity.kt index f23e578..9f0357b 100644 --- a/app/src/main/java/com/appttude/h_mal/farmr/ui/MainActivity.kt +++ b/app/src/main/java/com/appttude/h_mal/farmr/ui/MainActivity.kt @@ -1,16 +1,11 @@ package com.appttude.h_mal.farmr.ui import android.os.Bundle -import android.view.Menu import android.view.MenuItem import androidx.appcompat.widget.Toolbar -import androidx.core.app.ActivityCompat import androidx.navigation.fragment.NavHostFragment 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.utils.goBack -import com.appttude.h_mal.farmr.utils.popBackStack class MainActivity : BaseActivity() { private lateinit var toolbar: Toolbar @@ -28,33 +23,6 @@ class MainActivity : BaseActivity() { 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 { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long diff --git a/app/src/main/res/navigation/shift_navigation.xml b/app/src/main/res/navigation/shift_navigation.xml index 9a6584d..7289dd2 100644 --- a/app/src/main/res/navigation/shift_navigation.xml +++ b/app/src/main/res/navigation/shift_navigation.xml @@ -24,7 +24,7 @@ android:id="@+id/fragmentAddItem" android:name="com.appttude.h_mal.farmr.ui.FragmentAddItem" android:label="fragment_add_item" - tools:layout="@layout/fragment_add_item" > + tools:layout="@layout/fragment_add_item">