Skip to content

Instantly share code, notes, and snippets.

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
override suspend fun load(loadType: LoadType, state: PagingState<Int, Repo>): MediatorResult {
val page = when (loadType) {
LoadType.REFRESH -> …
LoadType.PREPEND -> …
LoadType.APPEND -> …
}
val apiQuery = query + IN_QUALIFIER
override suspend fun initialize(): InitializeAction {
return InitializeAction.LAUNCH_INITIAL_REFRESH
}
@OptIn(ExperimentalPagingApi::class)
class GithubRemoteMediator(
) : RemoteMediator<Int, Repo>() {
}