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

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.

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)

[![Hits](https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2Fhoc081098%2FDemoCoroutinesChannelResult&count_bg=%2379C83D&title_bg=%23555555&icon=&icon_color=%23E7E7E7&title=hits&edge_flat=false)](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 = HomeFragmentResult

companion object : Key
}

data class DashboardFragmentResult(val text: String) : MainSingleEvent {
override val key = DashboardFragmentResult

companion object : Key
}

data class HomeDetailsResult(val text: String) : MainSingleEvent {
override val key = HomeDetailsResult

companion 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)
```