Skip to content

Commit

Permalink
fix: wire make payment with send payment
Browse files Browse the repository at this point in the history
  • Loading branch information
therajanmaurya committed May 25, 2024
1 parent a86d8e2 commit 0a35938
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.mifospay.common.navigation

import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavOptions
import androidx.navigation.NavType
import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import org.mifospay.common.ui.MakeTransferScreenRoute
import org.mifospay.payments.send.navigation.PAYEE_EXTERNAL_ID_ARG
import org.mifospay.payments.send.navigation.TRANSFER_AMOUNT_ARG

const val MAKE_TRANSFER_ROUTE_BASE = "make_transfer_route"
const val MAKE_TRANSFER_ROUTE = MAKE_TRANSFER_ROUTE_BASE +
"?${PAYEE_EXTERNAL_ID_ARG}={$PAYEE_EXTERNAL_ID_ARG}" +
"&${TRANSFER_AMOUNT_ARG}={$TRANSFER_AMOUNT_ARG}"

fun NavController.navigateToMakeTransferScreen(
externalId: String? = null,
transferAmount: String? = null,
navOptions: NavOptions? = null
) {
val route = MAKE_TRANSFER_ROUTE_BASE + if (transferAmount != null) {
"?${PAYEE_EXTERNAL_ID_ARG}=$externalId" +
"&${TRANSFER_AMOUNT_ARG}=$transferAmount"
} else {
"?${PAYEE_EXTERNAL_ID_ARG}=$externalId" +
"&${TRANSFER_AMOUNT_ARG}=${"0.0"}"
}
navigate(route, navOptions)
}

