diff --git a/android/app/src/main/java/com/fabledsword/minstrel/player/OfflineGatedDataSource.kt b/android/app/src/main/java/com/fabledsword/minstrel/player/OfflineGatedDataSource.kt index 542a915f..314597f0 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/player/OfflineGatedDataSource.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/player/OfflineGatedDataSource.kt @@ -26,6 +26,19 @@ class OfflineGatedDataSource( ) : DataSource { 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) { ServerHealth.Offline -> throw OfflineException( "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. 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()