|
|
|
@@ -46,6 +46,8 @@ import androidx.lifecycle.compose.LocalLifecycleOwner
|
|
|
|
|
import androidx.tv.material3.Text
|
|
|
|
|
import kotlinx.coroutines.CancellationException
|
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
|
import kotlinx.coroutines.async
|
|
|
|
|
import kotlinx.coroutines.awaitAll
|
|
|
|
|
import kotlinx.coroutines.Job
|
|
|
|
|
import kotlinx.coroutines.ensureActive
|
|
|
|
|
import kotlinx.coroutines.delay
|
|
|
|
@@ -54,6 +56,57 @@ import kotlinx.coroutines.withContext
|
|
|
|
|
|
|
|
|
|
private const val RESTORE_QUIET_TRIES = 4
|
|
|
|
|
|
|
|
|
|
/** The connection screen's state, held in one place so the screen itself stays small enough for the runtime to compile. */
|
|
|
|
|
private class ConnectionUi(savedTicket: Long) {
|
|
|
|
|
var setupSaved by mutableStateOf(false)
|
|
|
|
|
var pendingSaved by mutableStateOf<RememberedState?>(null)
|
|
|
|
|
var restoring by mutableStateOf(true)
|
|
|
|
|
var restoreAttempt by mutableIntStateOf(0)
|
|
|
|
|
var restoreFailures by mutableIntStateOf(0)
|
|
|
|
|
var saveTicket by mutableLongStateOf(savedTicket)
|
|
|
|
|
var username by mutableStateOf("")
|
|
|
|
|
var serverText by mutableStateOf(EmbyServer.origin)
|
|
|
|
|
var busy by mutableStateOf(false)
|
|
|
|
|
var seriesMatches by mutableStateOf<List<SeriesMatches>?>(null)
|
|
|
|
|
var episodeSamples by mutableStateOf<List<SeriesEpisodes>?>(null)
|
|
|
|
|
var approvedSeries by mutableStateOf<Set<String>>(emptySet())
|
|
|
|
|
var sample by mutableStateOf<EmbySample?>(null)
|
|
|
|
|
var session by mutableStateOf<EmbySampleSession?>(null)
|
|
|
|
|
var darkMovies by mutableStateOf<List<AfterDarkMovie>>(emptyList())
|
|
|
|
|
var approvedDark by mutableStateOf<Set<String>>(emptySet())
|
|
|
|
|
var lastNumber by mutableStateOf<Int?>(null)
|
|
|
|
|
var afterDarkChannel by mutableStateOf<SampleChannel?>(null)
|
|
|
|
|
var toonamiChannel by mutableStateOf<SampleChannel?>(null)
|
|
|
|
|
var extraChannels by mutableStateOf<List<SampleChannel>>(emptyList())
|
|
|
|
|
var selectedNumber by mutableIntStateOf(101)
|
|
|
|
|
var channel by mutableStateOf<SampleChannel?>(null)
|
|
|
|
|
var watchingChannel by mutableStateOf(false)
|
|
|
|
|
var watchingDriveIn by mutableStateOf(false)
|
|
|
|
|
var driveInPlan by mutableStateOf<ChannelPlan?>(null)
|
|
|
|
|
var tickets by mutableStateOf<Map<Long, Int>>(emptyMap())
|
|
|
|
|
var pendingDriveInWatch by mutableStateOf(false)
|
|
|
|
|
var reminder by mutableStateOf<DriveInReminder?>(null)
|
|
|
|
|
var reminders by mutableStateOf<List<ProgrammeReminder>>(emptyList())
|
|
|
|
|
var programmeReminder by mutableStateOf<ProgrammeReminder?>(null)
|
|
|
|
|
var windingDown by mutableStateOf(false)
|
|
|
|
|
var catchUpOpen by mutableStateOf<CatchUpEntry?>(null)
|
|
|
|
|
var driveInVisits by mutableIntStateOf((System.currentTimeMillis() / 60_000 % DRIVE_IN_SCENES).toInt())
|
|
|
|
|
var guideCategory by mutableStateOf(ChannelCategory.All)
|
|
|
|
|
var scheduledChannels by mutableStateOf<Map<Int, ScheduledChannel>>(emptyMap())
|
|
|
|
|
var suggesting by mutableStateOf<ChannelProgramming?>(null)
|
|
|
|
|
var suggestions by mutableStateOf<List<Suggestion>>(emptyList())
|
|
|
|
|
var settingsSection by mutableStateOf(SettingsSection.Channels)
|
|
|
|
|
var savingSuggestions by mutableStateOf(false)
|
|
|
|
|
var tickedSuggestions by mutableStateOf<Set<String>>(emptySet())
|
|
|
|
|
var lastSmartPress by mutableLongStateOf(0L)
|
|
|
|
|
var channelClockOffset by mutableLongStateOf(0)
|
|
|
|
|
var busyLabel by mutableStateOf("Connecting…")
|
|
|
|
|
var message by mutableStateOf<String?>(null)
|
|
|
|
|
var nextAttemptAt by mutableLongStateOf(0)
|
|
|
|
|
var now by mutableLongStateOf(System.currentTimeMillis())
|
|
|
|
|
var job by mutableStateOf<Job?>(null)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
|
internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolean,
|
|
|
|
|
/** The first-launch flow is showing: it draws everything once signed in, so this screen draws nothing. */
|
|
|
|
@@ -65,64 +118,65 @@ internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolea
|
|
|
|
|
// schedules are never open at the same time, and work begun for one keeps writing only to its own.
|
|
|
|
|
val workspace = Workspaces.current(appContext)
|
|
|
|
|
val savedStore = remember(workspace) { workspace.connection }
|
|
|
|
|
var setupSaved by remember { mutableStateOf(false) }
|
|
|
|
|
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("") }
|
|
|
|
|
var serverText by remember { mutableStateOf(EmbyServer.origin) }
|
|
|
|
|
val ui = remember { ConnectionUi(savedStore.ticket()) }
|
|
|
|
|
var setupSaved by ui::setupSaved
|
|
|
|
|
var pendingSaved by ui::pendingSaved
|
|
|
|
|
var restoring by ui::restoring
|
|
|
|
|
var restoreAttempt by ui::restoreAttempt
|
|
|
|
|
var restoreFailures by ui::restoreFailures
|
|
|
|
|
var saveTicket by ui::saveTicket
|
|
|
|
|
var username by ui::username
|
|
|
|
|
var serverText by ui::serverText
|
|
|
|
|
// Deliberately not rememberSaveable: credentials never enter saved instance state.
|
|
|
|
|
val password = remember { TextFieldState() }
|
|
|
|
|
var busy by remember { mutableStateOf(false) }
|
|
|
|
|
var seriesMatches by remember { mutableStateOf<List<SeriesMatches>?>(null) }
|
|
|
|
|
var episodeSamples by remember { mutableStateOf<List<SeriesEpisodes>?>(null) }
|
|
|
|
|
var approvedSeries by remember { mutableStateOf<Set<String>>(emptySet()) }
|
|
|
|
|
var sample by remember { mutableStateOf<EmbySample?>(null) }
|
|
|
|
|
var session by remember { mutableStateOf<EmbySampleSession?>(null) }
|
|
|
|
|
var darkMovies by remember { mutableStateOf<List<AfterDarkMovie>>(emptyList()) }
|
|
|
|
|
var approvedDark by remember { mutableStateOf<Set<String>>(emptySet()) }
|
|
|
|
|
var busy by ui::busy
|
|
|
|
|
var seriesMatches by ui::seriesMatches
|
|
|
|
|
var episodeSamples by ui::episodeSamples
|
|
|
|
|
var approvedSeries by ui::approvedSeries
|
|
|
|
|
var sample by ui::sample
|
|
|
|
|
var session by ui::session
|
|
|
|
|
var darkMovies by ui::darkMovies
|
|
|
|
|
var approvedDark by ui::approvedDark
|
|
|
|
|
val catalogue = remember(workspace) { workspace.catalogue }
|
|
|
|
|
var lastNumber by remember { mutableStateOf<Int?>(null) }
|
|
|
|
|
var afterDarkChannel by remember { mutableStateOf<SampleChannel?>(null) }
|
|
|
|
|
var toonamiChannel by remember { mutableStateOf<SampleChannel?>(null) }
|
|
|
|
|
var extraChannels by remember { mutableStateOf<List<SampleChannel>>(emptyList()) }
|
|
|
|
|
var selectedNumber by remember { mutableIntStateOf(101) }
|
|
|
|
|
var channel by remember { mutableStateOf<SampleChannel?>(null) }
|
|
|
|
|
var watchingChannel by remember { mutableStateOf(false) }
|
|
|
|
|
var watchingDriveIn by remember { mutableStateOf(false) }
|
|
|
|
|
var driveInPlan by remember { mutableStateOf<ChannelPlan?>(null) }
|
|
|
|
|
var tickets by remember { mutableStateOf<Map<Long, Int>>(emptyMap()) }
|
|
|
|
|
var pendingDriveInWatch by remember { mutableStateOf(false) }
|
|
|
|
|
var reminder by remember { mutableStateOf<DriveInReminder?>(null) }
|
|
|
|
|
var reminders by remember { mutableStateOf<List<ProgrammeReminder>>(emptyList()) }
|
|
|
|
|
var programmeReminder by remember { mutableStateOf<ProgrammeReminder?>(null) }
|
|
|
|
|
var windingDown by remember { mutableStateOf(false) }
|
|
|
|
|
var catchUpOpen by remember { mutableStateOf<CatchUpEntry?>(null) }
|
|
|
|
|
var lastNumber by ui::lastNumber
|
|
|
|
|
var afterDarkChannel by ui::afterDarkChannel
|
|
|
|
|
var toonamiChannel by ui::toonamiChannel
|
|
|
|
|
var extraChannels by ui::extraChannels
|
|
|
|
|
var selectedNumber by ui::selectedNumber
|
|
|
|
|
var channel by ui::channel
|
|
|
|
|
var watchingChannel by ui::watchingChannel
|
|
|
|
|
var watchingDriveIn by ui::watchingDriveIn
|
|
|
|
|
var driveInPlan by ui::driveInPlan
|
|
|
|
|
var tickets by ui::tickets
|
|
|
|
|
var pendingDriveInWatch by ui::pendingDriveInWatch
|
|
|
|
|
var reminder by ui::reminder
|
|
|
|
|
var reminders by ui::reminders
|
|
|
|
|
var programmeReminder by ui::programmeReminder
|
|
|
|
|
var windingDown by ui::windingDown
|
|
|
|
|
var catchUpOpen by ui::catchUpOpen
|
|
|
|
|
// Each visit opens on the next of the three scenes.
|
|
|
|
|
var driveInVisits by remember { mutableIntStateOf((System.currentTimeMillis() / 60_000 % DRIVE_IN_SCENES).toInt()) }
|
|
|
|
|
var guideCategory by remember { mutableStateOf(ChannelCategory.All) }
|
|
|
|
|
var driveInVisits by ui::driveInVisits
|
|
|
|
|
var guideCategory by ui::guideCategory
|
|
|
|
|
val liveHandoff = remember { LiveHandoff() }
|
|
|
|
|
val scheduleStore = remember(workspace) { workspace.schedules }
|
|
|
|
|
// Your own channels (Create your own) are this server's, read before anything plans or restores.
|
|
|
|
|
remember(workspace) { CustomChannels.load(workspace.scheduleDir); true }
|
|
|
|
|
val scheduler = remember(workspace) { ChannelScheduler(scheduleStore, copies = { if (catalogue.complete()) catalogue.idsByImdb(it) else null }, original = workspace.original) }
|
|
|
|
|
var scheduledChannels by remember { mutableStateOf<Map<Int, ScheduledChannel>>(emptyMap()) }
|
|
|
|
|
var suggesting by remember { mutableStateOf<ChannelProgramming?>(null) }
|
|
|
|
|
var suggestions by remember { mutableStateOf<List<Suggestion>>(emptyList()) }
|
|
|
|
|
var settingsSection by remember { mutableStateOf(SettingsSection.Channels) }
|
|
|
|
|
var savingSuggestions by remember { mutableStateOf(false) }
|
|
|
|
|
var tickedSuggestions by remember { mutableStateOf<Set<String>>(emptySet()) }
|
|
|
|
|
var lastSmartPress by remember { mutableLongStateOf(0L) }
|
|
|
|
|
var scheduledChannels by ui::scheduledChannels
|
|
|
|
|
var suggesting by ui::suggesting
|
|
|
|
|
var suggestions by ui::suggestions
|
|
|
|
|
var settingsSection by ui::settingsSection
|
|
|
|
|
var savingSuggestions by ui::savingSuggestions
|
|
|
|
|
var tickedSuggestions by ui::tickedSuggestions
|
|
|
|
|
var lastSmartPress by ui::lastSmartPress
|
|
|
|
|
fun onAir(loop: SampleChannel) = scheduledChannels[loop.number]?.takeIf { it.loop === loop } ?: loop
|
|
|
|
|
var channelClockOffset by remember { mutableLongStateOf(0) }
|
|
|
|
|
var busyLabel by remember { mutableStateOf("Connecting…") }
|
|
|
|
|
var channelClockOffset by ui::channelClockOffset
|
|
|
|
|
var busyLabel by ui::busyLabel
|
|
|
|
|
val sessionSlot = remember { EmbySessionSlot() }
|
|
|
|
|
var message by remember { mutableStateOf<String?>(null) }
|
|
|
|
|
var nextAttemptAt by remember { mutableLongStateOf(0) }
|
|
|
|
|
var now by remember { mutableLongStateOf(System.currentTimeMillis()) }
|
|
|
|
|
var job by remember { mutableStateOf<Job?>(null) }
|
|
|
|
|
var message by ui::message
|
|
|
|
|
var nextAttemptAt by ui::nextAttemptAt
|
|
|
|
|
var now by ui::now
|
|
|
|
|
var job by ui::job
|
|
|
|
|
val scope = rememberCoroutineScope()
|
|
|
|
|
val keyboard = LocalSoftwareKeyboardController.current
|
|
|
|
|
val focus = LocalFocusManager.current
|
|
|
|
@@ -190,24 +244,38 @@ internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolea
|
|
|
|
|
LaunchedEffect(restoreAttempt) {
|
|
|
|
|
restoring = true
|
|
|
|
|
try {
|
|
|
|
|
val opening = System.currentTimeMillis()
|
|
|
|
|
val saved = withContext(Dispatchers.IO) { savedStore.load() }
|
|
|
|
|
android.util.Log.i("OrbitStartup", "saved sign-in readMs=${System.currentTimeMillis() - opening}")
|
|
|
|
|
pendingSaved = saved
|
|
|
|
|
if (saved?.identity?.token != null && session == null) {
|
|
|
|
|
val slotTicket = sessionSlot.ticket()
|
|
|
|
|
val resumed = withContext(Dispatchers.IO) {
|
|
|
|
|
// The server this sign-in was saved with: always this workspace's own.
|
|
|
|
|
EmbyServer.origin = saved.origin
|
|
|
|
|
val active = EmbyClient(HttpsEmbyTransport(saved.identity.deviceId, saved.origin)).resume(saved)
|
|
|
|
|
if (!sessionSlot.attach(active, slotTicket)) { active.detach(); throw CancellationException() }
|
|
|
|
|
active
|
|
|
|
|
val started = System.currentTimeMillis()
|
|
|
|
|
// The channels' plans are on the box: read them while the server is asked about the sign-in, four at a time,
|
|
|
|
|
// so the guide is ready as soon as the server answers. A plan that can't be read leaves its channel on the loop.
|
|
|
|
|
val loops = saved.extra + listOfNotNull(saved.orbitOne, saved.afterDark, saved.toonami)
|
|
|
|
|
val reading = async(Dispatchers.IO) {
|
|
|
|
|
loops.chunked(((loops.size + 3) / 4).coerceAtLeast(1)).map { group -> async { group.mapNotNull { loop ->
|
|
|
|
|
try { scheduler.load(saved.identity.userId, loop) } catch (cancelled: CancellationException) { throw cancelled } catch (_: Exception) { null }
|
|
|
|
|
} } }.awaitAll().flatten()
|
|
|
|
|
}
|
|
|
|
|
val resumed = try {
|
|
|
|
|
withContext(Dispatchers.IO) {
|
|
|
|
|
// The server this sign-in was saved with: always this workspace's own.
|
|
|
|
|
EmbyServer.origin = saved.origin
|
|
|
|
|
val active = EmbyClient(HttpsEmbyTransport(saved.identity.deviceId, saved.origin)).resume(saved)
|
|
|
|
|
if (!sessionSlot.attach(active, slotTicket)) { active.detach(); throw CancellationException() }
|
|
|
|
|
active
|
|
|
|
|
}
|
|
|
|
|
} catch (failure: Throwable) { reading.cancel(); throw failure }
|
|
|
|
|
val resumedMs = System.currentTimeMillis() - started
|
|
|
|
|
// Read before the channels appear, so the guide never shows the loop for a moment first.
|
|
|
|
|
// 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.orbitOne, 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() }
|
|
|
|
|
val plans = try { reading.await() } catch (cancelled: CancellationException) { throw cancelled } catch (_: Exception) { emptyList() }
|
|
|
|
|
// Every channel's items at once: adding them channel by channel copied the growing map each time.
|
|
|
|
|
resumed.rememberScheduledItems(plans.flatMap { it.plan.entries.map { entry -> entry.media.item } })
|
|
|
|
|
scheduledChannels = plans.associateBy { it.number }
|
|
|
|
|
android.util.Log.i("OrbitStartup", "restore resumeMs=$resumedMs readyMs=${System.currentTimeMillis() - started} channels=${plans.size}")
|
|
|
|
|
session = resumed; sample = resumed.sample; applyChoices(saved)
|
|
|
|
|
saveTicket = savedStore.ticket()
|
|
|
|
|
} else if (saved != null && session == null) message = "Sign in again to restore your saved channel choices."
|
|
|
|
@@ -564,7 +632,8 @@ internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolea
|
|
|
|
|
finally { busy = false; savingSuggestions = false }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
val channelMaker = session?.let { active -> ChannelMaker(
|
|
|
|
|
// Remembered, so the phone link isn't handed a new one on every redraw.
|
|
|
|
|
val channelMaker = remember(session, workspace) { session?.let { active -> ChannelMaker(
|
|
|
|
|
suggest = { description, chosen, skipped -> withContext(Dispatchers.IO) {
|
|
|
|
|
val wish = wishOf(description)
|
|
|
|
|
val rows = catalogue.chairRows(wish.genres, wish.years, wish.type, if (wish.genres.isEmpty() && wish.years == null) description else null)
|
|
|
|
@@ -580,7 +649,7 @@ internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolea
|
|
|
|
|
android.util.Log.i("OrbitSchedule", "yours channel=${channel.number} style=${channel.style} titles=${shows.size}")
|
|
|
|
|
SmartChannels.firstOrNull { it.number == channel.number }?.let { setSmartSchedule(it, true) }
|
|
|
|
|
"${channel.name} is ready on ${channel.number}. Its first programmes are being planned now."
|
|
|
|
|
}) }
|
|
|
|
|
}) } }
|
|
|
|
|
// The phone page (Create your own from a phone) asks the same of this screen as Settings does, and can tune the TV.
|
|
|
|
|
DisposableEffect(channelMaker) {
|
|
|
|
|
PhoneLink.handler = channelMaker?.let { m -> PhoneHandler(m, isOn = { n -> scheduledChannels.containsKey(n) }, watch = { n ->
|
|
|
|
@@ -661,13 +730,63 @@ internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolea
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// The sign-in form is a composable of its own, so this screen's method stays small enough for the runtime to compile.
|
|
|
|
|
fun signIn() {
|
|
|
|
|
if (!ready) return
|
|
|
|
|
val loginName = username.trim()
|
|
|
|
|
val secret = password.text.toString()
|
|
|
|
|
val server = EmbyServer.normalise(serverText)
|
|
|
|
|
if (server == null) { message = "Enter your server's https:// address, like https://emby.example.com, or http:// with a home-network address."; return }
|
|
|
|
|
clearPassword(); keyboard?.hide(); focus.clearFocus()
|
|
|
|
|
closeSession()
|
|
|
|
|
val ticket = sessionSlot.ticket()
|
|
|
|
|
busyLabel = "Connecting…"
|
|
|
|
|
busy = true; message = null; sample = null
|
|
|
|
|
nextAttemptAt = System.currentTimeMillis() + 15_000
|
|
|
|
|
job = scope.launch {
|
|
|
|
|
try {
|
|
|
|
|
var switched = false
|
|
|
|
|
val connected = withContext(Dispatchers.IO) {
|
|
|
|
|
// It must answer as an Emby server before the password goes anywhere.
|
|
|
|
|
EmbyClient(HttpsEmbyTransport(origin = server)).checkServer()
|
|
|
|
|
// Another server opens its own workspace; this one's data stays exactly where it is.
|
|
|
|
|
if (server != Workspaces.current(appContext).origin) { Workspaces.open(appContext, server); switched = true }
|
|
|
|
|
val opened = EmbyClient(HttpsEmbyTransport(origin = server)).connect(loginName, secret)
|
|
|
|
|
if (!sessionSlot.attach(opened, ticket)) { opened.close(); throw CancellationException() }
|
|
|
|
|
opened
|
|
|
|
|
}
|
|
|
|
|
val saved = if (switched) withContext(Dispatchers.IO) { runCatching { Workspaces.current(appContext).connection.load() }.getOrNull() }.also { pendingSaved = it }
|
|
|
|
|
else pendingSaved
|
|
|
|
|
if (saved != null && connected.matchesAccount(saved.identity)) {
|
|
|
|
|
connected.restoreRemembered(saved); applyChoices(saved)
|
|
|
|
|
}
|
|
|
|
|
session = connected; sample = connected.sample
|
|
|
|
|
saveTicket = Workspaces.current(appContext).connection.ticket()
|
|
|
|
|
}
|
|
|
|
|
catch (cancelled: CancellationException) { throw cancelled }
|
|
|
|
|
catch (failure: EmbyCheckException) { message = failure.message }
|
|
|
|
|
catch (_: Exception) { message = "Signing in couldn't finish. Try again later." }
|
|
|
|
|
finally { busy = false }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
SignInForm(modifier, serverText, { serverText = it }, username, { username = it }, password, busy, restoring, ready, now < nextAttemptAt, message,
|
|
|
|
|
pendingSaved?.identity?.token != null, keyboard, focus, passwordFocus, submitFocus, ::signIn, { restoreAttempt++ }, { closeSession(signOut = true) })
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Signing in: the server's address, username and password, and what to do with them. */
|
|
|
|
|
@Composable
|
|
|
|
|
private fun SignInForm(modifier: Modifier, serverText: String, onServer: (String) -> Unit, username: String, onUsername: (String) -> Unit,
|
|
|
|
|
password: TextFieldState, busy: Boolean, restoring: Boolean, ready: Boolean, waiting: Boolean, message: String?, hasSaved: Boolean,
|
|
|
|
|
keyboard: androidx.compose.ui.platform.SoftwareKeyboardController?, focus: androidx.compose.ui.focus.FocusManager,
|
|
|
|
|
passwordFocus: FocusRequester, submitFocus: FocusRequester, onSignIn: () -> Unit, onRetry: () -> Unit, onForget: () -> Unit) {
|
|
|
|
|
Box(modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
|
|
|
|
|
Column(Modifier.width(460.dp).verticalScroll(rememberScrollState()), verticalArrangement = Arrangement.spacedBy(8.dp)) {
|
|
|
|
|
Text("Sign in to Emby or Jellyfin", color = Ink, fontSize = 26.sp, fontWeight = FontWeight.SemiBold)
|
|
|
|
|
// The server's address (release essentials, 6 November 2026): typed in, HTTPS, or plain HTTP to a home server only.
|
|
|
|
|
Text("Server address", color = Muted, fontSize = 14.sp)
|
|
|
|
|
var serverFocused by remember { mutableStateOf(false) }
|
|
|
|
|
BasicTextField(value = serverText, onValueChange = { if (it.length <= 255) serverText = it.replace("\n", "").trim() },
|
|
|
|
|
BasicTextField(value = serverText, onValueChange = { if (it.length <= 255) onServer(it.replace("\n", "").trim()) },
|
|
|
|
|
enabled = !busy && !restoring, singleLine = true, textStyle = TextStyle(color = Ink, fontSize = 18.sp),
|
|
|
|
|
cursorBrush = SolidColor(Yellow),
|
|
|
|
|
keyboardOptions = KeyboardOptions(autoCorrectEnabled = false, keyboardType = KeyboardType.Uri, imeAction = ImeAction.Next),
|
|
|
|
@@ -687,7 +806,7 @@ internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolea
|
|
|
|
|
color = Yellow, fontSize = 11.sp)
|
|
|
|
|
Text("Username", color = Muted, fontSize = 14.sp)
|
|
|
|
|
var userFocused by remember { mutableStateOf(false) }
|
|
|
|
|
BasicTextField(value = username, onValueChange = { if (it.length <= 256) username = it.replace("\n", "") },
|
|
|
|
|
BasicTextField(value = username, onValueChange = { if (it.length <= 256) onUsername(it.replace("\n", "")) },
|
|
|
|
|
enabled = !busy && !restoring, singleLine = true, textStyle = TextStyle(color = Ink, fontSize = 18.sp),
|
|
|
|
|
cursorBrush = SolidColor(Yellow),
|
|
|
|
|
keyboardOptions = KeyboardOptions(autoCorrectEnabled = false, imeAction = ImeAction.Next),
|
|
|
|
@@ -717,51 +836,14 @@ internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolea
|
|
|
|
|
true
|
|
|
|
|
} else false
|
|
|
|
|
}.padding(12.dp))
|
|
|
|
|
OrbitButton(if (busy) "Signing in…" else if (now < nextAttemptAt) "Wait before trying again" else "Sign in",
|
|
|
|
|
Modifier.fillMaxWidth().focusRequester(submitFocus), enabled = ready, onClick = {
|
|
|
|
|
if (!ready) return@OrbitButton
|
|
|
|
|
val loginName = username.trim()
|
|
|
|
|
val secret = password.text.toString()
|
|
|
|
|
val server = EmbyServer.normalise(serverText)
|
|
|
|
|
if (server == null) { message = "Enter your server's https:// address, like https://emby.example.com, or http:// with a home-network address."; return@OrbitButton }
|
|
|
|
|
clearPassword(); keyboard?.hide(); focus.clearFocus()
|
|
|
|
|
closeSession()
|
|
|
|
|
val ticket = sessionSlot.ticket()
|
|
|
|
|
busyLabel = "Connecting…"
|
|
|
|
|
busy = true; message = null; sample = null
|
|
|
|
|
nextAttemptAt = System.currentTimeMillis() + 15_000
|
|
|
|
|
job = scope.launch {
|
|
|
|
|
try {
|
|
|
|
|
var switched = false
|
|
|
|
|
val connected = withContext(Dispatchers.IO) {
|
|
|
|
|
// It must answer as an Emby server before the password goes anywhere.
|
|
|
|
|
EmbyClient(HttpsEmbyTransport(origin = server)).checkServer()
|
|
|
|
|
// Another server opens its own workspace; this one's data stays exactly where it is.
|
|
|
|
|
if (server != Workspaces.current(appContext).origin) { Workspaces.open(appContext, server); switched = true }
|
|
|
|
|
val opened = EmbyClient(HttpsEmbyTransport(origin = server)).connect(loginName, secret)
|
|
|
|
|
if (!sessionSlot.attach(opened, ticket)) { opened.close(); throw CancellationException() }
|
|
|
|
|
opened
|
|
|
|
|
}
|
|
|
|
|
val saved = if (switched) withContext(Dispatchers.IO) { runCatching { Workspaces.current(appContext).connection.load() }.getOrNull() }.also { pendingSaved = it }
|
|
|
|
|
else pendingSaved
|
|
|
|
|
if (saved != null && connected.matchesAccount(saved.identity)) {
|
|
|
|
|
connected.restoreRemembered(saved); applyChoices(saved)
|
|
|
|
|
}
|
|
|
|
|
session = connected; sample = connected.sample
|
|
|
|
|
saveTicket = Workspaces.current(appContext).connection.ticket()
|
|
|
|
|
}
|
|
|
|
|
catch (cancelled: CancellationException) { throw cancelled }
|
|
|
|
|
catch (failure: EmbyCheckException) { message = failure.message }
|
|
|
|
|
catch (_: Exception) { message = "Signing in couldn't finish. Try again later." }
|
|
|
|
|
finally { busy = false }
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
OrbitButton(if (busy) "Signing in…" else if (waiting) "Wait before trying again" else "Sign in",
|
|
|
|
|
Modifier.fillMaxWidth().focusRequester(submitFocus), enabled = ready, onClick = onSignIn)
|
|
|
|
|
message?.let { Text(it, color = Yellow, fontSize = 14.sp, maxLines = 4) }
|
|
|
|
|
if (restoring) Text("Restoring your saved connection…", color = Yellow, fontSize = 14.sp)
|
|
|
|
|
if (pendingSaved?.identity?.token != null)
|
|
|
|
|
OrbitButton("Try the saved connection again", Modifier.fillMaxWidth(), enabled = !restoring && !busy, onClick = { restoreAttempt++ })
|
|
|
|
|
if (pendingSaved?.identity?.token != null)
|
|
|
|
|
OrbitButton("Forget the saved sign-in", Modifier.fillMaxWidth(), enabled = !restoring && !busy, onClick = { closeSession(signOut = true) })
|
|
|
|
|
if (hasSaved)
|
|
|
|
|
OrbitButton("Try the saved connection again", Modifier.fillMaxWidth(), enabled = !restoring && !busy, onClick = onRetry)
|
|
|
|
|
if (hasSaved)
|
|
|
|
|
OrbitButton("Forget the saved sign-in", Modifier.fillMaxWidth(), enabled = !restoring && !busy, onClick = onForget)
|
|
|
|
|
Text("Your password is never saved. This box only keeps a sign-in token, in encrypted storage.", color = Muted, fontSize = 12.sp)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|