https://github.com/prongbang/mvi-flow
MVI = Coroutine + Flow Android
https://github.com/prongbang/mvi-flow
mvi mvi-android mvi-architecture mvi-clean-architecture mvi-coroutines-flow mvi-coroutines-flow-kotlin mvi-flow
Last synced: 2 months ago
JSON representation
MVI = Coroutine + Flow Android
- Host: GitHub
- URL: https://github.com/prongbang/mvi-flow
- Owner: prongbang
- Created: 2020-12-22T18:50:39.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-05-24T16:20:19.000Z (about 3 years ago)
- Last Synced: 2024-10-24T08:07:22.224Z (8 months ago)
- Topics: mvi, mvi-android, mvi-architecture, mvi-clean-architecture, mvi-coroutines-flow, mvi-coroutines-flow-kotlin, mvi-flow
- Language: Kotlin
- Homepage:
- Size: 159 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# mvi-flow
[](https://jitpack.io/#prongbang/mvi-flow)
## Setup
- `build.gradle`
```
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
```- `app/build.gradle`
```gradle
implementation 'com.github.prongbang:mvi-flow:1.0.5'
```## How to use
- MainIntent.kt
```kotlin
import com.prongbang.flow.FlowIntentsealed class MainIntent : FlowIntent {
object GetData : MainIntent()
}
```- MainSate.kt
```kotlin
import com.prongbang.flow.FlowStatesealed class MainState : FlowState {
data class Result(val data: String) : MainState()
}
```- MainEffect.kt
```kotlin
import com.prongbang.flow.FlowEffectsealed class MainEffect : FlowEffect {
data class Error(val data: String) : MainEffect()
}
```- GetMessageUseCase.kt
```kotlin
import com.prongbang.flow.FlowUseCase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContextclass GetMessageUseCase : FlowUseCase {
override suspend fun execute(params: Unit): String {
return withContext(Dispatchers.Default) {
"Hello MVI Flow"
}
}
}
```- MainViewModel.kt
```kotlin
import com.prongbang.flow.FlowViewModel
import com.prongbang.mviflowex.domain.MainEffect
import com.prongbang.mviflowex.domain.MainIntent
import com.prongbang.mviflowex.domain.MainStateclass MainViewModel(
private val getMessageUseCase: GetMessageUseCase
) : FlowViewModel() {
override fun onProcessIntent(intent: MainIntent) {
when (intent) {
MainIntent.GetData -> processGetData()
}
}private fun processGetData() = viewModelScope.launch(Dispatchers.IO) {
val result = getMessageUseCase.execute(Unit)
setState(MainState.Result(result))
setEffect(MainEffect.Error("Hello Effect"))
}
}
```- MainActivity.kt
```kotlin
import com.prongbang.flow.FlowViewRendererclass MainActivity : AppCompatActivity(), FlowViewRenderer {
private val mainViewModel: MainViewModel by viewModel()
private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
initObserve()
initLoad()
}override fun initObserve() {
mainViewModel.subscribe(::render, ::renderEffect)
}private fun initLoad() {
mainViewModel.process(MainIntent.GetData)
}override fun render(state: MainState) {
when (state) {
is MainState.Result -> {
binding.messageText.text = state.data
}
}
}override fun renderEffect(effects: MainEffect) {
when (effects) {
is MainEffect.Error -> {
binding.effectText.text = effects.data
}
}
}
}
```