Skip to content

Commit

Permalink
Add support for buy data to home screen and buy data, borrow airtime,…
Browse files Browse the repository at this point in the history
… and data balance to SIM cards
  • Loading branch information
davkutalek committed Nov 22, 2023
1 parent 6fff17a commit 30cf2b2
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import androidx.core.content.ContextCompat
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.hover.sdk.actions.HoverAction.BALANCE
import com.hover.stax.R
import com.hover.stax.databinding.FragmentAccountBinding
import com.hover.stax.domain.model.Account
Expand Down Expand Up @@ -218,7 +219,7 @@ class AccountDetailFragment :
}

private fun onTapBalanceRefresh(account: Account?) {
balancesViewModel.requestBalance(account)
balancesViewModel.requestAction(account, BALANCE)
}

private fun setUpRemoveAccount(account: Account) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ data class SimWithAccount(
val sim: SimInfo,
val account: Account,
val balanceAction: HoverAction?,
val airtimeActions: List<HoverAction?> = emptyList()
val airtimeActions: List<HoverAction?> = emptyList(),
val dataActions: List<HoverAction?> = emptyList()
)

class ListSimsUseCase(
Expand All @@ -45,15 +46,17 @@ class ListSimsUseCase(
var account = accountRepository.getAccountBySim(sim.subscriptionId)
var balanceAct: HoverAction? = null
var airtimeActs: List<HoverAction> = emptyList()
var dataActs: List<HoverAction> = emptyList()

if (account == null)
account = accountRepository.createAccount(sim)

if (account.channelId != -1) {
balanceAct = actionRepository.getFirstAction(account.institutionId, account.countryAlpha2, HoverAction.BALANCE)
airtimeActs = actionRepository.getActionsByRecipientInstitution(account.institutionId, account.countryAlpha2, HoverAction.AIRTIME)
dataActs = actionRepository.getActionsByRecipientInstitution(account.institutionId, account.countryAlpha2, HoverAction.BUY_DATA)
}
result.add(SimWithAccount(sim, account, balanceAct, airtimeActs))
result.add(SimWithAccount(sim, account, balanceAct, airtimeActs, dataActs))
}
result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,27 @@ class BalancesViewModel(
getAccounts()
}

fun requestBalance(account: Account?) {
fun requestAction(account: Account?, type: String) {
if (account == null) {
AnalyticsUtil.logAnalyticsEvent(
(getApplication() as Context).getString(R.string.refresh_balance_failed),
(getApplication() as Context).getString(R.string.action_failed, type),
getApplication()
)
Toast.makeText(getApplication(), R.string.refresh_balance_failed, Toast.LENGTH_LONG).show()
Toast.makeText(getApplication(), (getApplication() as Context).getString(R.string.action_failed, type), Toast.LENGTH_LONG).show()
} else {
AnalyticsUtil.logAnalyticsEvent(
(getApplication() as Context).getString(R.string.refresh_balance),
(getApplication() as Context).getString(R.string.action_starting, type),
getApplication()
)
userRequestedBalanceAccount.value = account
startBalanceActionFor(userRequestedBalanceAccount.value)
startActionFor(userRequestedBalanceAccount.value, type)
}
}

private fun startBalanceActionFor(account: Account?) = viewModelScope.launch(Dispatchers.IO) {
private fun startActionFor(account: Account?, type: String) = viewModelScope.launch(Dispatchers.IO) {
if (account == null) return@launch

val action = actionRepo.getFirstAction(account.institutionId, account.countryAlpha2, HoverAction.BALANCE)
val action = actionRepo.getFirstAction(account.institutionId, account.countryAlpha2, type)
action?.let { _balanceAction.emit(action) } ?: run { _actionRunError.send((getApplication() as Context).getString(R.string.error_running_action)) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class HomeFragment : AbstractBalanceCheckerFragment(), FinancialTipClickInterfac
private fun getHomeClickFunctions(): HomeClickFunctions {
fun onSendMoneyClicked() = navigateTo(getTransferDirection(HoverAction.P2P))
fun onBuyAirtimeClicked() = navigateTo(getTransferDirection(HoverAction.AIRTIME))
fun onBuyDataClicked() = navigateTo(getTransferDirection(HoverAction.BUY_DATA))
fun onBuyGoodsClicked() = navigateTo(HomeFragmentDirections.actionNavigationHomeToMerchantFragment())
fun onPayBillClicked() = navigateTo(HomeFragmentDirections.actionNavigationHomeToPaybillFragment())
fun onRequestMoneyClicked() = navigateTo(HomeFragmentDirections.actionNavigationHomeToNavigationRequest())
Expand All @@ -81,6 +82,7 @@ class HomeFragment : AbstractBalanceCheckerFragment(), FinancialTipClickInterfac
return HomeClickFunctions(
onSendMoneyClicked = { onSendMoneyClicked() },
onBuyAirtimeClicked = { onBuyAirtimeClicked() },
onBuyDataClicked = { onBuyDataClicked() },
onBuyGoodsClicked = { onBuyGoodsClicked() },
onPayBillClicked = { onPayBillClicked() },
onRequestMoneyClicked = { onRequestMoneyClicked() },
Expand Down Expand Up @@ -153,7 +155,7 @@ class HomeFragment : AbstractBalanceCheckerFragment(), FinancialTipClickInterfac
}

override fun onTapBalanceRefresh(account: Account?) {
balancesViewModel.requestBalance(account)
balancesViewModel.requestAction(account, HoverAction.BALANCE)
}

override fun onTapBalanceDetail(accountId: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import com.hover.stax.utils.AnalyticsUtil
data class HomeClickFunctions(
val onSendMoneyClicked: () -> Unit,
val onBuyAirtimeClicked: () -> Unit,
val onBuyDataClicked: () -> Unit,
val onBuyGoodsClicked: () -> Unit,
val onPayBillClicked: () -> Unit,
val onRequestMoneyClicked: () -> Unit,
Expand Down Expand Up @@ -109,6 +110,7 @@ fun HomeScreen(
PrimaryFeatures(
onSendMoneyClicked = homeClickFunctions.onSendMoneyClicked,
onBuyAirtimeClicked = homeClickFunctions.onBuyAirtimeClicked,
onBuyDataClicked = homeClickFunctions.onBuyDataClicked,
onBuyGoodsClicked = homeClickFunctions.onBuyGoodsClicked,
onPayBillClicked = homeClickFunctions.onPayBillClicked,
onRequestMoneyClicked = homeClickFunctions.onRequestMoneyClicked,
Expand Down Expand Up @@ -210,6 +212,7 @@ fun HomeScreenPreview() {
PrimaryFeatures(
onSendMoneyClicked = { },
onBuyAirtimeClicked = { },
onBuyDataClicked = { },
onBuyGoodsClicked = { },
onPayBillClicked = { },
onRequestMoneyClicked = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.hover.stax.R
fun PrimaryFeatures(
onSendMoneyClicked: () -> Unit,
onBuyAirtimeClicked: () -> Unit,
onBuyDataClicked: () -> Unit,
onBuyGoodsClicked: () -> Unit,
onPayBillClicked: () -> Unit,
onRequestMoneyClicked: () -> Unit,
Expand All @@ -49,6 +50,11 @@ fun PrimaryFeatures(
drawable = R.drawable.ic_system_upate_24,
stringRes = R.string.cta_airtime
)
VerticalImageTextView(
onItemClick = onBuyDataClicked,
drawable = R.drawable.ic_data,
stringRes = R.string.cta_buy_data
)
if (showKenyaFeatures) {
VerticalImageTextView(
onItemClick = onBuyGoodsClicked,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ class SimFragment : AbstractBalanceCheckerFragment(), BalanceTapListener {

setContent {
SimScreen(
refreshBalance = { acct -> balancesViewModel.requestBalance(acct) },
refreshBalance = { acct -> balancesViewModel.requestAction(acct, HoverAction.BALANCE) },
buyAirtime = { navigateTo(SimFragmentDirections.toTransferFragment(HoverAction.AIRTIME)) },
navTo = { dest -> navigateTo(dest) }
navTo = { dest -> navigateTo(dest) },
refreshDataBalance = { acct -> balancesViewModel.requestAction(acct, HoverAction.DATA_BALANCE) },
buyData = { acct -> balancesViewModel.requestAction(acct, HoverAction.BUY_DATA) },
borrow = { acct -> balancesViewModel.requestAction(acct, HoverAction.BORROW_AIRTIME) },
)
}
}
Expand Down Expand Up @@ -82,7 +85,7 @@ class SimFragment : AbstractBalanceCheckerFragment(), BalanceTapListener {
(requireActivity() as MainActivity).checkPermissionsAndNavigate(navDirections)

override fun onTapBalanceRefresh(account: Account?) {
balancesViewModel.requestBalance(account)
balancesViewModel.requestAction(account, HoverAction.BALANCE)
}

override fun onTapBalanceDetail(accountId: Int) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ fun SimScreen(
refreshBalance: (Account) -> Unit,
buyAirtime: (Account) -> Unit,
navTo: (dest: Int) -> Unit,
refreshDataBalance: (Account) -> Unit,
buyData: (Account) -> Unit,
borrow: (Account) -> Unit,
simViewModel: SimViewModel = getViewModel()
) {
val sims by simViewModel.sims.collectAsState()
Expand Down Expand Up @@ -92,7 +95,7 @@ fun SimScreen(
items(sims.sortedWith(comparator)) { sim ->
// Don't show removed SIM cards that we don't support, it is confusing
if (sim.account.channelId != -1 || sim.sim.slotIdx != -1)
SimItem(sim, refreshBalance, buyAirtime)
SimItem(sim, refreshBalance, buyAirtime, refreshDataBalance, buyData, borrow)
}
}
}
Expand Down Expand Up @@ -135,7 +138,7 @@ private fun SimScreenPreview(
itemsIndexed(sims) { index, sim ->
SimItem(
simWithAccount = sim,
{ }, { }
{ }, { }, { }, { }, { }
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@
package com.hover.stax.presentation.sims.components

import android.content.Context
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
Expand Down Expand Up @@ -55,7 +50,10 @@ import com.hover.stax.utils.Utils
internal fun SimItem(
simWithAccount: SimWithAccount,
refreshBalance: (Account) -> Unit,
buyAirtime: (Account) -> Unit
buyAirtime: (Account) -> Unit,
dataBalance: (Account) -> Unit,
buyData: (Account) -> Unit,
borrow: (Account) -> Unit
) {
StaxCard {
val context = LocalContext.current
Expand Down Expand Up @@ -92,17 +90,59 @@ internal fun SimItem(

Spacer(modifier = Modifier.height(32.dp))

if (simWithAccount.account.channelId == -1) {
SecondaryButton(
context.getString(R.string.email_support), null,
onClick = { emailStax(simWithAccount, context) }
)
} else {
val bonus = getBonus(simWithAccount.airtimeActions)
SecondaryButton(
getAirtimeButtonLabel(bonus, context), getAirtimeButtonIcon(bonus),
onClick = { buyAirtime(simWithAccount.account) }
)
Row(
modifier = Modifier.padding(bottom = dimensionResource(id = R.dimen.margin_13)),
verticalAlignment = Alignment.CenterVertically

) {
Column(modifier = Modifier.weight(1f)) {

if (simWithAccount.account.channelId == -1) {
SecondaryButton(
context.getString(R.string.email_support), null,
onClick = { emailStax(simWithAccount, context) }
)
} else {
val bonus = getBonus(simWithAccount.airtimeActions)
SecondaryButton(
getAirtimeButtonLabel(bonus, context), null,
onClick = { buyAirtime(simWithAccount.account) }
)
}
}

Column(modifier = Modifier.weight(1f),
horizontalAlignment = Alignment.End) {
SecondaryButton(
context.getString(R.string.cta_borrow),
null,
onClick = { borrow(simWithAccount.account) }
)
}
}

Row(
modifier = Modifier.padding(bottom = dimensionResource(id = R.dimen.margin_13)),
verticalAlignment = Alignment.CenterVertically

) {

Column(modifier = Modifier.weight(1f)) {
SecondaryButton(
context.getString(R.string.cta_data_balance),
null,
onClick = { dataBalance(simWithAccount.account) }
)
}

Column(modifier = Modifier.weight(1f),
horizontalAlignment = Alignment.End) {
SecondaryButton(
context.getString(R.string.cta_buy_data),
null,
onClick = { buyData(simWithAccount.account) }
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,10 @@ abstract class AbstractFormFragment : Fragment() {
}

private fun fabClicked() {
if (accountsViewModel.activeAccount.value != null && !accountsViewModel.isValidAccount())
askToCheckBalance(accountsViewModel.activeAccount.value!!)
else if (validates()) {
// if (accountsViewModel.activeAccount.value != null && !accountsViewModel.isValidAccount())
// askToCheckBalance(accountsViewModel.activeAccount.value!!)
// else
if (validates()) {
if (abstractFormViewModel.isEditing.value == true) {
onFinishForm()
} else {
Expand All @@ -155,7 +156,7 @@ abstract class AbstractFormFragment : Fragment() {
.setDialogTitle(R.string.finish_adding_title)
.setDialogMessage(getString(R.string.finish_adding_desc, account.userAlias))
.setNegButton(R.string.btn_cancel, null)
.setPosButton(R.string.connect_cta) { balancesViewModel.requestBalance(account) }
.setPosButton(R.string.connect_cta) { balancesViewModel.requestAction(account, HoverAction.BALANCE) }
dialog.showIt()
}

Expand All @@ -170,6 +171,7 @@ abstract class AbstractFormFragment : Fragment() {
return when {
isEditing -> getString(R.string.btn_continue)
accountsViewModel.getActionType() == HoverAction.AIRTIME -> getString(R.string.fab_airtimenow)
accountsViewModel.getActionType() == HoverAction.BUY_DATA -> getString(R.string.fab_datanow)
accountsViewModel.getActionType() == HoverAction.P2P ||
accountsViewModel.getActionType() == HoverAction.MERCHANT ||
accountsViewModel.getActionType() == HoverAction.BILL -> getString(R.string.fab_transfernow)
Expand Down
Loading

0 comments on commit 30cf2b2

Please sign in to comment.