Refactor navigation event
This commit is contained in:
parent
586c811e10
commit
61b82f1ba7
5 changed files with 15 additions and 17 deletions
|
|
@ -135,7 +135,7 @@ dependencies {
|
||||||
androidTestImplementation "androidx.test.espresso:espresso-contrib:$testing_espresso_version"
|
androidTestImplementation "androidx.test.espresso:espresso-contrib:$testing_espresso_version"
|
||||||
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$androidx_compose"
|
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$androidx_compose"
|
||||||
testImplementation "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 project(':mockserver')
|
||||||
androidTestImplementation "androidx.arch.core:core-testing:$testing_androidx_arch_core_version"
|
androidTestImplementation "androidx.arch.core:core-testing:$testing_androidx_arch_core_version"
|
||||||
androidTestRuntimeOnly "org.junit.vintage:junit-vintage-engine:$testing_junit5_version"
|
androidTestRuntimeOnly "org.junit.vintage:junit-vintage-engine:$testing_junit5_version"
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,8 @@
|
||||||
package org.fnives.test.showcase.ui
|
package org.fnives.test.showcase.ui
|
||||||
|
|
||||||
import androidx.compose.ui.test.junit4.createAndroidComposeRule
|
|
||||||
import androidx.compose.ui.test.junit4.createComposeRule
|
import androidx.compose.ui.test.junit4.createComposeRule
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||||
import org.fnives.test.showcase.R
|
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.compose.screen.AppNavigation
|
||||||
import org.fnives.test.showcase.core.integration.fake.FakeUserDataLocalStorage
|
import org.fnives.test.showcase.core.integration.fake.FakeUserDataLocalStorage
|
||||||
import org.fnives.test.showcase.core.login.IsUserLoggedInUseCase
|
import org.fnives.test.showcase.core.login.IsUserLoggedInUseCase
|
||||||
|
|
@ -24,7 +22,7 @@ import org.koin.test.KoinTest
|
||||||
class AuthComposeInstrumentedTest : KoinTest {
|
class AuthComposeInstrumentedTest : KoinTest {
|
||||||
|
|
||||||
@get:Rule
|
@get:Rule
|
||||||
val composeTestRule = createAndroidComposeRule<ComposeActivity>()
|
val composeTestRule = createComposeRule()
|
||||||
|
|
||||||
private val mockServerScenarioSetupTestRule = MockServerScenarioSetupResetingTestRule(networkSynchronizationTestRule = ComposeNetworkSynchronizationTestRule(composeTestRule))
|
private val mockServerScenarioSetupTestRule = MockServerScenarioSetupResetingTestRule(networkSynchronizationTestRule = ComposeNetworkSynchronizationTestRule(composeTestRule))
|
||||||
private val mockServerScenarioSetup get() = mockServerScenarioSetupTestRule.mockServerScenarioSetup
|
private val mockServerScenarioSetup get() = mockServerScenarioSetupTestRule.mockServerScenarioSetup
|
||||||
|
|
|
||||||
|
|
@ -34,11 +34,10 @@ fun AppNavigation(isUserLogeInUseCase: IsUserLoggedInUseCase = get()) {
|
||||||
) {
|
) {
|
||||||
composable("Splash") { SplashScreen() }
|
composable("Splash") { SplashScreen() }
|
||||||
composable("Auth") {
|
composable("Auth") {
|
||||||
val authState = rememberAuthScreenState()
|
AuthScreen(modifier = Modifier.testTag(AppNavigationTag.AuthScreen),
|
||||||
AuthScreen(Modifier.testTag(AppNavigationTag.AuthScreen), authState)
|
authScreenState = rememberAuthScreenState(
|
||||||
if (authState.navigateToHome?.consume() != null) {
|
onLoginSuccess = { navController.navigate("Home") }
|
||||||
navController.navigate("Home")
|
))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
composable("Home") {
|
composable("Home") {
|
||||||
HomeScreen(
|
HomeScreen(
|
||||||
|
|
|
||||||
|
|
@ -1,26 +1,29 @@
|
||||||
package org.fnives.test.showcase.compose.screen.auth
|
package org.fnives.test.showcase.compose.screen.auth
|
||||||
|
|
||||||
import androidx.compose.runtime.*
|
import androidx.compose.runtime.*
|
||||||
|
import androidx.compose.ui.platform.AndroidUiDispatcher
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import org.fnives.test.showcase.core.login.LoginUseCase
|
import org.fnives.test.showcase.core.login.LoginUseCase
|
||||||
import org.fnives.test.showcase.model.auth.LoginCredentials
|
import org.fnives.test.showcase.model.auth.LoginCredentials
|
||||||
import org.fnives.test.showcase.model.auth.LoginStatus
|
import org.fnives.test.showcase.model.auth.LoginStatus
|
||||||
import org.fnives.test.showcase.model.shared.Answer
|
import org.fnives.test.showcase.model.shared.Answer
|
||||||
import org.fnives.test.showcase.ui.shared.Event
|
|
||||||
import org.koin.androidx.compose.get
|
import org.koin.androidx.compose.get
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun rememberAuthScreenState(
|
fun rememberAuthScreenState(
|
||||||
stateScope: CoroutineScope = rememberCoroutineScope(),
|
stateScope: CoroutineScope = rememberCoroutineScope { Dispatchers.Main },
|
||||||
loginUseCase: LoginUseCase = get(),
|
loginUseCase: LoginUseCase = get(),
|
||||||
|
onLoginSuccess: () -> Unit = {},
|
||||||
): AuthScreenState {
|
): AuthScreenState {
|
||||||
return remember { AuthScreenState(stateScope, loginUseCase) }
|
return remember { AuthScreenState(stateScope, loginUseCase, onLoginSuccess) }
|
||||||
}
|
}
|
||||||
|
|
||||||
class AuthScreenState(
|
class AuthScreenState(
|
||||||
private val stateScope: CoroutineScope,
|
private val stateScope: CoroutineScope,
|
||||||
private val loginUseCase: LoginUseCase,
|
private val loginUseCase: LoginUseCase,
|
||||||
|
private val onLoginSuccess: () -> Unit = {},
|
||||||
) {
|
) {
|
||||||
|
|
||||||
var username by mutableStateOf("")
|
var username by mutableStateOf("")
|
||||||
|
|
@ -31,8 +34,6 @@ class AuthScreenState(
|
||||||
private set
|
private set
|
||||||
var error by mutableStateOf<ErrorType?>(null)
|
var error by mutableStateOf<ErrorType?>(null)
|
||||||
private set
|
private set
|
||||||
var navigateToHome by mutableStateOf<Event<Unit>?>(null)
|
|
||||||
private set
|
|
||||||
|
|
||||||
fun onUsernameChanged(username: String) {
|
fun onUsernameChanged(username: String) {
|
||||||
this.username = username
|
this.username = username
|
||||||
|
|
@ -62,7 +63,7 @@ class AuthScreenState(
|
||||||
|
|
||||||
private fun processLoginStatus(loginStatus: LoginStatus) {
|
private fun processLoginStatus(loginStatus: LoginStatus) {
|
||||||
when (loginStatus) {
|
when (loginStatus) {
|
||||||
LoginStatus.SUCCESS -> navigateToHome = Event(Unit)
|
LoginStatus.SUCCESS -> onLoginSuccess()
|
||||||
LoginStatus.INVALID_CREDENTIALS -> error = ErrorType.INVALID_CREDENTIALS
|
LoginStatus.INVALID_CREDENTIALS -> error = ErrorType.INVALID_CREDENTIALS
|
||||||
LoginStatus.INVALID_USERNAME -> error = ErrorType.UNSUPPORTED_USERNAME
|
LoginStatus.INVALID_USERNAME -> error = ErrorType.UNSUPPORTED_USERNAME
|
||||||
LoginStatus.INVALID_PASSWORD -> error = ErrorType.UNSUPPORTED_PASSWORD
|
LoginStatus.INVALID_PASSWORD -> error = ErrorType.UNSUPPORTED_PASSWORD
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,8 @@ project.ext {
|
||||||
activity_ktx_version = "1.4.0"
|
activity_ktx_version = "1.4.0"
|
||||||
androidx_navigation = "2.4.0"
|
androidx_navigation = "2.4.0"
|
||||||
|
|
||||||
androidx_compose = "1.1.0-rc03"
|
androidx_compose = "1.1.0"
|
||||||
google_accompanist = "0.20.3"
|
google_accompanist = "0.23.1"
|
||||||
|
|
||||||
coroutines_version = "1.6.0"
|
coroutines_version = "1.6.0"
|
||||||
turbine_version = "0.7.0"
|
turbine_version = "0.7.0"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue