Skip to content

Instantly share code, notes, and snippets.

View motorro's full-sized avatar

Nikolai Kotchetkov motorro

View GitHub Profile
@motorro
motorro / ContextTest.kt
Created January 24, 2023 14:31
Context receiver mocking with mock
View ContextTest.kt
package com.myexample
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.junit.Test
import kotlin.test.assertEquals
interface CallContext
@motorro
motorro / MainActivity.kt
Last active September 22, 2022 07:39
Main activity setup
View MainActivity.kt
@AndroidEntryPoint
class MainActivity : BaseActivity() {
// The model to run authentication and content visibility
private val model: MainScreenModel by viewModels
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
MainContent(model) {
@motorro
motorro / FlowStarter.kt
Created August 16, 2022 10:25
Flow started
View FlowStarter.kt
/**
* Common interface to start feature-flow
*/
interface FlowStarter<G: Any, U: Any> {
/**
* Creates a starting state
* @param email Email to proceed with
*/
fun start(email: String): CommonMachineState<G, U>
}
@motorro
motorro / EmailCheckState.kt
Last active August 14, 2022 15:38
Email check state
View EmailCheckState.kt
class EmailCheckState(
context: WelcomeContext,
private val data: WelcomeDataState,
private val checkEmail: CheckEmail
) : WelcomeState(context) {
private val email = requireNotNull(data.email) {
"Email is not provided"
}
@motorro
motorro / LoginFlowProxy.kt
Created August 14, 2022 15:18
Login flow state proxy
View LoginFlowProxy.kt
/**
* 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
>
@motorro
motorro / ProxyMachineState.kt
Created August 14, 2022 14:47
Proxy machine state
View ProxyMachineState.kt
abstract class ProxyMachineState<PG: Any, PU: Any, CG: Any, CU: Any> : CommonMachineState<PG, PU>() {
/**
* Proxy state machine
*/
private val machine = object : CommonStateMachine.Base<CG, CU>(::init) {
fun doStart() {
start()
}
@motorro
motorro / BaseInterfaces.kt
Created August 14, 2022 14:38
Bound machine
View BaseInterfaces.kt
/**
* Common state machine
* @param G UI gesture
* @param U UI state
*/
interface CommonStateMachine<G: Any, U: Any> : MachineInput<G>, MachineOutput<G, U>
/**
* Common state-machine state
* @param G UI gesture
@motorro
motorro / WelcomeScreen.kt
Created August 14, 2022 14:28
Application master view
View WelcomeScreen.kt
fun WelcomeScreen(onTerminate: @Composable () -> Unit) {
val model = hiltViewModel<WelcomeViewModel>()
val state = model.state.collectAsState(WelcomeUiState.Loading)
BackHandler(onBack = { model.process(Back) })
when (val uiState = state.value) {
// Native ui-state rendering...
@motorro
motorro / MasterView.kt
Created August 14, 2022 11:34
Feature master view
View MasterView.kt
@Composable
fun LoginScreen(state: LoginUiState, onGesture: (LoginGesture) -> Unit) {
// Login screen rendering...
}
@Composable
fun RegistrationScreen(state: RegisterUiState, onGesture: (RegisterGesture) -> Unit) {
// Registration screen rendering...
}
@motorro
motorro / WelcomeUiState.kt
Created August 14, 2022 11:31
UI-state adapter
View WelcomeUiState.kt
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