8000 MBL-1292: Launch LogIn user when Continue button clicked by Arkariang · Pull Request #1989 · kickstarter/android-oss · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

MBL-1292: Launch LogIn user when Continue button clicked #1989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 27, 2024
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
Expand Up @@ -512,7 +512,9 @@ class ProjectPageActivity :
val checkoutPayment by confirmDetailsViewModel.checkoutPayment.collectAsStateWithLifecycle()

LaunchedEffect(checkoutPayment.id) {
if (checkoutPayment.id != 0L) checkoutFlowViewModel.onConfirmDetailsContinueClicked()
if (checkoutPayment.id != 0L) checkoutFlowViewModel.onConfirmDetailsContinueClicked {
startLoginToutActivity()
}
}

val pagerState = rememberPagerState(initialPage = 0, pageCount = { 4 })
Expand Down Expand Up @@ -591,7 +593,9 @@ class ProjectPageActivity :
shippingAmount = shippingAmount,
>
confirmDetailsViewModel.onContinueClicked {
checkoutFlowViewModel.onConfirmDetailsContinueClicked()
checkoutFlowViewModel.onConfirmDetailsContinueClicked {
startLoginToutActivity()
}
}
},
confirmDetailsViewModel.decrementBonusSupport() },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.launch
import kotlinx.coroutines.rx2.asFlow

data class FlowUIState(
val currentPage: Int = 0,
Expand All @@ -20,6 +22,7 @@ data class FlowUIState(
class CheckoutFlowViewModel(val environment: Environment) : ViewModel() {

private lateinit var newUserReward: Reward
private val currentUser = requireNotNull(environment.currentUserV2())

private val mutableFlowUIState = MutableStateFlow(FlowUIState())
val flowUIState: StateFlow<FlowUIState>
Expand Down Expand Up @@ -83,10 +86,17 @@ class CheckoutFlowViewModel(val environment: Environment) : ViewModel() {
}
}

fun onConfirmDetailsContinueClicked() {
fun onConfirmDetailsContinueClicked(logInCallback: () -> Unit) {
viewModelScope.launch {
// Show pledge page
mutableFlowUIState.emit(FlowUIState(currentPage = 3, expanded = true))
currentUser.isLoggedIn
.asFlow()
.take(1)
.collect { userLoggedIn ->
// - Show pledge page
if (userLoggedIn) mutableFlowUIState.emit(FlowUIState(currentPage = 3, expanded = true))
// - Trigger LoginFlow callback
else logInCallback()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.kickstarter.viewmodels.usecases

import com.kickstarter.KSRobolectricTestCase
import com.kickstarter.libs.Environment
import com.kickstarter.libs.MockCurrentUserV2
import com.kickstarter.mock.factories.UserFactory
import com.kickstarter.viewmodels.projectpage.CheckoutFlowViewModel
import com.kickstarter.viewmodels.projectpage.FlowUIState
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Test

class CheckoutFlowViewModelTest : KSRobolectricTestCase() {
private lateinit var vm: CheckoutFlowViewModel

private fun setUpEnvironment(environment: Environment) {
this.vm = CheckoutFlowViewModel.Factory(environment).create(CheckoutFlowViewModel::class.java)
}

@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun testLaunchLogInCallBack_whenNoUser_loggedIn() = runTest {
var callbackCalled = 0

// - No user present on environment
setUpEnvironment(environment())

// - Call onConfirmDetailsContinueClicked with a VM loaded with Environment without user
vm.onConfirmDetailsContinueClicked { callbackCalled++ }

// - Make sure the callback provided is called when no user present, `onConfirmDetailsContinueClicked` will produce states ONLY if user present
assertTrue(callbackCalled == 1)

val state = mutableListOf<FlowUIState>()
backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) {
vm.flowUIState.toList(state)
}

// - make sure empty FlowUISate has been produced, `onConfirmDetailsContinueClicked` will produce states ONLY if user present
assertEquals(state, listOf(FlowUIState()))
assertNotSame(state, listOf(FlowUIState(currentPage = 3, expanded = true)))
assert(state.size == 1)
}

@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun testProduceNextPageState_whenUser_LoggedIn() = runTest {
var callbackCalled = 0

// - Environment with user present
val environment = environment()
.toBuilder()
.currentUserV2(MockCurrentUserV2(UserFactory.user()))
.build()

setUpEnvironment(environment)

// - Call onConfirmDetailsContinueClicked with a VM loaded with Environment containing an user
vm.onConfirmDetailsContinueClicked { callbackCalled++ }

5462 // - Make sure the callback is not called
assertTrue(callbackCalled == 0)

val state = mutableListOf<FlowUIState>()
backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) {
vm.flowUIState.toList(state)
}

// - make sure next page FlowUIState has been generated, not just the initial empty state
assertEquals(state, listOf(FlowUIState(), FlowUIState(currentPage = 3, expanded = true)))
assert(state.size == 2)
}
}
0