From 74a58c128b01512ab17c1599eb0fad0ef83c37b7 Mon Sep 17 00:00:00 2001 From: Marco Gomiero Date: Sat, 9 Sep 2023 11:52:03 +0200 Subject: [PATCH] Fix read count --- iosApp/Source/App/FeedFlowApp.swift | 11 +++++--- .../feed/retriever/FeedRetrieverRepository.kt | 6 +++++ .../retriever/FeedRetrieverRepositoryImpl.kt | 26 +++++++++++++++++++ .../feedflow/presentation/HomeViewModel.kt | 24 ++++------------- 4 files changed, 44 insertions(+), 23 deletions(-) diff --git a/iosApp/Source/App/FeedFlowApp.swift b/iosApp/Source/App/FeedFlowApp.swift index d7d06d7b..b6a2f5f9 100644 --- a/iosApp/Source/App/FeedFlowApp.swift +++ b/iosApp/Source/App/FeedFlowApp.swift @@ -26,10 +26,13 @@ struct FeedFlowApp: App { } class AppDelegate: NSObject, UIApplicationDelegate { - func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool { - #if !DEBUG - FirebaseApp.configure() - #endif + func application( + _ application: UIApplication, + didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil + ) -> Bool { +#if !DEBUG + FirebaseApp.configure() +#endif return true } } diff --git a/shared/src/commonMain/kotlin/com/prof18/feedflow/domain/feed/retriever/FeedRetrieverRepository.kt b/shared/src/commonMain/kotlin/com/prof18/feedflow/domain/feed/retriever/FeedRetrieverRepository.kt index 78fa2519..7eb7c952 100644 --- a/shared/src/commonMain/kotlin/com/prof18/feedflow/domain/feed/retriever/FeedRetrieverRepository.kt +++ b/shared/src/commonMain/kotlin/com/prof18/feedflow/domain/feed/retriever/FeedRetrieverRepository.kt @@ -23,6 +23,12 @@ interface FeedRetrieverRepository { fun getFeeds() + @NativeCoroutinesIgnore + suspend fun updateReadStatus( + lastUpdateIndex: Int, + lastVisibleIndex: Int, + ) + @NativeCoroutinesIgnore suspend fun updateReadStatus(itemsToUpdates: List) diff --git a/shared/src/commonMain/kotlin/com/prof18/feedflow/domain/feed/retriever/FeedRetrieverRepositoryImpl.kt b/shared/src/commonMain/kotlin/com/prof18/feedflow/domain/feed/retriever/FeedRetrieverRepositoryImpl.kt index df8db4bc..872225a5 100644 --- a/shared/src/commonMain/kotlin/com/prof18/feedflow/domain/feed/retriever/FeedRetrieverRepositoryImpl.kt +++ b/shared/src/commonMain/kotlin/com/prof18/feedflow/domain/feed/retriever/FeedRetrieverRepositoryImpl.kt @@ -73,6 +73,32 @@ internal class FeedRetrieverRepositoryImpl( } } + override suspend fun updateReadStatus( + lastUpdateIndex: Int, + lastVisibleIndex: Int, + ) { + val urlToUpdates = mutableListOf() + + val items = feedState.value.toMutableList() + if (lastVisibleIndex <= lastUpdateIndex) { + return + } + for (index in lastUpdateIndex..lastVisibleIndex) { + items.getOrNull(index)?.let { item -> + if (!item.isRead) { + urlToUpdates.add( + FeedItemId( + id = item.id, + ), + ) + } + items[index] = item.copy(isRead = true) + } + } + mutableFeedState.update { items } + databaseHelper.updateReadStatus(urlToUpdates) + } + override suspend fun updateReadStatus(itemsToUpdates: List) = databaseHelper.updateReadStatus(itemsToUpdates) diff --git a/shared/src/commonMain/kotlin/com/prof18/feedflow/presentation/HomeViewModel.kt b/shared/src/commonMain/kotlin/com/prof18/feedflow/presentation/HomeViewModel.kt index a8db5111..ff7da62c 100644 --- a/shared/src/commonMain/kotlin/com/prof18/feedflow/presentation/HomeViewModel.kt +++ b/shared/src/commonMain/kotlin/com/prof18/feedflow/presentation/HomeViewModel.kt @@ -94,27 +94,13 @@ class HomeViewModel( if (loadingState.value.isLoading()) { return } - val urlToUpdates = mutableListOf() - val items = feedState.value.toMutableList() - if (lastVisibleIndex <= lastUpdateIndex) { - return - } - for (index in lastUpdateIndex..lastVisibleIndex) { - items.getOrNull(index)?.let { item -> - if (!item.isRead) { - urlToUpdates.add( - FeedItemId( - id = item.id, - ), - ) - } - } - } - - lastUpdateIndex = lastVisibleIndex scope.launch { - feedRetrieverRepository.updateReadStatus(urlToUpdates) + feedRetrieverRepository.updateReadStatus( + lastUpdateIndex = lastUpdateIndex, + lastVisibleIndex = lastVisibleIndex, + ) + lastUpdateIndex = lastVisibleIndex } }