Skip to content

Instantly share code, notes, and snippets.

private fun Flow<Action.GridSize>.gridSizeMutations(): Flow<Mutation<State>> =
distinctUntilChanged()
.map {
Mutation {
copy(gridSize = it.size)
}
}
fun archiveMutator(
scope: CoroutineScope,
route: ArchiveRoute,
repo: ArchiveRepository,
appMutator: AppMutator,
): Mutator<Action, StateFlow<State>> = stateFlowMutator(
scope = scope,
initialState = State(...),
started = SharingStarted.WhileSubscribed(stopTimeoutMillis = 2000),
transform = { actions ->
data class State(
val count: Int = 0
)
sealed class Action {
abstract val value: Int
data class Add(override val value: Int) : Action()
data class Subtract(override val value: Int) : Action()
}
data class Mutation<T : Any>(
val mutate: T.() -> T
)
interface Mutator<Action : Any, State : Any> {
val state: State
val accept: (Action) -> Unit
}
internal class CalculatorStoreFactory(private val storeFactory: StoreFactory) {
fun create(): CalculatorStore =
object : CalculatorStore, Store<Intent, State, Nothing> by storeFactory.create(
name = "CounterStore",
initialState = State(),
reducer = ReducerImpl
) {
}
public static void main(String[] args) {
// Let's make a Mobius Loop
MobiusLoop<Integer, CounterEvent, CounterEffect> loop = Mobius
.loop(new CounterLogic(), new CounterEffectHandler())
.startFrom(0);
// And start using our loop
loop.dispatchEvent(CounterEvent.INCREMENT); // Model is now 1
loop.dispatchEvent(CounterEvent.DECREMENT); // Model is now 0
loop.dispatchEvent(CounterEvent.DECREMENT); // Sound effect plays! Model is still 0
@Composable
fun SomePresenter(events: Flow<EventType>): ModelType {
// ...
}
val models: StateFlow<ModelType> = scope.launchMolecule {
SomePresenter(events)
}
val udfImplementation: (Flow<Action>) -> Flow<UiState> = ...
lifecycleScope.launch {
repoAdapter.loadStateFlow.collect { loadState ->
// Show a retry header if there was an error refreshing, and items were previously
// cached OR default to the default prepend state
header.loadState = loadState.mediator
?.refresh
?.takeIf { it is LoadState.Error && repoAdapter.itemCount > 0 }
?: loadState.prepend
val isListEmpty = loadState.refresh is LoadState.NotLoading && repoAdapter.itemCount == 0