From a4ef2ef29c3ee6e2aaf3d021726f7da9ac5fb306 Mon Sep 17 00:00:00 2001 From: lollerfirst <43107113+lollerfirst@users.noreply.github.com> Date: Sun, 14 Sep 2025 16:38:39 +0200 Subject: [PATCH] fix: bookmark removal persistence (#424) --- .../android/geohash/GeohashBookmarksStore.kt | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/bitchat/android/geohash/GeohashBookmarksStore.kt b/app/src/main/java/com/bitchat/android/geohash/GeohashBookmarksStore.kt index bce90c7c..b498dd83 100644 --- a/app/src/main/java/com/bitchat/android/geohash/GeohashBookmarksStore.kt +++ b/app/src/main/java/com/bitchat/android/geohash/GeohashBookmarksStore.kt @@ -68,8 +68,9 @@ class GeohashBookmarksStore private constructor(private val context: Context) { val gh = normalize(geohash) if (gh.isEmpty() || membership.contains(gh)) return membership.add(gh) - _bookmarks.postValue(listOf(gh) + (_bookmarks.value ?: emptyList())) - persist() + val updated = listOf(gh) + (_bookmarks.value ?: emptyList()) + _bookmarks.postValue(updated) + persist(updated) // Resolve friendly name asynchronously resolveNameIfNeeded(gh) } @@ -78,14 +79,15 @@ class GeohashBookmarksStore private constructor(private val context: Context) { val gh = normalize(geohash) if (!membership.contains(gh)) return membership.remove(gh) - _bookmarks.postValue((_bookmarks.value ?: emptyList()).filterNot { it == gh }) + val updated = (_bookmarks.value ?: emptyList()).filterNot { it == gh } + _bookmarks.postValue(updated) // Remove stored name to avoid stale cache growth val names = _bookmarkNames.value?.toMutableMap() ?: mutableMapOf() if (names.remove(gh) != null) { _bookmarkNames.postValue(names) - persistNames() + persistNames(names) } - persist() + persist(updated) } // MARK: - Persistence @@ -210,7 +212,7 @@ class GeohashBookmarksStore private constructor(private val context: Context) { val current = _bookmarkNames.value?.toMutableMap() ?: mutableMapOf() current[gh] = name _bookmarkNames.postValue(current) - persistNames() + persistNames(current) } } catch (e: Exception) { Log.w(TAG, "Name resolution failed for #$gh: ${e.message}") @@ -230,4 +232,18 @@ class GeohashBookmarksStore private constructor(private val context: Context) { else -> address.subLocality ?: address.locality ?: address.adminArea ?: address.countryName } } + + private fun persist(list: List) { + try { + val json = gson.toJson(list) + prefs.edit().putString(STORE_KEY, json).apply() + } catch (_: Exception) {} + } + + private fun persistNames(map: Map) { + try { + val json = gson.toJson(map) + prefs.edit().putString(NAMES_STORE_KEY, json).apply() + } catch (_: Exception) {} + } }