Skip to content

Commit

Permalink
Completed saving details
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya-gupta99 committed Aug 13, 2023
1 parent 115463a commit 9db0a68
Show file tree
Hide file tree
Showing 43 changed files with 972 additions and 635 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.ViewModelProvider
import com.mifos.mifosxdroid.R
import com.mifos.mifosxdroid.core.ProgressableDialogFragment
import com.mifos.mifosxdroid.core.util.Toaster.show
Expand All @@ -24,11 +25,12 @@ import com.mifos.objects.client.ChargeCreationResponse
import com.mifos.objects.client.Charges
import com.mifos.objects.templates.clients.ChargeTemplate
import com.mifos.services.data.ChargesPayload
import com.mifos.states.ChargeDialogUiState
import com.mifos.utils.Constants
import com.mifos.utils.DateHelper
import com.mifos.utils.FragmentConstants
import com.mifos.viewmodels.ChargeDialogViewModel
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

/**
* Created by nellyk on 1/22/2016.
Expand All @@ -37,15 +39,14 @@ import javax.inject.Inject
* Use this Dialog Fragment to Create and/or Update charges
*/
@AndroidEntryPoint
class ChargeDialogFragment : ProgressableDialogFragment(), OnDatePickListener, ChargeDialogMvpView,
class ChargeDialogFragment : ProgressableDialogFragment(), OnDatePickListener,
OnItemSelectedListener {
val LOG_TAG = javaClass.simpleName

private lateinit var binding: DialogFragmentChargeBinding

@JvmField
@Inject
var mChargeDialogPresenter: ChargeDialogPresenter? = null
private lateinit var viewModel: ChargeDialogViewModel

private var chargeCreateListener: OnChargeCreateListener? = null
private val chargeNameList: MutableList<String> = ArrayList()
private lateinit var chargeNameAdapter: ArrayAdapter<String>
Expand Down Expand Up @@ -78,10 +79,32 @@ class ChargeDialogFragment : ProgressableDialogFragment(), OnDatePickListener, C
true
)
binding = DialogFragmentChargeBinding.inflate(inflater, container, false)
mChargeDialogPresenter?.attachView(this)
viewModel = ViewModelProvider(this)[ChargeDialogViewModel::class.java]
inflateDueDate()
inflateChargesSpinner()
inflateChargeNameSpinner()

viewModel.chargeDialogUiState.observe(viewLifecycleOwner) {
when (it) {
is ChargeDialogUiState.ShowAllChargesV2 -> {
showProgressbar(false)
showAllChargesV2(it.chargeTemplate)
}

is ChargeDialogUiState.ShowChargesCreatedSuccessfully -> {
showProgressbar(false)
showChargesCreatedSuccessfully(it.chargeCreationResponse)
}

is ChargeDialogUiState.ShowFetchingError -> {
showProgressbar(false)
showFetchingError(it.message)
}

is ChargeDialogUiState.ShowProgressbar -> showProgressbar(true)
}
}

return binding.root
}

Expand Down Expand Up @@ -129,12 +152,12 @@ class ChargeDialogFragment : ProgressableDialogFragment(), OnDatePickListener, C

//Charges Fetching API
private fun inflateChargesSpinner() {
mChargeDialogPresenter?.loadAllChargesV2(clientId)
viewModel.loadAllChargesV2(clientId)
}

//Charges Creation APi
private fun initiateChargesCreation(chargesPayload: ChargesPayload) {
mChargeDialogPresenter?.createCharges(clientId, chargesPayload)
viewModel.createCharges(clientId, chargesPayload)
}

private fun inflateDueDate() {
Expand Down Expand Up @@ -164,10 +187,9 @@ class ChargeDialogFragment : ProgressableDialogFragment(), OnDatePickListener, C
binding.spChargeName.onItemSelectedListener = this
}

override fun showAllChargesV2(chargeTemplate: ChargeTemplate) {
private fun showAllChargesV2(chargeTemplate: ChargeTemplate) {
mChargeTemplate = chargeTemplate
mChargeDialogPresenter?.filterChargeName(chargeTemplate.chargeOptions)
?.let { chargeNameList.addAll(it) }
viewModel.filterChargeName(chargeTemplate.chargeOptions).let { chargeNameList.addAll(it) }
chargeNameAdapter.notifyDataSetChanged()
}

Expand All @@ -181,7 +203,7 @@ class ChargeDialogFragment : ProgressableDialogFragment(), OnDatePickListener, C
}

