Refactor navigation event

This commit is contained in:
Alex Gabor 2022-04-02 11:37:35 +03:00
parent 586c811e10
commit 61b82f1ba7
5 changed files with 15 additions and 17 deletions

View file

@ -135,7 +135,7 @@ dependencies {
androidTestImplementation "androidx.test.espresso:espresso-contrib:$testing_espresso_version"
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$androidx_compose"
testImplementation "androidx.compose.ui:ui-test-junit4:$androidx_compose"
// debugImplementation "androidx.compose.ui:ui-test-manifest:$androidx_compose"
debugImplementation "androidx.compose.ui:ui-test-manifest:$androidx_compose"
androidTestImplementation project(':mockserver')
androidTestImplementation "androidx.arch.core:core-testing:$testing_androidx_arch_core_version"
androidTestRuntimeOnly "org.junit.vintage:junit-vintage-engine:$testing_junit5_version"

View file

@ -1,10 +1,8 @@
package org.fnives.test.showcase.ui
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.fnives.test.showcase.R
import org.fnives.test.showcase.compose.ComposeActivity
import org.fnives.test.showcase.compose.screen.AppNavigation
import org.fnives.test.showcase.core.integration.fake.FakeUserDataLocalStorage
import org.fnives.test.showcase.core.login.IsUserLoggedInUseCase
@ -24,7 +22,7 @@ import org.koin.test.KoinTest
class AuthComposeInstrumentedTest : KoinTest {
@get:Rule
val composeTestRule = createAndroidComposeRule<ComposeActivity>()
val composeTestRule = createComposeRule()
private val mockServerScenarioSetupTestRule = MockServerScenarioSetupResetingTestRule(networkSynchronizationTestRule = ComposeNetworkSynchronizationTestRule(composeTestRule))
private val mockServerScenarioSetup get() = mockServerScenarioSetupTestRule.mockServerScenarioSetup

View file

@ -34,11 +34,10 @@ fun AppNavigation(isUserLogeInUseCase: IsUserLoggedInUseCase = get()) {
) {
composable("Splash") { SplashScreen() }
composable("Auth") {
val authState = rememberAuthScreenState()
AuthScreen(Modifier.testTag(AppNavigationTag.AuthScreen), authState)
if (authState.navigateToHome?.consume() != null) {
navController.navigate("Home")
}
AuthScreen(modifier = Modifier.testTag(AppNavigationTag.AuthScreen),
authScreenState = rememberAuthScreenState(
onLoginSuccess = { navController.navigate("Home") }
))
}
composable("Home") {
HomeScreen(

View file

@ -1,26 +1,29 @@
package org.fnives.test.showcase.compose.screen.auth
import androidx.compose.runtime.*
import androidx.compose.ui.platform.AndroidUiDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.fnives.test.showcase.core.login.LoginUseCase
import org.fnives.test.showcase.model.auth.LoginCredentials
import org.fnives.test.showcase.model.auth.LoginStatus
import org.fnives.test.showcase.model.shared.Answer
import org.fnives.test.showcase.ui.shared.Event
import org.koin.androidx.compose.get
@Composable
fun rememberAuthScreenState(
stateScope: CoroutineScope = rememberCoroutineScope(),
stateScope: CoroutineScope = rememberCoroutineScope { Dispatchers.Main },
loginUseCase: LoginUseCase = get(),
onLoginSuccess: () -> Unit = {},
): AuthScreenState {
return remember { AuthScreenState(stateScope, loginUseCase) }
return remember { AuthScreenState(stateScope, loginUseCase, onLoginSuccess) }
}
class AuthScreenState(
private val stateScope: CoroutineScope,
private val loginUseCase: LoginUseCase,
private val onLoginSuccess: () -> Unit = {},
) {
var username by mutableStateOf("")
@ -31,8 +34,6 @@ class AuthScreenState(
private set
var error by mutableStateOf<ErrorType?>(null)
private set
var navigateToHome by mutableStateOf<Event<Unit>?>(null)
private set
fun onUsernameChanged(username: String) {
this.username = username
@ -62,7 +63,7 @@ class AuthScreenState(
private fun processLoginStatus(loginStatus: LoginStatus) {
when (loginStatus) {
LoginStatus.SUCCESS -> navigateToHome = Event(Unit)
LoginStatus.SUCCESS -> onLoginSuccess()
LoginStatus.INVALID_CREDENTIALS -> error = ErrorType.INVALID_CREDENTIALS
LoginStatus.INVALID_USERNAME -> error = ErrorType.UNSUPPORTED_USERNAME
LoginStatus.INVALID_PASSWORD -> error = ErrorType.UNSUPPORTED_PASSWORD