Skip to content

Commit

Permalink
[MBL-1397] Remove unneeded pledge reasons references (#2052)
Browse files Browse the repository at this point in the history
* remove unneeded pledge reasons references from rewards selection viewmodel

* remove unneeded tests and alert dialog

* lint
  • Loading branch information
mtgriego authored Jun 10, 2024
1 parent 1a9059c commit fc9b18c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 440 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,6 @@ class ProjectPageActivity :
val projectData = rewardSelectionUIState.project
val indexOfBackedReward = rewardSelectionUIState.initialRewardIndex
val rewardsList = rewardSelectionUIState.rewardList
val showRewardCarouselAlertDialog = rewardSelectionUIState.showAlertDialog
val selectedReward = rewardSelectionUIState.selectedReward
rewardsSelectionViewModel.sendEvent(expanded, currentPage, projectData)

Expand Down Expand Up @@ -638,13 +637,6 @@ class ProjectPageActivity :
environment = getEnvironment(),
initialRewardCarouselPosition = indexOfBackedReward,
rewardsList = rewardsList,
showRewardCarouselDialog = showRewardCarouselAlertDialog,
onRewardAlertDialogNegativeClicked = {
rewardsSelectionViewModel.onRewardCarouselAlertClicked(wasPositive = false)
},
onRewardAlertDialogPositiveClicked = {
rewardsSelectionViewModel.onRewardCarouselAlertClicked(wasPositive = true)
},
addOns = addOns,
project = projectData.project(),
onRewardSelected = { reward ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import com.kickstarter.models.Reward
import com.kickstarter.models.ShippingRule
import com.kickstarter.models.StoredCard
import com.kickstarter.ui.activities.DisclaimerItems
import com.kickstarter.ui.compose.designsystem.KSAlertDialog
import com.kickstarter.ui.compose.designsystem.KSPrimaryGreenButton
import com.kickstarter.ui.compose.designsystem.KSTheme
import com.kickstarter.ui.compose.designsystem.KSTheme.colors
Expand Down Expand Up @@ -102,9 +101,6 @@ private fun ProjectPledgeButtonAndContainerPreview() {
shippingSelectorIsGone = false,
currentShippingRule = ShippingRule.builder().build(),
onShippingRuleSelected = {},
showRewardCarouselDialog = false,
onRewardAlertDialogPositiveClicked = {},
onRewardAlertDialogNegativeClicked = {},
onConfirmDetailsContinueClicked = {},
selectedRewardAndAddOnList = listOf(),
onBonusSupportMinusClicked = {},
Expand Down Expand Up @@ -134,9 +130,6 @@ fun ProjectPledgeButtonAndFragmentContainer(
currentShippingRule: ShippingRule,
environment: Environment?,
initialRewardCarouselPosition: Int = 0,
showRewardCarouselDialog: Boolean,
onRewardAlertDialogNegativeClicked: () -> Unit,
onRewardAlertDialogPositiveClicked: () -> Unit,
rewardsList: List<Reward>,
addOns: List<Reward>,
project: Project,
Expand Down Expand Up @@ -239,19 +232,6 @@ fun ProjectPledgeButtonAndFragmentContainer(
.padding(padding)
.fillMaxSize()
) {

if (showRewardCarouselDialog) {
KSAlertDialog(
setShowDialog = { },
headlineText = stringResource(id = R.string.Continue_with_this_reward),
bodyText = stringResource(id = R.string.It_may_not_offer_some_or_all_of_your_add_ons),
leftButtonText = stringResource(id = R.string.No_go_back),
leftButtonAction = onRewardAlertDialogNegativeClicked,
rightButtonText = stringResource(id = R.string.Yes_continue),
rightButtonAction = onRewardAlertDialogPositiveClicked
)
}

HorizontalPager(
userScrollEnabled = false,
state = pagerState
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.kickstarter.viewmodels.projectpage

import android.util.Pair
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
Expand All @@ -13,7 +12,6 @@ import com.kickstarter.models.Project
import com.kickstarter.models.Reward
import com.kickstarter.ui.data.PledgeData
import com.kickstarter.ui.data.PledgeFlowContext
import com.kickstarter.ui.data.PledgeReason
import com.kickstarter.ui.data.ProjectData
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -24,14 +22,12 @@ import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import java.util.Locale

data class RewardSelectionUIState(
val rewardList: List<Reward> = listOf(),
val selectedReward: Reward = Reward.builder().build(),
val initialRewardIndex: Int = 0,
val project: ProjectData = ProjectData.builder().build(),
val showAlertDialog: Boolean = false
)

class RewardsSelectionViewModel(environment: Environment) : ViewModel() {
Expand Down Expand Up @@ -70,82 +66,18 @@ class RewardsSelectionViewModel(environment: Environment) : ViewModel() {

fun onUserRewardSelection(reward: Reward) {
viewModelScope.launch {
val pledgeDataAndReason = pledgeDataAndPledgeReason(currentProjectData, reward)
newUserReward = pledgeDataAndReason.first.reward()
val pledgeData =
PledgeData.with(PledgeFlowContext.NEW_PLEDGE, currentProjectData, reward)
newUserReward = reward
emitCurrentState()
analytics.trackSelectRewardCTA(pledgeDataAndReason.first)

when (pledgeDataAndReason.second) {
PledgeReason.UPDATE_REWARD -> {
if (previouslyBackedReward?.hasAddons() == true && !newUserReward.hasAddons())
// Show warning to user
emitCurrentState(showAlertDialog = true)

if (previouslyBackedReward?.hasAddons() == false && !newUserReward.hasAddons())
// Go to confirm page
mutableFlowUIRequest.emit(FlowUIState(currentPage = 2, expanded = true))

if (previouslyBackedReward?.hasAddons() == true && newUserReward.hasAddons()) {
if (differentShippingTypes(previouslyBackedReward, newUserReward))
// Show warning to user
emitCurrentState(showAlertDialog = true)
// Go to add-ons
else mutableFlowUIRequest.emit(FlowUIState(currentPage = 1, expanded = true))
}

if (previouslyBackedReward?.hasAddons() == false && newUserReward.hasAddons()) {
// Go to add-ons
mutableFlowUIRequest.emit(FlowUIState(currentPage = 1, expanded = true))
}
}

PledgeReason.PLEDGE -> {
if (newUserReward.hasAddons())
// Show add-ons
mutableFlowUIRequest.emit(FlowUIState(currentPage = 1, expanded = true))
else
// Show confirm page
mutableFlowUIRequest.emit(FlowUIState(currentPage = 2, expanded = true))
}

else -> {
}
}
}
}

fun onRewardCarouselAlertClicked(wasPositive: Boolean) {
viewModelScope.launch {
emitCurrentState()
if (wasPositive) {
if (newUserReward.hasAddons()) {
// Go to add-ons
mutableFlowUIRequest.emit(FlowUIState(currentPage = 1, expanded = true))
} else {
// Show confirm page
mutableFlowUIRequest.emit(FlowUIState(currentPage = 2, expanded = true))
}
}
}
}

private fun pledgeDataAndPledgeReason(
projectData: ProjectData,
reward: Reward
): Pair<PledgeData, PledgeReason> {
val pledgeReason =
if (projectData.project().isBacking()) PledgeReason.UPDATE_REWARD
else PledgeReason.PLEDGE
val pledgeData =
PledgeData.with(PledgeFlowContext.forPledgeReason(pledgeReason), projectData, reward)
return Pair(pledgeData, pledgeReason)
}

private fun differentShippingTypes(newRW: Reward?, backedRW: Reward): Boolean {
return if (newRW == null) false
else if (newRW.id() == backedRW.id()) false
else {
(newRW.shippingType()?.lowercase(Locale.getDefault()) ?: "") != (backedRW.shippingType()?.lowercase(Locale.getDefault()) ?: "")
analytics.trackSelectRewardCTA(pledgeData)

if (newUserReward.hasAddons())
// Show add-ons
mutableFlowUIRequest.emit(FlowUIState(currentPage = 1, expanded = true))
else
// Show confirm page
mutableFlowUIRequest.emit(FlowUIState(currentPage = 2, expanded = true))
}
}

Expand Down Expand Up @@ -174,16 +106,14 @@ class RewardsSelectionViewModel(environment: Environment) : ViewModel() {
analytics.trackRewardsCarouselViewed(projectData = projectData)
}
}
private suspend fun emitCurrentState(
showAlertDialog: Boolean = false
) {

private suspend fun emitCurrentState() {
val filteredRewards = currentProjectData.project().rewards()?.filter { RewardUtils.isNoReward(it) || it.isAvailable() } ?: listOf()
mutableRewardSelectionUIState.emit(
RewardSelectionUIState(
rewardList = filteredRewards,
initialRewardIndex = indexOfBackedReward,
project = currentProjectData,
showAlertDialog = showAlertDialog,
selectedReward = newUserReward
)
)
Expand Down
Loading

0 comments on commit fc9b18c

Please sign in to comment.