Skip to content
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
@@ -0,0 +1,17 @@
package com.gemwallet.android.features.rewards.presents

import com.gemwallet.android.features.rewards.viewmodels.models.RewardsSheetType
import uniffi.gemstone.GemRewardsRedemption

internal sealed interface RewardsAction {
data object Refresh : RewardsAction
data object Close : RewardsAction
data object SelectWallet : RewardsAction
data object InviteFriends : RewardsAction
data object CodeHandled : RewardsAction
data object DismissSheet : RewardsAction
data class ShowSheet(val sheet: RewardsSheetType) : RewardsAction
data class CreateReferral(val username: String) : RewardsAction
data class UseCode(val code: String) : RewardsAction
data class Redeem(val redemption: GemRewardsRedemption) : RewardsAction
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
Expand All @@ -33,9 +29,9 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.gemwallet.android.features.rewards.presents.components.rewardsHead
import com.gemwallet.android.features.rewards.presents.components.rewardsInfo
import com.gemwallet.android.features.rewards.presents.dialogs.CreateRewardsCodeDialog
import com.gemwallet.android.features.rewards.presents.dialogs.RedeemRewardsCodeDialog
import com.gemwallet.android.features.rewards.presents.dialogs.RewardsCodeDialog
import com.gemwallet.android.features.rewards.viewmodels.models.RewardsSectionUIModel
import com.gemwallet.android.features.rewards.viewmodels.models.RewardsSheetType
import com.gemwallet.android.model.AuthRequest
import com.gemwallet.android.model.text
import com.gemwallet.android.ui.R
Expand All @@ -48,7 +44,6 @@ import com.gemwallet.android.ui.components.list_item.ListItemModel
import com.gemwallet.android.ui.components.list_item.listItem
import com.gemwallet.android.ui.components.screen.PullToRefreshBox
import com.gemwallet.android.ui.components.screen.Scene
import com.gemwallet.android.ui.components.screen.showSnackbar
import com.gemwallet.android.ui.icons.AppIcons
import com.gemwallet.android.ui.models.ListPosition
import com.gemwallet.android.ui.models.buttonState
Expand All @@ -58,7 +53,6 @@ import com.gemwallet.android.ui.theme.WalletTheme
import com.gemwallet.android.ui.theme.paddingDefault
import com.gemwallet.android.ui.theme.paddingSmall
import com.gemwallet.android.ui.theme.sceneContentPadding
import kotlinx.coroutines.launch
import uniffi.gemstone.GemEmptyStateKind
import uniffi.gemstone.GemIncomingCode
import uniffi.gemstone.GemListRow
Expand All @@ -72,7 +66,7 @@ import uniffi.gemstone.GemRewardsWallet
private val referralCodeMaxWidth = 250.dp

@Composable
fun RewardsScene(
internal fun RewardsScene(
state: GemLoadState,
isRefreshing: Boolean,
wallet: GemRewardsWallet?,
Expand All @@ -84,44 +78,19 @@ fun RewardsScene(
inviteDescription: String,
sections: List<RewardsSectionUIModel>,
redemptions: List<GemRewardsRedemption>,
sheet: RewardsSheetType?,
isLoading: Boolean,
incomingCode: GemIncomingCode? = null,
onUsername: (String, (Throwable?) -> Unit) -> Unit,
onCode: (String, (Throwable?) -> Unit) -> Unit,
onCodeHandled: () -> Unit,
onRefresh: () -> Unit,
onWallet: () -> Unit,
onRedeem: (GemRewardsRedemption) -> Unit,
onClose: () -> Unit,
onInvite: () -> Unit,
onError: (Throwable) -> Unit,
snackbar: SnackbarHostState = remember { SnackbarHostState() },
onAction: (RewardsAction) -> Unit,
) {
val context = LocalContext.current

var getStartedDialogShow by remember(inviteAction) { mutableStateOf(false) }
var codeDialogShow by remember { mutableStateOf(false) }
var codeRetryShow by remember { mutableStateOf(false) }
val referralCode = when (incomingCode) {
is GemIncomingCode.Activate -> incomingCode.code
is GemIncomingCode.Confirm -> incomingCode.code
null -> null
}

val successStr = stringResource(R.string.common_done)
val scope = rememberCoroutineScope()

val onCodeResult = fun (error: Throwable?) {
when (error) {
null -> scope.launch { snackbar.showSnackbar(successStr, R.drawable.ic_check_circle) }
else -> onError(error)
}
}

LaunchedEffect(incomingCode) {
val code = (incomingCode as? GemIncomingCode.Activate)?.code ?: return@LaunchedEffect
context.requestAuth(AuthRequest.Default, onCancel = { codeRetryShow = true }) {
onCodeHandled()
onCode(code, onCodeResult)
context.requestAuth(AuthRequest.Default, onCancel = { onAction(RewardsAction.ShowSheet(RewardsSheetType.ActivateCode(code))) }) {
onAction(RewardsAction.CodeHandled)
onAction(RewardsAction.UseCode(code))
}
}

Expand All @@ -136,7 +105,7 @@ fun RewardsScene(
.padding(horizontal = sceneContentPadding())
.clip(RoundedCornerShape(paddingDefault))
.background(MaterialTheme.colorScheme.primary, RoundedCornerShape(paddingDefault))
.clickable(onWallet)
.clickable { onAction(RewardsAction.SelectWallet) }
.padding(start = paddingDefault, end = paddingSmall)
.padding(vertical = paddingSmall),
verticalAlignment = Alignment.CenterVertically,
Expand All @@ -149,13 +118,13 @@ fun RewardsScene(
)
Icon(
imageVector = AppIcons.KeyboardArrowDown,
contentDescription = "select wallet",
contentDescription = null,
tint = MaterialTheme.colorScheme.onPrimary,
)
}
}
},
onClose = onClose,
onClose = { onAction(RewardsAction.Close) },
) {
if (state == GemLoadState.Loading) {
Box(modifier = Modifier.fillMaxSize()) {
Expand All @@ -165,9 +134,11 @@ fun RewardsScene(
}
PullToRefreshBox(
isRefreshing = isRefreshing,
onRefresh = onRefresh,
onRefresh = { onAction(RewardsAction.Refresh) },
) {
val onGetStarted: () -> Unit = { getStartedDialogShow = true }
val onGetStarted: () -> Unit = { onAction(RewardsAction.ShowSheet(RewardsSheetType.CreateCode)) }
val onShare: () -> Unit = { onAction(RewardsAction.InviteFriends) }
val onRedeem: (GemRewardsRedemption) -> Unit = { onAction(RewardsAction.Redeem(it)) }
LazyColumn(modifier = Modifier.fillMaxSize()) {
when (state) {
GemLoadState.NoData -> {
Expand All @@ -187,7 +158,7 @@ fun RewardsScene(
intro = introItems,
action = inviteAction,
onGetStarted = onGetStarted,
onShare = onInvite,
onShare = onShare,
)

if (canUseReferralCode) {
Expand All @@ -200,7 +171,7 @@ fun RewardsScene(
containerColor = Color.White,
contentColor = Color.Black,
),
) { codeDialogShow = true }
) { onAction(RewardsAction.ShowSheet(RewardsSheetType.ActivateCode(""))) }
}
Spacer8()
Text(
Expand All @@ -223,7 +194,7 @@ fun RewardsScene(
title = stringResource(R.string.transfer_confirm),
state = buttonState(enabled = pending.isEnabled),
) {
context.requestAuth(AuthRequest.Default) { onCode(pending.code, onCodeResult) }
context.requestAuth(AuthRequest.Default) { onAction(RewardsAction.UseCode(pending.code)) }
}
}
}
Expand All @@ -234,19 +205,26 @@ fun RewardsScene(
}
}

CreateRewardsCodeDialog(isVisible = getStartedDialogShow, onUsername = onUsername) {
getStartedDialogShow = false
}
RewardsCodeDialog(
isVisible = sheet == RewardsSheetType.CreateCode,
title = stringResource(R.string.rewards_create_referral_code_title),
label = stringResource(R.string.rewards_username),
text = "",
info = stringResource(R.string.rewards_create_referral_code_info),
isLoading = isLoading,
onDone = { onAction(RewardsAction.CreateReferral(it)) },
onDismiss = { onAction(RewardsAction.DismissSheet) },
)

RedeemRewardsCodeDialog(
isVisible = codeDialogShow || codeRetryShow || incomingCode is GemIncomingCode.Confirm,
referralCode = referralCode,
onCode = onCode,
) {
codeDialogShow = false
codeRetryShow = false
onCodeHandled()
}
RewardsCodeDialog(
isVisible = sheet is RewardsSheetType.ActivateCode,
title = stringResource(R.string.rewards_referral_code),
label = stringResource(R.string.rewards_referral_code),
text = (sheet as? RewardsSheetType.ActivateCode)?.code.orEmpty(),
isLoading = isLoading,
onDone = { onAction(RewardsAction.UseCode(it)) },
onDismiss = { onAction(RewardsAction.DismissSheet) },
)
}

@Preview
Expand All @@ -265,15 +243,9 @@ private fun RewardsScenePreview() {
inviteDescription = "Invite friends and earn 100 points",
sections = emptyList(),
redemptions = emptyList(),
onUsername = { _, _ -> },
onCode = { _, _ -> },
onCodeHandled = {},
onRefresh = {},
onWallet = {},
onRedeem = {},
onClose = {},
onInvite = {},
onError = {},
sheet = null,
isLoading = false,
onAction = {},
)
}
}
Expand All @@ -294,15 +266,9 @@ private fun RewardsSceneNoRewardsPreview() {
inviteDescription = "Invite friends and earn 100 points",
sections = emptyList(),
redemptions = emptyList(),
onUsername = { _, _ -> },
onCode = { _, _ -> },
onCodeHandled = {},
onRefresh = {},
onWallet = {},
onRedeem = {},
onClose = {},
onInvite = {},
onError = {},
sheet = null,
isLoading = false,
onAction = {},
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,35 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.gemwallet.android.ext.errorText
import com.gemwallet.android.features.rewards.viewmodels.RewardsViewModel
import com.gemwallet.android.model.AuthRequest
import com.gemwallet.android.ui.R
import com.gemwallet.android.ui.components.InfoBottomSheet
import com.gemwallet.android.ui.components.infoSheet
import com.gemwallet.android.ui.components.list_item.walletSections
import com.gemwallet.android.ui.components.screen.ModalBottomSheet
import com.gemwallet.android.ui.components.screen.showSnackbar
import com.gemwallet.android.ui.localization.text
import com.gemwallet.android.ui.components.screen.ToastEffect
import com.gemwallet.android.ui.requestAuth
import com.gemwallet.android.ui.shareText
import kotlinx.coroutines.launch

@Composable
fun RewardsScreen(onClose: () -> Unit, viewModel: RewardsViewModel = hiltViewModel()) {
val infoSheet by viewModel.infoSheet.collectAsStateWithLifecycle()
InfoBottomSheet(item = infoSheet?.infoSheet(), onClose = { viewModel.infoSheet.value = null })
val snackbar = remember { SnackbarHostState() }
val scope = rememberCoroutineScope()
ToastEffect(viewModel.toastEvents, snackbar)
val context = LocalContext.current
val insufficientPointsMessage = stringResource(R.string.rewards_insufficient_points)
val doneMessage = stringResource(R.string.common_done)

var isShowSelectWallets by remember { mutableStateOf(false) }
var showErrorDialog by remember { mutableStateOf<Throwable?>(null) }
var showMessageDialog by remember { mutableStateOf<String?>(null) }

val error by viewModel.error.collectAsStateWithLifecycle()
val sheet by viewModel.sheet.collectAsStateWithLifecycle()
val isLoading by viewModel.isLoading.collectAsStateWithLifecycle()
val wallet by viewModel.wallet.collectAsStateWithLifecycle()
val referralLink by viewModel.referralLink.collectAsStateWithLifecycle()
val state by viewModel.state.collectAsStateWithLifecycle()
Expand Down Expand Up @@ -77,34 +72,36 @@ fun RewardsScreen(onClose: () -> Unit, viewModel: RewardsViewModel = hiltViewMod
inviteDescription = inviteDescription,
sections = sections,
redemptions = redemptions,
onInvite = {
viewModel.inviteFriends(
onInvite = { context.shareText(subject = referralLink.orEmpty(), text = shareText.orEmpty(), chooserTitle = shareTitle) },
)
},
onUsername = viewModel::createReferral,
onCode = viewModel::useCode,
onCodeHandled = viewModel::onCodeHandled,
onRefresh = viewModel::sync,
onWallet = { isShowSelectWallets = true },
onRedeem = {
if (!it.canRedeem) {
showMessageDialog = insufficientPointsMessage
return@RewardsScene
}
context.requestAuth(AuthRequest.Default) {
viewModel.redeem(it) { error ->
if (error == null) {
scope.launch { snackbar.showSnackbar(doneMessage, R.drawable.ic_check_circle) }
} else {
showErrorDialog = error
}
sheet = sheet,
isLoading = isLoading,
snackbar = snackbar,
onAction = { action ->
when (action) {
RewardsAction.Refresh -> viewModel.sync()

RewardsAction.Close -> onClose()

RewardsAction.SelectWallet -> isShowSelectWallets = true

RewardsAction.InviteFriends -> viewModel.inviteFriends {
context.shareText(subject = referralLink.orEmpty(), text = shareText.orEmpty(), chooserTitle = shareTitle)
}

RewardsAction.CodeHandled -> viewModel.onCodeHandled()

RewardsAction.DismissSheet -> viewModel.dismissSheet()

is RewardsAction.ShowSheet -> viewModel.showSheet(action.sheet)

is RewardsAction.CreateReferral -> viewModel.createReferral(action.username)

is RewardsAction.UseCode -> viewModel.useCode(action.code)

is RewardsAction.Redeem -> viewModel.onSelectRedemption(action.redemption) {
context.requestAuth(AuthRequest.Default) { viewModel.redeem(action.redemption.id) }
}
}
},
onClose = onClose,
onError = { showErrorDialog = it },
snackbar = snackbar,
)

ModalBottomSheet(
Expand All @@ -121,25 +118,13 @@ fun RewardsScreen(onClose: () -> Unit, viewModel: RewardsViewModel = hiltViewMod
}
}

if (showMessageDialog != null) {
AlertDialog(
containerColor = MaterialTheme.colorScheme.background,
onDismissRequest = { showMessageDialog = null },
text = { Text(showMessageDialog.orEmpty()) },
confirmButton = {
Button({ showMessageDialog = null }) { Text(stringResource(R.string.common_cancel)) }
},
)
}

if (showErrorDialog != null) {
val message = showErrorDialog?.errorText()?.text(LocalContext.current) ?: stringResource(R.string.transaction_status_failed)
error?.let { message ->
AlertDialog(
containerColor = MaterialTheme.colorScheme.background,
onDismissRequest = { showErrorDialog = null },
onDismissRequest = viewModel::clearError,
text = { Text(message) },
confirmButton = {
Button({ showErrorDialog = null }) { Text(stringResource(R.string.common_cancel)) }
Button(viewModel::clearError) { Text(stringResource(R.string.common_cancel)) }
},
)
}
Expand Down
Loading
Loading