override fun onNothingSelected(parent: AdapterView<*>?) {}
override fun showChargesCreatedSuccessfully(chargeCreationResponse: ChargeCreationResponse) {
private fun showChargesCreatedSuccessfully(chargeCreationResponse: ChargeCreationResponse) {
if (chargeCreateListener != null) {
createdCharge?.clientId = chargeCreationResponse.clientId
createdCharge?.id = chargeCreationResponse.resourceId
Expand All @@ -192,27 +214,22 @@ class ChargeDialogFragment : ProgressableDialogFragment(), OnDatePickListener, C
dialog?.dismiss()
}

override fun showChargeCreatedFailure(errorMessage: String) {
private fun showChargeCreatedFailure(errorMessage: String) {
if (chargeCreateListener != null) {
chargeCreateListener?.onChargeCreatedFailure(errorMessage)
} else {
show(binding.root, errorMessage)
}
}

override fun showFetchingError(s: String) {
private fun showFetchingError(s: String) {
show(binding.root, s)
}

override fun showProgressbar(b: Boolean) {
private fun showProgressbar(b: Boolean) {
showProgress(b)
}

override fun onDestroyView() {
super.onDestroyView()
mChargeDialogPresenter?.detachView()
}

fun setOnChargeCreatedListener(chargeCreatedListener: OnChargeCreateListener?) {
chargeCreateListener = chargeCreatedListener
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import android.view.WindowManager
import android.widget.Button
import android.widget.Toast
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.ViewModelProvider
import com.mifos.api.GenericResponse
import com.mifos.exceptions.RequiredFieldException
import com.mifos.mifosxdroid.R
Expand All @@ -24,24 +25,24 @@ import com.mifos.mifosxdroid.formwidgets.FormSpinner
import com.mifos.mifosxdroid.formwidgets.FormToggleButton
import com.mifos.mifosxdroid.formwidgets.FormWidget
import com.mifos.objects.noncore.DataTable
import com.mifos.states.DataTableRowDialogUiState
import com.mifos.utils.Constants
import com.mifos.utils.SafeUIBlockingUtility
import com.mifos.viewmodels.DataTableRowDialogViewModel
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

/**
* Created by ishankhanna on 01/08/14.
*/
@AndroidEntryPoint
class DataTableRowDialogFragment : DialogFragment(), DataTableRowDialogMvpView {
class DataTableRowDialogFragment : DialogFragment() {

private lateinit var binding: DialogFragmentAddEntryToDatatableBinding

private val LOG_TAG = javaClass.simpleName

@JvmField
@Inject
var dataTableRowDialogPresenter: DataTableRowDialogPresenter? = null
private lateinit var viewModel: DataTableRowDialogViewModel

private var dataTable: DataTable? = null
private var entityId = 0
private var safeUIBlockingUtility: SafeUIBlockingUtility? = null
Expand All @@ -59,13 +60,28 @@ class DataTableRowDialogFragment : DialogFragment(), DataTableRowDialogMvpView {
*/
dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
binding = DialogFragmentAddEntryToDatatableBinding.inflate(inflater, container, false)
dataTableRowDialogPresenter?.attachView(this)
viewModel = ViewModelProvider(this)[DataTableRowDialogViewModel::class.java]
dialog?.setTitle(dataTable?.registeredTableName)
safeUIBlockingUtility = SafeUIBlockingUtility(
requireContext(), getString(R.string.data_table_row_dialog_loading_message)
)
createForm(dataTable)
addSaveButton()

viewModel.dataTableRowDialogUiState.observe(viewLifecycleOwner){
when(it){
is DataTableRowDialogUiState.ShowDataTableEntrySuccessfully -> {
showProgressbar(false)
showDataTableEntrySuccessfully(it.genericResponse)
}
is DataTableRowDialogUiState.ShowError -> {
showProgressbar(false)
showError(it.message)
}
is DataTableRowDialogUiState.ShowProgressbar -> showProgressbar(true)
}
}

return binding.root
}

Expand Down Expand Up @@ -146,9 +162,8 @@ class DataTableRowDialogFragment : DialogFragment(), DataTableRowDialogMvpView {
}
}

@Throws(RequiredFieldException::class)
fun onSaveActionRequested() {
dataTableRowDialogPresenter?.addDataTableEntry(
private fun onSaveActionRequested() {
viewModel.addDataTableEntry(
dataTable?.registeredTableName,
entityId, addDataTableInput()
)
Expand Down Expand Up @@ -181,7 +196,7 @@ class DataTableRowDialogFragment : DialogFragment(), DataTableRowDialogMvpView {
return payload
}

override fun showDataTableEntrySuccessfully(genericResponse: GenericResponse) {
private fun showDataTableEntrySuccessfully(genericResponse: GenericResponse) {
Toast.makeText(activity, R.string.data_table_entry_added, Toast.LENGTH_LONG).show()
targetFragment?.onActivityResult(
targetRequestCode, Activity.RESULT_OK,
Expand All @@ -190,24 +205,19 @@ class DataTableRowDialogFragment : DialogFragment(), DataTableRowDialogMvpView {
requireActivity().supportFragmentManager.popBackStack()
}

override fun showError(message: String) {
private fun showError(message: String) {
Toast.makeText(activity, message, Toast.LENGTH_LONG).show()
requireActivity().supportFragmentManager.popBackStack()
}

override fun showProgressbar(b: Boolean) {
private fun showProgressbar(b: Boolean) {
if (b) {
safeUIBlockingUtility?.safelyBlockUI()
} else {
safeUIBlockingUtility?.safelyUnBlockUI()
}
}

override fun onDestroyView() {
super.onDestroyView()
dataTableRowDialogPresenter?.detachView()
}

companion object {
//TODO Check for Static vs Bundle Approach
fun newInstance(dataTable: DataTable?, entityId: Int): DataTableRowDialogFragment {
Expand Down
Loading

0 comments on commit 9db0a68

Please sign in to comment.