Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dhimandasgupta/learningmolecule
Trying to combine molecue and flowredux together to build some composables.
https://github.com/dhimandasgupta/learningmolecule
flowredux molecule
Last synced: 4 days ago
JSON representation
Trying to combine molecue and flowredux together to build some composables.
- Host: GitHub
- URL: https://github.com/dhimandasgupta/learningmolecule
- Owner: DhimanDasgupta
- Created: 2024-04-13T18:38:20.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-09-22T17:50:34.000Z (3 months ago)
- Last Synced: 2024-11-01T08:42:14.936Z (about 2 months ago)
- Topics: flowredux, molecule
- Language: Kotlin
- Homepage:
- Size: 168 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Getting Network State as a State Machine via FlowRedux
```
class NetworkStateMachine(
private val context: Context
) : FlowReduxStateMachine(initialState = defaultConnectionState()) {
init {
if (context !is Application) throw IllegalArgumentException("Context passed in NetworkStateMachines must be Application Context")spec {
inState {
collectWhileInState(context.observeConnectivityAsFlow()) { connectionState, state ->
state.override { connectionState }
}
}inState {
collectWhileInState(context.observeConnectivityAsFlow()) { connectionState, state ->
state.override { connectionState }
}
}
}
}fun immediateConnectedState(): ConnectionState = context.currentConnectivityState
companion object {
fun defaultConnectionState(): ConnectionState = ConnectionState.Unavailable
}
}
```# Observing the State machine and using the as a constructore parameter to the molecule presenter
```
class NetworkPresenter(
private val networkStateMachines: NetworkStateMachine
) {
@Composable
fun uiModel(): ConnectionState {
var connectionState by remember {
mutableStateOf(networkStateMachines.immediateConnectedState())
}LaunchedEffect(Unit) {
networkStateMachines.state.collect {
connectionState = it
}
}return connectionState
}
}
```# Getting Counter State as a State Machine via FlowRedux
```
class CounterStateMachine: FlowReduxStateMachine(initialState = defaultCounterState()) {
init {
spec {
// ....
}
}
companion object {
fun defaultCounterState(): CounterState = NotInitialized()
}
}
```# Observing the State machine and using the as a constructore parameter to the molecule presenter, passing the event to the State Machine via the Presenter
```
class CounterPresenter(
private val counterStateMachine: CounterStateMachine
) {
private val events = MutableSharedFlow(extraBufferCapacity = 1)@Composable
fun uiModel(): CounterState {
var counterState by remember {
mutableStateOf(CounterStateMachine.defaultCounterState())
}LaunchedEffect(Unit) {
events.collect { counterEvent ->
counterStateMachine.dispatch(counterEvent)
}
}LaunchedEffect(Unit) {
counterStateMachine.state.collect { currentState ->
counterState = currentState
}
}return counterState
}fun processEvent(event: CounterEvent) {
events.tryEmit(event)
}
}
```