Created
August 14, 2022 15:18
-
-
Save motorro/f243891ce03fac7a7dc7388a74288a23 to your computer and use it in GitHub Desktop.
Login flow state proxy
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
/** | |
* Proxy definition (for readability) | |
*/ | |
private typealias LoginProxy = ProxyMachineState< | |
WelcomeGesture, // Host gesture system | |
WelcomeUiState, // Host ui-state system | |
LoginGesture, // Feature gesture system | |
LoginUiState // Feature ui-state system | |
> | |
class LoginFlowState( | |
private val context: WelcomeContext, | |
private val data: WelcomeDataState, | |
private val loginComponentBuilder: LoginComponentBuilder | |
) : LoginProxy(), WelcomeFeatureHost { | |
/** | |
* Should have valid email at this point | |
*/ | |
private val email = requireNotNull(data.email) { | |
"Email is not provided" | |
} | |
/** | |
* Creates initial child state | |
*/ | |
override fun init(): CommonMachineState<LoginGesture, LoginUiState> { | |
val component = loginComponentBuilder.host(this).build() | |
val starter = EntryPoints.get(component, LoginEntryPoint::class.java).flowStarter() | |
return starter.start(email) | |
} | |
override fun mapGesture(parent: WelcomeGesture): LoginGesture? = when (parent) { | |
is WelcomeGesture.Login -> parent.value // Unwraps LoginGesture from host system | |
WelcomeGesture.Back -> LoginGesture.Back // Translates from one system to another | |
else -> null // Ignores irrelevant gestures | |
} | |
override fun mapUiState(child: LoginUiState): WelcomeUiState = WelcomeUiState.Login(child) | |
override fun backToEmailEntry() { | |
Timber.d("Transferring to e-mail entry...") | |
setMachineState(context.factory.emailEntry(data)) | |
} | |
override fun complete() { | |
Timber.d("Transferring to complete screen...") | |
setMachineState(context.factory.complete(email)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment