From 891b1b6efd2a91dce85e05c1408c00d86123fde9 Mon Sep 17 00:00:00 2001 From: Yuriy Panov Date: Fri, 23 Oct 2026 16:27:59 +0600 Subject: [PATCH] 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 --- .../java/it/panov/musyx/state/AppState.kt | 21 ++++++++++++------- .../main/java/it/panov/musyx/mobile/Musyx.kt | 7 +++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/it/panov/musyx/state/AppState.kt b/core/src/main/java/it/panov/musyx/state/AppState.kt index 1046cd7..2dfed71 100644 --- a/core/src/main/java/it/panov/musyx/state/AppState.kt +++ b/core/src/main/java/it/panov/musyx/state/AppState.kt @@ -89,6 +89,8 @@ class AppState( buffering: Buffering = Buffering.Default, /** Cover sizes the interface shows, kept with downloads so they appear offline. */ coverSizes: List = 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) private set @@ -197,6 +199,9 @@ class AppState( session = signedOut() 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 if (creds.needsRefresh()) creds = refresh(creds) ?: creds try { @@ -214,18 +219,18 @@ class AppState( } catch (e: Throwable) { val err = YmError.of(e) // Credentials stay for a retry when Yandex is only unreachable. - if (err.isRetryable) { - credentials = creds - // Downloads are what's left to play; the phone shows them. - nav.reset(Route.Downloads()) - session = Session.Unreachable - } else { - forget(err) - } + if (err.isRetryable) unreachable(creds) 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. */ private suspend fun refresh(old: Credentials): Credentials? { if (old.refreshToken == null) return null diff --git a/mobile/src/main/java/it/panov/musyx/mobile/Musyx.kt b/mobile/src/main/java/it/panov/musyx/mobile/Musyx.kt index aa2fc48..357f7c4 100644 --- a/mobile/src/main/java/it/panov/musyx/mobile/Musyx.kt +++ b/mobile/src/main/java/it/panov/musyx/mobile/Musyx.kt @@ -44,6 +44,12 @@ class Musyx : Application(), SingletonImageLoader.Factory { 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. */ private fun watchNetwork() { 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. buffering = Buffering.Mobile, coverSizes = listOf(COVER_SMALL, COVER_LARGE), + online = ::online, ) }