Skip to content

Commit

Permalink
MBL-1679: not logged in user flow for crowdfund (#2118)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkariang authored Sep 4, 2024
1 parent 23ea68e commit 8fcbaf0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.kickstarter.ui.compose.designsystem.KickstarterApp
import com.kickstarter.ui.data.PledgeData
import com.kickstarter.ui.data.PledgeReason
import com.kickstarter.ui.extensions.showErrorToast
import com.kickstarter.ui.extensions.startLoginActivity
import com.kickstarter.viewmodels.projectpage.AddOnsViewModel

class BackingAddOnsFragment : Fragment() {
Expand Down Expand Up @@ -84,8 +85,15 @@ class BackingAddOnsFragment : Fragment() {
isLoading = addOnsIsLoading,
currentShippingRule = shippingRule,
onContinueClicked = {
viewModelC.getPledgeDataAndReason()?.let { pDataAndReason ->
showPledgeFragment(pledgeData = pDataAndReason.first, pledgeReason = pDataAndReason.second)
if (viewModelC.isUserLoggedIn()) {
viewModelC.getPledgeDataAndReason()?.let { pDataAndReason ->
showPledgeFragment(
pledgeData = pDataAndReason.first,
pledgeReason = pDataAndReason.second
)
}
} else {
activity?.startLoginActivity()
}
},
bonusAmountChanged = { bonusAmount ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onStart
Expand All @@ -46,6 +47,8 @@ data class AddOnsUIState(

class AddOnsViewModel(val environment: Environment, bundle: Bundle? = null) : ViewModel() {
private val apolloClient = requireNotNull(environment.apolloClientV2())
private val currentUser = requireNotNull(environment.currentUserV2())
private var isUserLoggedIn = false

private var currentUserReward: Reward = Reward.builder().build()
private var pledgeData: PledgeData? = null
Expand Down Expand Up @@ -90,6 +93,15 @@ class AddOnsViewModel(val environment: Environment, bundle: Bundle? = null) : Vi
this.dispatcher = dispatcher
}

init {
scope.launch {
currentUser.observable().asFlow()
.collectLatest {
isUserLoggedIn = it.isPresent()
}
}
}

/**
* Used in Crowdfund checkout
*/
Expand Down Expand Up @@ -279,6 +291,7 @@ class AddOnsViewModel(val environment: Environment, bundle: Bundle? = null) : Vi
}
}

fun isUserLoggedIn(): Boolean = isUserLoggedIn
fun getProject() = this.project
fun getSelectedReward() = this.currentUserReward
fun sendEvent() = this.pledgeData?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package com.kickstarter.viewmodels
import android.os.Bundle
import com.kickstarter.KSRobolectricTestCase
import com.kickstarter.libs.Environment
import com.kickstarter.libs.MockCurrentUserV2
import com.kickstarter.libs.utils.EventName
import com.kickstarter.libs.utils.extensions.pledgeAmountTotal
import com.kickstarter.mock.factories.BackingFactory
import com.kickstarter.mock.factories.ProjectFactory
import com.kickstarter.mock.factories.RewardFactory
import com.kickstarter.mock.factories.ShippingRuleFactory
import com.kickstarter.mock.factories.UserFactory
import com.kickstarter.mock.services.MockApolloClientV2
import com.kickstarter.models.Backing
import com.kickstarter.models.Location
Expand Down Expand Up @@ -345,4 +347,24 @@ class AddOnsViewModelTest : KSRobolectricTestCase() {
assertEquals(pledgeDataAndReason?.first?.addOns()?.first()?.quantity(), 3)
assertEquals(pledgeDataAndReason?.first?.addOns()?.last(), backedAddOnq)
}

@Test
fun `test if the VM has being reached by a loggedOut user`() = runTest {
val env = environment().toBuilder()
.currentUserV2(MockCurrentUserV2()) // empty user
.build()

createViewModel(env)
assertFalse(viewModel.isUserLoggedIn())
}

@Test
fun `test if the VM has being reached by a loggedIn user`() = runTest {
val env = environment().toBuilder()
.currentUserV2(MockCurrentUserV2(UserFactory.user())) // empty user
.build()

createViewModel(env)
assertTrue(viewModel.isUserLoggedIn())
}
}

0 comments on commit 8fcbaf0

Please sign in to comment.