mirror of
https://github.com/hmalik144/Farmr.git
synced 2025-12-10 02:25:19 +00:00
- Fragment navigation library added
- on back pressed fixed
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -89,6 +89,7 @@ gen-external-apklibs
|
||||
.idea/assetWizardSettings.xml
|
||||
.idea/gradle.xml
|
||||
.idea/jarRepositories.xml
|
||||
.idea/navEditor.xml
|
||||
|
||||
# Gem/fastlane
|
||||
Gemfile.lock
|
||||
|
||||
@@ -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<SubmissionViewModel>(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<SubmissionViewModel>(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<SubmissionViewModel>(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<SubmissionViewModel>(R.layout.fragment_add_
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBackPressed(): Boolean {
|
||||
fun onBackPressed() {
|
||||
if (didFormChange()) {
|
||||
requireContext().createDialog(
|
||||
title = "Discard Changes?",
|
||||
@@ -299,7 +316,6 @@ class FragmentAddItem : FormFragment<SubmissionViewModel>(R.layout.fragment_add_
|
||||
} else {
|
||||
goBack()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onSuccess(data: Any?) {
|
||||
|
||||
@@ -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<MainViewModel>(R.layout.fragment_main), BackPressedListener {
|
||||
class FragmentMain : BaseFragment<MainViewModel>(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<MainViewModel>(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<MainViewModel>(R.layout.fragment_main), BackPr
|
||||
exitProcess(0)
|
||||
}
|
||||
)
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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">
|
||||
<argument
|
||||
android:name="shiftId"
|
||||
app:argType="long" />
|
||||
|
||||
Reference in New Issue
Block a user