Skip to content

Commit

Permalink
remove mvi-viewmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
toluo-stripe committed Sep 27, 2024
1 parent da0a993 commit dbc537d
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 253 deletions.
38 changes: 15 additions & 23 deletions link/src/main/java/com/stripe/android/link/LinkActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.LaunchedEffect
import androidx.core.os.bundleOf
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.stripe.android.link.ui.cardedit.CardEditScreen
import com.stripe.android.link.ui.paymentmenthod.PaymentMethodScreen
import com.stripe.android.link.ui.signup.SignUpScreen
import com.stripe.android.link.ui.signup.SignUpViewModel
import com.stripe.android.link.ui.verification.VerificationScreen
import com.stripe.android.link.ui.wallet.WalletScreen

Expand All @@ -36,34 +34,17 @@ internal class LinkActivity : AppCompatActivity() {
setContent {
navController = rememberNavController()

LaunchedEffect("LinkEffects") {
viewModel.effect.collect { effect ->
when (effect) {
LinkEffect.GoBack -> navController.popBackStack()
is LinkEffect.SendResult -> {
val bundle = bundleOf(
LinkActivityContract.EXTRA_RESULT to LinkActivityContract.Result(effect.result)
)
this@LinkActivity.setResult(
Activity.RESULT_OK,
Intent().putExtras(bundle)
)
this@LinkActivity.finish()
}
}
}
LaunchedEffect(Unit) {
viewModel.navController = navController
viewModel.dismissWithResult = ::dismissWithResult
}

NavHost(
navController = navController,
startDestination = LinkScreen.SignUp.route
) {
composable(LinkScreen.SignUp.route) {
val vm = viewModel<SignUpViewModel>()
SignUpScreen(
viewModel = vm,
navController = navController
)
SignUpScreen()
}

composable(LinkScreen.Verification.route) {
Expand All @@ -84,4 +65,15 @@ internal class LinkActivity : AppCompatActivity() {
}
}
}

private fun dismissWithResult(result: LinkActivityResult) {
val bundle = bundleOf(
LinkActivityContract.EXTRA_RESULT to LinkActivityContract.Result(result)
)
this@LinkActivity.setResult(
Activity.RESULT_OK,
Intent().putExtras(bundle)
)
this@LinkActivity.finish()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,20 @@ package com.stripe.android.link

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import androidx.navigation.NavHostController

internal class LinkActivityViewModel : StateAndEffectsVM<LinkState, LinkAction, LinkResult, LinkEffect>(
initialState = LinkState,
) {
override fun actionToResult(action: LinkAction): Flow<LinkResult> {
return when (action) {
internal class LinkActivityViewModel : ViewModel() {
lateinit var navController: NavHostController
lateinit var dismissWithResult: (LinkActivityResult) -> Unit

fun handleViewAction(action: LinkAction) {
when (action) {
LinkAction.BackPressed -> handleBackPressed()
}
}

private fun handleBackPressed(): Flow<LinkResult> {
return flowOf(LinkResult.SendEffect(LinkEffect.GoBack))
}

override fun resultToState(currentState: LinkState, result: LinkResult) = currentState

override fun resultToEffect(result: LinkResult): LinkEffect? {
return when (result) {
is LinkResult.SendEffect -> result.effect
}
private fun handleBackPressed() {
dismissWithResult(LinkActivityResult.Canceled())
}

internal class Factory : ViewModelProvider.Factory {
Expand Down
18 changes: 0 additions & 18 deletions link/src/main/java/com/stripe/android/link/ui/signup/Model.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
package com.stripe.android.link.ui.signup

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.Text
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(
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()
}
internal fun SignUpScreen() {
Text(text = "SignUpScreen")
}

This file was deleted.

75 changes: 0 additions & 75 deletions link/src/test/java/com/stripe/android/link/LinkActivityTest.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.stripe.android.link

import app.cash.turbine.test
import com.google.common.truth.Truth
import androidx.navigation.NavHostController
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
Expand All @@ -11,16 +10,22 @@ import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
internal class LinkActivityViewModelTest {
private val dispatcher = UnconfinedTestDispatcher()
private val vm = LinkActivityViewModel()
private val navController: NavHostController = mock()
private val dismissWithResult: (LinkActivityResult) -> Unit = mock()

@Before
fun setUp() {
Dispatchers.setMain(dispatcher)
vm.dismissWithResult = dismissWithResult
vm.navController = navController
}

@After
Expand All @@ -29,17 +34,9 @@ internal class LinkActivityViewModelTest {
}

@Test
fun `test that viewmodel has correct initial state`() = runTest(dispatcher) {
vm.state.test {
Truth.assertThat(awaitItem()).isEqualTo(LinkState)
}
}
fun `test that cancel result is called on back pressed`() = runTest(dispatcher) {
vm.handleViewAction(LinkAction.BackPressed)

@Test
fun `test that correct effect is emitted on back pressed`() = runTest(dispatcher) {
vm.effect.test {
vm.handleAction(LinkAction.BackPressed)
Truth.assertThat(awaitItem()).isEqualTo(LinkEffect.GoBack)
}
verify(dismissWithResult).invoke(LinkActivityResult.Canceled())
}
}

This file was deleted.

0 comments on commit dbc537d

Please sign in to comment.