From c4be269fb7298546e6fbd334bb1cd7bc770a5316 Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Sun, 5 Nov 2023 23:32:09 +0800 Subject: [PATCH] Fix local favorites scroll state loss --- app/src/main/kotlin/com/hippo/ehviewer/EhDB.kt | 15 ++++++++++----- .../kotlin/com/hippo/ehviewer/dao/GalleryDao.kt | 3 +++ .../com/hippo/ehviewer/ui/scene/FavoritesScene.kt | 2 +- .../hippo/ehviewer/ui/scene/GalleryDetailScene.kt | 6 ++++-- .../hippo/ehviewer/util/FavouriteStatusRouter.kt | 2 +- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/EhDB.kt b/app/src/main/kotlin/com/hippo/ehviewer/EhDB.kt index 6cd36ccac..404289f16 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/EhDB.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/EhDB.kt @@ -38,8 +38,13 @@ import splitties.arch.room.roomDb object EhDB { private val db = roomDb("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 diff --git a/app/src/main/kotlin/com/hippo/ehviewer/dao/GalleryDao.kt b/app/src/main/kotlin/com/hippo/ehviewer/dao/GalleryDao.kt index 000eb7477..ba1433beb 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/dao/GalleryDao.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/dao/GalleryDao.kt @@ -17,6 +17,9 @@ interface GalleryDao { @Query("SELECT * FROM GALLERIES") suspend fun list(): List + @Insert(onConflict = OnConflictStrategy.IGNORE) + suspend fun insertOrIgnore(galleryInfo: BaseGalleryInfo) + @Insert(onConflict = OnConflictStrategy.IGNORE) suspend fun insertOrIgnore(galleryInfoList: List) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/FavoritesScene.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/FavoritesScene.kt index 7a6c03f79..18c94d8f3 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/FavoritesScene.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/FavoritesScene.kt @@ -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 } diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt index 326ca4876..f17a1402b 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt @@ -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 { diff --git a/app/src/main/kotlin/com/hippo/ehviewer/util/FavouriteStatusRouter.kt b/app/src/main/kotlin/com/hippo/ehviewer/util/FavouriteStatusRouter.kt index 47bc3b94b..f65c54921 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/util/FavouriteStatusRouter.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/util/FavouriteStatusRouter.kt @@ -39,7 +39,7 @@ object FavouriteStatusRouter { private val _globalFlow = MutableSharedFlow>(extraBufferCapacity = 1).apply { listenerScope.launch { collect { (gid, slot) -> - EhDB.modifyHistoryInfoFavslotNonRefresh(gid, slot) + EhDB.updateFavoriteSlot(gid, slot) } } }