Skip to content

Commit c8435e2

Browse files
Surjit Kumar SahooSurjit Kumar Sahoo
authored andcommitted
feat: Introduce host preference flow for dynamic server availability checks
1 parent f72609b commit c8435e2

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

.agent/rules/project-context.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ trigger: always_on
66

77
## About Project
88

9-
This is an offline first Android client app for the [audiobookshelf](https://github.com/advplyr/audiobookshelf) server.
9+
This is an offline-first Android client app for the [audiobookshelf](https://github.com/advplyr/audiobookshelf) server.
1010

1111
## Feature Requirements (Offline first behavior)
1212

1313
- As the app is an offline-first app, assume that the server is not always reachable.
14-
- Playback progress should be saved in local database first **immediately**.
15-
- The app should watch the network changes, such as connecting to a new wifi network, or lan network, or disconnecting from a network and connecting to a new network, disconnecting from wifi and connecting to celular network etc, and try to ping or reachout the audiobook server to check whether the server is reachable.
16-
- If the server is reachable, the app should sync the local progress to the server and pull the latest progress updates from the server to the local database, it should merge the updates from both.
17-
- If the server is reachable, and some chapters of any audiobook is download, i.e. available offline, then offline track should be given priority for playback.
18-
- If the offline track is deleted / cleared while the book is being played, the player should attempt to fallback to the online URL if the server is reachable, otherwise pause playback and persist the last playback state (track ID, playback position/timestamp, current chapter/index, and playback status) plus a flag indicating offline content was removed; ensure the rule notes that these persisted fields are used to resume or report playback state and are written atomically to the player state store.
14+
- Playback progress should be saved in the local database first **immediately**.
15+
- The app should watch the network changes, such as connecting to a new wifi network, or Ethernet LAN, or disconnecting from a network and connecting to a new network, disconnecting from wifi and connecting to celular network, etc., and try to ping or reach out to the audiobook server to check whether the server is reachable.
16+
- If the server is reachable, the app should sync the local progress to the server and pull the latest progress updates from the server to the local database; it should merge the updates from both.
17+
- If the server is reachable, and some chapters of any audiobook are downloaded, i.e., available offline, then the offline track should be given priority for playback.
18+
- If the offline track is deleted/cleared while the book is being played, the player should attempt to fallback to the online URL if the server is reachable, otherwise pause playback and persist the last playback state (track ID, playback position/timestamp, current chapter/index, and playback status) plus a flag indicating offline content was removed; ensure the rule notes that these persisted fields are used to resume or report playback state and are written atomically to the player state store.
1919
- The app must be fully functional for downloaded content when offline.
2020

2121
## Ensure Stability

.vscode/tasks.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"type": "gradle",
6+
"id": "${workspaceFolder}app:installDebugapp",
7+
"script": "app:installDebug",
8+
"description": "Installs the Debug build.",
9+
"group": "install",
10+
"project": "app",
11+
"buildFile": "${workspaceFolder}/app/build.gradle.kts",
12+
"rootProject": "Lissen",
13+
"projectFolder": "${workspaceFolder}",
14+
"workspaceFolder": "${workspaceFolder}",
15+
"args": "",
16+
"javaDebug": false,
17+
"problemMatcher": ["$gradle"],
18+
"label": "gradle: app:installDebug"
19+
}
20+
]
21+
}

app/src/main/kotlin/org/grakovne/lissen/common/NetworkService.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ class NetworkService
7474

7575
connectivityManager.registerNetworkCallback(networkRequest, networkCallback)
7676
checkServerAvailability()
77+
78+
scope.launch {
79+
preferences.hostFlow.collect {
80+
checkServerAvailability()
81+
}
82+
}
7783
}
7884

7985
private var checkJob: Job? = null

app/src/main/kotlin/org/grakovne/lissen/persistence/preferences/LissenSharedPreferences.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,19 @@ class LissenSharedPreferences
579579
awaitClose { sharedPreferences.unregisterOnSharedPreferenceChangeListener(listener) }
580580
}.distinctUntilChanged()
581581

582+
val hostFlow: Flow<String?> =
583+
callbackFlow {
584+
val listener =
585+
SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
586+
if (key == KEY_HOST) {
587+
trySend(getHost())
588+
}
589+
}
590+
sharedPreferences.registerOnSharedPreferenceChangeListener(listener)
591+
trySend(getHost())
592+
awaitClose { sharedPreferences.unregisterOnSharedPreferenceChangeListener(listener) }
593+
}.distinctUntilChanged()
594+
582595
companion object {
583596
private const val KEY_ALIAS = "secure_key_alias"
584597
private const val KEY_HOST = "host"

0 commit comments

Comments
 (0)