https://github.com/hoc081098/democoroutineschannelresult
Use `Kotlinx Coroutines Channel` to send and receive events between Fragments.
https://github.com/hoc081098/democoroutineschannelresult
android-mvi android-mvi-architecture android-mvi-flow android-mvi-sample coroutines-android coroutines-channels coroutines-flow coroutines-flow-mvi kotlin-coroutine-flow kotlin-coroutines kotlin-coroutines-flow kotlin-coroutines-mvvm kotlinx-coroutines mvi-android mvi-architecture mvi-coroutines-flow mvi-coroutines-flow-kotlin mvi-kotlin mvikotlin mvim
Last synced: 3 months ago
JSON representation
Use `Kotlinx Coroutines Channel` to send and receive events between Fragments.
- Host: GitHub
- URL: https://github.com/hoc081098/democoroutineschannelresult
- Owner: hoc081098
- Created: 2022-12-14T05:48:42.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T08:49:18.000Z (over 1 year ago)
- Last Synced: 2025-01-30T23:36:53.503Z (5 months ago)
- Topics: android-mvi, android-mvi-architecture, android-mvi-flow, android-mvi-sample, coroutines-android, coroutines-channels, coroutines-flow, coroutines-flow-mvi, kotlin-coroutine-flow, kotlin-coroutines, kotlin-coroutines-flow, kotlin-coroutines-mvvm, kotlinx-coroutines, mvi-android, mvi-architecture, mvi-coroutines-flow, mvi-coroutines-flow-kotlin, mvi-kotlin, mvikotlin, mvim
- Language: Kotlin
- Homepage:
- Size: 4.78 MB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# DemoCoroutinesChannelResult
Use `Kotlinx Coroutines Channel` to send and receive events between Fragments.
## Author: [Petrus Nguyễn Thái Học](https://github.com/hoc081098)
[](https://hits.seeyoufarm.com)
![]()
## Overview
### 1. Create `MainSingleEvent` class.
```kotlin
sealed interface MainSingleEvent> {
interface Key>val key: Key
data class HomeFragmentResult(val text: String) : MainSingleEvent {
override val key = HomeFragmentResultcompanion object : Key
}data class DashboardFragmentResult(val text: String) : MainSingleEvent {
override val key = DashboardFragmentResultcompanion object : Key
}data class HomeDetailsResult(val text: String) : MainSingleEvent {
override val key = HomeDetailsResultcompanion object : Key
}companion object {
val KEYS: Set> = setOf(
HomeFragmentResult,
DashboardFragmentResult,
HomeDetailsResult,
)
}
}
```### 2. Create `MainVM` class with buffered channels as event bus.
```kotlin
class MainVM : ViewModel() {
private val eventChannels: Map, Channel> =
MainSingleEvent.KEYS.associateBy(
keySelector = { it },
valueTransform = { Channel>(Channel.UNLIMITED) }
)fun > sendEvent(event: T) {
checkNotNull(eventChannels[event.key]) { "Must register ${event.key} in MainSingleEvent.Companion.KEYS before using!" }
.trySend(event)
.getOrThrow()
.also { Log.d("@@@", "Sent $event") }
}@Suppress("UNCHECKED_CAST")
fun , K : MainSingleEvent.Key> receiveEventFlow(key: K): Flow =
checkNotNull(eventChannels[key]) { "Must register $key in MainSingleEvent.Companion.KEYS before using!" }
.receiveAsFlow()
.map { it as T }
}
```### 3. Send and receive events in `Fragment`s.
We will share `MainVM` instance between `Fragment`s using `Activity` as owner.
```kotlin
private val mainVM by viewModels(
ownerProducer = { requireActivity() }
)// send in HomeFragment
mainVM.sendEvent(MainSingleEvent.HomeFragmentResult("Hello from HomeFragment"))// receive in others
mainVM.receiveEventFlow(MainSingleEvent.HomeFragmentResult)
.onEach { Log.d("###", "Received $it") }
.launchIn(lifecycleScope)
```