Use a val for the StateFlow, and expose a StateFlow.

This commit is contained in:
Benoit Marty 2023-07-06 17:18:16 +02:00
parent b6c26d69b3
commit a2e04f7dc2
5 changed files with 9 additions and 14 deletions

View file

@ -119,7 +119,7 @@ class RootFlowNode @AssistedInject constructor(
private fun isUserLoggedInFlow(): Flow<Boolean> { private fun isUserLoggedInFlow(): Flow<Boolean> {
return combine( return combine(
authenticationService.isLoggedIn(), authenticationService.isLoggedIn(),
loginUserStory.loginFlowIsDone() loginUserStory.loginFlowIsDone
) { isLoggedIn, loginFlowIsDone -> ) { isLoggedIn, loginFlowIsDone ->
isLoggedIn && loginFlowIsDone isLoggedIn && loginFlowIsDone
} }

View file

@ -16,8 +16,8 @@
package io.element.android.features.login.api package io.element.android.features.login.api
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.StateFlow
interface LoginUserStory { interface LoginUserStory {
fun loginFlowIsDone(): Flow<Boolean> val loginFlowIsDone: StateFlow<Boolean>
} }

View file

@ -20,7 +20,6 @@ import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.features.login.api.LoginUserStory import io.element.android.features.login.api.LoginUserStory
import io.element.android.libraries.di.AppScope import io.element.android.libraries.di.AppScope
import io.element.android.libraries.di.SingleIn import io.element.android.libraries.di.SingleIn
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import javax.inject.Inject import javax.inject.Inject
@ -28,9 +27,7 @@ import javax.inject.Inject
@ContributesBinding(AppScope::class) @ContributesBinding(AppScope::class)
class DefaultLoginUserStory @Inject constructor() : LoginUserStory { class DefaultLoginUserStory @Inject constructor() : LoginUserStory {
// True by default, will be set to false when the login user story is started, and set to true again once it's done. // True by default, will be set to false when the login user story is started, and set to true again once it's done.
private val loginFlowIsDone: MutableStateFlow<Boolean> = MutableStateFlow(true) override val loginFlowIsDone: MutableStateFlow<Boolean> = MutableStateFlow(true)
override fun loginFlowIsDone(): Flow<Boolean> = loginFlowIsDone
fun setLoginFlowIsDone(value: Boolean) { fun setLoginFlowIsDone(value: Boolean) {
loginFlowIsDone.value = value loginFlowIsDone.value = value

View file

@ -31,7 +31,6 @@ import io.element.android.libraries.matrix.test.A_SESSION_ID
import io.element.android.libraries.matrix.test.A_THROWABLE import io.element.android.libraries.matrix.test.A_THROWABLE
import io.element.android.libraries.matrix.test.A_USER_NAME import io.element.android.libraries.matrix.test.A_USER_NAME
import io.element.android.libraries.matrix.test.auth.FakeAuthenticationService import io.element.android.libraries.matrix.test.auth.FakeAuthenticationService
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.junit.Test import org.junit.Test
@ -97,7 +96,7 @@ class LoginPasswordPresenterTest {
moleculeFlow(RecompositionClock.Immediate) { moleculeFlow(RecompositionClock.Immediate) {
presenter.present() presenter.present()
}.test { }.test {
assertThat(loginUserStory.loginFlowIsDone().first()).isFalse() assertThat(loginUserStory.loginFlowIsDone.value).isFalse()
val initialState = awaitItem() val initialState = awaitItem()
initialState.eventSink.invoke(LoginPasswordEvents.SetLogin(A_USER_NAME)) initialState.eventSink.invoke(LoginPasswordEvents.SetLogin(A_USER_NAME))
initialState.eventSink.invoke(LoginPasswordEvents.SetPassword(A_PASSWORD)) initialState.eventSink.invoke(LoginPasswordEvents.SetPassword(A_PASSWORD))
@ -108,7 +107,7 @@ class LoginPasswordPresenterTest {
assertThat(submitState.loginAction).isInstanceOf(Async.Loading::class.java) assertThat(submitState.loginAction).isInstanceOf(Async.Loading::class.java)
val loggedInState = awaitItem() val loggedInState = awaitItem()
assertThat(loggedInState.loginAction).isEqualTo(Async.Success(A_SESSION_ID)) assertThat(loggedInState.loginAction).isEqualTo(Async.Success(A_SESSION_ID))
assertThat(loginUserStory.loginFlowIsDone().first()).isTrue() assertThat(loginUserStory.loginFlowIsDone.value).isTrue()
} }
} }

View file

@ -30,7 +30,6 @@ import io.element.android.libraries.matrix.test.A_THROWABLE
import io.element.android.libraries.matrix.test.A_USER_ID import io.element.android.libraries.matrix.test.A_USER_ID
import io.element.android.libraries.matrix.test.auth.FakeAuthenticationService import io.element.android.libraries.matrix.test.auth.FakeAuthenticationService
import io.element.android.libraries.matrix.test.core.aBuildMeta import io.element.android.libraries.matrix.test.core.aBuildMeta
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.junit.Test import org.junit.Test
@ -101,7 +100,7 @@ class WaitListPresenterTest {
moleculeFlow(RecompositionClock.Immediate) { moleculeFlow(RecompositionClock.Immediate) {
presenter.present() presenter.present()
}.test { }.test {
assertThat(loginUserStory.loginFlowIsDone().first()).isFalse() assertThat(loginUserStory.loginFlowIsDone.value).isFalse()
val initialState = awaitItem() val initialState = awaitItem()
// First usage of AttemptLogin, nothing should happen // First usage of AttemptLogin, nothing should happen
initialState.eventSink.invoke(WaitListEvents.AttemptLogin) initialState.eventSink.invoke(WaitListEvents.AttemptLogin)
@ -111,9 +110,9 @@ class WaitListPresenterTest {
assertThat(submitState.loginAction).isInstanceOf(Async.Loading::class.java) assertThat(submitState.loginAction).isInstanceOf(Async.Loading::class.java)
val successState = awaitItem() val successState = awaitItem()
assertThat(successState.loginAction).isEqualTo(Async.Success(A_USER_ID)) assertThat(successState.loginAction).isEqualTo(Async.Success(A_USER_ID))
assertThat(loginUserStory.loginFlowIsDone().first()).isFalse() assertThat(loginUserStory.loginFlowIsDone.value).isFalse()
successState.eventSink.invoke(WaitListEvents.Continue) successState.eventSink.invoke(WaitListEvents.Continue)
assertThat(loginUserStory.loginFlowIsDone().first()).isTrue() assertThat(loginUserStory.loginFlowIsDone.value).isTrue()
} }
} }
} }