Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ A detailed inventory of what the Android **phone** and **TV** clients do today.

File pointers are repository-relative.

> **Important exposure note:** Requests is live on both Android surfaces, gated by the server's `requests_enabled` flag (`/api/v2/requests/status`), and reached from the profile menu and search on phone and from a top-bar tab and search on TV — matching the Apple clients. Request administration (approve, decline, retry, limits, auto-approve) stays in web admin. The admin screens (users/sessions/logs/scans) remain inaccessible. Watch Party is experimental, behind Settings → Experimental → Watch Party.
> **Important exposure note:** Requests is live on both Android surfaces, gated by the server's `requests_enabled` flag (`/api/v2/requests/status`), and reached from the profile menu and search on phone and TV — matching the Apple clients. Request administration (approve, decline, retry, limits, auto-approve) stays in web admin. The admin screens (users/sessions/logs/scans) remain inaccessible. Watch Party is experimental, behind Settings → Experimental → Watch Party.

---

Expand Down Expand Up @@ -89,7 +89,7 @@ Shown only while Settings → Experimental → Watch Party is on (default on in
| Release calendar | ✅ | ✅ | Top-level mobile tab and TV top-menu tab |
| Live home refresh (events websocket) | ✅ | ✅ | `HomeRealtimeCoordinator`: user_state/catalog channels, 2s debounce; TV also refreshes on resume |
| System "Watch Next" row integration | ➖ | ✅ | `WatchNextRepository` (tvprovider) |
| Requests | ✅ | ✅ | Server-gated by `requests_enabled`; phone: profile menu + search; TV: top-bar tab + search; "Notify me when available" on titles someone else requested (when the server reports `follow_supported`); request administration stays in web admin |
| Requests | ✅ | ✅ | Server-gated by `requests_enabled`, re-checked when the app returns to the foreground and when the server refuses a requests call as off (open Requests screens close once it answers off, never on a failed check); phone: profile menu + search; TV: profile menu + search; "Notify me when available" on titles someone else requested (when the server reports `follow_supported`); request administration stays in web admin |
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

## Reading (ebooks)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Built as a Kotlin Multiplatform project: one shared business-logic core, two Jet

> **Status:** WIP (`v0.3.x`). Silo Android is pre-1.0 and some areas remain under active development (see [Roadmap](#roadmap)).
>
> **Current exposure note:** Requests is live on both Android surfaces, gated by the server's `requests_enabled` flag (`/api/v2/requests/status`), and reached from the profile menu and search on phone and from a top-bar tab and search on TV — matching the Apple clients. Request administration (approve, decline, retry, limits, auto-approve) stays in web admin. Other admin surfaces and session management are not exposed in the Android clients. Watch Party is experimental, behind Settings → Experimental → Watch Party (on in debug builds, off in release builds).
> **Current exposure note:** Requests is live on both Android surfaces, gated by the server's `requests_enabled` flag (`/api/v2/requests/status`), and reached from the profile menu and search on phone and TV — matching the Apple clients. Request administration (approve, decline, retry, limits, auto-approve) stays in web admin. Other admin surfaces and session management are not exposed in the Android clients. Watch Party is experimental, behind Settings → Experimental → Watch Party (on in debug builds, off in release builds).

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,38 @@ fun AppNavigation(
}
}

// The server can turn Requests off while a Requests screen is open (seen
// when the app returns to the foreground, or when it refuses a requests
// call). Leave the Requests screens for whatever opened them, as the TV
// shell does. A screen opened from Requests (an item detail) stays; Back
// into Requests then closes it here.
val requestsFeatureStore: org.siloserver.silo.model.feature.RequestsFeatureStore = koinInject()
LaunchedEffect(navController, requestsFeatureStore) {
kotlinx.coroutines.flow.combine(
requestsFeatureStore.isKnownUnavailable,
navController.currentBackStackEntryFlow,
) { _, entry -> entry }.collect { entry ->
val leave = shouldLeaveRequests(
currentRoute = entry.destination.route,
requestsKnownUnavailable = requestsFeatureStore.isKnownUnavailable.value,
)
if (!leave || navController.currentBackStackEntry != entry) return@collect
// Pop the run of Requests screens on top one at a time; public
// NavController calls only (currentBackStack is restricted API).
while (isRequestsRoute(navController.currentBackStackEntry?.destination?.route) &&
navController.previousBackStackEntry != null
) {
if (!navController.popBackStack()) break
}
if (isRequestsRoute(navController.currentBackStackEntry?.destination?.route)) {
navController.navigate(Tab.Home.route) {
popUpTo(0) { inclusive = true }
launchSingleTop = true
}
}
}
}

// Android can restore Silo's previous back stack over the start route
// MainActivity chose: after its process was killed in the background.
// When Profile Selection has already cleared the profile at launch, that
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.siloserver.silo.android.ui.navigation

/** Destination patterns of the Requests screens: hub, a Discover row's See all, My Requests, detail. */
internal val RequestsRoutes: Set<String> = setOf(
Route.Requests.route,
Route.RequestDiscoverRow.ROUTE,
Route.MyRequests.route,
Route.RequestDetail.ROUTE,
)

internal fun isRequestsRoute(route: String?): Boolean = route in RequestsRoutes

/**
* Whether a Requests screen on top has to close because the server turned
* Requests off. Only an answer counts
* ([org.siloserver.silo.model.feature.RequestsFeatureStore.isKnownUnavailable]):
* the store reads "disabled" by default before its first probe, again after
* every reset, and keeps its previous answer when a probe fails, so a restored
* Requests screen never closes while a probe runs or the server is unreachable.
* The TV shell applies the same rule to its Requests screens.
*/
internal fun shouldLeaveRequests(
currentRoute: String?,
requestsKnownUnavailable: Boolean,
): Boolean = isRequestsRoute(currentRoute) && requestsKnownUnavailable
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.siloserver.silo.android.ui.navigation

import kotlin.test.Test
import kotlin.test.assertEquals

class RequestsRouteVisibilityTest {

@Test
fun onlyRequestsScreensCloseAndOnlyOnceTheServerSaysRequestsAreOff() {
val requestsScreens = listOf("requests", Route.RequestDiscoverRow.ROUTE, "requests/mine", Route.RequestDetail.ROUTE)
for (route in requestsScreens) {
assertEquals(true, shouldLeaveRequests(route, requestsKnownUnavailable = true), route)
// Before the first answer, after a reset, or after a failed probe.
assertEquals(false, shouldLeaveRequests(route, requestsKnownUnavailable = false), route)
}
for (route in listOf(Tab.Home.route, null)) {
assertEquals(false, shouldLeaveRequests(route, requestsKnownUnavailable = true), route.toString())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ fun TvMainShell(
val serverRegistry: ServerRegistry = koinInject()
val reachabilityState by reachabilityMonitor.state.collectAsState()
val requestsEnabled by requestsFeatureStore.isEnabled.collectAsState()
val requestsResolved by requestsFeatureStore.isResolved.collectAsState()
val requestsKnownUnavailable by requestsFeatureStore.isKnownUnavailable.collectAsState()
val activeServerEntry by serverRegistry.activeEntry.collectAsState()
val tvLibraryScopeStore: TvLibraryScopeStore = koinInject()
// One shell-scoped Home owner feeds both the Home screen and General's
Expand Down Expand Up @@ -1042,21 +1042,36 @@ fun TvMainShell(
}
}

LaunchedEffect(currentRoute, visibleRoots, librariesLoaded, showAudiobooksTabResolved, requestsResolved, requestsEnabled) {
LaunchedEffect(currentRoute, visibleRoots, librariesLoaded, showAudiobooksTabResolved, requestsKnownUnavailable) {
// The Requests page and request details close once the server answers
// that requests are off, back to whatever opened them, as Back would:
// a detail opened from Search lands on Search, and Requests opened
// from a tab lands on that tab. A plain pop saves nothing, so a later
// tab switch can't restore a Requests screen (navigateToRoute's
// saveState pop would, and the tab would bounce Home every time).
// Stacked details ("More like this") share one route pattern, so this
// effect would not run again between them: pop the whole run at once.
// A restored Requests route (or a return from a title opened there)
// stays while the status read runs, or if it fails.
fun isRequestsRoute(route: String?) =
route == TvMainRoute.Requests.route || route == TvMainRoute.RequestDetail.ROUTE
if (isRequestsRoute(currentRoute)) {
if (requestsKnownUnavailable) {
while (isRequestsRoute(nestedNav.currentDestination?.route) &&
nestedNav.previousBackStackEntry != null
) {
if (!nestedNav.popBackStack()) break
}
}
return@LaunchedEffect
}
// Wait until libraries have actually loaded — before that `visibleRoots`
// is just Home + Calendar, and a restored/deep-linked `main/movies` route
// would be wrongly ejected even though that type exists. Likewise wait for
// the async Audiobooks-tab read to resolve: until it lands `visibleRoots`
// omits Audiobooks, so a restored `main/audiobooks` route would be ejected
// to Home before the DataStore read completes.
if (!librariesLoaded || !showAudiobooksTabResolved) return@LaunchedEffect
// Requests closes once the server's requests status reads as off. A
// restored Requests route (or a return from a title opened there) must
// not be ejected while that read runs.
if (currentRoute == TvMainRoute.Requests.route) {
if (requestsResolved && !requestsEnabled) navigateToRoute(firstTvRoute())
return@LaunchedEffect
}
// Only media-root tabs are eligible for the "tab no longer visible"
// redirect. Non-tab routes (Settings, Favorites, Search, …) map
// to null and must be left alone — otherwise navigating to Settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ val repositoryModule = module {
scope = CoroutineScope(SupervisorJob() + Dispatchers.Default),
)
}
single { org.siloserver.silo.repository.ForegroundAccessCheck(get(), get()) }
single { org.siloserver.silo.repository.ForegroundAccessCheck(get(), get(), get(), get()) }
single { CollectionRepository(get()) }
single {
SectionRepository(
Expand All @@ -128,7 +128,13 @@ val repositoryModule = module {
),
)
}
single { RequestsFeatureStore(get()) }
single {
RequestsFeatureStore(
repository = get(),
// Main, like the repository it listens to and the shells that probe it.
scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate),
)
}
single { ActiveProfileStore(get()) }
single { org.siloserver.silo.repository.MetadataAiRepository(get()) }
single { org.siloserver.silo.model.feature.MetadataAiFeatureStore(get()) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package org.siloserver.silo.model.feature

import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import org.siloserver.silo.network.ApiResult
import org.siloserver.silo.repository.RequestsRepository

Expand All @@ -17,11 +22,16 @@ import org.siloserver.silo.repository.RequestsRepository
* server or profile switch. The feature counts only when the server
* says this profile is `allowed` and the state is `available`.
*
* With a [scope], a requests call the server refuses as requests-off
* ([RequestsRepository.capabilityRefusals]) probes again, so an app that stays
* in the foreground still sees the change.
*
* Request administration (approve, decline, retry, limits) stays in web admin:
* the native clients never probe or expose it.
*/
class RequestsFeatureStore(
private val repository: RequestsRepository,
scope: CoroutineScope? = null,
) {
private val _isEnabled = MutableStateFlow(false)
val isEnabled: StateFlow<Boolean> = _isEnabled.asStateFlow()
Expand All @@ -30,6 +40,14 @@ class RequestsFeatureStore(
/** A probe has answered (or failed) since the last [reset]; until then [isEnabled] is only a default. */
val isResolved: StateFlow<Boolean> = _isResolved.asStateFlow()

private val _isKnownUnavailable = MutableStateFlow(false)
/**
* The server answered, since the last [reset], that requests are not
* available to this profile. Open Requests screens close on this, never on
* the default before the first answer or on a failed probe.
*/
val isKnownUnavailable: StateFlow<Boolean> = _isKnownUnavailable.asStateFlow()

private val _followSupported = MutableStateFlow(false)
/** The server lets a viewer follow a title someone else requested (`follow_supported`). */
val followSupported: StateFlow<Boolean> = _followSupported.asStateFlow()
Expand All @@ -43,30 +61,70 @@ class RequestsFeatureStore(
@kotlin.concurrent.Volatile
private var generation: Int = 0

// Probes are numbered as they start, and an answer counts only if no newer
// probe has answered yet: an older "available" can't undo a newer
// "disabled", and a newer probe that fails doesn't discard an older answer.
private val probeOrder = Mutex()
private var probesStarted = 0
private var newestAnswer = 0

init {
scope?.launch {
repository.capabilityRefusals.collect {
// Probe even while the shell's own probe is out: it may have
// read the status before requests were turned off, and the
// newer probe's answer wins.
try {
refresh()
} catch (e: CancellationException) {
throw e
} catch (_: Exception) {
// A failed probe keeps the previous answer; the next refusal or foreground retries.
}
}
}
}

suspend fun refresh() {
val refreshGeneration = generation
val ordinal = probeOrder.withLock { ++probesStarted }
val status = repository.status()
val enabled = when (status) {
is ApiResult.Success -> status.data.isAvailable
// A transient failure shouldn't yank an already-visible entry point.
is ApiResult.Error,
is ApiResult.NetworkError,
-> _isEnabled.value
probeOrder.withLock {
if (refreshGeneration != generation) return
if (status !is ApiResult.Success) {
// A transient failure shouldn't yank an already-visible entry point.
_isResolved.value = true
return
}
if (ordinal <= newestAnswer) return
newestAnswer = ordinal
val enabled = status.data.isAvailable
// Follow and remove support first, so nothing that watches isEnabled reads a stale value of them.
_followSupported.value = enabled && status.data.followSupported
_removeFailedSupported.value = enabled && status.data.removeFailedSupported
_isEnabled.value = enabled
_isKnownUnavailable.value = !enabled
_isResolved.value = true
}
// Like [isEnabled], a failed probe keeps the previous answer.
val follows = enabled && ((status as? ApiResult.Success)?.data?.followSupported ?: _followSupported.value)
val removes = enabled && ((status as? ApiResult.Success)?.data?.removeFailedSupported ?: _removeFailedSupported.value)
if (refreshGeneration != generation) return
_followSupported.value = follows
_removeFailedSupported.value = removes
_isEnabled.value = enabled
_isResolved.value = true
}

/**
* Re-probe when the app returns to the foreground, so an admin turning
* requests off (or on) while the app was in the background shows up
* without a restart, as on Apple. The caller checks that someone is signed
* in. It doesn't wait for the shell's own probe: a phone restored onto a
* Requests screen never shows the tab screen that starts that probe, and a
* shell probe still out may have read the status before requests were
* turned off. The newer answer wins, and an answer from before a server or
* profile switch is discarded.
*/
suspend fun refreshAfterReturnToForeground() = refresh()

fun reset() {
generation += 1
repository.reset()
_isEnabled.value = false
_isKnownUnavailable.value = false
_followSupported.value = false
_removeFailedSupported.value = false
_isResolved.value = false
Expand Down
Loading
Loading