ProgressBus: replace deprecated implementation.

Convert Progress from deprecated BroadcastChannel to a StateFlow.
This commit is contained in:
Hugh Daschbach 2022-08-20 01:05:42 -07:00 committed by Ryan Harg
parent 72b4aea35a
commit 6d1ad9cd78
No known key found for this signature in database
GPG Key ID: 89106F3A84E6958C
2 changed files with 6 additions and 7 deletions

View File

@ -12,7 +12,6 @@ import audio.funkwhale.ffa.utils.FFACache
import audio.funkwhale.ffa.utils.Request
import com.preference.PowerPreference
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.ConflatedBroadcastChannel
import org.koin.core.context.startKoin
import java.text.SimpleDateFormat
import java.util.Date
@ -31,7 +30,6 @@ class FFA : Application() {
val eventBus: BroadcastChannel<Event> = BroadcastChannel(10)
val commandBus: BroadcastChannel<Command> = BroadcastChannel(10)
val requestBus: BroadcastChannel<Request> = BroadcastChannel(10)
val progressBus: BroadcastChannel<Triple<Int, Int, Int>> = ConflatedBroadcastChannel()
override fun onCreate() {
super.onCreate()

View File

@ -8,8 +8,9 @@ import com.google.android.exoplayer2.offline.DownloadCursor
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.launch
sealed class Command {
@ -105,13 +106,13 @@ object RequestBus {
}
object ProgressBus {
private var _progress = MutableStateFlow(Triple(0, 0, 0))
val progress = _progress.asStateFlow()
fun send(current: Int, duration: Int, percent: Int) {
GlobalScope.launch(IO) {
FFA.get().progressBus.send(Triple(current, duration, percent))
}
_progress.value = Triple(current, duration, percent)
}
fun get() = FFA.get().progressBus.asFlow().conflate()
fun get() = progress
}
suspend inline fun <reified T> Channel<Response>.wait(): T? {