Skip to content

Commit

Permalink
Fix read count
Browse files Browse the repository at this point in the history
  • Loading branch information
prof18 committed Sep 9, 2023
1 parent cd76651 commit 74a58c1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 23 deletions.
11 changes: 7 additions & 4 deletions iosApp/Source/App/FeedFlowApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ interface FeedRetrieverRepository {

fun getFeeds()

@NativeCoroutinesIgnore
suspend fun updateReadStatus(
lastUpdateIndex: Int,
lastVisibleIndex: Int,
)

@NativeCoroutinesIgnore
suspend fun updateReadStatus(itemsToUpdates: List<FeedItemId>)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,32 @@ internal class FeedRetrieverRepositoryImpl(
}
}

override suspend fun updateReadStatus(
lastUpdateIndex: Int,
lastVisibleIndex: Int,
) {
val urlToUpdates = mutableListOf<FeedItemId>()

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<FeedItemId>) =
databaseHelper.updateReadStatus(itemsToUpdates)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,13 @@ class HomeViewModel(
if (loadingState.value.isLoading()) {
return
}
val urlToUpdates = mutableListOf<FeedItemId>()

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
}
}

Expand Down

0 comments on commit 74a58c1

Please sign in to comment.