feat: Update WatchedStorage to support profile-specific payloads; modify load and save methods to accept profileId

This commit is contained in:
tapframe 2026-03-29 13:18:27 +05:30
parent e569301fe0
commit c1337e8b70
5 changed files with 49 additions and 17 deletions

View file

@ -5,7 +5,7 @@ import android.content.SharedPreferences
actual object WatchedStorage {
private const val preferencesName = "nuvio_watched"
private const val payloadKey = "watched_payload"
private fun payloadKey(profileId: Int) = "watched_payload_$profileId"
private var preferences: SharedPreferences? = null
@ -13,13 +13,13 @@ actual object WatchedStorage {
preferences = context.getSharedPreferences(preferencesName, Context.MODE_PRIVATE)
}
actual fun loadPayload(): String? =
preferences?.getString(payloadKey, null)
actual fun loadPayload(profileId: Int): String? =
preferences?.getString(payloadKey(profileId), null)
actual fun savePayload(payload: String) {
actual fun savePayload(profileId: Int, payload: String) {
preferences
?.edit()
?.putString(payloadKey, payload)
?.putString(payloadKey(profileId), payload)
?.apply()
}
}

View file

@ -4,6 +4,7 @@ import co.touchlab.kermit.Logger
import com.nuvio.app.core.auth.AuthRepository
import com.nuvio.app.core.auth.AuthState
import com.nuvio.app.core.network.SupabaseProvider
import com.nuvio.app.features.watched.WatchedRepository
import io.github.jan.supabase.postgrest.postgrest
import io.github.jan.supabase.postgrest.rpc
import kotlinx.coroutines.CoroutineScope
@ -106,6 +107,7 @@ object ProfileRepository {
activeProfile = _state.value.profiles.find { it.profileIndex == profileIndex },
)
persist()
WatchedRepository.onProfileChanged(profileIndex)
}
suspend fun pushProfiles(profiles: List<ProfilePushPayload>) {

View file

@ -44,6 +44,8 @@ private data class WatchedDeleteKey(
)
object WatchedRepository {
private const val watchedItemsPageSize = 900
private val syncScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
private val log = Logger.withTag("WatchedRepository")
private val json = Json {
@ -55,13 +57,25 @@ object WatchedRepository {
val uiState: StateFlow<WatchedUiState> = _uiState.asStateFlow()
private var hasLoaded = false
private var currentProfileId: Int = 1
private var itemsByKey: MutableMap<String, WatchedItem> = mutableMapOf()
fun ensureLoaded() {
if (hasLoaded) return
hasLoaded = true
loadFromDisk(ProfileRepository.activeProfileId)
}
val payload = WatchedStorage.loadPayload().orEmpty().trim()
fun onProfileChanged(profileId: Int) {
if (profileId == currentProfileId && hasLoaded) return
loadFromDisk(profileId)
}
private fun loadFromDisk(profileId: Int) {
currentProfileId = profileId
hasLoaded = true
itemsByKey.clear()
val payload = WatchedStorage.loadPayload(profileId).orEmpty().trim()
if (payload.isNotEmpty()) {
val items = runCatching {
json.decodeFromString<StoredWatchedPayload>(payload).items
@ -73,10 +87,25 @@ object WatchedRepository {
}
suspend fun pullFromServer(profileId: Int) {
currentProfileId = profileId
runCatching {
val params = buildJsonObject { put("p_profile_id", profileId) }
val result = SupabaseProvider.client.postgrest.rpc("sync_pull_watched_items", params)
val serverItems = result.decodeList<WatchedSyncItem>()
val serverItems = mutableListOf<WatchedSyncItem>()
var page = 1
while (true) {
val params = buildJsonObject {
put("p_profile_id", profileId)
put("p_page", page)
put("p_page_size", watchedItemsPageSize)
}
val result = SupabaseProvider.client.postgrest.rpc("sync_pull_watched_items", params)
val pageItems = result.decodeList<WatchedSyncItem>()
serverItems += pageItems
if (pageItems.size < watchedItemsPageSize) break
page += 1
}
itemsByKey = serverItems.map { syncItem ->
WatchedItem(
id = syncItem.contentId,
@ -176,6 +205,7 @@ object WatchedRepository {
private fun persist() {
WatchedStorage.savePayload(
currentProfileId,
json.encodeToString(
StoredWatchedPayload(
items = itemsByKey.values.sortedByDescending { it.markedAtEpochMs },

View file

@ -1,7 +1,7 @@
package com.nuvio.app.features.watched
expect object WatchedStorage {
fun loadPayload(): String?
fun savePayload(payload: String)
fun loadPayload(profileId: Int): String?
fun savePayload(profileId: Int, payload: String)
}

View file

@ -3,13 +3,13 @@ package com.nuvio.app.features.watched
import platform.Foundation.NSUserDefaults
actual object WatchedStorage {
private const val payloadKey = "watched_payload"
private fun payloadKey(profileId: Int) = "watched_payload_$profileId"
actual fun loadPayload(): String? =
NSUserDefaults.standardUserDefaults.stringForKey(payloadKey)
actual fun loadPayload(profileId: Int): String? =
NSUserDefaults.standardUserDefaults.stringForKey(payloadKey(profileId))
actual fun savePayload(payload: String) {
NSUserDefaults.standardUserDefaults.setObject(payload, forKey = payloadKey)
actual fun savePayload(profileId: Int, payload: String) {
NSUserDefaults.standardUserDefaults.setObject(payload, forKey = payloadKey(profileId))
}
}