Skip to content

Commit

Permalink
[MBL-1300] Make confirm details screen use same default location logi…
Browse files Browse the repository at this point in the history
…c as add-ons (#1990)

* update shipping rule logic in confirm details to use add-ons screen logic/value

* remove forced new flow logic

* lint

* populate confirm details screen with current shipping rule
  • Loading branch information
mtgriego authored Mar 27, 2024
1 parent a3d31c4 commit 1f9f6bd
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,8 @@ class ProjectPageActivity :
val addOns = addOnsUIState.addOns
val shippingRules = addOnsUIState.shippingRules

LaunchedEffect(Unit) {
addOnsViewModel.flowUIRequest.collect {
checkoutFlowViewModel.changePage(it)
}
LaunchedEffect(currentUserShippingRule) {
confirmDetailsViewModel.provideCurrentShippingRule(currentUserShippingRule)
}

val confirmUiState by confirmDetailsViewModel.confirmDetailsUIState.collectAsStateWithLifecycle()
Expand All @@ -505,7 +503,6 @@ class ProjectPageActivity :
val shippingAmount = confirmUiState.shippingAmount
val initialBonusAmount = confirmUiState.initialBonusSupportAmount
val totalBonusSupportAmount = confirmUiState.totalBonusSupportAmount
val currentShippingRule = confirmUiState.currentShippingRule
val maxPledgeAmount = confirmUiState.maxPledgeAmount
val minStepAmount = confirmUiState.minStepAmount

Expand Down Expand Up @@ -548,9 +545,9 @@ class ProjectPageActivity :
},
pagerState = pagerState,
onAddOnsContinueClicked = {
addOnsViewModel.onAddOnsContinueClicked()
checkoutFlowViewModel.onAddOnsContinueClicked()
},
currentShippingRule = currentShippingRule ?: currentUserShippingRule,
currentShippingRule = currentUserShippingRule,
shippingSelectorIsGone = shippingSelectorIsGone,
shippingRules = shippingRules,
environment = getEnvironment(),
Expand Down Expand Up @@ -588,7 +585,6 @@ class ProjectPageActivity :
minStepAmount = minStepAmount,
onShippingRuleSelected = { shippingRule ->
addOnsViewModel.onShippingLocationChanged(shippingRule)
confirmDetailsViewModel.onShippingRuleSelected(shippingRule)
},
shippingAmount = shippingAmount,
onConfirmDetailsContinueClicked = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ fun CountryInputWithDropdown(
}

var countryInput by remember {
mutableStateOf(initialCountryInput ?: "United States")
mutableStateOf(initialCountryInput ?: "")
}

val focusManager = LocalFocusManager.current
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private fun ConfirmPledgeDetailsScreenPreviewNoRewards() {
selectedReward = null,
onContinueClicked = {},
rewardsContainAddOns = false,
rewardsHaveShippables = true,
currentShippingRule = ShippingRule.builder().build(),
totalAmount = 1.0,
initialBonusSupport = 1.0,
Expand All @@ -79,6 +80,7 @@ private fun ConfirmPledgeDetailsScreenPreviewNoRewardsWarning() {
selectedReward = null,
onContinueClicked = {},
rewardsContainAddOns = false,
rewardsHaveShippables = true,
currentShippingRule = ShippingRule.builder().build(),
totalAmount = 1001.0,
initialBonusSupport = 1.0,
Expand Down Expand Up @@ -107,6 +109,7 @@ private fun ConfirmPledgeDetailsScreenPreviewNoAddOnsOrBonusSupport() {
Pair("Cool Item $it", "$20")
},
rewardsContainAddOns = false,
rewardsHaveShippables = true,
shippingAmount = 5.0,
currentShippingRule = ShippingRule.builder().build(),
totalAmount = 55.0,
Expand Down Expand Up @@ -137,6 +140,7 @@ private fun ConfirmPledgeDetailsScreenPreviewAddOnsOnly() {
Pair("Cool Item $it", "$20")
},
rewardsContainAddOns = true,
rewardsHaveShippables = true,
shippingAmount = 5.0,
currentShippingRule = ShippingRule.builder().build(),
totalAmount = 105.0,
Expand Down Expand Up @@ -166,6 +170,7 @@ private fun ConfirmPledgeDetailsScreenPreviewBonusSupportOnly() {
Pair("Cool Item $it", "$20")
},
rewardsContainAddOns = false,
rewardsHaveShippables = true,
shippingAmount = 5.0,
currentShippingRule = ShippingRule.builder().build(),
totalAmount = 55.0,
Expand Down Expand Up @@ -196,6 +201,7 @@ private fun ConfirmPledgeDetailsScreenPreviewAddOnsAndBonusSupport() {
Pair("Cool Item $it", "$20")
},
rewardsContainAddOns = true,
rewardsHaveShippables = true,
shippingAmount = 5.0,
currentShippingRule = ShippingRule.builder().build(),
totalAmount = 115.0,
Expand All @@ -219,6 +225,7 @@ fun ConfirmPledgeDetailsScreen(
onContinueClicked: () -> Unit,
rewardsList: List<Pair<String, String>> = listOf(),
rewardsContainAddOns: Boolean,
rewardsHaveShippables: Boolean,
shippingAmount: Double = 0.0,
currentShippingRule: ShippingRule,
countryList: List<ShippingRule> = listOf(),
Expand Down Expand Up @@ -383,9 +390,10 @@ fun ConfirmPledgeDetailsScreen(
Spacer(modifier = Modifier.height(dimensions.paddingMediumSmall))

Row(verticalAlignment = Alignment.CenterVertically) {
if (countryList.isNotEmpty() && !rewardsContainAddOns) {
if (countryList.isNotEmpty() && !rewardsContainAddOns && rewardsHaveShippables) {
CountryInputWithDropdown(
interactionSource = interactionSource,
initialCountryInput = shippingLocation,
countryList = countryList,
onShippingRuleSelected = onShippingRuleSelected
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import com.kickstarter.R
import com.kickstarter.libs.Environment
import com.kickstarter.libs.utils.RewardUtils
import com.kickstarter.libs.utils.RewardViewUtils
import com.kickstarter.libs.utils.extensions.isNullOrZero
import com.kickstarter.models.Project
Expand Down Expand Up @@ -287,6 +288,7 @@ fun ProjectPledgeButtonAndFragmentContainer(
selectedRewardAndAddOnList, environment, project
),
rewardsContainAddOns = selectedRewardAndAddOnList.any { it.isAddOn() },
rewardsHaveShippables = selectedRewardAndAddOnList.any { RewardUtils.isShippable(it) },
onBonusSupportPlusClicked = onBonusSupportPlusClicked,
onBonusSupportMinusClicked = onBonusSupportMinusClicked
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.PublishSubject
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
Expand Down Expand Up @@ -67,18 +64,8 @@ class AddOnsViewModel(val environment: Environment) : ViewModel() {
initialValue = AddOnsUIState()
)

private val mutableFlowUIRequest = MutableSharedFlow<FlowUIState>()
val flowUIRequest: SharedFlow<FlowUIState>
get() = mutableFlowUIRequest
.asSharedFlow()

init {
currentUserReward
.filter {
!RewardUtils.isDigital(it) && RewardUtils.isShippable(it) && !RewardUtils.isLocalPickup(
it
)
}
.compose<Pair<Reward, List<ShippingRule>>>(
Transformers.combineLatestPair(
shippingRulesObservable
Expand Down Expand Up @@ -217,13 +204,6 @@ class AddOnsViewModel(val environment: Environment) : ViewModel() {
}
}

fun onAddOnsContinueClicked() {
viewModelScope.launch {
// Go to confirm page
mutableFlowUIRequest.emit(FlowUIState(currentPage = 2, expanded = true))
}
}

private suspend fun emitCurrentState() {
mutableAddOnsUIState.emit(
AddOnsUIState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ class CheckoutFlowViewModel(val environment: Environment) : ViewModel() {
}
}

fun onAddOnsContinueClicked() {
viewModelScope.launch {
// Go to confirm page
mutableFlowUIState.emit(FlowUIState(currentPage = 2, expanded = true))
}
}

class Factory(private val environment: Environment) :
ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import androidx.lifecycle.viewModelScope
import com.kickstarter.libs.Environment
import com.kickstarter.libs.models.Country
import com.kickstarter.libs.utils.RewardUtils
import com.kickstarter.libs.utils.extensions.isNotNull
import com.kickstarter.models.CheckoutPayment
import com.kickstarter.models.Reward
import com.kickstarter.models.ShippingRule
Expand All @@ -16,7 +15,6 @@ 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 io.reactivex.Observable
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
Expand All @@ -34,21 +32,19 @@ data class ConfirmDetailsUIState(
val totalBonusSupportAmount: Double = 0.0,
val shippingAmount: Double = 0.0,
val totalAmount: Double = 0.0,
val currentShippingRule: ShippingRule? = null,
val minStepAmount: Double = 0.0,
val maxPledgeAmount: Double = 0.0
)

class ConfirmDetailsViewModel(val environment: Environment) : ViewModel() {

private val apolloClient = requireNotNull(environment.apolloClientV2())
private val currentConfig = requireNotNull(environment.currentConfigV2())

private lateinit var projectData: ProjectData
private lateinit var userSelectedReward: Reward
private var rewardAndAddOns: List<Reward> = listOf()
private lateinit var pledgeReason: PledgeReason
private lateinit var defaultShippingRule: ShippingRule
private var pledgeReason: PledgeReason? = null
private lateinit var selectedShippingRule: ShippingRule
private var initialBonusSupport = 0.0
private var addedBonusSupport = 0.0
private var shippingAmount: Double = 0.0
Expand Down Expand Up @@ -85,31 +81,6 @@ class ConfirmDetailsViewModel(val environment: Environment) : ViewModel() {
minStepAmount = it.minPledge.toDouble()
maxPledgeAmount = it.maxPledge.toDouble()
}
projectData.project().rewards()?.let { rewards ->
val reward = rewards.firstOrNull { theOne ->
!theOne.isAddOn() && theOne.isAvailable() && RewardUtils.isShippable(theOne)
}
reward?.let { rw ->
apolloClient.getShippingRules(
reward = rw
)
.asFlow()
.map { shippingRulesEnvelope ->
if (shippingRulesEnvelope.isNotNull()) {
getDefaultShippingRule(shippingRulesEnvelope.shippingRules())
.asFlow()
.map {
defaultShippingRule = it
}.catch {
}
.collect()
}
}
.catch {
}
.collect()
}
}
}
}

Expand All @@ -126,9 +97,9 @@ class ConfirmDetailsViewModel(val environment: Environment) : ViewModel() {
pledgeReason = pledgeDataAndPledgeReason(projectData, reward).second
}

if (::defaultShippingRule.isInitialized) {
if (::selectedShippingRule.isInitialized) {
shippingAmount = getShippingAmount(
rule = defaultShippingRule,
rule = selectedShippingRule,
reason = pledgeReason,
bShippingAmount = null,
rewards = rewardAndAddOns
Expand Down Expand Up @@ -158,9 +129,9 @@ class ConfirmDetailsViewModel(val environment: Environment) : ViewModel() {

rewardAndAddOns = rewardsAndAddOns

if (::defaultShippingRule.isInitialized) {
if (::selectedShippingRule.isInitialized) {
shippingAmount = getShippingAmount(
rule = defaultShippingRule,
rule = selectedShippingRule,
reason = pledgeReason,
bShippingAmount = null,
rewards = rewardAndAddOns
Expand All @@ -187,7 +158,7 @@ class ConfirmDetailsViewModel(val environment: Environment) : ViewModel() {
*/
private fun getShippingAmount(
rule: ShippingRule,
reason: PledgeReason,
reason: PledgeReason? = null,
bShippingAmount: Float? = null,
rewards: List<Reward>
): Double {
Expand All @@ -198,9 +169,7 @@ class ConfirmDetailsViewModel(val environment: Environment) : ViewModel() {
rule
) + rule.cost() else rule.cost()

PledgeReason.FIX_PLEDGE,
PledgeReason.UPDATE_PAYMENT,
PledgeReason.UPDATE_PLEDGE -> bShippingAmount?.toDouble() ?: rule.cost()
else -> bShippingAmount?.toDouble() ?: rule.cost()
}
}

Expand Down Expand Up @@ -231,15 +200,6 @@ class ConfirmDetailsViewModel(val environment: Environment) : ViewModel() {
return Pair(pledgeData, pledgeReason)
}

private fun getDefaultShippingRule(shippingRules: List<ShippingRule>): Observable<ShippingRule> {
return this.currentConfig.observable()
.map { it.countryCode() }
.map { countryCode ->
shippingRules.firstOrNull { it.location()?.country() == countryCode }
?: shippingRules.first()
}
}

private fun getRewardsTotalAmount(rewards: List<Reward>): Double {
var total = 0.0
rewards.forEach { reward ->
Expand Down Expand Up @@ -279,7 +239,7 @@ class ConfirmDetailsViewModel(val environment: Environment) : ViewModel() {
CreateCheckoutData(
project = projectData.project(),
amount = totalAmount.toString(),
locationId = if (::defaultShippingRule.isInitialized) defaultShippingRule.location()
locationId = if (::selectedShippingRule.isInitialized) selectedShippingRule.location()
?.id()?.toString() else null,
rewardsIds = fullIdListForQuantities(rewardAndAddOns),
refTag = projectData.refTagFromIntent()
Expand Down Expand Up @@ -315,10 +275,10 @@ class ConfirmDetailsViewModel(val environment: Environment) : ViewModel() {
return mutableList.toList()
}

fun onShippingRuleSelected(shippingRule: ShippingRule) {
defaultShippingRule = shippingRule
fun provideCurrentShippingRule(shippingRule: ShippingRule) {
selectedShippingRule = shippingRule
shippingAmount = getShippingAmount(
rule = defaultShippingRule,
rule = selectedShippingRule,
reason = pledgeReason,
bShippingAmount = null,
rewards = rewardAndAddOns
Expand All @@ -337,7 +297,6 @@ class ConfirmDetailsViewModel(val environment: Environment) : ViewModel() {
totalBonusSupportAmount = initialBonusSupport + addedBonusSupport,
shippingAmount = shippingAmount,
totalAmount = totalAmount,
currentShippingRule = if (::defaultShippingRule.isInitialized) defaultShippingRule else null,
minStepAmount = minStepAmount,
maxPledgeAmount = maxPledgeAmount
)
Expand Down

0 comments on commit 1f9f6bd

Please sign in to comment.