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
interface LoginStateFactory { | |
/** | |
* Creates a state to handle existing user's password entry | |
* @param data Login data state | |
*/ | |
fun passwordEntry(data: LoginDataState): LoginState | |
/** | |
* Creates a state to check email/password | |
* @param data Data state |
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
class CredentialsCheckState( | |
context: LoginContext, | |
private val data: LoginDataState, | |
private val checkCredentials: CheckCredentials | |
) : LoginState(context) { | |
override fun doStart() { | |
// Use a context-provided dependency | |
setUiState(renderer.renderLoading(data)) | |
} |
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
abstract class LoginState( | |
context: LoginContext | |
): CoroutineState<LoginGesture, LoginUiState>(), LoginContext by context { | |
override fun doProcess(gesture: LoginGesture) { | |
Timber.w("Unsupported gesture: %s", gesture) | |
} | |
} |
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
interface LoginContext { | |
/** | |
* Common state factory (see below) | |
*/ | |
val factory: LoginStateFactory | |
/** | |
* External interface | |
*/ | |
val host: WelcomeFeatureHost |
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
class CredentialsCheckState(data: LoginDataState): CoroutineState<LoginGesture, LoginUiState>() { | |
/** | |
* Should have valid email at this point passed with inter-state data | |
*/ | |
private val email = requireNotNull(data.commonData.email) { | |
"Email is not provided" | |
} | |
/** |
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
class CredentialsCheckState( | |
data: LoginDataState, | |
private val checkCredentials: CheckCredentials | |
) : CoroutineState<LoginGesture, LoginUiState>() { | |
// State logic | |
/** | |
* Dedicated state factory | |
*/ |
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
internal class LoginRenderer { | |
/** | |
* Renders password form | |
*/ | |
fun renderPassword( | |
data: LoginDataState, | |
isValid: Boolean | |
): LoginUiState.PasswordEntry = LoginUiState.PasswordEntry( | |
data.email, | |
data.password.orEmpty(), |
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
class CredentialsCheckState(private val checkCredentials: CheckCredentials) { | |
// State logic | |
override fun doStart() { | |
stateScope.launch { | |
// Runs use-case | |
val valid = checkCredentials() | |
} | |
} |
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
@Composable | |
fun LceScreen(onExit: @Composable () -> Unit) { | |
val model: LceViewModel = viewModel() | |
val state = model.state.collectAsState(LceUiState.Loading) | |
BackHandler(onBack = { model.process(Back) }) | |
Scaffold( | |
modifier = Modifier.fillMaxSize(), | |
backgroundColor = MaterialTheme.colors.background, |
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
class LceViewModel : ViewModel() { | |
private fun initStateMachine(): CommonMachineState<LceGesture, LceUiState> = ItemListState() | |
private val stateMachine = FlowStateMachine(::initStateMachine) | |
val state: SharedFlow<LceUiState> = stateMachine.uiState | |
fun process(gesture: LceGesture) { | |
stateMachine.process(gesture) |