From fc9b18c783d787dd515a96ed21631c2b7a1d70a5 Mon Sep 17 00:00:00 2001 From: mtgriego Date: Mon, 10 Jun 2024 10:17:45 -0700 Subject: [PATCH] [MBL-1397] Remove unneeded pledge reasons references (#2052) * remove unneeded pledge reasons references from rewards selection viewmodel * remove unneeded tests and alert dialog * lint --- .../ui/activities/ProjectPageActivity.kt | 8 - ...ProjectPledgeButtonAndFragmentContainer.kt | 20 -- .../projectpage/RewardsSelectionViewModel.kt | 96 +---- .../RewardsSelectionViewModelTest.kt | 329 ------------------ 4 files changed, 13 insertions(+), 440 deletions(-) diff --git a/app/src/main/java/com/kickstarter/ui/activities/ProjectPageActivity.kt b/app/src/main/java/com/kickstarter/ui/activities/ProjectPageActivity.kt index 2a707ae3f4..b43c9de042 100644 --- a/app/src/main/java/com/kickstarter/ui/activities/ProjectPageActivity.kt +++ b/app/src/main/java/com/kickstarter/ui/activities/ProjectPageActivity.kt @@ -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) @@ -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 -> diff --git a/app/src/main/java/com/kickstarter/ui/activities/compose/projectpage/ProjectPledgeButtonAndFragmentContainer.kt b/app/src/main/java/com/kickstarter/ui/activities/compose/projectpage/ProjectPledgeButtonAndFragmentContainer.kt index 1e299bb02d..bf01331949 100644 --- a/app/src/main/java/com/kickstarter/ui/activities/compose/projectpage/ProjectPledgeButtonAndFragmentContainer.kt +++ b/app/src/main/java/com/kickstarter/ui/activities/compose/projectpage/ProjectPledgeButtonAndFragmentContainer.kt @@ -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 @@ -102,9 +101,6 @@ private fun ProjectPledgeButtonAndContainerPreview() { shippingSelectorIsGone = false, currentShippingRule = ShippingRule.builder().build(), onShippingRuleSelected = {}, - showRewardCarouselDialog = false, - onRewardAlertDialogPositiveClicked = {}, - onRewardAlertDialogNegativeClicked = {}, onConfirmDetailsContinueClicked = {}, selectedRewardAndAddOnList = listOf(), onBonusSupportMinusClicked = {}, @@ -134,9 +130,6 @@ fun ProjectPledgeButtonAndFragmentContainer( currentShippingRule: ShippingRule, environment: Environment?, initialRewardCarouselPosition: Int = 0, - showRewardCarouselDialog: Boolean, - onRewardAlertDialogNegativeClicked: () -> Unit, - onRewardAlertDialogPositiveClicked: () -> Unit, rewardsList: List, addOns: List, project: Project, @@ -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 diff --git a/app/src/main/java/com/kickstarter/viewmodels/projectpage/RewardsSelectionViewModel.kt b/app/src/main/java/com/kickstarter/viewmodels/projectpage/RewardsSelectionViewModel.kt index d5b7bb7338..70808c228f 100644 --- a/app/src/main/java/com/kickstarter/viewmodels/projectpage/RewardsSelectionViewModel.kt +++ b/app/src/main/java/com/kickstarter/viewmodels/projectpage/RewardsSelectionViewModel.kt @@ -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 @@ -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 @@ -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 = 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() { @@ -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 { - 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)) } } @@ -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 ) ) diff --git a/app/src/test/java/com/kickstarter/viewmodels/RewardsSelectionViewModelTest.kt b/app/src/test/java/com/kickstarter/viewmodels/RewardsSelectionViewModelTest.kt index 7311e3ceda..8a123fa475 100644 --- a/app/src/test/java/com/kickstarter/viewmodels/RewardsSelectionViewModelTest.kt +++ b/app/src/test/java/com/kickstarter/viewmodels/RewardsSelectionViewModelTest.kt @@ -54,7 +54,6 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() { rewardList = testRewards, initialRewardIndex = 2, project = testProjectData, - showAlertDialog = false ) ) } @@ -89,7 +88,6 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() { rewardList = testRewards, initialRewardIndex = 0, project = testProjectData, - showAlertDialog = false ) ) @@ -135,7 +133,6 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() { rewardList = testRewards, initialRewardIndex = 0, project = testProjectData, - showAlertDialog = false ) ) @@ -185,7 +182,6 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() { rewardList = testRewards, initialRewardIndex = 2, project = testProjectData, - showAlertDialog = false ) ) @@ -235,7 +231,6 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() { rewardList = testRewards, initialRewardIndex = 3, project = testProjectData, - showAlertDialog = false ) ) @@ -251,62 +246,6 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() { this@RewardsSelectionViewModelTest.segmentTrack.assertValue(EventName.CTA_CLICKED.eventName) } - @OptIn(ExperimentalCoroutinesApi::class) - @Test - fun test_selecting_reward_with_no_addOns_previous_backing_has_addOns() = runTest { - createViewModel() - - val testRewards = - (0..5).map { Reward.builder().title("$it").id(it.toLong()).isAvailable(true).hasAddons(it != 2).build() } - val testBacking = - Backing.builder().reward(testRewards[3]).rewardId(testRewards[3].id()).build() - val testProject = - Project.builder().rewards(testRewards).backing(testBacking).isBacking(true).build() - val testProjectData = - ProjectData.builder().project(testProject).backing(testBacking).build() - - viewModel.provideProjectData(testProjectData) - - val uiState = mutableListOf() - backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) { - viewModel.rewardSelectionUIState.toList(uiState) - } - - val flowState = mutableListOf() - backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) { - viewModel.flowUIRequest.toList(flowState) - } - - assert(uiState.size == 2) - assert(flowState.size == 0) - assertEquals( - uiState.last(), - RewardSelectionUIState( - rewardList = testRewards, - initialRewardIndex = 3, - project = testProjectData, - showAlertDialog = false - ) - ) - - viewModel.onUserRewardSelection(testRewards[2]) - - assert(uiState.size == 3) - assert(flowState.size == 0) - assertEquals( - uiState.last(), - RewardSelectionUIState( - rewardList = testRewards, - initialRewardIndex = 3, - project = testProjectData, - showAlertDialog = true, - selectedReward = testRewards[2] - ) - ) - - this@RewardsSelectionViewModelTest.segmentTrack.assertValue(EventName.CTA_CLICKED.eventName) - } - @OptIn(ExperimentalCoroutinesApi::class) @Test fun test_selecting_reward_with_no_addOns_previous_backing_no_addOns() = runTest { @@ -341,7 +280,6 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() { rewardList = testRewards, initialRewardIndex = 3, project = testProjectData, - showAlertDialog = false ) ) @@ -357,272 +295,6 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() { this@RewardsSelectionViewModelTest.segmentTrack.assertValue(EventName.CTA_CLICKED.eventName) } - @OptIn(ExperimentalCoroutinesApi::class) - @Test - fun test_selecting_reward_with_addOns_previous_backing_has_addOns_different_shipping() = - runTest { - createViewModel() - - val testRewards = (0..5).map { - Reward.builder().title("$it").id(it.toLong()).isAvailable(true).hasAddons(true).shippingType("$it") - .build() - } - val testBacking = - Backing.builder().reward(testRewards[3]).rewardId(testRewards[3].id()).build() - val testProject = - Project.builder().rewards(testRewards).backing(testBacking).isBacking(true).build() - val testProjectData = - ProjectData.builder().project(testProject).backing(testBacking).build() - - viewModel.provideProjectData(testProjectData) - - val uiState = mutableListOf() - backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) { - viewModel.rewardSelectionUIState.toList(uiState) - } - - val flowState = mutableListOf() - backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) { - viewModel.flowUIRequest.toList(flowState) - } - - assert(uiState.size == 2) - assert(flowState.size == 0) - assertEquals( - uiState.last(), - RewardSelectionUIState( - rewardList = testRewards, - initialRewardIndex = 3, - project = testProjectData, - showAlertDialog = false - ) - ) - - viewModel.onUserRewardSelection(testRewards[2]) - - assert(uiState.size == 3) - assert(flowState.size == 0) - assertEquals( - uiState.last(), - RewardSelectionUIState( - rewardList = testRewards, - initialRewardIndex = 3, - project = testProjectData, - showAlertDialog = true, - selectedReward = testRewards[2] - ) - ) - - this@RewardsSelectionViewModelTest.segmentTrack.assertValue(EventName.CTA_CLICKED.eventName) - } - - @OptIn(ExperimentalCoroutinesApi::class) - @Test - fun test_alert_dialog_positive_option_reward_has_add_ons() = runTest { - createViewModel() - - val testRewards = (0..5).map { - Reward.builder().title("$it").id(it.toLong()).isAvailable(true).hasAddons(true).shippingType("$it") - .build() - } - val testBacking = - Backing.builder().reward(testRewards[3]).rewardId(testRewards[3].id()).build() - val testProject = - Project.builder().rewards(testRewards).backing(testBacking).isBacking(true).build() - val testProjectData = - ProjectData.builder().project(testProject).backing(testBacking).build() - - viewModel.provideProjectData(testProjectData) - - val uiState = mutableListOf() - backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) { - viewModel.rewardSelectionUIState.toList(uiState) - } - - val flowState = mutableListOf() - backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) { - viewModel.flowUIRequest.toList(flowState) - } - - assert(uiState.size == 2) - assert(flowState.size == 0) - assertEquals( - uiState.last(), - RewardSelectionUIState( - rewardList = testRewards, - initialRewardIndex = 3, - project = testProjectData, - showAlertDialog = false - ) - ) - - viewModel.onUserRewardSelection(testRewards[2]) - - assert(uiState.size == 3) - assert(flowState.size == 0) - assertEquals( - uiState.last(), - RewardSelectionUIState( - rewardList = testRewards, - initialRewardIndex = 3, - project = testProjectData, - showAlertDialog = true, - selectedReward = testRewards[2] - ) - ) - - viewModel.onRewardCarouselAlertClicked(wasPositive = true) - - assert(uiState.size == 4) - assert(flowState.size == 1) - assertEquals( - flowState.last(), - FlowUIState(currentPage = 1, expanded = true) - ) - - this@RewardsSelectionViewModelTest.segmentTrack.assertValue(EventName.CTA_CLICKED.eventName) - } - - @OptIn(ExperimentalCoroutinesApi::class) - @Test - fun test_alert_dialog_positive_option_reward_no_add_ons() = runTest { - createViewModel() - - val testRewards = (0..5).map { - Reward.builder().title("$it").id(it.toLong()).isAvailable(true).hasAddons(it != 2).shippingType("$it") - .build() - } - val testBacking = - Backing.builder().reward(testRewards[3]).rewardId(testRewards[3].id()).build() - val testProject = - Project.builder().rewards(testRewards).backing(testBacking).isBacking(true).build() - val testProjectData = - ProjectData.builder().project(testProject).backing(testBacking).build() - - viewModel.provideProjectData(testProjectData) - - val uiState = mutableListOf() - backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) { - viewModel.rewardSelectionUIState.toList(uiState) - } - - val flowState = mutableListOf() - backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) { - viewModel.flowUIRequest.toList(flowState) - } - - assert(uiState.size == 2) - assert(flowState.size == 0) - assertEquals( - uiState.last(), - RewardSelectionUIState( - rewardList = testRewards, - initialRewardIndex = 3, - project = testProjectData, - showAlertDialog = false - ) - ) - - viewModel.onUserRewardSelection(testRewards[2]) - - assert(uiState.size == 3) - assert(flowState.size == 0) - assertEquals( - uiState.last(), - RewardSelectionUIState( - rewardList = testRewards, - initialRewardIndex = 3, - project = testProjectData, - showAlertDialog = true, - selectedReward = testRewards[2] - ) - ) - - viewModel.onRewardCarouselAlertClicked(wasPositive = true) - - assert(uiState.size == 4) - assert(flowState.size == 1) - assertEquals( - flowState.last(), - FlowUIState(currentPage = 2, expanded = true) - ) - - this@RewardsSelectionViewModelTest.segmentTrack.assertValue(EventName.CTA_CLICKED.eventName) - } - - @OptIn(ExperimentalCoroutinesApi::class) - @Test - fun test_alert_dialog_negative_option_reward_no_add_ons() = runTest { - createViewModel() - - val testRewards = (0..5).map { - Reward.builder().title("$it").id(it.toLong()).isAvailable(true).hasAddons(it != 2).shippingType("$it") - .build() - } - val testBacking = - Backing.builder().reward(testRewards[3]).rewardId(testRewards[3].id()).build() - val testProject = - Project.builder().rewards(testRewards).backing(testBacking).isBacking(true).build() - val testProjectData = - ProjectData.builder().project(testProject).backing(testBacking).build() - - viewModel.provideProjectData(testProjectData) - - val uiState = mutableListOf() - backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) { - viewModel.rewardSelectionUIState.toList(uiState) - } - - val flowState = mutableListOf() - backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) { - viewModel.flowUIRequest.toList(flowState) - } - - assert(uiState.size == 2) - assert(flowState.size == 0) - assertEquals( - uiState.last(), - RewardSelectionUIState( - rewardList = testRewards, - initialRewardIndex = 3, - project = testProjectData, - showAlertDialog = false - ) - ) - - viewModel.onUserRewardSelection(testRewards[2]) - - assert(uiState.size == 3) - assert(flowState.size == 0) - assertEquals( - uiState.last(), - RewardSelectionUIState( - rewardList = testRewards, - initialRewardIndex = 3, - project = testProjectData, - showAlertDialog = true, - selectedReward = testRewards[2] - ) - ) - - viewModel.onRewardCarouselAlertClicked(wasPositive = false) - - assert(uiState.size == 4) - assert(flowState.size == 0) - assertEquals( - uiState.last(), - RewardSelectionUIState( - rewardList = testRewards, - initialRewardIndex = 3, - project = testProjectData, - showAlertDialog = false, - selectedReward = testRewards[2] - ) - ) - - this@RewardsSelectionViewModelTest.segmentTrack.assertValue(EventName.CTA_CLICKED.eventName) - } - @Test fun `Test rewards list when given a list of rewards that contains unavailable rewards will produce a list of rewards with only rewards available`() = runTest { createViewModel() @@ -655,7 +327,6 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() { rewardList = filteredRewards, initialRewardIndex = 0, project = testProjectData, - showAlertDialog = false ) )