Fix local favorites scroll state loss

This commit is contained in:
FooIbar committed 2023-11-06 16:18:25 +08:00
1 parent 1238edfe2b
commit c4be269fb7
5 files changed
+19 -9

No files matched your search

+10 -5
View File
@@ -38,8 +38,13 @@ import splitties.arch.room.roomDb
object EhDB {
private val db = roomDb<EhDatabase>("eh.db")
private suspend fun putGalleryInfo(galleryInfo: BaseGalleryInfo) {
db.galleryDao().upsert(galleryInfo)
private suspend fun putGalleryInfo(galleryInfo: BaseGalleryInfo, update: Boolean = false) {
val dao = db.galleryDao()
if (update) {
dao.upsert(galleryInfo)
} else {
dao.insertOrIgnore(galleryInfo)
}
}
private suspend fun deleteGalleryInfo(galleryInfo: BaseGalleryInfo) {
@@ -190,12 +195,12 @@ object EhDB {
fun searchLocalFav(keyword: String) = db.localFavoritesDao().joinListLazy("%$keyword%")
suspend fun putHistoryInfo(galleryInfo: BaseGalleryInfo) {
putGalleryInfo(galleryInfo)
suspend fun putHistoryInfo(galleryInfo: BaseGalleryInfo, updateGalleryInfo: Boolean = false) {
putGalleryInfo(galleryInfo, updateGalleryInfo)
db.historyDao().upsert(HistoryInfo(galleryInfo.gid))
}
suspend fun modifyHistoryInfoFavslotNonRefresh(gid: Long, slot: Int) {
suspend fun updateFavoriteSlot(gid: Long, slot: Int) {
val dao = db.galleryDao()
dao.load(gid)?.let {
it.favoriteSlot = slot
@@ -17,6 +17,9 @@ interface GalleryDao {
@Query("SELECT * FROM GALLERIES")
suspend fun list(): List<BaseGalleryInfo>
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insertOrIgnore(galleryInfo: BaseGalleryInfo)
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insertOrIgnore(galleryInfoList: List<BaseGalleryInfo>)
@@ -141,7 +141,7 @@ class VMStorage : ViewModel() {
EhDB.searchLocalFav(keyword)
}
}.flow.cachedIn(viewModelScope)
fun dataflow() = if (urlBuilder.favCat == -2) localFavDataFlow else cloudDataFlow
fun dataflow() = if (urlBuilder.favCat == FavListUrlBuilder.FAV_CAT_LOCAL) localFavDataFlow else cloudDataFlow
val localFavCount = EhDB.localFavCount
}
@@ -222,7 +222,6 @@ class GalleryDetailScene : BaseScene() {
mAction = action
if (ACTION_GALLERY_INFO == action) {
composeBindingGI = args.getParcelableCompat(KEY_GALLERY_INFO)
composeBindingGI?.let { lifecycleScope.launchIO { EhDB.putHistoryInfo(it) } }
} else if (ACTION_GID_TOKEN == action) {
mGid = args.getLong(KEY_GID)
mToken = args.getString(KEY_TOKEN)
@@ -867,7 +866,10 @@ class GalleryDetailScene : BaseScene() {
EhEngine.getGalleryDetail(url)
}.onSuccess { galleryDetail ->
galleryDetailCache.put(galleryDetail.gid, galleryDetail)
EhDB.putHistoryInfo(galleryDetail.galleryInfo)
// Don't update gallery info in database if previous destination is favorites,
// since it will invalidate local favorites PagingSource and lose scroll state
val previousDestinationId = findNavController().previousBackStackEntry?.destination?.id
EhDB.putHistoryInfo(galleryDetail.galleryInfo, previousDestinationId != R.id.nav_favourite)
if (Settings.preloadThumbAggressively) {
lifecycleScope.launchIO {
galleryDetail.previewList.forEach {
@@ -39,7 +39,7 @@ object FavouriteStatusRouter {
private val _globalFlow = MutableSharedFlow<Pair<Long, Int>>(extraBufferCapacity = 1).apply {
listenerScope.launch {
collect { (gid, slot) ->
EhDB.modifyHistoryInfoFavslotNonRefresh(gid, slot)
EhDB.updateFavoriteSlot(gid, slot)
}
}
}