Skip to content

Commit

Permalink
MIFOSAC-197 migrate loan repayment fragment to compose (#2127)
Browse files Browse the repository at this point in the history
  • Loading branch information
itsPronay authored Jul 5, 2024
1 parent 2415da5 commit c95b401
Show file tree
Hide file tree
Showing 4 changed files with 491 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,97 +9,57 @@ import android.view.LayoutInflater
import android.view.Menu
import android.view.View
import android.view.ViewGroup
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.mifos.core.objects.accounts.loan.LoanWithAssociations
import com.mifos.core.objects.accounts.loan.RepaymentSchedule
import com.mifos.mifosxdroid.R
import com.mifos.mifosxdroid.adapters.LoanRepaymentScheduleAdapter
import com.mifos.mifosxdroid.core.MifosBaseFragment
import com.mifos.mifosxdroid.core.ProgressableFragment
import com.mifos.mifosxdroid.core.util.Toaster
import com.mifos.mifosxdroid.databinding.FragmentLoanRepaymentScheduleBinding
import com.mifos.mifosxdroid.online.datatable.DataTableScreen
import com.mifos.mifosxdroid.online.loanrepayment.LoanRepaymentViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class LoanRepaymentScheduleFragment : ProgressableFragment() {
class LoanRepaymentScheduleFragment : MifosBaseFragment() {

private lateinit var binding: FragmentLoanRepaymentScheduleBinding
private val arg: LoanRepaymentScheduleFragmentArgs by navArgs()
private val viewModel: LoanRepaymentScheduleViewModel by viewModels()

private lateinit var viewModel: LoanRepaymentScheduleViewModel

private var loanAccountNumber = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
loanAccountNumber = arg.loanId
setHasOptionsMenu(false)
viewModel.loanId = arg.loanId
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentLoanRepaymentScheduleBinding.inflate(inflater, container, false)
setToolbarTitle(resources.getString(R.string.loan_repayment_schedule))
viewModel = ViewModelProvider(this)[LoanRepaymentScheduleViewModel::class.java]
inflateRepaymentSchedule()

viewModel.loanRepaymentScheduleUiState.observe(viewLifecycleOwner) {
when (it) {
is LoanRepaymentScheduleUiState.ShowFetchingError -> {
showProgressbar(false)
showFetchingError(it.message)
}

is LoanRepaymentScheduleUiState.ShowLoanRepaySchedule -> {
showProgressbar(false)
showLoanRepaySchedule(it.loanWithAssociations)
}

is LoanRepaymentScheduleUiState.ShowProgressbar -> showProgressbar(true)
return ComposeView(requireContext()).apply {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setContent {
LoanRepaymentScheduleScreen(
navigateBack = { findNavController().popBackStack() }
)
}
}

return binding.root
}

override fun onPrepareOptionsMenu(menu: Menu) {
menu.clear()
super.onPrepareOptionsMenu(menu)
}

private fun inflateRepaymentSchedule() {
viewModel.loadLoanRepaySchedule(loanAccountNumber)
}

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

private fun showLoanRepaySchedule(loanWithAssociations: LoanWithAssociations) {
/* Activity is null - Fragment has been detached; no need to do anything. */
if (activity == null) return
val listOfActualPeriods = loanWithAssociations
.repaymentSchedule
.getlistOfActualPeriods()
val loanRepaymentScheduleAdapter =
LoanRepaymentScheduleAdapter(requireActivity(), listOfActualPeriods)
binding.lvRepaymentSchedule.adapter = loanRepaymentScheduleAdapter
val totalRepaymentsCompleted = resources.getString(R.string.complete) + "" +
" : "
val totalRepaymentsOverdue = resources.getString(R.string.overdue) + " : "
val totalRepaymentsPending = resources.getString(R.string.pending) + " : "
//Implementing the Footer here
binding.flrsFooter.tvTotalPaid.text = totalRepaymentsCompleted + RepaymentSchedule
.getNumberOfRepaymentsComplete(listOfActualPeriods)
binding.flrsFooter.tvTotalOverdue.text = totalRepaymentsOverdue + RepaymentSchedule
.getNumberOfRepaymentsOverDue(listOfActualPeriods)
binding.flrsFooter.tvTotalUpcoming.text = totalRepaymentsPending + RepaymentSchedule
.getNumberOfRepaymentsPending(listOfActualPeriods)
override fun onResume() {
super.onResume()
toolbar?.visibility = View.GONE
}

private fun showFetchingError(s: String?) {
Toaster.show(binding.root, s)
override fun onStop() {
super.onStop()
toolbar?.visibility = View.VISIBLE
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
///*
// * This project is licensed under the open source MPL V2.
// * See https://github.com/openMF/android-client/blob/master/LICENSE.md
// */
//package com.mifos.mifosxdroid.online.loanrepaymentschedule
//
//import android.os.Bundle
//import android.view.LayoutInflater
//import android.view.Menu
//import android.view.View
//import android.view.ViewGroup
//import androidx.lifecycle.ViewModelProvider
//import androidx.navigation.fragment.navArgs
//import com.mifos.core.objects.accounts.loan.LoanWithAssociations
//import com.mifos.core.objects.accounts.loan.RepaymentSchedule
//import com.mifos.mifosxdroid.R
//import com.mifos.mifosxdroid.adapters.LoanRepaymentScheduleAdapter
//import com.mifos.mifosxdroid.core.ProgressableFragment
//import com.mifos.mifosxdroid.core.util.Toaster
//import com.mifos.mifosxdroid.databinding.FragmentLoanRepaymentScheduleBinding
//import dagger.hilt.android.AndroidEntryPoint
//
//@AndroidEntryPoint
//class LoanRepaymentScheduleFragment : ProgressableFragment() {
//
// private lateinit var binding: FragmentLoanRepaymentScheduleBinding
// private val arg: LoanRepaymentScheduleFragmentArgs by navArgs()
//
// private lateinit var viewModel: LoanRepaymentScheduleViewModel
//
// private var loanAccountNumber = 0
// override fun onCreate(savedInstanceState: Bundle?) {
// super.onCreate(savedInstanceState)
// loanAccountNumber = arg.loanId
// setHasOptionsMenu(false)
// }
//
// override fun onCreateView(
// inflater: LayoutInflater,
// container: ViewGroup?,
// savedInstanceState: Bundle?
// ): View {
// binding = FragmentLoanRepaymentScheduleBinding.inflate(inflater, container, false)
// setToolbarTitle(resources.getString(R.string.loan_repayment_schedule))
// viewModel = ViewModelProvider(this)[LoanRepaymentScheduleViewModel::class.java]
// inflateRepaymentSchedule()
//
// viewModel.loanRepaymentScheduleUiState.observe(viewLifecycleOwner) {
// when (it) {
// is LoanRepaymentScheduleUiState.ShowFetchingError -> {
// showProgressbar(false)
// showFetchingError(it.message)
// }
//
// is LoanRepaymentScheduleUiState.ShowLoanRepaySchedule -> {
// showProgressbar(false)
// showLoanRepaySchedule(it.loanWithAssociations)
// }
//
// is LoanRepaymentScheduleUiState.ShowProgressbar -> showProgressbar(true)
// }
// }
//
// return binding.root
// }
//
// override fun onPrepareOptionsMenu(menu: Menu) {
// menu.clear()
// super.onPrepareOptionsMenu(menu)
// }
//
// private fun inflateRepaymentSchedule() {
// viewModel.loadLoanRepaySchedule(loanAccountNumber)
// }
//
// private fun showProgressbar(b: Boolean) {
// showProgress(b)
// }
//
// private fun showLoanRepaySchedule(loanWithAssociations: LoanWithAssociations) {
// /* Activity is null - Fragment has been detached; no need to do anything. */
// if (activity == null) return
// val listOfActualPeriods = loanWithAssociations
// .repaymentSchedule
// .getlistOfActualPeriods()
// val loanRepaymentScheduleAdapter =
// LoanRepaymentScheduleAdapter(requireActivity(), listOfActualPeriods)
// binding.lvRepaymentSchedule.adapter = loanRepaymentScheduleAdapter
// val totalRepaymentsCompleted = resources.getString(R.string.complete) + "" +
// " : "
// val totalRepaymentsOverdue = resources.getString(R.string.overdue) + " : "
// val totalRepaymentsPending = resources.getString(R.string.pending) + " : "
// //Implementing the Footer here
// binding.flrsFooter.tvTotalPaid.text = totalRepaymentsCompleted + RepaymentSchedule
// .getNumberOfRepaymentsComplete(listOfActualPeriods)
// binding.flrsFooter.tvTotalOverdue.text = totalRepaymentsOverdue + RepaymentSchedule
// .getNumberOfRepaymentsOverDue(listOfActualPeriods)
// binding.flrsFooter.tvTotalUpcoming.text = totalRepaymentsPending + RepaymentSchedule
// .getNumberOfRepaymentsPending(listOfActualPeriods)
// }
//
// private fun showFetchingError(s: String?) {
// Toaster.show(binding.root, s)
// }
//}
Loading

0 comments on commit c95b401

Please sign in to comment.