-
Notifications
You must be signed in to change notification settings - Fork 662
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
39d7507
commit 37fe570
Showing
10 changed files
with
150 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
link/src/main/java/com/stripe/android/link/ui/signup/Model.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.stripe.android.link.ui.signup | ||
|
||
data class SignUpViewState( | ||
val loading: Boolean = false | ||
) | ||
|
||
sealed interface SignUpAction { | ||
data object SignUpClicked : SignUpAction | ||
} | ||
|
||
sealed interface SignUpResult { | ||
data object ShowLoader : SignUpResult | ||
data class SendEffect(val effect: SignUpEffect) : SignUpResult | ||
} | ||
|
||
sealed interface SignUpEffect { | ||
data object NavigateToWallet : SignUpEffect | ||
} |
25 changes: 22 additions & 3 deletions
25
link/src/main/java/com/stripe/android/link/ui/signup/SignUpScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,28 @@ | ||
package com.stripe.android.link.ui.signup | ||
|
||
import androidx.compose.material.Text | ||
import androidx.compose.animation.AnimatedVisibility | ||
import androidx.compose.material.CircularProgressIndicator | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.navigation.NavHostController | ||
import com.stripe.android.link.LinkScreen | ||
import com.stripe.android.uicore.utils.collectAsState | ||
|
||
@Composable | ||
internal fun SignUpScreen() { | ||
Text(text = "SignUpScreen") | ||
internal fun SignUpScreen( | ||
viewModel: SignUpViewModel, | ||
navController: NavHostController | ||
) { | ||
LaunchedEffect("SignUpEffects") { | ||
viewModel.effect.collect { effect -> | ||
when (effect) { | ||
SignUpEffect.NavigateToWallet -> navController.navigate(LinkScreen.Wallet.route) | ||
} | ||
} | ||
} | ||
val state by viewModel.state.collectAsState() | ||
AnimatedVisibility(visible = state.loading) { | ||
CircularProgressIndicator() | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
link/src/main/java/com/stripe/android/link/ui/signup/SignUpViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.stripe.android.link.ui.signup | ||
|
||
import com.stripe.android.link.LinkViewModel | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
|
||
internal class SignUpViewModel : LinkViewModel<SignUpViewState, SignUpAction, SignUpResult, SignUpEffect>( | ||
initialState = SignUpViewState() | ||
) { | ||
override fun actionToResult(action: SignUpAction): Flow<SignUpResult> { | ||
return when (action) { | ||
SignUpAction.SignUpClicked -> handleSignUpClicked() | ||
} | ||
} | ||
|
||
private fun handleSignUpClicked() = flow { | ||
emit(SignUpResult.ShowLoader) | ||
emit( | ||
value = SignUpResult.SendEffect( | ||
effect = SignUpEffect.NavigateToWallet | ||
) | ||
) | ||
} | ||
|
||
override fun resultToState(currentState: SignUpViewState, result: SignUpResult): SignUpViewState { | ||
return when (result) { | ||
is SignUpResult.SendEffect -> currentState | ||
SignUpResult.ShowLoader -> { | ||
currentState.copy(loading = true) | ||
} | ||
} | ||
} | ||
|
||
override fun resultToEffect(result: SignUpResult): SignUpEffect? { | ||
return when (result) { | ||
is SignUpResult.SendEffect -> result.effect | ||
SignUpResult.ShowLoader -> null | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
link/src/test/java/com/stripe/android/link/ui/signup/SignUpViewModelTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.stripe.android.link.ui.signup | ||
|
||
import app.cash.turbine.test | ||
import com.google.common.truth.Truth | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.test.UnconfinedTestDispatcher | ||
import kotlinx.coroutines.test.resetMain | ||
import kotlinx.coroutines.test.runTest | ||
import kotlinx.coroutines.test.setMain | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class SignUpViewModelTest { | ||
private val dispatcher = UnconfinedTestDispatcher() | ||
private val vm = SignUpViewModel() | ||
|
||
@Before | ||
fun setUp() { | ||
Dispatchers.setMain(dispatcher) | ||
} | ||
|
||
@After | ||
fun tearDown() { | ||
Dispatchers.resetMain() | ||
} | ||
|
||
@Test | ||
fun `test that viewmodel has correct initial state`() = runTest(dispatcher) { | ||
vm.state.test { | ||
Truth.assertThat(awaitItem()).isEqualTo(SignUpViewState()) | ||
} | ||
} | ||
|
||
@Test | ||
fun `test that correct effect is emitted on successful sign up`() = runTest(dispatcher) { | ||
vm.effect.test { | ||
vm.handleAction(SignUpAction.SignUpClicked) | ||
Truth.assertThat(awaitItem()).isEqualTo(SignUpEffect.NavigateToWallet) | ||
} | ||
} | ||
} |