funkwhale-app-android/app/src/main/java/audio/funkwhale/ffa/utils/Bus.kt

130 lines
3.7 KiB
Kotlin
Raw Normal View History

package audio.funkwhale.ffa.utils
2019-08-19 16:50:33 +02:00
import audio.funkwhale.ffa.model.Radio
import audio.funkwhale.ffa.model.Track
import com.google.android.exoplayer2.offline.Download
import com.google.android.exoplayer2.offline.DownloadCursor
import kotlinx.coroutines.Dispatchers.IO
2019-08-19 16:50:33 +02:00
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
2019-08-19 16:50:33 +02:00
import kotlinx.coroutines.launch
sealed class Command {
class StartService(val command: Command) : Command()
2019-08-19 16:50:33 +02:00
object RefreshService : Command()
object ToggleState : Command()
class SetState(val state: Boolean) : Command()
object NextTrack : Command()
object PreviousTrack : Command()
class Seek(val progress: Int) : Command()
class AddToQueue(val tracks: List<Track>) : Command()
class AddToPlaylist(val tracks: List<Track>) : Command()
2019-08-19 16:50:33 +02:00
class PlayNext(val track: Track) : Command()
class ReplaceQueue(val queue: List<Track>, val fromRadio: Boolean = false) : Command()
2019-08-19 16:50:33 +02:00
class RemoveFromQueue(val track: Track) : Command()
class MoveFromQueue(val oldPosition: Int, val newPosition: Int) : Command()
2019-10-30 22:06:57 +01:00
object ClearQueue : Command()
object ShuffleQueue : Command()
class PlayRadio(val radio: Radio) : Command()
2019-08-19 16:50:33 +02:00
class SetRepeatMode(val mode: Int) : Command()
2019-08-19 16:50:33 +02:00
class PlayTrack(val index: Int) : Command()
2020-06-10 16:25:20 +02:00
class PinTrack(val track: Track) : Command()
2020-06-13 16:45:58 +02:00
class PinTracks(val tracks: List<Track>) : Command()
class RefreshTrack(val track: Track?) : Command()
2019-08-19 16:50:33 +02:00
}
sealed class Event {
object LogOut : Event()
class PlaybackError(val message: String) : Event()
object PlaybackStopped : Event()
class Buffering(val value: Boolean) : Event()
class TrackFinished(val track: Track?) : Event()
2019-08-19 16:50:33 +02:00
class StateChanged(val playing: Boolean) : Event()
object QueueChanged : Event()
object RadioStarted : Event()
object ListingsChanged : Event()
class DownloadChanged(val download: Download) : Event()
2019-08-19 16:50:33 +02:00
}
sealed class Request(var channel: Channel<Response>? = null) {
object GetState : Request()
object GetQueue : Request()
object GetCurrentTrack : Request()
object GetDownloads : Request()
2019-08-19 16:50:33 +02:00
}
sealed class Response {
class State(val playing: Boolean) : Response()
class Queue(val queue: List<Track>) : Response()
class CurrentTrack(val track: Track?) : Response()
class Downloads(val cursor: DownloadCursor) : Response()
2019-08-19 16:50:33 +02:00
}
object EventBus {
private var _events = MutableSharedFlow<Event>()
val events = _events.asSharedFlow()
2019-08-19 16:50:33 +02:00
fun send(event: Event) {
GlobalScope.launch(IO) {
_events.emit(event)
2019-08-19 16:50:33 +02:00
}
}
fun get() = events
2019-08-19 16:50:33 +02:00
}
object CommandBus {
private var _commands = MutableSharedFlow<Command>()
var commands = _commands.asSharedFlow()
2019-08-19 16:50:33 +02:00
fun send(command: Command) {
GlobalScope.launch(IO) {
_commands.emit(command)
2019-08-19 16:50:33 +02:00
}
}
fun get() = commands
2019-08-19 16:50:33 +02:00
}
object RequestBus {
private var _requests = MutableSharedFlow<Request>()
var requests = _requests.asSharedFlow()
2019-08-19 16:50:33 +02:00
fun send(request: Request): Channel<Response> {
return Channel<Response>().also {
GlobalScope.launch(IO) {
2019-08-19 16:50:33 +02:00
request.channel = it
_requests.emit(request)
2019-08-19 16:50:33 +02:00
}
}
}
fun get() = requests
2019-08-19 16:50:33 +02:00
}
object ProgressBus {
private var _progress = MutableStateFlow(Triple(0, 0, 0))
val progress = _progress.asStateFlow()
2019-08-19 16:50:33 +02:00
fun send(current: Int, duration: Int, percent: Int) {
_progress.value = Triple(current, duration, percent)
2019-08-19 16:50:33 +02:00
}
fun get() = progress
2019-08-19 16:50:33 +02:00
}
suspend inline fun <reified T> Channel<Response>.wait(): T? {
return when (val response = this.receive()) {
is T -> response
else -> null
}
}