Skip to content

Commit

Permalink
Merge pull request #2245 from openMF/mvvm_architecture
Browse files Browse the repository at this point in the history
Mvvm architecture
  • Loading branch information
jawidMuhammadi authored Aug 1, 2023
2 parents ea2036e + 58d1802 commit 933db04
Show file tree
Hide file tree
Showing 32 changed files with 1,883 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,9 @@ import dagger.Module
import dagger.Provides
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
import org.mifos.mobile.repositories.ClientRepositoryImp
import org.mifos.mobile.repositories.RecentTransactionRepository
import org.mifos.mobile.repositories.RecentTransactionRepositoryImp
import org.mifos.mobile.repositories.UserAuthRepository
import org.mifos.mobile.repositories.UserAuthRepositoryImp
import org.mifos.mobile.repositories.*

@Module
@InstallIn(SingletonComponent::class)
Expand All @@ -30,9 +21,9 @@ class RepositoryModule {
fun providesLoanRepository(dataManager: DataManager): LoanRepository {
return LoanRepositoryImp(dataManager)
}

@Provides
fun providesNotificationRepository(dataManager: DataManager) : NotificationRepository {
fun providesNotificationRepository(dataManager: DataManager): NotificationRepository {
return NotificationRepositoryImp(dataManager)
}

Expand All @@ -45,6 +36,16 @@ class RepositoryModule {

@Provides
fun providesRecentTransactionRepository(dataManager: DataManager): RecentTransactionRepository {
return RecentTransactionRepositoryImp(dataManager)
return RecentTransactionRepositoryImp(dataManager)
}

@Provides
fun providesGuarantorRepository(dataManager: DataManager): GuarantorRepository {
return GuarantorRepositoryImp(dataManager)
}

@Provides
fun providesBeneficiaryRepository(dataManager: DataManager): BeneficiaryRepository {
return BeneficiaryRepositoryImp(dataManager)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.mifos.mobile.repositories

import io.reactivex.Observable
import okhttp3.ResponseBody
import org.mifos.mobile.models.beneficiary.Beneficiary
import org.mifos.mobile.models.beneficiary.BeneficiaryPayload
import org.mifos.mobile.models.beneficiary.BeneficiaryUpdatePayload
import org.mifos.mobile.models.templates.beneficiary.BeneficiaryTemplate

interface BeneficiaryRepository {

fun beneficiaryTemplate(): Observable<BeneficiaryTemplate?>?

fun createBeneficiary(beneficiaryPayload: BeneficiaryPayload?): Observable<ResponseBody?>?

fun updateBeneficiary(
beneficiaryId: Long?,
payload: BeneficiaryUpdatePayload?,
): Observable<ResponseBody?>?

fun deleteBeneficiary(beneficiaryId: Long?): Observable<ResponseBody?>?

fun beneficiaryList(): Observable<List<Beneficiary?>?>?

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.mifos.mobile.repositories

import io.reactivex.Observable
import okhttp3.ResponseBody
import org.mifos.mobile.api.DataManager
import org.mifos.mobile.models.beneficiary.Beneficiary
import org.mifos.mobile.models.beneficiary.BeneficiaryPayload
import org.mifos.mobile.models.beneficiary.BeneficiaryUpdatePayload
import org.mifos.mobile.models.templates.beneficiary.BeneficiaryTemplate
import javax.inject.Inject

class BeneficiaryRepositoryImp @Inject constructor(private val dataManager: DataManager) :
BeneficiaryRepository {

override fun beneficiaryTemplate(): Observable<BeneficiaryTemplate?>? {
return dataManager.beneficiaryTemplate
}

override fun createBeneficiary(beneficiaryPayload: BeneficiaryPayload?): Observable<ResponseBody?>? {
return dataManager.createBeneficiary(beneficiaryPayload)
}

override fun updateBeneficiary(
beneficiaryId: Long?,
payload: BeneficiaryUpdatePayload?
): Observable<ResponseBody?>? {
return dataManager.updateBeneficiary(beneficiaryId, payload)
}

override fun deleteBeneficiary(beneficiaryId: Long?): Observable<ResponseBody?>? {
return dataManager.deleteBeneficiary(beneficiaryId)
}

override fun beneficiaryList(): Observable<List<Beneficiary?>?>? {
return dataManager.beneficiaryList
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.mifos.mobile.repositories

import io.reactivex.Observable
import okhttp3.ResponseBody
import org.mifos.mobile.models.guarantor.GuarantorApplicationPayload
import org.mifos.mobile.models.guarantor.GuarantorPayload
import org.mifos.mobile.models.guarantor.GuarantorTemplatePayload

interface GuarantorRepository {

fun getGuarantorTemplate(loanId: Long?): Observable<GuarantorTemplatePayload?>?

fun createGuarantor(
loanId: Long?,
payload: GuarantorApplicationPayload?,
): Observable<ResponseBody?>?

fun updateGuarantor(
payload: GuarantorApplicationPayload?,
loanId: Long?,
guarantorId: Long?,
): Observable<ResponseBody?>?

fun deleteGuarantor(loanId: Long?, guarantorId: Long?): Observable<ResponseBody?>?

fun getGuarantorList(loanId: Long): Observable<List<GuarantorPayload?>?>?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.mifos.mobile.repositories

import io.reactivex.Observable
import okhttp3.ResponseBody
import org.mifos.mobile.api.DataManager
import org.mifos.mobile.models.guarantor.GuarantorApplicationPayload
import org.mifos.mobile.models.guarantor.GuarantorPayload
import org.mifos.mobile.models.guarantor.GuarantorTemplatePayload
import javax.inject.Inject

class GuarantorRepositoryImp @Inject constructor(private val dataManager: DataManager) :
GuarantorRepository {

override fun getGuarantorTemplate(loanId: Long?): Observable<GuarantorTemplatePayload?>? {
return dataManager.getGuarantorTemplate(loanId)
}

override fun createGuarantor(
loanId: Long?,
payload: GuarantorApplicationPayload?
): Observable<ResponseBody?>? {
return dataManager.createGuarantor(loanId, payload)
}

override fun updateGuarantor(
payload: GuarantorApplicationPayload?,
loanId: Long?,
guarantorId: Long?
): Observable<ResponseBody?>? {
return dataManager.updateGuarantor(payload, loanId, guarantorId)
}

override fun deleteGuarantor(loanId: Long?, guarantorId: Long?): Observable<ResponseBody?>? {
return dataManager.deleteGuarantor(loanId, guarantorId)
}

override fun getGuarantorList(loanId: Long): Observable<List<GuarantorPayload?>?>? {
return dataManager.getGuarantorList(loanId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,35 @@ import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.textfield.MaterialAutoCompleteTextView
import dagger.hilt.android.AndroidEntryPoint
import org.mifos.mobile.R
import org.mifos.mobile.databinding.FragmentAddGuarantorBinding
import org.mifos.mobile.models.guarantor.GuarantorApplicationPayload
import org.mifos.mobile.models.guarantor.GuarantorPayload
import org.mifos.mobile.models.guarantor.GuarantorTemplatePayload
import org.mifos.mobile.presenters.AddGuarantorPresenter
import org.mifos.mobile.ui.enums.GuarantorState
import org.mifos.mobile.ui.fragments.base.BaseFragment
import org.mifos.mobile.ui.views.AddGuarantorView
import org.mifos.mobile.utils.Constants
import org.mifos.mobile.utils.GuarantorUiState
import org.mifos.mobile.utils.RxBus.publish
import org.mifos.mobile.utils.RxEvent.AddGuarantorEvent
import org.mifos.mobile.utils.RxEvent.UpdateGuarantorEvent
import org.mifos.mobile.utils.Toaster
import javax.inject.Inject
import org.mifos.mobile.viewModels.AddGuarantorViewModel

/*
* Created by saksham on 23/July/2018
*/
@AndroidEntryPoint
class AddGuarantorFragment : BaseFragment(), AddGuarantorView {
class AddGuarantorFragment : BaseFragment() {

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

@JvmField
@Inject
var presenter: AddGuarantorPresenter? = null
private lateinit var viewModel: AddGuarantorViewModel

var guarantorTypeAdapter: ArrayAdapter<String?>? = null
var template: GuarantorTemplatePayload? = null
var payload: GuarantorPayload? = null
Expand All @@ -60,7 +59,7 @@ class AddGuarantorFragment : BaseFragment(), AddGuarantorView {
savedInstanceState: Bundle?,
): View {
_binding = FragmentAddGuarantorBinding.inflate(inflater, container, false)
presenter?.attachView(this)
viewModel = ViewModelProvider(this)[AddGuarantorViewModel::class.java]
if (guarantorState == GuarantorState.CREATE) {
setToolbarTitle(getString(R.string.add_guarantor))
} else if (guarantorState == GuarantorState.UPDATE) {
Expand All @@ -71,14 +70,42 @@ class AddGuarantorFragment : BaseFragment(), AddGuarantorView {

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

viewModel.guarantorUiState.observe(viewLifecycleOwner) {
when (it) {
is GuarantorUiState.Loading -> showProgress()
is GuarantorUiState.ShowError -> {
hideProgress()
showError(it.message)
}
is GuarantorUiState.ShowGuarantorApplication -> {
hideProgress()
showGuarantorApplication(it.template)
}
is GuarantorUiState.ShowGuarantorUpdation -> {
hideProgress()
showGuarantorUpdation(it.template)
}
is GuarantorUiState.SubmittedSuccessfully -> {
hideProgress()
submittedSuccessfully(it.message, it.payload)
}
is GuarantorUiState.GuarantorUpdatedSuccessfully -> {
hideProgress()
updatedSuccessfully(it.message)
}
else -> throw IllegalStateException("Undesired $it")
}
}

binding.btnSubmitGuarantor.setOnClickListener {
onSubmit()
}
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
presenter?.getGuarantorTemplate(guarantorState, loanId)
viewModel.getGuarantorTemplate(guarantorState, loanId)
}

fun onSubmit() {
Expand All @@ -90,9 +117,9 @@ class AddGuarantorFragment : BaseFragment(), AddGuarantorView {
if (isFieldsCompleted) {
guarantorApplicationPayload = generatePayload()
if (guarantorState == GuarantorState.CREATE) {
presenter?.createGuarantor(loanId, guarantorApplicationPayload)
viewModel.createGuarantor(loanId, guarantorApplicationPayload)
} else if (guarantorState == GuarantorState.UPDATE) {
presenter?.updateGuarantor(guarantorApplicationPayload, loanId, payload?.id)
viewModel.updateGuarantor(guarantorApplicationPayload, loanId, payload?.id)
}
}
}
Expand Down Expand Up @@ -147,17 +174,16 @@ class AddGuarantorFragment : BaseFragment(), AddGuarantorView {
return rv
}

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

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

override fun onDestroy() {
super.onDestroy()
presenter?.detachView()
hideProgressBar()
}

Expand All @@ -166,12 +192,12 @@ class AddGuarantorFragment : BaseFragment(), AddGuarantorView {
_binding = null
}

override fun showGuarantorApplication(template: GuarantorTemplatePayload?) {
fun showGuarantorApplication(template: GuarantorTemplatePayload?) {
this.template = template
setUpSpinner()
}

override fun showGuarantorUpdation(template: GuarantorTemplatePayload?) {
fun showGuarantorUpdation(template: GuarantorTemplatePayload?) {
this.template = template
setUpSpinner()
with(binding) {
Expand All @@ -198,19 +224,19 @@ class AddGuarantorFragment : BaseFragment(), AddGuarantorView {
}
}

override fun updatedSuccessfully(message: String?) {
fun updatedSuccessfully(message: String?) {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
publish(UpdateGuarantorEvent(guarantorApplicationPayload, index))
activity?.supportFragmentManager?.popBackStack()
}

override fun submittedSuccessfully(message: String?, payload: GuarantorApplicationPayload?) {
fun submittedSuccessfully(message: String?, payload: GuarantorApplicationPayload?) {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
publish(AddGuarantorEvent(payload, index))
activity?.supportFragmentManager?.popBackStack()
}

override fun showError(message: String?) {
fun showError(message: String?) {
Toaster.show(binding.root, message)
}

Expand Down
Loading

0 comments on commit 933db04

Please sign in to comment.