Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrated loan related presenter to mvvm #2209

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import org.mifos.mobile.repositories.NotificationRepositoryImp
import org.mifos.mobile.api.DataManager
import org.mifos.mobile.repositories.LoanRepository
import org.mifos.mobile.repositories.LoanRepositoryImp
import org.mifos.mobile.repositories.NotificationRepository
import org.mifos.mobile.api.local.PreferencesHelper
import org.mifos.mobile.repositories.ClientRepository
Expand All @@ -24,6 +26,11 @@ class RepositoryModule {
return UserAuthRepositoryImp(dataManager)
}

@Provides
fun providesLoanRepository(dataManager: DataManager): LoanRepository {
return LoanRepositoryImp(dataManager)
}

@Provides
fun providesNotificationRepository(dataManager: DataManager) : NotificationRepository {
return NotificationRepositoryImp(dataManager)
Expand Down
26 changes: 26 additions & 0 deletions app/src/main/java/org/mifos/mobile/repositories/LoanRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.mifos.mobile.repositories

import io.reactivex.Observable
import okhttp3.ResponseBody
import org.mifos.mobile.models.accounts.loan.LoanWithAssociations
import org.mifos.mobile.models.accounts.loan.LoanWithdraw
import org.mifos.mobile.models.templates.loans.LoanTemplate

interface LoanRepository {

fun getLoanWithAssociations(
associationType: String?,
loanId: Long?
): Observable<LoanWithAssociations?>?

fun withdrawLoanAccount(
loanId: Long?,
loanWithdraw: LoanWithdraw?,
): Observable<ResponseBody?>?

fun template(): Observable<LoanTemplate?>?

fun getLoanTemplateByProduct(
productId: Int?
): Observable<LoanTemplate?>?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.mifos.mobile.repositories

import io.reactivex.Observable
import okhttp3.ResponseBody
import org.mifos.mobile.api.DataManager
import org.mifos.mobile.models.accounts.loan.LoanWithAssociations
import org.mifos.mobile.models.accounts.loan.LoanWithdraw
import org.mifos.mobile.models.templates.loans.LoanTemplate
import javax.inject.Inject

class LoanRepositoryImp @Inject constructor(private val dataManager: DataManager) : LoanRepository {

override fun getLoanWithAssociations(
associationType: String?,
loanId: Long?
): Observable<LoanWithAssociations?>? {
return dataManager.getLoanWithAssociations(associationType, loanId)
}

override fun withdrawLoanAccount(
loanId: Long?,
loanWithdraw: LoanWithdraw?
): Observable<ResponseBody?>? {
return dataManager.withdrawLoanAccount(loanId, loanWithdraw)
}

override fun template(): Observable<LoanTemplate?>? {
return dataManager.loanTemplate
}

override fun getLoanTemplateByProduct(productId: Int?): Observable<LoanTemplate?>? {
return dataManager.getLoanTemplateByProduct(productId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.github.therajanmaurya.sweeterror.SweetUIErrorHandler
import dagger.hilt.android.AndroidEntryPoint
import org.mifos.mobile.R
import org.mifos.mobile.databinding.FragmentLoanAccountTransactionsBinding
import org.mifos.mobile.models.accounts.loan.LoanWithAssociations
import org.mifos.mobile.presenters.LoanAccountsTransactionPresenter
import org.mifos.mobile.ui.adapters.RecentTransactionListAdapter
import org.mifos.mobile.ui.fragments.base.BaseFragment
import org.mifos.mobile.ui.views.LoanAccountsTransactionView
import org.mifos.mobile.utils.Constants
import org.mifos.mobile.utils.LoanUiState
import org.mifos.mobile.utils.Network
import org.mifos.mobile.viewModels.LoanAccountTransactionViewModel
import javax.inject.Inject

/*
Expand All @@ -29,17 +30,16 @@ import javax.inject.Inject
* Created by dilpreet on 4/3/17.
*/
@AndroidEntryPoint
class LoanAccountTransactionFragment : BaseFragment(), LoanAccountsTransactionView {
class LoanAccountTransactionFragment : BaseFragment() {
private var _binding: FragmentLoanAccountTransactionsBinding? = null
private val binding get() = _binding!!

@JvmField
@Inject
var transactionsListAdapter: RecentTransactionListAdapter? = null

@JvmField
@Inject
var loanAccountsTransactionPresenter: LoanAccountsTransactionPresenter? = null
lateinit var viewModel: LoanAccountTransactionViewModel

private var loanId: Long? = 0
private var loanWithAssociations: LoanWithAssociations? = null
private var sweetUIErrorHandler: SweetUIErrorHandler? = null
Expand All @@ -56,13 +56,13 @@ class LoanAccountTransactionFragment : BaseFragment(), LoanAccountsTransactionVi
savedInstanceState: Bundle?,
): View {
_binding = FragmentLoanAccountTransactionsBinding.inflate(inflater, container, false)
viewModel = ViewModelProvider(this)[LoanAccountTransactionViewModel::class.java]
val rootView = binding.root
setToolbarTitle(getString(R.string.transactions))
loanAccountsTransactionPresenter?.attachView(this)
sweetUIErrorHandler = SweetUIErrorHandler(context, rootView)
showUserInterface()
if (savedInstanceState == null) {
loanAccountsTransactionPresenter?.loadLoanAccountDetails(loanId)
viewModel.loadLoanAccountDetails(loanId)
}
return rootView
}
Expand All @@ -82,7 +82,7 @@ class LoanAccountTransactionFragment : BaseFragment(), LoanAccountsTransactionVi
/**
* Initialized [RecyclerView] `rvLoanTransactions`
*/
override fun showUserInterface() {
fun showUserInterface() {
val layoutManager = LinearLayoutManager(activity)
layoutManager.orientation = LinearLayoutManager.VERTICAL
with(binding) {
Expand All @@ -98,7 +98,7 @@ class LoanAccountTransactionFragment : BaseFragment(), LoanAccountsTransactionVi
*
* @param loanWithAssociations object containing details about a Loan Account with Associations
*/
override fun showLoanTransactions(loanWithAssociations: LoanWithAssociations?) {
fun showLoanTransactions(loanWithAssociations: LoanWithAssociations?) {
this.loanWithAssociations = loanWithAssociations
binding.llLoanAccountTrans.visibility = View.VISIBLE
binding.tvLoanProductName.text = loanWithAssociations?.loanProductName
Expand All @@ -108,7 +108,7 @@ class LoanAccountTransactionFragment : BaseFragment(), LoanAccountsTransactionVi
/**
* Sets a [TextView] with a msg if Transactions list is empty
*/
override fun showEmptyTransactions(loanWithAssociations: LoanWithAssociations?) {
fun showEmptyTransactions(loanWithAssociations: LoanWithAssociations?) {
sweetUIErrorHandler?.showSweetEmptyUI(
getString(R.string.transactions),
R.drawable.ic_compare_arrows_black_24dp,
Expand All @@ -122,7 +122,7 @@ class LoanAccountTransactionFragment : BaseFragment(), LoanAccountsTransactionVi
*
* @param message Error message that tells the user about the problem.
*/
override fun showErrorFetchingLoanAccountsDetail(message: String?) {
fun showErrorFetchingLoanAccountsDetail(message: String?) {
with(binding) {
if (!Network.isConnected(activity)) {
sweetUIErrorHandler?.showSweetNoInternetUI(
Expand All @@ -142,6 +142,26 @@ class LoanAccountTransactionFragment : BaseFragment(), LoanAccountsTransactionVi

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

viewModel.loanUiState.observe(viewLifecycleOwner) {
when (it) {
is LoanUiState.Loading -> showProgress()
is LoanUiState.ShowError -> {
hideProgress()
showErrorFetchingLoanAccountsDetail(getString(it.message))
}
is LoanUiState.ShowLoan -> {
hideProgress()
showLoanTransactions(it.loanWithAssociations)
}
is LoanUiState.ShowEmpty -> {
hideProgress()
showEmptyTransactions(it.loanWithAssociations)
}
else -> throw IllegalStateException("Unexpected state: $it")
}
}

binding.layoutError.btnTryAgain.setOnClickListener {
retryClicked()
}
Expand All @@ -153,7 +173,7 @@ class LoanAccountTransactionFragment : BaseFragment(), LoanAccountsTransactionVi
binding.rvLoanTransactions,
binding.layoutError.root,
)
loanAccountsTransactionPresenter?.loadLoanAccountDetails(loanId)
viewModel.loadLoanAccountDetails(loanId)
} else {
Toast.makeText(
context,
Expand All @@ -163,18 +183,17 @@ class LoanAccountTransactionFragment : BaseFragment(), LoanAccountsTransactionVi
}
}

override fun showProgress() {
fun showProgress() {
showProgressBar()
}

override fun hideProgress() {
fun hideProgress() {
hideProgressBar()
}

override fun onDestroyView() {
super.onDestroyView()
hideProgressBar()
loanAccountsTransactionPresenter?.detachView()
_binding = null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,30 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import dagger.hilt.android.AndroidEntryPoint
import org.mifos.mobile.R
import org.mifos.mobile.databinding.FragmentLoanWithdrawBinding
import org.mifos.mobile.models.accounts.loan.LoanWithAssociations
import org.mifos.mobile.models.accounts.loan.LoanWithdraw
import org.mifos.mobile.presenters.LoanAccountWithdrawPresenter
import org.mifos.mobile.ui.fragments.base.BaseFragment
import org.mifos.mobile.ui.views.LoanAccountWithdrawView
import org.mifos.mobile.utils.Constants
import org.mifos.mobile.utils.DateHelper
import org.mifos.mobile.utils.LoanUiState
import org.mifos.mobile.utils.Toaster
import javax.inject.Inject
import org.mifos.mobile.viewModels.LoanAccountWithdrawViewModel

/**
* Created by dilpreet on 7/6/17.
*/
@AndroidEntryPoint
class LoanAccountWithdrawFragment : BaseFragment(), LoanAccountWithdrawView {
class LoanAccountWithdrawFragment : BaseFragment() {

private var _binding: FragmentLoanWithdrawBinding? = null
private val binding get() = _binding!!

@JvmField
@Inject
var loanAccountWithdrawPresenter: LoanAccountWithdrawPresenter? = null
lateinit var viewModel: LoanAccountWithdrawViewModel

private var loanWithAssociations: LoanWithAssociations? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -43,14 +42,30 @@ class LoanAccountWithdrawFragment : BaseFragment(), LoanAccountWithdrawView {
savedInstanceState: Bundle?,
): View {
_binding = FragmentLoanWithdrawBinding.inflate(inflater, container, false)
viewModel = ViewModelProvider(this)[LoanAccountWithdrawViewModel::class.java]
setToolbarTitle(getString(R.string.withdraw_loan))
showUserInterface()
loanAccountWithdrawPresenter?.attachView(this)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

viewModel.loanUiState.observe(viewLifecycleOwner) {
when (it) {
is LoanUiState.Loading -> showProgress()
is LoanUiState.ShowError -> {
hideProgress()
showLoanAccountWithdrawError(getString(it.message))
}
is LoanUiState.WithdrawSuccess -> {
hideProgress()
showLoanAccountWithdrawSuccess()
}
else -> throw IllegalStateException("Unexpected state: $it")
}
}

binding.btnWithdrawLoan.setOnClickListener {
onLoanWithdraw()
}
Expand All @@ -72,7 +87,7 @@ class LoanAccountWithdrawFragment : BaseFragment(), LoanAccountWithdrawView {
loanWithdraw.note = binding.etWithdrawReason.text.toString()
loanWithdraw.withdrawnOnDate = DateHelper
.getDateAsStringFromLong(System.currentTimeMillis())
loanAccountWithdrawPresenter?.withdrawLoanAccount(
viewModel.withdrawLoanAccount(
loanWithAssociations?.id?.toLong(),
loanWithdraw,
)
Expand All @@ -81,7 +96,7 @@ class LoanAccountWithdrawFragment : BaseFragment(), LoanAccountWithdrawView {
/**
* Receives A confirmation after successfull withdrawing of Loan Application.
*/
override fun showLoanAccountWithdrawSuccess() {
fun showLoanAccountWithdrawSuccess() {
Toaster.show(binding.root, R.string.loan_application_withdrawn_successfully)
activity?.supportFragmentManager?.popBackStack()
}
Expand All @@ -92,22 +107,21 @@ class LoanAccountWithdrawFragment : BaseFragment(), LoanAccountWithdrawView {
*
* @param message Error Message displayed
*/
override fun showLoanAccountWithdrawError(message: String?) {
fun showLoanAccountWithdrawError(message: String?) {
Toaster.show(binding.root, message)
}

override fun showProgress() {
fun showProgress() {
showProgressBar()
}

override fun hideProgress() {
fun hideProgress() {
hideProgressBar()
}

override fun onDestroyView() {
super.onDestroyView()
hideProgress()
loanAccountWithdrawPresenter?.detachView()
_binding = null
}

Expand Down
Loading
Loading