2019-08-19 16:50:33 +02:00
|
|
|
package com.github.apognu.otter.utils
|
|
|
|
|
2019-10-22 20:03:52 +02:00
|
|
|
import com.github.apognu.otter.Otter
|
2019-08-19 16:50:33 +02:00
|
|
|
import kotlinx.coroutines.Dispatchers.Main
|
|
|
|
import kotlinx.coroutines.GlobalScope
|
2019-10-29 23:41:44 +01:00
|
|
|
import kotlinx.coroutines.channels.Channel
|
|
|
|
import kotlinx.coroutines.channels.ReceiveChannel
|
|
|
|
import kotlinx.coroutines.channels.filter
|
|
|
|
import kotlinx.coroutines.channels.map
|
2019-08-19 16:50:33 +02:00
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
|
|
|
sealed class Command {
|
|
|
|
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 PlayNext(val track: Track) : Command()
|
|
|
|
class ReplaceQueue(val queue: List<Track>) : Command()
|
|
|
|
class RemoveFromQueue(val track: Track) : Command()
|
|
|
|
class MoveFromQueue(val oldPosition: Int, val newPosition: Int) : Command()
|
|
|
|
|
|
|
|
class PlayTrack(val index: Int) : Command()
|
|
|
|
}
|
|
|
|
|
|
|
|
sealed class Event {
|
|
|
|
object LogOut : Event()
|
|
|
|
|
|
|
|
class PlaybackError(val message: String) : Event()
|
|
|
|
object PlaybackStopped : Event()
|
|
|
|
class Buffering(val value: Boolean) : Event()
|
|
|
|
class TrackPlayed(val track: Track?, val play: Boolean) : Event()
|
|
|
|
class StateChanged(val playing: Boolean) : Event()
|
|
|
|
object QueueChanged : Event()
|
|
|
|
}
|
|
|
|
|
|
|
|
sealed class Request(var channel: Channel<Response>? = null) {
|
|
|
|
object GetState : Request()
|
|
|
|
object GetQueue : Request()
|
|
|
|
object GetCurrentTrack : Request()
|
|
|
|
}
|
|
|
|
|
|
|
|
sealed class Response {
|
|
|
|
class State(val playing: Boolean) : Response()
|
|
|
|
class Queue(val queue: List<Track>) : Response()
|
|
|
|
class CurrentTrack(val track: Track?) : Response()
|
|
|
|
}
|
|
|
|
|
|
|
|
object EventBus {
|
|
|
|
fun send(event: Event) {
|
|
|
|
GlobalScope.launch {
|
2019-10-22 20:03:52 +02:00
|
|
|
get().offer(event)
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 20:03:52 +02:00
|
|
|
fun get() = Otter.get().eventBus
|
2019-08-19 16:50:33 +02:00
|
|
|
|
|
|
|
inline fun <reified T : Event> asChannel(): ReceiveChannel<T> {
|
|
|
|
return get().openSubscription().filter { it is T }.map { it as T }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
object CommandBus {
|
|
|
|
fun send(command: Command) {
|
|
|
|
GlobalScope.launch {
|
2019-10-22 20:03:52 +02:00
|
|
|
get().offer(command)
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 20:03:52 +02:00
|
|
|
fun get() = Otter.get().commandBus
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
object RequestBus {
|
|
|
|
fun send(request: Request): Channel<Response> {
|
|
|
|
return Channel<Response>().also {
|
|
|
|
GlobalScope.launch(Main) {
|
|
|
|
request.channel = it
|
|
|
|
|
2019-10-22 20:03:52 +02:00
|
|
|
get().offer(request)
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 20:03:52 +02:00
|
|
|
fun get() = Otter.get().requestBus
|
2019-08-19 16:50:33 +02:00
|
|
|
|
|
|
|
inline fun <reified T> asChannel(): ReceiveChannel<T> {
|
|
|
|
return get().openSubscription().filter { it is T }.map { it as T }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
object ProgressBus {
|
|
|
|
fun send(current: Int, duration: Int, percent: Int) {
|
|
|
|
GlobalScope.launch {
|
2019-10-22 20:03:52 +02:00
|
|
|
Otter.get().progressBus.send(Triple(current, duration, percent))
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun asChannel(): ReceiveChannel<Triple<Int, Int, Int>> {
|
2019-10-22 20:03:52 +02:00
|
|
|
return Otter.get().progressBus.openSubscription()
|
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
|
|
|
|
}
|
|
|
|
}
|