mirror of
https://github.com/hmalik144/Farmr.git
synced 2026-01-31 02:41:49 +00:00
@@ -0,0 +1,46 @@
|
||||
package com.appttude.h_mal.farmr.base
|
||||
|
||||
import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
import android.view.ViewGroup
|
||||
import android.widget.EditText
|
||||
import androidx.annotation.LayoutRes
|
||||
import androidx.core.view.children
|
||||
|
||||
|
||||
open class FormFragment<V : BaseViewModel>(@LayoutRes contentLayoutId: Int) : BaseFragment<V>(contentLayoutId) {
|
||||
private val initialFormData = mutableMapOf<Int, String>()
|
||||
private val formData = mutableMapOf<Int, String>()
|
||||
|
||||
fun applyFormListener(view: ViewGroup) {
|
||||
view.children.forEach {
|
||||
if (it is EditText) {
|
||||
initialFormData[it.id] = it.text.trim().toString()
|
||||
setDataInMap(it.id, it.text.trim().toString())
|
||||
it.addCustomTextWatch()
|
||||
} else if (it is ViewGroup) {
|
||||
applyFormListener(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun didFormChange(): Boolean {
|
||||
return !(initialFormData.all { (k, v) ->
|
||||
formData[k] == v
|
||||
})
|
||||
}
|
||||
|
||||
private fun EditText.addCustomTextWatch() {
|
||||
addTextChangedListener(object : TextWatcher{
|
||||
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { }
|
||||
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
|
||||
setDataInMap(id, p0.toString())
|
||||
}
|
||||
override fun afterTextChanged(p0: Editable?) { }
|
||||
})
|
||||
}
|
||||
|
||||
private fun setDataInMap(id: Int, text: String) {
|
||||
formData[id] = text
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,6 @@ class FilterDataFragment : BaseFragment<FilterViewModel>(R.layout.fragment_filte
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
setTitle(getString(R.string.title_activity_filter_data))
|
||||
|
||||
LocationET = view.findViewById(R.id.filterLocationEditText)
|
||||
dateFromET = view.findViewById(R.id.fromdateInEditText)
|
||||
@@ -75,6 +74,16 @@ class FilterDataFragment : BaseFragment<FilterViewModel>(R.layout.fragment_filte
|
||||
submit.setOnClickListener(this)
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setHasOptionsMenu(false)
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
setTitle(getString(R.string.title_activity_filter_data))
|
||||
}
|
||||
|
||||
override fun onItemSelected(
|
||||
parentView: AdapterView<*>?,
|
||||
selectedItemView: View?,
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.appttude.h_mal.farmr.ui
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Button
|
||||
import android.widget.EditText
|
||||
import android.widget.LinearLayout
|
||||
@@ -12,7 +13,7 @@ import android.widget.TextView
|
||||
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.BaseFragment
|
||||
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
|
||||
@@ -28,7 +29,7 @@ import com.appttude.h_mal.farmr.utils.show
|
||||
import com.appttude.h_mal.farmr.utils.validateField
|
||||
import com.appttude.h_mal.farmr.viewmodel.SubmissionViewModel
|
||||
|
||||
class FragmentAddItem : BaseFragment<SubmissionViewModel>(R.layout.fragment_add_item),
|
||||
class FragmentAddItem : FormFragment<SubmissionViewModel>(R.layout.fragment_add_item),
|
||||
RadioGroup.OnCheckedChangeListener, BackPressedListener {
|
||||
|
||||
private lateinit var mHourlyRadioButton: RadioButton
|
||||
@@ -117,12 +118,25 @@ class FragmentAddItem : BaseFragment<SubmissionViewModel>(R.layout.fragment_add_
|
||||
setupViewAfterViewCreated()
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setHasOptionsMenu(false)
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
val title = when (arguments?.containsKey(ID)) {
|
||||
true -> getString(R.string.edit_item_title)
|
||||
else -> getString(R.string.add_item_title)
|
||||
}
|
||||
setTitle(title)
|
||||
}
|
||||
|
||||
private fun setupViewAfterViewCreated() {
|
||||
id = arguments?.getLong(ID)
|
||||
wholeView.hide()
|
||||
|
||||
val title = when (arguments?.containsKey(ID)) {
|
||||
true -> {
|
||||
if (arguments?.containsKey(ID) == true) {
|
||||
// Since we are editing a shift lets load the shift data into the views
|
||||
viewModel.getCurrentShift(arguments!!.getLong(ID))?.run {
|
||||
mLocationEditText.setText(description)
|
||||
@@ -164,14 +178,9 @@ class FragmentAddItem : BaseFragment<SubmissionViewModel>(R.layout.fragment_add_
|
||||
|
||||
calculateTotalPay()
|
||||
}
|
||||
|
||||
// Return title
|
||||
getString(R.string.edit_item_title)
|
||||
}
|
||||
|
||||
else -> getString(R.string.add_item_title)
|
||||
}
|
||||
setTitle(title)
|
||||
applyFormListener(view = view as ViewGroup)
|
||||
}
|
||||
|
||||
override fun onCheckedChanged(radioGroup: RadioGroup, id: Int) {
|
||||
@@ -264,6 +273,7 @@ class FragmentAddItem : BaseFragment<SubmissionViewModel>(R.layout.fragment_add_
|
||||
StringBuilder().append(mDuration).append(" hours").toString()
|
||||
mDuration!! * mPayRate
|
||||
}
|
||||
|
||||
ShiftType.PIECE -> {
|
||||
(mUnits ?: 0f) * mPayRate
|
||||
}
|
||||
@@ -273,9 +283,7 @@ class FragmentAddItem : BaseFragment<SubmissionViewModel>(R.layout.fragment_add_
|
||||
}
|
||||
|
||||
override fun onBackPressed(): Boolean {
|
||||
if (mRadioGroup.checkedRadioButtonId == -1) {
|
||||
mActivity?.popBackStack()
|
||||
} else {
|
||||
if (didFormChange()) {
|
||||
requireContext().createDialog(
|
||||
title = "Discard Changes?",
|
||||
message = "Are you sure you want to discard changes?",
|
||||
@@ -284,6 +292,8 @@ class FragmentAddItem : BaseFragment<SubmissionViewModel>(R.layout.fragment_add_
|
||||
mActivity?.popBackStack()
|
||||
}
|
||||
)
|
||||
} else {
|
||||
mActivity?.popBackStack()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package com.appttude.h_mal.farmr.ui
|
||||
import android.app.AlertDialog
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.view.Menu
|
||||
import android.view.MenuInflater
|
||||
import android.view.MenuItem
|
||||
import android.view.View
|
||||
import androidx.core.content.FileProvider
|
||||
@@ -33,11 +35,15 @@ class FragmentMain : BaseFragment<MainViewModel>(R.layout.fragment_main), BackPr
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setTitle("Shift List")
|
||||
// Inflate the layout for this fragment
|
||||
setHasOptionsMenu(true)
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
setTitle("Shift List")
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
@@ -59,6 +65,11 @@ class FragmentMain : BaseFragment<MainViewModel>(R.layout.fragment_main), BackPr
|
||||
viewModel.refreshLiveData()
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
||||
// Inflate the menu; this adds items to the action bar if it is present.
|
||||
inflater.inflate(R.menu.menu_main, menu)
|
||||
}
|
||||
|
||||
override fun onSuccess(data: Any?) {
|
||||
super.onSuccess(data)
|
||||
if (data is List<*>) {
|
||||
|
||||
@@ -35,7 +35,6 @@ class FurtherInfoFragment : BaseFragment<InfoViewModel>(R.layout.fragment_futher
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
setTitle(getString(R.string.further_info_title))
|
||||
|
||||
progressBarFI = view.findViewById(R.id.progressBar_info)
|
||||
wholeView = view.findViewById(R.id.further_info_view)
|
||||
@@ -59,6 +58,16 @@ class FurtherInfoFragment : BaseFragment<InfoViewModel>(R.layout.fragment_futher
|
||||
viewModel.retrieveData(arguments)
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setHasOptionsMenu(false)
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
setTitle(getString(R.string.further_info_title))
|
||||
}
|
||||
|
||||
override fun onSuccess(data: Any?) {
|
||||
super.onSuccess(data)
|
||||
if (data is ShiftObject) data.setupView()
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
package com.appttude.h_mal.farmr.ui
|
||||
|
||||
import android.Manifest
|
||||
import android.app.Activity
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Bundle
|
||||
import android.view.Menu
|
||||
import androidx.appcompat.widget.Toolbar
|
||||
import androidx.core.app.ActivityCompat
|
||||
import com.appttude.h_mal.farmr.R
|
||||
import com.appttude.h_mal.farmr.base.BackPressedListener
|
||||
import com.appttude.h_mal.farmr.base.BaseActivity
|
||||
@@ -25,12 +20,6 @@ class MainActivity : BaseActivity() {
|
||||
fragmentTransaction.replace(R.id.container, FragmentMain()).addToBackStack("main").commit()
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||
// Inflate the menu; this adds items to the action bar if it is present.
|
||||
menuInflater.inflate(R.menu.menu_main, menu)
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onBackPressed() {
|
||||
val currentFragment = supportFragmentManager.findFragmentById(R.id.container)
|
||||
if (currentFragment is BackPressedListener) {
|
||||
|
||||
@@ -284,6 +284,7 @@ class SubmissionViewModel(
|
||||
description = description,
|
||||
date = date,
|
||||
units = units!!,
|
||||
|
||||
rateOfPay = rateOfPay,
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user