Browse Source

Refactored and rationalized some events and commands on the buses.

housekeeping/remove-warnings
Antoine POPINEAU 4 years ago
parent
commit
b2d26a8127
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
  1. 3
      app/src/main/java/com/github/apognu/otter/Otter.kt
  2. 31
      app/src/main/java/com/github/apognu/otter/activities/MainActivity.kt
  3. 5
      app/src/main/java/com/github/apognu/otter/activities/SettingsActivity.kt
  4. 2
      app/src/main/java/com/github/apognu/otter/adapters/DownloadsAdapter.kt
  5. 1
      app/src/main/java/com/github/apognu/otter/adapters/PlaylistsAdapter.kt
  6. 20
      app/src/main/java/com/github/apognu/otter/fragments/FavoritesFragment.kt
  7. 10
      app/src/main/java/com/github/apognu/otter/fragments/LandscapeQueueFragment.kt
  8. 17
      app/src/main/java/com/github/apognu/otter/fragments/PlaylistTracksFragment.kt
  9. 10
      app/src/main/java/com/github/apognu/otter/fragments/QueueFragment.kt
  10. 20
      app/src/main/java/com/github/apognu/otter/fragments/TracksFragment.kt
  11. 44
      app/src/main/java/com/github/apognu/otter/playback/PlayerService.kt
  12. 8
      app/src/main/java/com/github/apognu/otter/utils/Bus.kt
  13. 3
      app/src/main/java/com/github/apognu/otter/utils/Models.kt

3
app/src/main/java/com/github/apognu/otter/Otter.kt

