fix(favorites): не терять избранное после перезапуска приложения
refreshStations пересоздавал каталог с isFavorite=false, а insertAll (OnConflictStrategy.REPLACE) затирал строки — отметки «избранное» пропадали при каждом старте. Перед вставкой считываем текущие id избранного (getFavoriteIdsOnce) и проставляем их новым записям. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -37,4 +37,9 @@ interface StationDao {
|
|||||||
|
|
||||||
@Query("SELECT id FROM stations WHERE isFavorite = 1")
|
@Query("SELECT id FROM stations WHERE isFavorite = 1")
|
||||||
fun getFavoriteIds(): Flow<List<Int>>
|
fun getFavoriteIds(): Flow<List<Int>>
|
||||||
|
|
||||||
|
// Разовое чтение id избранного — чтобы при пересоздании каталога (refreshStations)
|
||||||
|
// не потерять отметки «избранное» (insertAll = REPLACE затирает строки).
|
||||||
|
@Query("SELECT id FROM stations WHERE isFavorite = 1")
|
||||||
|
suspend fun getFavoriteIdsOnce(): List<Int>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,8 +72,10 @@ class StationRepositoryImpl @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Save to DB
|
// 4. Save to DB. Сохраняем текущие отметки «избранное», иначе REPLACE
|
||||||
android.util.Log.d("StationRepo", "Saving ${merged.size} merged stations to DB")
|
// в insertAll затрёт их при каждом пересоздании каталога (на старте).
|
||||||
|
val favoriteIds = db.stationDao().getFavoriteIdsOnce().toSet()
|
||||||
|
android.util.Log.d("StationRepo", "Saving ${merged.size} merged stations to DB (избранных сохранено: ${favoriteIds.size})")
|
||||||
val entities = merged.mapIndexed { index, station ->
|
val entities = merged.mapIndexed { index, station ->
|
||||||
StationEntity(
|
StationEntity(
|
||||||
id = station.id,
|
id = station.id,
|
||||||
@@ -85,7 +87,7 @@ class StationRepositoryImpl @Inject constructor(
|
|||||||
tags = station.tags.joinToString(","),
|
tags = station.tags.joinToString(","),
|
||||||
sortOrder = index,
|
sortOrder = index,
|
||||||
source = station.source,
|
source = station.source,
|
||||||
isFavorite = false,
|
isFavorite = station.id in favoriteIds,
|
||||||
qualities = encodeQualities(station.qualities)
|
qualities = encodeQualities(station.qualities)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user