diff --git a/app/build.gradle.kts b/app/build.gradle.kts index af7df66..88eda5f 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -97,12 +97,12 @@ play { dependencies { implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar", "*.aar")))) - implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.61") - implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2") - implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2") + implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.71") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.6") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.6") implementation("androidx.appcompat:appcompat:1.1.0") - implementation("androidx.core:core-ktx:1.4.0-alpha01") + implementation("androidx.core:core-ktx:1.5.0-alpha01") implementation("androidx.coordinatorlayout:coordinatorlayout:1.1.0") implementation("androidx.preference:preference:1.1.1") implementation("androidx.recyclerview:recyclerview:1.1.0") diff --git a/app/src/main/java/com/github/apognu/otter/activities/MainActivity.kt b/app/src/main/java/com/github/apognu/otter/activities/MainActivity.kt index ce9cf0b..cbab4fd 100644 --- a/app/src/main/java/com/github/apognu/otter/activities/MainActivity.kt +++ b/app/src/main/java/com/github/apognu/otter/activities/MainActivity.kt @@ -72,11 +72,19 @@ class MainActivity : AppCompatActivity() { .beginTransaction() .replace(R.id.container, BrowseFragment()) .commit() + + if (bus == null) { + watchEventBus() + } + + CommandBus.send(Command.RefreshService) } override fun onResume() { super.onResume() + CommandBus.send(Command.RefreshService) + startService(Intent(this, PlayerService::class.java)) DownloadService.start(this, PinService::class.java) @@ -115,12 +123,6 @@ class MainActivity : AppCompatActivity() { landscape_queue?.let { supportFragmentManager.beginTransaction().replace(R.id.landscape_queue, LandscapeQueueFragment()).commit() } - - if (bus == null) { - watchEventBus() - } - - CommandBus.send(Command.RefreshService) } override fun onPause() { @@ -489,12 +491,15 @@ class MainActivity : AppCompatActivity() { private fun incrementListenCount(track: Track?) { track?.let { GlobalScope.launch(IO) { - Fuel - .post(mustNormalizeUrl("/api/v1/history/listenings/")) - .authorize() - .header("Content-Type", "application/json") - .body(Gson().toJson(mapOf("track" to track.id))) - .awaitStringResponse() + try { + Fuel + .post(mustNormalizeUrl("/api/v1/history/listenings/")) + .authorize() + .header("Content-Type", "application/json") + .body(Gson().toJson(mapOf("track" to track.id))) + .awaitStringResponse() + } catch (_: Exception) { + } } } } diff --git a/app/src/main/java/com/github/apognu/otter/playback/PlayerService.kt b/app/src/main/java/com/github/apognu/otter/playback/PlayerService.kt index 41d7146..7be22da 100644 --- a/app/src/main/java/com/github/apognu/otter/playback/PlayerService.kt +++ b/app/src/main/java/com/github/apognu/otter/playback/PlayerService.kt @@ -135,7 +135,7 @@ class PlayerService : Service() { EventBus.send(Event.QueueChanged) if (queue.metadata.isNotEmpty()) { - EventBus.send(Event.RefreshTrack(queue.current(), player.playWhenReady)) + EventBus.send(Event.RefreshTrack(queue.current())) EventBus.send(Event.StateChanged(player.playWhenReady)) } } @@ -148,12 +148,7 @@ class PlayerService : Service() { state(true) - EventBus.send( - Event.RefreshTrack( - queue.current(), - true - ) - ) + EventBus.send(Event.RefreshTrack(queue.current())) } is Command.AddToQueue -> queue.append(message.tracks) @@ -167,7 +162,7 @@ class PlayerService : Service() { state(true) - EventBus.send(Event.RefreshTrack(queue.current(), true)) + EventBus.send(Event.RefreshTrack(queue.current())) } is Command.ToggleState -> toggle() @@ -351,7 +346,6 @@ class PlayerService : Service() { when (playbackState) { Player.STATE_READY -> mediaControlsManager.updateNotification(queue.current(), true) Player.STATE_BUFFERING -> EventBus.send(Event.Buffering(true)) - Player.STATE_IDLE -> state(false) Player.STATE_ENDED -> EventBus.send(Event.PlaybackStopped) } @@ -359,7 +353,6 @@ class PlayerService : Service() { } false -> { - EventBus.send(Event.StateChanged(false)) EventBus.send(Event.Buffering(false)) if (playbackState == Player.STATE_READY) { @@ -387,7 +380,7 @@ class PlayerService : Service() { Cache.set(this@PlayerService, "current", queue.current.toString().toByteArray()) - EventBus.send(Event.RefreshTrack(queue.current(), true)) + EventBus.send(Event.RefreshTrack(queue.current())) } override fun onPositionDiscontinuity(reason: Int) { @@ -401,10 +394,12 @@ class PlayerService : Service() { override fun onPlayerError(error: ExoPlaybackException) { EventBus.send(Event.PlaybackError(getString(R.string.error_playback))) - queue.current()?.let { - queue.remove(it) - player.prepare(queue.datasources) - } + queue.current++ + player.prepare(queue.datasources, true, true) + player.seekTo(queue.current, 0) + player.playWhenReady = true + + EventBus.send(Event.RefreshTrack(queue.current())) } } diff --git a/app/src/main/java/com/github/apognu/otter/utils/EventBus.kt b/app/src/main/java/com/github/apognu/otter/utils/EventBus.kt index fe58128..c5c35d6 100644 --- a/app/src/main/java/com/github/apognu/otter/utils/EventBus.kt +++ b/app/src/main/java/com/github/apognu/otter/utils/EventBus.kt @@ -3,6 +3,7 @@ package com.github.apognu.otter.utils import com.github.apognu.otter.Otter import com.google.android.exoplayer2.offline.Download import com.google.android.exoplayer2.offline.DownloadCursor +import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.channels.Channel @@ -43,7 +44,7 @@ sealed class 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?, val play: Boolean) : Event() + class RefreshTrack(val track: Track?) : Event() class StateChanged(val playing: Boolean) : Event() object QueueChanged : Event() object RadioStarted : Event() @@ -67,8 +68,8 @@ sealed class Response { object EventBus { fun send(event: Event) { - GlobalScope.launch { - Otter.get().eventBus.send(event) + GlobalScope.launch(IO) { + Otter.get().eventBus.offer(event) } }