Module name | Gestures | UI-states |
---|---|---|
welcome | WelcomeGesture | WelcomeUiState |
login | LoginGesture | LoginUiState |
commonregister | RegisterGesture | RegisterUiState |
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
sealed class WelcomeUiState { | |
/** | |
* Login state wrapper | |
* @property value Login UI state | |
*/ | |
data class Login(val value: LoginUiState) : WelcomeUiState() | |
/** | |
* Register state wrapper | |
* @property value Register UI 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
sealed class WelcomeGesture { | |
// Native gestures... | |
/** | |
* Login flow gesture | |
* @property value Login flow gesture | |
*/ | |
data class Login(val value: LoginGesture) : WelcomeGesture() | |
/** |
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 { | |
/** | |
* Flow host | |
*/ | |
val host: WelcomeFeatureHost | |
// Other dependencies... | |
} |
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 WelcomeFeatureHost { | |
/** | |
* Returns user to email entry screen | |
*/ | |
fun backToEmailEntry() | |
/** | |
* Authentication complete | |
*/ | |
fun complete() |
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 WithIdleViewModel : ViewModel() { | |
/** | |
* Creates initial state for state-machine | |
* You could process a deep-link here or restore from a saved state | |
*/ | |
private fun initStateMachine(): CommonMachineState<SomeGesture, SomeUiState> = InitialState() | |
/** | |
* State-machine instance | |
*/ |
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
@HiltViewModel | |
class LoginViewModel @Inject constructor(private val factory: LoginStateFactory) : ViewModel() { | |
/** | |
* Creates initializing state | |
*/ | |
private fun initializeStateMachine(): CommonMachineState<WelcomeGesture, WelcomeUiState> { | |
// Obtain data required to start from a saved-state handle or injection | |
val commonData = LoginDataState() | |
return factory.passwordEntry(commonData) |
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 CredentialsCheckStateTest { | |
private val data = LoginDataState() | |
private val factory: LoginStateFactory = mockk() | |
private val passwordEntry: LoginState = mockk() | |
@Test | |
fun returnsToPasswordEntryOnBack() = runTest { | |
every { factory.passwordEntry(any()) } returns passwordEntry | |
state.start(stateMachine) |
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) : LoginState(context) { | |
// State logic... | |
/** | |
* A part of [process] template to process UI gesture | |
*/ | |
override fun doProcess(gesture: LoginGesture) = when(gesture) { | |
LoginGesture.Back -> onBack() | |
else -> super.doProcess(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
@LoginScope | |
class LoginStateFactoryImpl @Inject constructor( | |
host: WelcomeFeatureHost, // External interface | |
renderer: LoginRenderer, // Renderer | |
private val createCredentialsCheck: CredentialsCheckState.Factory // Concrete state factory | |
) : LoginStateFactory { | |
// Dependencies common for each state provided through the context | |
private val context: LoginContext = object : LoginContext { | |
override val factory: LoginStateFactory = this@Impl |