Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ekeitho/uni

Unidirectional "Flow" architecture
https://github.com/ekeitho/uni

Last synced: about 2 months ago
JSON representation

Unidirectional "Flow" architecture

Awesome Lists containing this project

README

        

![uni github actions](https://github.com/ekeitho/uni/actions/workflows/android.yml/badge.svg)

# uni
Unidirectional "Flow" architecture. Main intention of usage is for Android.

An example usage
```kotlin

data class Wiki(private val wikiService: WikiService) {

data class State(val wikiResponse: WikiResponse? = null)
sealed class Action {
object FetchRandomWikiAction : Action()
data class WikiResponseAction(val wikiResponse: WikiResponse) : Action()
}

fun getViewModel() =
uniViewModelDSL(State()) {
effect { flow ->
flow
.filterIsInstance()
.flatMapLatest {
wikiService.getRandomWiki().map { Action.WikiResponseAction(it) }
}
}

reducer { action, state ->
when (action) {
is Action.WikiResponseAction -> state.copy(wikiResponse = action.wikiResponse)
is Action.FetchRandomWikiAction -> state.copy(wikiResponse = null)
}
}
}
}
```

Then in Compose, using whichever DI framework, in this case using Koins `getViewModel` we can do...

```kotlin
setContent {
val vm = getViewModel>()
val state by vm.liveDataState.observeAsState(initial = Wiki.State())

WikiScreen(
wikiResponse = state?.wikiResponse,
onButtonTap = {
vm.dispatch(Wiki.Action.FetchRandomWikiAction)
}
)
}
```

[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.ekeitho.uni/uni/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.ekeitho.uni/uni)

To use in your project:

```
implementation "com.ekeitho.uni:uni:$latest-version"
```