@ -14,7 +14,6 @@ import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor @@ -14,7 +14,6 @@ import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor
import com.google.android.exoplayer2.upstream.cache.SimpleCache
import com.preference.PowerPreference
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ConflatedBroadcastChannel
import java.text.SimpleDateFormat
import java.util.*
@ -29,7 +28,7 @@ class Otter : Application() { @@ -29,7 +28,7 @@ class Otter : Application() {
var defaultExceptionHandler: Thread.UncaughtExceptionHandler? = null
val eventBus: BroadcastChannel<Event> = BroadcastChannel(10)
val commandBus: Channel<Command> = Channel(10)
val commandBus: BroadcastChannel<Command> = BroadcastChannel(10)
val requestBus: BroadcastChannel<Request> = BroadcastChannel(10)
val progressBus: BroadcastChannel<Triple<Int, Int, Int>> = ConflatedBroadcastChannel()

31
app/src/main/java/com/github/apognu/otter/activities/MainActivity.kt

@ -38,6 +38,7 @@ import com.squareup.picasso.Picasso @@ -38,6 +38,7 @@ import com.squareup.picasso.Picasso
import jp.wasabeef.picasso.transformations.RoundedCornersTransformation
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.partial_now_playing.*
import kotlinx.android.synthetic.main.row_download.*
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.GlobalScope
@ -54,7 +55,9 @@ class MainActivity : AppCompatActivity() { @@ -54,7 +55,9 @@ class MainActivity : AppCompatActivity() {
private val favoriteRepository = FavoritesRepository(this)
private val favoriteCheckRepository = FavoritedRepository(this)
private var bus: Job? = null
private var eventBus: Job? = null
private var commandBus: Job? = null
private var progressBus: Job? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -79,7 +82,7 @@ class MainActivity : AppCompatActivity() { @@ -79,7 +82,7 @@ class MainActivity : AppCompatActivity() {
override fun onResume() {
super.onResume()
if (bus == null) {
if (eventBus == null) {
watchEventBus()
}
@ -132,8 +135,14 @@ class MainActivity : AppCompatActivity() { @@ -132,8 +135,14 @@ class MainActivity : AppCompatActivity() {
override fun onPause() {
super.onPause()
bus?.cancel()
bus = null
eventBus?.cancel()
eventBus = null
commandBus?.cancel()
commandBus = null
progressBus?.cancel()
progressBus = null
}
override fun onBackPressed() {
@ -228,7 +237,7 @@ class MainActivity : AppCompatActivity() { @@ -228,7 +237,7 @@ class MainActivity : AppCompatActivity() {
@SuppressLint("NewApi")
private fun watchEventBus() {
bus = GlobalScope.launch(Main) {
eventBus = GlobalScope.launch(Main) {
EventBus.get().collect { message ->
when (message) {
is Event.LogOut -> {
@ -274,8 +283,6 @@ class MainActivity : AppCompatActivity() { @@ -274,8 +283,6 @@ class MainActivity : AppCompatActivity() {
}
}
is Event.TrackPlayed -> refreshCurrentTrack(message.track)
is Event.RefreshTrack -> refreshCurrentTrack(message.track)
is Event.TrackFinished -> incrementListenCount(message.track)
is Event.StateChanged -> {
@ -305,7 +312,15 @@ class MainActivity : AppCompatActivity() { @@ -305,7 +312,15 @@ class MainActivity : AppCompatActivity() {
}
}
GlobalScope.launch(Main) {
commandBus = GlobalScope.launch(Main) {
CommandBus.get().collect { command ->
when (command) {
is Command.RefreshTrack -> refreshCurrentTrack(command.track)
}
}
}
progressBus = GlobalScope.launch(Main) {
ProgressBus.get().collect { (current, duration, percent) ->
now_playing_progress.progress = percent
now_playing_details_progress.progress = percent

5
app/src/main/java/com/github/apognu/otter/activities/SettingsActivity.kt

@ -13,8 +13,9 @@ import androidx.preference.SeekBarPreference @@ -13,8 +13,9 @@ import androidx.preference.SeekBarPreference
import com.github.apognu.otter.BuildConfig
import com.github.apognu.otter.Otter
import com.github.apognu.otter.R
import com.github.apognu.otter.utils.*
import com.preference.PowerPreference
import com.github.apognu.otter.utils.Cache
import com.github.apognu.otter.utils.Command
import com.github.apognu.otter.utils.CommandBus
class SettingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {

2
app/src/main/java/com/github/apognu/otter/adapters/DownloadsAdapter.kt

@ -79,7 +79,7 @@ class DownloadsAdapter(private val context: Context, private val listener: OnDow @@ -79,7 +79,7 @@ class DownloadsAdapter(private val context: Context, private val listener: OnDow
Download.STATE_QUEUED, Download.STATE_DOWNLOADING -> DownloadService.sendSetStopReason(context, PinService::class.java, download.contentId, 1, false)
Download.STATE_FAILED -> {
Track(download.id, download.title, Artist(0, download.artist, listOf()),Album(0, Album.Artist(""), "", Covers("")), 0, listOf(Track.Upload(download.contentId, 0, 0))).also {
Track(download.id, download.title, Artist(0, download.artist, listOf()), Album(0, Album.Artist(""), "", Covers("")), 0, listOf(Track.Upload(download.contentId, 0, 0))).also {
PinService.download(context, it)
}
}

1
app/src/main/java/com/github/apognu/otter/adapters/PlaylistsAdapter.kt

@ -8,7 +8,6 @@ import androidx.recyclerview.widget.RecyclerView @@ -8,7 +8,6 @@ import androidx.recyclerview.widget.RecyclerView
import com.github.apognu.otter.R
import com.github.apognu.otter.fragments.FunkwhaleAdapter
import com.github.apognu.otter.utils.Playlist
import com.github.apognu.otter.utils.log
import com.github.apognu.otter.utils.toDurationString
import com.squareup.picasso.Picasso
import jp.wasabeef.picasso.transformations.RoundedCornersTransformation

20
app/src/main/java/com/github/apognu/otter/fragments/FavoritesFragment.kt

@ -52,12 +52,18 @@ class FavoritesFragment : FunkwhaleFragment<Track, FavoritesAdapter>() { @@ -52,12 +52,18 @@ class FavoritesFragment : FunkwhaleFragment<Track, FavoritesAdapter>() {
GlobalScope.launch(Main) {
EventBus.get().collect { message ->
when (message) {
is Event.TrackPlayed -> refreshCurrentTrack()
is Event.RefreshTrack -> refreshCurrentTrack()
is Event.DownloadChanged -> refreshDownloadedTrack(message.download)
}
}
}
GlobalScope.launch(Main) {
CommandBus.get().collect { command ->
when (command) {
is Command.RefreshTrack -> refreshCurrentTrack(command.track)
}
}
}
}
private suspend fun refreshDownloadedTracks() {
@ -86,12 +92,10 @@ class FavoritesFragment : FunkwhaleFragment<Track, FavoritesAdapter>() { @@ -86,12 +92,10 @@ class FavoritesFragment : FunkwhaleFragment<Track, FavoritesAdapter>() {
}
}
private fun refreshCurrentTrack() {
GlobalScope.launch(Main) {
RequestBus.send(Request.GetCurrentTrack).wait<Response.CurrentTrack>()?.let { response ->
adapter.currentTrack = response.track
adapter.notifyDataSetChanged()
}
private fun refreshCurrentTrack(track: Track?) {
track?.let {
adapter.currentTrack = track
adapter.notifyDataSetChanged()
}
}

10
app/src/main/java/com/github/apognu/otter/fragments/LandscapeQueueFragment.kt

@ -66,11 +66,17 @@ class LandscapeQueueFragment : Fragment() { @@ -66,11 +66,17 @@ class LandscapeQueueFragment : Fragment() {
GlobalScope.launch(Main) {
EventBus.get().collect { message ->
when (message) {
is Event.TrackPlayed -> refresh()
is Event.RefreshTrack -> refresh()
is Event.QueueChanged -> refresh()
}
}
}
GlobalScope.launch(Main) {
CommandBus.get().collect { command ->
when (command) {
is Command.RefreshTrack -> refresh()
}
}
}
}
}

17
app/src/main/java/com/github/apognu/otter/fragments/PlaylistTracksFragment.kt

@ -158,21 +158,18 @@ class PlaylistTracksFragment : FunkwhaleFragment<PlaylistTrack, PlaylistTracksAd @@ -158,21 +158,18 @@ class PlaylistTracksFragment : FunkwhaleFragment<PlaylistTrack, PlaylistTracksAd
private fun watchEventBus() {
GlobalScope.launch(Main) {
EventBus.get().collect { message ->
when (message) {
is Event.TrackPlayed -> refreshCurrentTrack()
is Event.RefreshTrack -> refreshCurrentTrack()
CommandBus.get().collect { command ->
when (command) {
is Command.RefreshTrack -> refreshCurrentTrack(command.track)
}
}
}
}
private fun refreshCurrentTrack() {
GlobalScope.launch(Main) {
RequestBus.send(Request.GetCurrentTrack).wait<Response.CurrentTrack>()?.let { response ->
adapter.currentTrack = response.track
adapter.notifyDataSetChanged()
}
private fun refreshCurrentTrack(track: Track?) {
track?.let {
adapter.currentTrack = track
adapter.notifyDataSetChanged()
}
}

10
app/src/main/java/com/github/apognu/otter/fragments/QueueFragment.kt

@ -90,12 +90,18 @@ class QueueFragment : BottomSheetDialogFragment() { @@ -90,12 +90,18 @@ class QueueFragment : BottomSheetDialogFragment() {
GlobalScope.launch(Main) {
EventBus.get().collect { message ->
when (message) {
is Event.TrackPlayed -> refresh()
is Event.RefreshTrack -> refresh()
is Event.QueueChanged -> refresh()
}
}
}
GlobalScope.launch(Main) {
CommandBus.get().collect { command ->
when (command) {
is Command.RefreshTrack -> refresh()
}
}
}
}
inner class FavoriteListener : TracksAdapter.OnFavoriteListener {

20
app/src/main/java/com/github/apognu/otter/fragments/TracksFragment.kt

@ -141,12 +141,18 @@ class TracksFragment : FunkwhaleFragment<Track, TracksAdapter>() { @@ -141,12 +141,18 @@ class TracksFragment : FunkwhaleFragment<Track, TracksAdapter>() {
GlobalScope.launch(IO) {
EventBus.get().collect { message ->
when (message) {
is Event.TrackPlayed -> refreshCurrentTrack()
is Event.RefreshTrack -> refreshCurrentTrack()
is Event.DownloadChanged -> refreshDownloadedTrack(message.download)
}
}
}
GlobalScope.launch(Main) {
CommandBus.get().collect { command ->
when (command) {
is Command.RefreshTrack -> refreshCurrentTrack(command.track)
}
}
}
}
private suspend fun refreshDownloadedTracks() {
@ -175,12 +181,10 @@ class TracksFragment : FunkwhaleFragment<Track, TracksAdapter>() { @@ -175,12 +181,10 @@ class TracksFragment : FunkwhaleFragment<Track, TracksAdapter>() {
}
}
private suspend fun refreshCurrentTrack() {
RequestBus.send(Request.GetCurrentTrack).wait<Response.CurrentTrack>()?.let { response ->
withContext(Main) {
adapter.currentTrack = response.track
adapter.notifyDataSetChanged()
}
private fun refreshCurrentTrack(track: Track?) {
track?.let {
adapter.currentTrack = track
adapter.notifyDataSetChanged()
}
}

44
app/src/main/java/com/github/apognu/otter/playback/PlayerService.kt

@ -128,44 +128,44 @@ class PlayerService : Service() { @@ -128,44 +128,44 @@ class PlayerService : Service() {
private fun watchEventBus() {
jobs.add(GlobalScope.launch(Main) {
for (message in CommandBus.get()) {
when (message) {
CommandBus.get().collect { command ->
when (command) {
is Command.RefreshService -> {
EventBus.send(Event.QueueChanged)
if (queue.metadata.isNotEmpty()) {
EventBus.send(Event.RefreshTrack(queue.current()))
CommandBus.send(Command.RefreshTrack(queue.current()))
EventBus.send(Event.StateChanged(player.playWhenReady))
}
}
is Command.ReplaceQueue -> {
if (!message.fromRadio) radioPlayer.stop()
if (!command.fromRadio) radioPlayer.stop()
queue.replace(message.queue)
queue.replace(command.queue)
player.prepare(queue.datasources, true, true)
state(true)
EventBus.send(Event.RefreshTrack(queue.current()))
CommandBus.send(Command.RefreshTrack(queue.current()))
}
is Command.AddToQueue -> queue.append(message.tracks)
is Command.PlayNext -> queue.insertNext(message.track)
is Command.RemoveFromQueue -> queue.remove(message.track)
is Command.MoveFromQueue -> queue.move(message.oldPosition, message.newPosition)
is Command.AddToQueue -> queue.append(command.tracks)
is Command.PlayNext -> queue.insertNext(command.track)
is Command.RemoveFromQueue -> queue.remove(command.track)
is Command.MoveFromQueue -> queue.move(command.oldPosition, command.newPosition)
is Command.PlayTrack -> {
queue.current = message.index
player.seekTo(message.index, C.TIME_UNSET)
queue.current = command.index
player.seekTo(command.index, C.TIME_UNSET)
state(true)
EventBus.send(Event.RefreshTrack(queue.current()))
CommandBus.send(Command.RefreshTrack(queue.current()))
}
is Command.ToggleState -> toggle()
is Command.SetState -> state(message.state)
is Command.SetState -> state(command.state)
is Command.NextTrack -> {
player.next()
@ -174,19 +174,19 @@ class PlayerService : Service() { @@ -174,19 +174,19 @@ class PlayerService : Service() {
ProgressBus.send(0, 0, 0)
}
is Command.PreviousTrack -> previousTrack()
is Command.Seek -> progress(message.progress)
is Command.Seek -> progress(command.progress)
is Command.ClearQueue -> queue.clear()
is Command.PlayRadio -> {
queue.clear()
radioPlayer.play(message.radio)
radioPlayer.play(command.radio)
}
is Command.SetRepeatMode -> player.repeatMode = message.mode
is Command.SetRepeatMode -> player.repeatMode = command.mode
is Command.PinTrack -> PinService.download(this@PlayerService, message.track)
is Command.PinTracks -> message.tracks.forEach { PinService.download(this@PlayerService, it) }
is Command.PinTrack -> PinService.download(this@PlayerService, command.track)
is Command.PinTracks -> command.tracks.forEach { PinService.download(this@PlayerService, it) }
}
if (player.playWhenReady) {
@ -337,7 +337,7 @@ class PlayerService : Service() { @@ -337,7 +337,7 @@ class PlayerService : Service() {
EventBus.send(Event.StateChanged(playWhenReady))
if (queue.current == -1) {
EventBus.send(Event.TrackPlayed(queue.current(), playWhenReady))
CommandBus.send(Command.RefreshTrack(queue.current()))
}
when (playWhenReady) {
@ -379,7 +379,7 @@ class PlayerService : Service() { @@ -379,7 +379,7 @@ class PlayerService : Service() {
Cache.set(this@PlayerService, "current", queue.current.toString().toByteArray())
EventBus.send(Event.RefreshTrack(queue.current()))
CommandBus.send(Command.RefreshTrack(queue.current()))
}
override fun onPositionDiscontinuity(reason: Int) {
@ -398,7 +398,7 @@ class PlayerService : Service() { @@ -398,7 +398,7 @@ class PlayerService : Service() {
player.seekTo(queue.current, 0)
player.playWhenReady = true
EventBus.send(Event.RefreshTrack(queue.current()))
CommandBus.send(Command.RefreshTrack(queue.current()))
}
}

8
app/src/main/java/com/github/apognu/otter/utils/EventBus.kt → app/src/main/java/com/github/apognu/otter/utils/Bus.kt

@ -33,6 +33,8 @@ sealed class Command { @@ -33,6 +33,8 @@ sealed class Command {
class PlayTrack(val index: Int) : Command()
class PinTrack(val track: Track) : Command()
class PinTracks(val tracks: List<Track>) : Command()
class RefreshTrack(val track: Track?) : Command()
}
sealed class Event {
@ -41,9 +43,7 @@ sealed class Event { @@ -41,9 +43,7 @@ sealed class 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 TrackFinished(val track: Track?) : Event()
class RefreshTrack(val track: Track?) : Event()
class StateChanged(val playing: Boolean) : Event()
object QueueChanged : Event()
object RadioStarted : Event()
@ -78,11 +78,11 @@ object EventBus { @@ -78,11 +78,11 @@ object EventBus {
object CommandBus {
fun send(command: Command) {
GlobalScope.launch {
get().offer(command)
Otter.get().commandBus.offer(command)
}
}
fun get() = Otter.get().commandBus
fun get() = Otter.get().commandBus.asFlow()
}
object RequestBus {

3
app/src/main/java/com/github/apognu/otter/utils/Models.kt

@ -160,4 +160,5 @@ data class DownloadInfo( @@ -160,4 +160,5 @@ data class DownloadInfo(
val contentId: String,
val title: String,
val artist: String,
var download: Download?)
var download: Download?
)
Loading…
Cancel
Save