2018-05-27 10:22:12 +02:00
|
|
|
package com.keylesspalace.tusky.appstore
|
|
|
|
|
2024-02-29 15:28:48 +01:00
|
|
|
import androidx.lifecycle.LifecycleOwner
|
|
|
|
import androidx.lifecycle.lifecycleScope
|
|
|
|
import java.util.function.Consumer
|
2022-01-23 20:24:55 +01:00
|
|
|
import javax.inject.Inject
|
|
|
|
import javax.inject.Singleton
|
2024-01-04 17:00:55 +01:00
|
|
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
2024-02-29 15:28:48 +01:00
|
|
|
import kotlinx.coroutines.flow.SharedFlow
|
|
|
|
import kotlinx.coroutines.flow.asSharedFlow
|
|
|
|
import kotlinx.coroutines.launch
|
2018-05-27 10:22:12 +02:00
|
|
|
|
|
|
|
interface Event
|
|
|
|
|
2022-01-23 20:24:55 +01:00
|
|
|
@Singleton
|
|
|
|
class EventHub @Inject constructor() {
|
2018-05-27 10:22:12 +02:00
|
|
|
|
2024-03-29 20:02:12 +01:00
|
|
|
private val _events = MutableSharedFlow<Event>()
|
|
|
|
val events: SharedFlow<Event> = _events.asSharedFlow()
|
2023-09-09 21:11:23 +02:00
|
|
|
|
2023-03-18 10:11:47 +01:00
|
|
|
suspend fun dispatch(event: Event) {
|
2024-03-29 20:02:12 +01:00
|
|
|
_events.emit(event)
|
2018-05-27 10:22:12 +02:00
|
|
|
}
|
2023-09-11 22:19:34 +02:00
|
|
|
|
2024-02-29 15:28:48 +01:00
|
|
|
// TODO remove as soon as NotificationsFragment is Kotlin
|
|
|
|
fun subscribe(lifecycleOwner: LifecycleOwner, consumer: Consumer<Event>) {
|
|
|
|
lifecycleOwner.lifecycleScope.launch {
|
|
|
|
events.collect { event ->
|
|
|
|
consumer.accept(event)
|
|
|
|
}
|
|
|
|
}
|
2023-09-11 22:19:34 +02:00
|
|
|
}
|
2021-06-28 21:13:24 +02:00
|
|
|
}
|