fun NavGraphBuilder.makeTransferScreen() {
composable(
route = MAKE_TRANSFER_ROUTE,
arguments = listOf(
navArgument(PAYEE_EXTERNAL_ID_ARG) {
type = NavType.StringType
defaultValue = null
nullable = true
},
navArgument(TRANSFER_AMOUNT_ARG) {
type = NavType.StringType
defaultValue = null
nullable = true
}
)
) {
MakeTransferScreenRoute()
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
package org.mifospay.common.presenter

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import org.mifospay.core.data.base.UseCase
import org.mifospay.core.data.base.UseCaseHandler
import org.mifospay.core.data.domain.usecase.account.TransferFunds
import org.mifospay.core.data.domain.usecase.client.SearchClient
import org.mifospay.data.local.LocalRepository
import org.mifospay.payments.send.navigation.PAYEE_EXTERNAL_ID_ARG
import org.mifospay.payments.send.navigation.TRANSFER_AMOUNT_ARG
import javax.inject.Inject

@HiltViewModel
class MakeTransferViewModel @Inject constructor(
private val mUsecaseHandler: UseCaseHandler,
savedStateHandle: SavedStateHandle,
private val useCaseHandler: UseCaseHandler,
private val searchClientUseCase: SearchClient,
private val transferFundsUseCase: TransferFunds,
private val localRepository: LocalRepository?
private val localRepository: LocalRepository
) : ViewModel() {

private val payeeExternalId: StateFlow<String> = savedStateHandle.getStateFlow(PAYEE_EXTERNAL_ID_ARG, "")
private val transferAmount: StateFlow<String?> = savedStateHandle.getStateFlow(TRANSFER_AMOUNT_ARG, null)

private val _makeTransferState = MutableStateFlow<MakeTransferState>(MakeTransferState.Loading)
val makeTransferState: StateFlow<MakeTransferState> = _makeTransferState.asStateFlow()

Expand All @@ -32,8 +44,18 @@ class MakeTransferViewModel @Inject constructor(
val showTransactionStatus: StateFlow<ShowTransactionStatus> =
_showTransactionStatus.asStateFlow()

fun fetchClient(externalId: String, transferAmount: Double) {
mUsecaseHandler.execute(searchClientUseCase, SearchClient.RequestValues(externalId),

// Fetch Payee client details
val fetchPayeeClient = combine(payeeExternalId, transferAmount, ::Pair)
.map {
it.takeIf { it.first.isNotEmpty() }?.let {
fetchClient(it.first, it.second?.toDouble() ?: 0.0)
}
}
.stateIn(scope = viewModelScope, started = SharingStarted.Eagerly, initialValue = null)

private fun fetchClient(externalId: String, transferAmount: Double) {
useCaseHandler.execute(searchClientUseCase, SearchClient.RequestValues(externalId),
object : UseCase.UseCaseCallback<SearchClient.ResponseValue> {
override fun onSuccess(response: SearchClient.ResponseValue) {
val searchResult = response.results[0]
Expand All @@ -55,8 +77,8 @@ class MakeTransferViewModel @Inject constructor(
}

fun makeTransfer(toClientId: Long, amount: Double) {
val fromClientId = localRepository?.clientDetails?.clientId ?: 0
mUsecaseHandler.execute(transferFundsUseCase,
val fromClientId = localRepository.clientDetails.clientId
useCaseHandler.execute(transferFundsUseCase,
TransferFunds.RequestValues(fromClientId, toClientId, amount),
object : UseCase.UseCaseCallback<TransferFunds.ResponseValue> {
override fun onSuccess(response: TransferFunds.ResponseValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.compose.material3.SheetState
import androidx.compose.material3.Text
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.rememberCoroutineScope
Expand Down Expand Up @@ -48,8 +49,9 @@ import org.mifospay.core.designsystem.component.MifosOverlayLoadingWheel

@Composable
fun MakeTransferScreenRoute(
viewModel: MakeTransferViewModel = hiltViewModel(),
viewModel: MakeTransferViewModel = hiltViewModel()
) {
val fetchPayeeClient by viewModel.fetchPayeeClient.collectAsStateWithLifecycle()
val makeTransferState by viewModel.makeTransferState.collectAsStateWithLifecycle()
val showTransactionStatus by viewModel.showTransactionStatus.collectAsStateWithLifecycle()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ fun PaymentScreenContent(

val tabContents = listOf(
TabContent(PaymentsScreenContents.SEND.name) {
SendScreenRoute(onBackClick = {}, showToolBar = false)
SendScreenRoute(
onBackClick = {},
showToolBar = false,
proceedWithMakeTransferFlow = { externalId, transferAmount ->

}
)
},
TabContent(PaymentsScreenContents.REQUEST.name) {
RequestScreen(showQr = { showQr.invoke(vpa) })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.navigation.compose.NavHost
import org.mifospay.common.Constants
import org.mifospay.common.navigation.makeTransferScreen
import org.mifospay.common.navigation.navigateToMakeTransferScreen
import org.mifospay.editprofile.ui.EditProfileActivity
import org.mifospay.home.navigation.HOME_ROUTE
import org.mifospay.home.navigation.financeScreen
Expand Down Expand Up @@ -59,8 +61,12 @@ fun MifosNavHost(
onSettings = { context.startActivitySettings() }
)
sendMoneyScreen(
onBackClick = navController::popBackStack
onBackClick = navController::popBackStack,
proceedWithMakeTransferFlow = { externalId, transferAmount ->
navController.navigateToMakeTransferScreen(externalId, transferAmount)
}
)
makeTransferScreen()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ class SendPaymentViewModel @Inject constructor(
onAnyError(R.string.insufficient_balance)
} else {
if (externalId != null) {
proceedWithTransferFlow(
externalId,
transferAmount
)
proceedWithTransferFlow(externalId, transferAmount)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ enum class SendMethodType {
@Composable
fun SendScreenRoute(
viewModel: SendPaymentViewModel = hiltViewModel(),
makeTransferViewModel: MakeTransferViewModel = hiltViewModel(),
showToolBar: Boolean,
onBackClick: () -> Unit
onBackClick: () -> Unit,
proceedWithMakeTransferFlow: (String, String) -> Unit
) {
val context = LocalContext.current
val selfVpa by viewModel.vpa.collectAsStateWithLifecycle()
Expand Down Expand Up @@ -100,7 +100,7 @@ fun SendScreenRoute(
showToast(context.getString(it))
},
proceedWithTransferFlow = { externalId, transferAmount ->
makeTransferViewModel.fetchClient(externalId, transferAmount)
proceedWithMakeTransferFlow.invoke(externalIdOrMobile, transferAmount.toString())
}
)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@ import androidx.navigation.compose.composable
import org.mifospay.payments.send.SendScreenRoute


const val PAYEE_EXTERNAL_ID_ARG = "payeeExternalId"
const val TRANSFER_AMOUNT_ARG = "transferAmount"
const val SEND_MONEY_ROUTE = "send_money_route"

fun NavController.navigateToSendMoneyScreen(
navOptions: NavOptions? = null
) = navigate(SEND_MONEY_ROUTE, navOptions)

fun NavGraphBuilder.sendMoneyScreen(
proceedWithMakeTransferFlow: (String, String?) -> Unit,
onBackClick: () -> Unit
) {
composable(route = SEND_MONEY_ROUTE) {
SendScreenRoute(
showToolBar = true,
onBackClick = onBackClick
onBackClick = onBackClick,
proceedWithMakeTransferFlow = proceedWithMakeTransferFlow
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ import com.google.android.material.textfield.TextInputLayout
import dagger.hilt.android.AndroidEntryPoint
import org.mifospay.R
import org.mifospay.base.BaseFragment
import org.mifospay.common.ui.MakeTransferFragment.Companion.newInstance
import org.mifospay.home.BaseHomeContract
import org.mifospay.qr.ui.ReadQrActivity
import org.mifospay.common.Constants
import org.mifospay.payments.TransferViewModel
import org.mifospay.utils.Toaster
import org.mifospay.common.Utils.hideSoftKeyboard
import org.mifospay.common.ui.MakeTransferFragment

/**
* Created by naman on 30/8/17.
Expand Down Expand Up @@ -330,7 +330,7 @@ class SendFragment : BaseFragment(), BaseHomeContract.TransferView {

override fun showMobile(mobileNo: String?) {}
override fun showClientDetails(externalId: String?, amount: Double) {
val fragment = newInstance(externalId, amount)
val fragment = MakeTransferFragment.newInstance(externalId, amount)
fragment.setTargetFragment(this, REQUEST_SHOW_DETAILS)
if (parentFragment != null) {
fragment.show(childFragmentManager, Constants.MAKE_TRANSFER_FRAGMENT)
Expand Down

0 comments on commit 0a35938

Please sign in to comment.