Skip to content

Commit

Permalink
refactor #2415: transfer related viewmodels with stateflow
Browse files Browse the repository at this point in the history
  • Loading branch information
PratyushSingh07 authored and therajanmaurya committed Jan 17, 2024
1 parent e3e2667 commit b4cb777
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 35 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/org/mifos/mobile/api/DataManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class DataManager @Inject constructor(
suspend fun accountTransferTemplate(): AccountOptionsTemplate =
baseApiManager.savingAccountsListApi.accountTransferTemplate()

suspend fun makeTransfer(transferPayload: TransferPayload?): Response<ResponseBody?>? {
suspend fun makeTransfer(transferPayload: TransferPayload?): ResponseBody {
return baseApiManager.savingAccountsListApi.makeTransfer(transferPayload)
}

Expand Down Expand Up @@ -193,7 +193,7 @@ class DataManager @Inject constructor(
suspend fun thirdPartyTransferTemplate(): AccountOptionsTemplate =
baseApiManager.thirdPartyTransferApi.accountTransferTemplate()

suspend fun makeThirdPartyTransfer(transferPayload: TransferPayload?): Response<ResponseBody?>? {
suspend fun makeThirdPartyTransfer(transferPayload: TransferPayload?): ResponseBody {
return baseApiManager.thirdPartyTransferApi.makeTransfer(transferPayload)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface SavingAccountsListService {
suspend fun accountTransferTemplate(): AccountOptionsTemplate

@POST(ApiEndPoints.ACCOUNT_TRANSFER)
suspend fun makeTransfer(@Body transferPayload: TransferPayload?): Response<ResponseBody?>?
suspend fun makeTransfer(@Body transferPayload: TransferPayload?): ResponseBody

@GET(ApiEndPoints.SAVINGS_ACCOUNTS + "/template")
suspend fun getSavingsAccountApplicationTemplate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ interface ThirdPartyTransferService {
suspend fun accountTransferTemplate(): AccountOptionsTemplate

@POST(ApiEndPoints.ACCOUNT_TRANSFER + "?type=tpt")
suspend fun makeTransfer(@Body transferPayload: TransferPayload?): Response<ResponseBody?>?
suspend fun makeTransfer(@Body transferPayload: TransferPayload?): ResponseBody
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.mifos.mobile.repositories

import kotlinx.coroutines.flow.Flow
import okhttp3.ResponseBody
import org.mifos.mobile.ui.enums.TransferType
import retrofit2.Response
Expand All @@ -23,5 +24,5 @@ interface TransferRepository {
fromAccountNumber: String?,
toAccountNumber: String?,
transferType: TransferType?
): Response<ResponseBody?>?
): Flow<ResponseBody>
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.mifos.mobile.repositories

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import okhttp3.ResponseBody
import org.mifos.mobile.api.DataManager
import org.mifos.mobile.models.payload.TransferPayload
Expand All @@ -26,7 +28,7 @@ class TransferRepositoryImp @Inject constructor(private val dataManager: DataMan
fromAccountNumber: String?,
toAccountNumber: String?,
transferType: TransferType?
): Response<ResponseBody?>? {
): Flow<ResponseBody> {
val transferPayload = TransferPayload().apply {
this.fromOfficeId = fromOfficeId
this.fromClientId = fromClientId
Expand All @@ -44,9 +46,13 @@ class TransferRepositoryImp @Inject constructor(private val dataManager: DataMan
this.fromAccountNumber = fromAccountNumber
this.toAccountNumber = toAccountNumber
}
return when (transferType) {
TransferType.SELF -> dataManager.makeTransfer(transferPayload)
else -> dataManager.makeThirdPartyTransfer(transferPayload)
return flow {
emit(
when (transferType) {
TransferType.SELF -> dataManager.makeTransfer(transferPayload)
else -> dataManager.makeThirdPartyTransfer(transferPayload)
}
)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,27 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import org.mifos.mobile.R
import org.mifos.mobile.databinding.FragmentTransferProcessBinding
import org.mifos.mobile.models.payload.TransferPayload
import org.mifos.mobile.models.templates.account.AccountOption
import org.mifos.mobile.ui.activities.SavingsAccountContainerActivity
import org.mifos.mobile.ui.enums.TransferType
import org.mifos.mobile.ui.fragments.base.BaseFragment
import org.mifos.mobile.utils.*
import org.mifos.mobile.utils.Constants
import org.mifos.mobile.utils.CurrencyUtil
import org.mifos.mobile.utils.DateHelper
import org.mifos.mobile.utils.MFErrorParser
import org.mifos.mobile.utils.Network
import org.mifos.mobile.utils.Toaster
import org.mifos.mobile.utils.TransferUiState
import org.mifos.mobile.utils.getTodayFormatted
import org.mifos.mobile.viewModels.TransferProcessViewModel

/**
Expand All @@ -25,7 +36,8 @@ class TransferProcessFragment : BaseFragment() {

private var _binding: FragmentTransferProcessBinding? = null
private val binding get() = _binding!!
private lateinit var viewModel: TransferProcessViewModel

private val viewModel: TransferProcessViewModel by viewModels()

private var toAccountOption: AccountOption? = null
private var fromAccountOption: AccountOption? = null
Expand All @@ -47,7 +59,6 @@ class TransferProcessFragment : BaseFragment() {
): View {
_binding = FragmentTransferProcessBinding.inflate(inflater, container, false)
setToolbarTitle(getString(R.string.transfer))
viewModel = ViewModelProvider(this)[TransferProcessViewModel::class.java]
with(binding) {
tvAmount.text = CurrencyUtil.formatCurrency(activity, payload?.transferAmount)
tvPayFrom.text = payload?.fromAccountNumber.toString()
Expand All @@ -62,16 +73,24 @@ class TransferProcessFragment : BaseFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

viewModel.transferUiState.observe(viewLifecycleOwner) {
when (it) {
is TransferUiState.Loading -> showProgress()
is TransferUiState.TransferSuccess -> {
hideProgress()
showTransferredSuccessfully()
}
is TransferUiState.Error -> {
hideProgress()
showError(MFErrorParser.errorMessage(it.errorMessage))
viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.transferUiState.collect {
when (it) {
is TransferUiState.Loading -> showProgress()

is TransferUiState.TransferSuccess -> {
hideProgress()
showTransferredSuccessfully()
}

is TransferUiState.Error -> {
hideProgress()
showError(MFErrorParser.errorMessage(it.errorMessage))
}

TransferUiState.Initial -> {}
}
}
}
}
Expand Down Expand Up @@ -147,7 +166,7 @@ class TransferProcessFragment : BaseFragment() {
/**
* Shows a {@link Snackbar} on succesfull transfer of money
*/
fun showTransferredSuccessfully() {
private fun showTransferredSuccessfully() {
Toaster.show(binding.root, getString(R.string.transferred_successfully))
binding.ivSuccess.visibility = View.VISIBLE
(binding.ivSuccess.drawable as Animatable).start()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.mifos.mobile.utils

sealed class TransferUiState {
object Initial : TransferUiState()
object Loading : TransferUiState()
object TransferSuccess : TransferUiState()
data class Error(val errorMessage: Throwable) : TransferUiState()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.mifos.mobile.viewModels

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.launch
import org.mifos.mobile.repositories.TransferRepository
import org.mifos.mobile.ui.enums.TransferType
Expand All @@ -15,8 +16,8 @@ import javax.inject.Inject
class TransferProcessViewModel @Inject constructor(private val transferRepositoryImp: TransferRepository) :
ViewModel() {

private val _transferUiState = MutableLiveData<TransferUiState>()
val transferUiState: LiveData<TransferUiState> get() = _transferUiState
private val _transferUiState = MutableStateFlow<TransferUiState>(TransferUiState.Initial)
val transferUiState: StateFlow<TransferUiState> get() = _transferUiState

fun makeTransfer(
fromOfficeId: Int?,
Expand All @@ -38,7 +39,7 @@ class TransferProcessViewModel @Inject constructor(private val transferRepositor
) {
viewModelScope.launch {
_transferUiState.value = TransferUiState.Loading
val response = transferRepositoryImp.makeTransfer(
transferRepositoryImp.makeTransfer(
fromOfficeId,
fromClientId,
fromAccountType,
Expand All @@ -55,13 +56,10 @@ class TransferProcessViewModel @Inject constructor(private val transferRepositor
fromAccountNumber,
toAccountNumber,
transferType
)
try {
if (response?.isSuccessful == true) {
_transferUiState.value = TransferUiState.TransferSuccess
}
} catch (e: Throwable) {
).catch { e ->
_transferUiState.value = TransferUiState.Error(e)
}.collect {
_transferUiState.value = TransferUiState.TransferSuccess
}
}
}
Expand Down

0 comments on commit b4cb777

Please sign in to comment.