From 6c3bf3a0fc98432c76d9c3aa09a2916988918c74 Mon Sep 17 00:00:00 2001 From: Marcin Czachurski Date: Fri, 20 Oct 2023 18:50:25 +0200 Subject: [PATCH] Fix information about amount of statuses --- .../Sources/EnvironmentKit/ApplicationState.swift | 2 +- README.md | 4 ++-- Vernissage/Services/HomeTimelineService.swift | 15 +++++++++++++++ Vernissage/Views/HomeTimelineView.swift | 13 +++++++++++-- Vernissage/Views/StatusesView.swift | 10 ++++++++-- 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/EnvironmentKit/Sources/EnvironmentKit/ApplicationState.swift b/EnvironmentKit/Sources/EnvironmentKit/ApplicationState.swift index a1ef1f2..ea2f101 100644 --- a/EnvironmentKit/Sources/EnvironmentKit/ApplicationState.swift +++ b/EnvironmentKit/Sources/EnvironmentKit/ApplicationState.swift @@ -37,7 +37,7 @@ import ClientKit /// Last status seen by the user. public var lastSeenStatusId: String? - + /// Amount of new statuses which are not displayed yet to the user. public var amountOfNewStatuses = 0 diff --git a/README.md b/README.md index 87b1c02..bfa88e3 100644 --- a/README.md +++ b/README.md @@ -47,8 +47,8 @@ Things that should be implemented in version 2.0: - [ ] Use auto generated resources (Color/Images) instead static extensions (how to do this in separete Swift Packages?) - [x] Move to xcstring (new Xcode transaction system) - - [ ] Move to new Observable macro (iOS 17) - - [ ] Migrate to SwiftData (iOS 17) + - [x] Move to new Observable macro (iOS 17) + - [x] Migrate to SwiftData (iOS 17) - [ ] Use ViewModels - [ ] Add tips (new TipKit framework in iOS 17) - [ ] Enable swiftlint (https://github.com/realm/SwiftLint/issues/5053) diff --git a/Vernissage/Services/HomeTimelineService.swift b/Vernissage/Services/HomeTimelineService.swift index 563e590..83aa3ec 100644 --- a/Vernissage/Services/HomeTimelineService.swift +++ b/Vernissage/Services/HomeTimelineService.swift @@ -106,6 +106,21 @@ public class HomeTimelineService { return visibleStatuses } + public func update(lastSeenStatusId: String?, lastLoadedStatusId: String?, applicationState: ApplicationState, modelContext: ModelContext) throws { + guard let accountId = applicationState.account?.id else { + return + } + + try AccountDataHandler.shared.update(lastSeenStatusId: lastSeenStatusId, + lastLoadedStatusId: lastLoadedStatusId, + accountId: accountId, + modelContext: modelContext) + + if (applicationState.lastSeenStatusId ?? "0") < (lastSeenStatusId ?? "0") { + applicationState.lastSeenStatusId = lastSeenStatusId + } + } + private func hasBeenAlreadyOnTimeline(accountId: String, status: Status, modelContext: ModelContext) -> Bool { return ViewedStatusHandler.shared.hasBeenAlreadyOnTimeline(accountId: accountId, status: status, modelContext: modelContext) } diff --git a/Vernissage/Views/HomeTimelineView.swift b/Vernissage/Views/HomeTimelineView.swift index adb7b4c..21c1458 100644 --- a/Vernissage/Views/HomeTimelineView.swift +++ b/Vernissage/Views/HomeTimelineView.swift @@ -194,7 +194,10 @@ struct HomeTimelineView: View { modelContext: modelContext) // Remeber first status returned by API in user context (when it's newer then remembered). - try AccountDataHandler.shared.update(lastSeenStatusId: nil, lastLoadedStatusId: statuses.first?.id, accountId: accountId, modelContext: modelContext) + try HomeTimelineService.shared.update(lastSeenStatusId: nil, + lastLoadedStatusId: statuses.first?.id, + applicationState: self.applicationState, + modelContext: modelContext) // Append statuses to viewed. try ViewedStatusHandler.shared.append(contentsOf: statuses, accountId: accountId, modelContext: modelContext) @@ -268,7 +271,10 @@ struct HomeTimelineView: View { modelContext: modelContext) // Remeber first status returned by API in user context (when it's newer then remembered). - try AccountDataHandler.shared.update(lastSeenStatusId: self.statusViewModels.first?.id, lastLoadedStatusId: statuses.first?.id, accountId: accountId, modelContext: modelContext) + try HomeTimelineService.shared.update(lastSeenStatusId: self.statusViewModels.first?.id, + lastLoadedStatusId: statuses.first?.id, + applicationState: self.applicationState, + modelContext: modelContext) // Append statuses to viewed. try ViewedStatusHandler.shared.append(contentsOf: statuses, accountId: accountId, modelContext: modelContext) @@ -281,6 +287,9 @@ struct HomeTimelineView: View { // Replace old collection with new one. self.statusViewModels = statusModels + + // Set that all statuses has been downloaded. + self.applicationState.amountOfNewStatuses = 0 } private func loadFromApi(maxId: String? = nil, sinceId: String? = nil, minId: String? = nil) async throws -> [Status] { diff --git a/Vernissage/Views/StatusesView.swift b/Vernissage/Views/StatusesView.swift index 25c9e3d..dd251e7 100644 --- a/Vernissage/Views/StatusesView.swift +++ b/Vernissage/Views/StatusesView.swift @@ -196,7 +196,10 @@ struct StatusesView: View { if self.listType == .home { // Remeber first status returned by API in user context (when it's newer then remembered). - try AccountDataHandler.shared.update(lastSeenStatusId: nil, lastLoadedStatusId: statuses.first?.id, accountId: accountId, modelContext: modelContext) + try HomeTimelineService.shared.update(lastSeenStatusId: nil, + lastLoadedStatusId: statuses.first?.id, + applicationState: self.applicationState, + modelContext: modelContext) // Append statuses to viewed. try ViewedStatusHandler.shared.append(contentsOf: statuses, accountId: accountId, modelContext: modelContext) @@ -271,7 +274,10 @@ struct StatusesView: View { if self.listType == .home { // Remeber first status returned by API in user context (when it's newer then remembered). - try AccountDataHandler.shared.update(lastSeenStatusId: self.statusViewModels.first?.id, lastLoadedStatusId: statuses.first?.id, accountId: accountId, modelContext: modelContext) + try HomeTimelineService.shared.update(lastSeenStatusId: self.statusViewModels.first?.id, + lastLoadedStatusId: statuses.first?.id, + applicationState: self.applicationState, + modelContext: modelContext) // Append statuses to viewed. try ViewedStatusHandler.shared.append(contentsOf: statuses, accountId: accountId, modelContext: modelContext)