Open offline at once when there's no connection

With airplane mode on, checking the stored sign-in waited out every retry and
timeout, about 90 seconds on a Galaxy S20, before showing the downloads. The
phone app now tells AppState whether the device has any network, and with none
it goes straight to the offline screen; the network callback signs in again
once a connection appears. The TV keeps trying as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Yuriy PanovandClaude Opus 5.5 committed 2026-10-23 16:27:59 +06:00
1 parent a60194c5a7
commit 891b1b6efd
2 files changed
+20 -8

No files matched your search

@@ -89,6 +89,8 @@ class AppState(
buffering: Buffering = Buffering.Default, buffering: Buffering = Buffering.Default,
/** Cover sizes the interface shows, kept with downloads so they appear offline. */ /** Cover sizes the interface shows, kept with downloads so they appear offline. */
coverSizes: List<Int> = emptyList(), coverSizes: List<Int> = emptyList(),
/** Whether the device has any connection; without one, sign-in isn't attempted. */
private val online: () -> Boolean = { true },
) { ) {
var session: Session by mutableStateOf(Session.Restoring) var session: Session by mutableStateOf(Session.Restoring)
private set private set
@@ -197,6 +199,9 @@ class AppState(
session = signedOut() session = signedOut()
return@launch return@launch
} }
// With no connection at all, every retry and timeout would be waited out first, a
// minute or more; the network callback restores once there's a connection.
if (!online()) return@launch unreachable(stored)
var creds = stored var creds = stored
if (creds.needsRefresh()) creds = refresh(creds) ?: creds if (creds.needsRefresh()) creds = refresh(creds) ?: creds
try { try {
@@ -214,18 +219,18 @@ class AppState(
} catch (e: Throwable) { } catch (e: Throwable) {
val err = YmError.of(e) val err = YmError.of(e)
// Credentials stay for a retry when Yandex is only unreachable. // Credentials stay for a retry when Yandex is only unreachable.
if (err.isRetryable) { if (err.isRetryable) unreachable(creds) else forget(err)
credentials = creds
// Downloads are what's left to play; the phone shows them.
nav.reset(Route.Downloads())
session = Session.Unreachable
} else {
forget(err)
}
} }
} }
} }
private fun unreachable(creds: Credentials) {
credentials = creds
// Downloads are what's left to play; the phone shows them.
nav.reset(Route.Downloads())
session = Session.Unreachable
}
/** Refresh and persist; null when there is nothing to refresh with or Yandex said no. */ /** Refresh and persist; null when there is nothing to refresh with or Yandex said no. */
private suspend fun refresh(old: Credentials): Credentials? { private suspend fun refresh(old: Credentials): Credentials? {
if (old.refreshToken == null) return null if (old.refreshToken == null) return null
@@ -44,6 +44,12 @@ class Musyx : Application(), SingletonImageLoader.Factory {
watchNetwork() watchNetwork()
} }
private fun online(): Boolean {
val connectivity = getSystemService(ConnectivityManager::class.java)
val network = connectivity.activeNetwork ?: return false
return connectivity.getNetworkCapabilities(network)?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) == true
}
/** A connection coming back reconnects an offline session and resumes downloads. */ /** A connection coming back reconnects an offline session and resumes downloads. */
private fun watchNetwork() { private fun watchNetwork() {
val request = NetworkRequest.Builder().addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build() val request = NetworkRequest.Builder().addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build()
@@ -70,6 +76,7 @@ class Musyx : Application(), SingletonImageLoader.Factory {
// Mobile data slows and drops out: buffer whole tracks and preload the next one. // Mobile data slows and drops out: buffer whole tracks and preload the next one.
buffering = Buffering.Mobile, buffering = Buffering.Mobile,
coverSizes = listOf(COVER_SMALL, COVER_LARGE), coverSizes = listOf(COVER_SMALL, COVER_LARGE),
online = ::online,
) )
} }