mirror of
https://github.com/austinried/subtracks.git
synced 2026-11-10 15:11:58 +01:00
toast a friendly message for playback connection errors
Map the Media3 network/HTTP error codes to readable text and show it as a toast, instead of leaving an opaque ERR_ code on the now playing screen.
This commit is contained in:
@@ -45,7 +45,7 @@ fun appModule(
|
||||
single { SyncManager(get(), get(), get()) }
|
||||
single { QueueRepository(get()) }
|
||||
single<PlayerConnection> { MediaSessionConnection(context.applicationContext, get()) }
|
||||
single { PlaybackController(get(), get(), get()) }
|
||||
single { PlaybackController(get(), get(), get(), showMessage = toast) }
|
||||
viewModel { RootViewModel(get()) }
|
||||
viewModel { LibraryViewModel(get(), get(), get(), get(), get()) }
|
||||
viewModel { SettingsViewModel(get(), get()) }
|
||||
|
||||
@@ -111,7 +111,7 @@ class Media3PlayerHandle(
|
||||
"Playback error ${error.errorCodeName} at ${controller.currentPosition}/${controller.duration}",
|
||||
error,
|
||||
)
|
||||
listener.onError(error.errorCodeName)
|
||||
listener.onError(playbackErrorMessage(error))
|
||||
}
|
||||
|
||||
override fun onPlaybackStateChanged(playbackState: Int) {
|
||||
@@ -171,3 +171,14 @@ class Media3PlayerHandle(
|
||||
const val EXTRA_DURATION_MS = "durationMs"
|
||||
}
|
||||
}
|
||||
|
||||
internal fun playbackErrorMessage(error: PlaybackException): String =
|
||||
when (error.errorCode) {
|
||||
PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED,
|
||||
PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_TIMEOUT,
|
||||
-> "Can't reach the server. Check your connection."
|
||||
|
||||
PlaybackException.ERROR_CODE_IO_BAD_HTTP_STATUS -> "The server refused to stream this track."
|
||||
|
||||
else -> error.errorCodeName
|
||||
}
|
||||
|
||||
@@ -98,6 +98,7 @@ class PlaybackController(
|
||||
private val sourceRepository: SourceRepository,
|
||||
private val queueRepository: QueueRepository,
|
||||
private val connection: PlayerConnection,
|
||||
private val showMessage: (String) -> Unit = {},
|
||||
dispatcher: CoroutineDispatcher = Dispatchers.Main.immediate,
|
||||
) {
|
||||
private val scope = CoroutineScope(SupervisorJob() + dispatcher)
|
||||
@@ -870,6 +871,7 @@ class PlaybackController(
|
||||
|
||||
override fun onError(message: String) {
|
||||
lastError = message
|
||||
showMessage(message)
|
||||
refresh()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.subtracks.playback
|
||||
|
||||
import android.content.Context
|
||||
import androidx.media3.common.PlaybackException
|
||||
import androidx.room3.Room
|
||||
import androidx.sqlite.driver.bundled.BundledSQLiteDriver
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
@@ -45,6 +46,7 @@ class PlaybackControllerTest {
|
||||
private lateinit var queues: QueueRepository
|
||||
private lateinit var handle: FakePlayerHandle
|
||||
private lateinit var controller: PlaybackController
|
||||
private val messages = CopyOnWriteArrayList<String>()
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
@@ -59,7 +61,8 @@ class PlaybackControllerTest {
|
||||
sources = SourceRepository(db, OkHttpClient(), prefs, networkMode = networkMode)
|
||||
queues = QueueRepository(db)
|
||||
handle = FakePlayerHandle()
|
||||
controller = PlaybackController(sources, queues, FakePlayerConnection(handle), dispatcher = dispatcher)
|
||||
controller =
|
||||
PlaybackController(sources, queues, FakePlayerConnection(handle), showMessage = { messages += it }, dispatcher = dispatcher)
|
||||
}
|
||||
|
||||
@After
|
||||
@@ -299,6 +302,29 @@ class PlaybackControllerTest {
|
||||
assertEquals(null, controller.state.value.error)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun playbackErrorsAreShownAsAToast() {
|
||||
seedAlbum(3, sourceId = 1)
|
||||
controller.playAlbum(1, "al1", 0)
|
||||
await { handle.operations.contains("play") }
|
||||
|
||||
handle.fail("Can't reach the server. Check your connection.")
|
||||
await { messages.isNotEmpty() }
|
||||
|
||||
assertEquals(listOf("Can't reach the server. Check your connection."), messages.toList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun networkErrorsAreReportedWithAFriendlyMessage() {
|
||||
val failed = PlaybackException("net", null, PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED)
|
||||
val timeout = PlaybackException("net", null, PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_TIMEOUT)
|
||||
val refused = PlaybackException("http", null, PlaybackException.ERROR_CODE_IO_BAD_HTTP_STATUS)
|
||||
|
||||
assertEquals("Can't reach the server. Check your connection.", playbackErrorMessage(failed))
|
||||
assertEquals("Can't reach the server. Check your connection.", playbackErrorMessage(timeout))
|
||||
assertEquals("The server refused to stream this track.", playbackErrorMessage(refused))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun startingAnEmptyQueueStopsThePlayer() {
|
||||
seedAlbum(3, sourceId = 1)
|
||||
|
||||
@@ -51,7 +51,7 @@ class QueueViewModelTest {
|
||||
.build()
|
||||
val sources = SourceRepository(db, OkHttpClient(), fakeUserPreferences())
|
||||
queues = QueueRepository(db)
|
||||
controller = PlaybackController(sources, queues, FakePlayerConnection(FakePlayerHandle()), dispatcher)
|
||||
controller = PlaybackController(sources, queues, FakePlayerConnection(FakePlayerHandle()), dispatcher = dispatcher)
|
||||
viewModel = QueueViewModel(queues, controller)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user