Keep retrying when Emby is slow at launch instead of opening the demo guide (0.43.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -41,7 +41,7 @@ internal class HttpsEmbyTransport(internal val deviceId: String = UUID.randomUUI
|
||||
connection.connectTimeout = 10_000
|
||||
connection.readTimeout = request.readTimeoutMs ?: if (request.path == "/Sessions/Logout") 5_000 else 15_000
|
||||
connection.setRequestProperty("Accept", "application/json")
|
||||
connection.setRequestProperty("X-Emby-Authorization", "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.42.0\"")
|
||||
connection.setRequestProperty("X-Emby-Authorization", "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.43.0\"")
|
||||
request.token?.let { connection.setRequestProperty("X-Emby-Token", it) }
|
||||
if (request.method == "POST") {
|
||||
val bytes = request.body.orEmpty().toByteArray(Charsets.UTF_8)
|
||||
|
||||
@@ -48,6 +48,8 @@ import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
private const val RESTORE_QUIET_TRIES = 4
|
||||
|
||||
@Composable
|
||||
internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolean,
|
||||
mainGuide: Boolean, mainHome: Boolean = false, onChannelAvailable: (Boolean) -> Unit, onRestoreChecked: () -> Unit = {}, onGuide: () -> Unit,
|
||||
@@ -58,6 +60,7 @@ internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolea
|
||||
var pendingSaved by remember { mutableStateOf<RememberedState?>(null) }
|
||||
var restoring by remember { mutableStateOf(true) }
|
||||
var restoreAttempt by remember { mutableIntStateOf(0) }
|
||||
var restoreFailures by remember { mutableIntStateOf(0) }
|
||||
var saveTicket by remember { mutableLongStateOf(savedStore.ticket()) }
|
||||
var username by remember { mutableStateOf("") }
|
||||
// Deliberately not rememberSaveable: credentials never enter saved instance state.
|
||||
@@ -173,9 +176,12 @@ internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolea
|
||||
active
|
||||
}
|
||||
// Read before the channels appear, so the guide never shows the loop for a moment first.
|
||||
scheduledChannels = withContext(Dispatchers.IO) {
|
||||
(saved.extra + listOfNotNull(saved.afterDark, saved.toonami)).mapNotNull { loop -> scheduler.load(saved.identity.userId, loop)?.also { resumed.rememberScheduledItems(it.plan.entries.map { entry -> entry.media.item }) } }.associateBy { it.number }
|
||||
}
|
||||
// A problem here falls back to the loops. It must never count against the saved connection below.
|
||||
scheduledChannels = try {
|
||||
withContext(Dispatchers.IO) {
|
||||
(saved.extra + listOfNotNull(saved.afterDark, saved.toonami)).mapNotNull { loop -> scheduler.load(saved.identity.userId, loop)?.also { resumed.rememberScheduledItems(it.plan.entries.map { entry -> entry.media.item }) } }.associateBy { it.number }
|
||||
}
|
||||
} catch (cancelled: CancellationException) { throw cancelled } catch (_: Exception) { emptyMap() }
|
||||
session = resumed; sample = resumed.sample; applyChoices(saved)
|
||||
saveTicket = savedStore.ticket()
|
||||
message = "Saved connection restored."
|
||||
@@ -188,13 +194,27 @@ internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolea
|
||||
catch (cancelled: CancellationException) { throw cancelled }
|
||||
catch (_: Exception) { /* Keep the expired token out of active use even if secure storage is unavailable. */ }
|
||||
message = "Your saved login expired. Sign in again; choices restore for the same account."
|
||||
} else message = "The saved connection could not be reached. Retry saved connection or sign in again."
|
||||
} else {
|
||||
message = "The saved connection could not be reached. Retry saved connection or sign in again."
|
||||
restoreFailures++
|
||||
}
|
||||
} catch (_: Exception) {
|
||||
// A corrupt file or unavailable key is never read as plaintext.
|
||||
try { withContext(Dispatchers.IO) { savedStore.clear() } } catch (_: Exception) { }
|
||||
pendingSaved = null
|
||||
message = "The saved connection could not be opened. Sign in again."
|
||||
} finally { restoring = false; onRestoreChecked() }
|
||||
} finally {
|
||||
restoring = false
|
||||
// A slow provider gets a few quiet retries before the app settles on anything else.
|
||||
if (restoreFailures !in 1 until RESTORE_QUIET_TRIES) onRestoreChecked()
|
||||
}
|
||||
}
|
||||
// Keeps trying after a provider outage, so channels come back without anyone pressing anything.
|
||||
LaunchedEffect(restoreFailures, session) {
|
||||
if (restoreFailures > 0 && session == null) {
|
||||
delay(if (restoreFailures < RESTORE_QUIET_TRIES) 5_000 else 60_000)
|
||||
if (session == null && !busy) restoreAttempt++
|
||||
}
|
||||
}
|
||||
LaunchedEffect(session, seriesMatches, approvedSeries, episodeSamples, channel, toonamiChannel, extraChannels, selectedNumber, restoring, afterDarkChannel, darkMovies, approvedDark) {
|
||||
val active = session
|
||||
|
||||
@@ -86,6 +86,8 @@ private fun OrbitApp() {
|
||||
val gearFocus = remember { FocusRequester() }
|
||||
LaunchedEffect(realChannelAvailable, restoreChecked) {
|
||||
if (!realChannelAvailable && restoreChecked && section == "Home") section = "TV Guide"
|
||||
// Channels that come back after a provider outage replace the demo guide with the main menu.
|
||||
if (realChannelAvailable && section == "TV Guide") section = "Home"
|
||||
}
|
||||
val programme = Schedule.programme(Schedule.channels[tuned], now)
|
||||
val context = androidx.compose.ui.platform.LocalContext.current
|
||||
|
||||
@@ -33,7 +33,7 @@ internal class SecureMediaDataSource(private val session: EmbySampleSession) : B
|
||||
next.instanceFollowRedirects = false
|
||||
next.connectTimeout = 10_000
|
||||
next.readTimeout = 20_000
|
||||
next.setRequestProperty("User-Agent", "OrbitTV/0.42.0")
|
||||
next.setRequestProperty("User-Agent", "OrbitTV/0.43.0")
|
||||
next.setRequestProperty("Accept-Encoding", "identity")
|
||||
session.mediaHeaders(address).forEach { (key, value) -> next.setRequestProperty(key, value) }
|
||||
if (dataSpec.position != 0L || dataSpec.length != C.LENGTH_UNSET.toLong()) {
|
||||
|
||||
Reference in New Issue
Block a user