ref: update trackedShowsByKey to use immutable map

This commit is contained in:
tapframe 2026-04-23 23:33:32 +05:30
parent f4b1e9ba73
commit 9b1fbb29f1

View file

@ -24,6 +24,7 @@ import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.Semaphore import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.sync.withPermit import kotlinx.coroutines.sync.withPermit
import kotlin.concurrent.Volatile
import kotlinx.coroutines.withTimeoutOrNull import kotlinx.coroutines.withTimeoutOrNull
import kotlinx.serialization.decodeFromString import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString import kotlinx.serialization.encodeToString
@ -44,8 +45,10 @@ object EpisodeReleaseNotificationsRepository {
private val _uiState = MutableStateFlow(EpisodeReleaseNotificationsUiState()) private val _uiState = MutableStateFlow(EpisodeReleaseNotificationsUiState())
val uiState: StateFlow<EpisodeReleaseNotificationsUiState> = _uiState.asStateFlow() val uiState: StateFlow<EpisodeReleaseNotificationsUiState> = _uiState.asStateFlow()
@Volatile
private var hasLoaded = false private var hasLoaded = false
private var trackedShowsByKey: MutableMap<String, TrackedFollowedShow> = mutableMapOf() @Volatile
private var trackedShowsByKey: Map<String, TrackedFollowedShow> = emptyMap()
init { init {
scope.launch { scope.launch {
@ -81,7 +84,7 @@ object EpisodeReleaseNotificationsRepository {
fun clearLocalState() { fun clearLocalState() {
hasLoaded = false hasLoaded = false
trackedShowsByKey.clear() trackedShowsByKey = emptyMap()
_uiState.value = EpisodeReleaseNotificationsUiState() _uiState.value = EpisodeReleaseNotificationsUiState()
scope.launch { scope.launch {
runCatching { EpisodeReleaseNotificationPlatform.clearScheduledEpisodeReleaseNotifications() } runCatching { EpisodeReleaseNotificationPlatform.clearScheduledEpisodeReleaseNotifications() }
@ -239,7 +242,6 @@ object EpisodeReleaseNotificationsRepository {
private fun loadFromDisk() { private fun loadFromDisk() {
hasLoaded = true hasLoaded = true
trackedShowsByKey.clear()
val payload = EpisodeReleaseNotificationsStorage.loadPayload().orEmpty().trim() val payload = EpisodeReleaseNotificationsStorage.loadPayload().orEmpty().trim()
val stored = payload.takeIf { it.isNotEmpty() } val stored = payload.takeIf { it.isNotEmpty() }
@ -251,11 +253,11 @@ object EpisodeReleaseNotificationsRepository {
}.getOrNull() }.getOrNull()
} }
stored?.followedShows trackedShowsByKey = buildMap {
.orEmpty() stored?.followedShows.orEmpty().forEach { trackedShow ->
.forEach { trackedShow -> put(buildTrackedShowKey(trackedShow.contentType, trackedShow.contentId), trackedShow)
trackedShowsByKey[buildTrackedShowKey(trackedShow.contentType, trackedShow.contentId)] = trackedShow
} }
}
_uiState.value = EpisodeReleaseNotificationsUiState( _uiState.value = EpisodeReleaseNotificationsUiState(
isEnabled = stored?.enabled ?: false, isEnabled = stored?.enabled ?: false,
@ -318,7 +320,7 @@ object EpisodeReleaseNotificationsRepository {
val changed = nextTrackedShows != trackedShowsByKey val changed = nextTrackedShows != trackedShowsByKey
if (changed) { if (changed) {
trackedShowsByKey = nextTrackedShows.toMutableMap() trackedShowsByKey = nextTrackedShows.toMap()
} }
updateTestTargetState() updateTestTargetState()
return changed return changed