Unify offline detection + offline playlist UX (NetworkStatusController) #86
+13
-8
@@ -26,6 +26,19 @@ class OfflineGatedDataSource(
|
|||||||
) : DataSource {
|
) : DataSource {
|
||||||
|
|
||||||
override fun open(dataSpec: DataSpec): Long {
|
override fun open(dataSpec: DataSpec): Long {
|
||||||
|
gateOnHealth()
|
||||||
|
return try {
|
||||||
|
val opened = delegate.open(dataSpec)
|
||||||
|
health.reportSuccess() // bytes flowing from the server == reachable
|
||||||
|
opened
|
||||||
|
} catch (e: IOException) {
|
||||||
|
health.reportFailure() // real network read failed → arbitrate via /healthz
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Fast-fail before touching the network when the server can't be reached. */
|
||||||
|
private fun gateOnHealth() {
|
||||||
when (health.state.value) {
|
when (health.state.value) {
|
||||||
ServerHealth.Offline -> throw OfflineException(
|
ServerHealth.Offline -> throw OfflineException(
|
||||||
"Track not in the on-device cache and the device is offline.",
|
"Track not in the on-device cache and the device is offline.",
|
||||||
@@ -36,14 +49,6 @@ class OfflineGatedDataSource(
|
|||||||
// Unstable is non-gating: still try the network. Healthy too.
|
// Unstable is non-gating: still try the network. Healthy too.
|
||||||
ServerHealth.Unstable, ServerHealth.Healthy -> Unit
|
ServerHealth.Unstable, ServerHealth.Healthy -> Unit
|
||||||
}
|
}
|
||||||
return try {
|
|
||||||
val opened = delegate.open(dataSpec)
|
|
||||||
health.reportSuccess() // bytes flowing from the server == reachable
|
|
||||||
opened
|
|
||||||
} catch (e: IOException) {
|
|
||||||
health.reportFailure() // real network read failed → arbitrate via /healthz
|
|
||||||
throw e
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun close() = delegate.close()
|
override fun close() = delegate.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user