mirror of
https://github.com/austinried/subtracks.git
synced 2026-11-11 07:29:14 +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()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user