From 222a930da255176fed621fbee1cde7c659556acb Mon Sep 17 00:00:00 2001 From: Xin Liu Date: Thu, 25 Jul 2024 01:32:45 -0700 Subject: [PATCH] Add a 5min timer in Planet sidebar for aggregation --- Planet/Entities/MyPlanetModel+Aggregate.swift | 10 ++++++++++ Planet/Entities/MyPlanetModel.swift | 3 +++ Planet/Views/Sidebar/PlanetSidebarView.swift | 9 +++++++++ Planet/versioning.xcconfig | 2 +- 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Planet/Entities/MyPlanetModel+Aggregate.swift b/Planet/Entities/MyPlanetModel+Aggregate.swift index 2ada67dd..9f326129 100644 --- a/Planet/Entities/MyPlanetModel+Aggregate.swift +++ b/Planet/Entities/MyPlanetModel+Aggregate.swift @@ -102,6 +102,13 @@ extension MyPlanetModel { /// /// Currently discovering feeds from domains is not supported. func aggregate() async { + if isAggregating { + debugPrint("Planet \(name) is already aggregating, skipping") + return + } + await MainActor.run { + self.isAggregating = true + } DispatchQueue.main.async { debugPrint("Aggregation: Started for \(self.name)") PlanetStore.shared.currentTaskMessage = "Fetching posts from other sites..." @@ -117,6 +124,9 @@ extension MyPlanetModel { PlanetStore.shared.isAggregating = false } } + Task { @MainActor in + self.isAggregating = false + } } guard let aggregation = aggregation, aggregation.count > 0 else { return diff --git a/Planet/Entities/MyPlanetModel.swift b/Planet/Entities/MyPlanetModel.swift index 98d35743..7f75db91 100644 --- a/Planet/Entities/MyPlanetModel.swift +++ b/Planet/Entities/MyPlanetModel.swift @@ -72,6 +72,7 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl @Published var isPublishing = false @Published var isRebuilding = false + @Published var isAggregating: Bool = false // populated when initializing @@ -380,6 +381,7 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl hasher.combine(lastPublishedCID) hasher.combine(isPublishing) hasher.combine(isRebuilding) + hasher.combine(isAggregating) hasher.combine(archived) hasher.combine(archivedAt) hasher.combine(plausibleEnabled) @@ -458,6 +460,7 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl && lhs.plausibleAPIServer == rhs.plausibleAPIServer && lhs.isPublishing == rhs.isPublishing && lhs.isRebuilding == rhs.isRebuilding + && lhs.isAggregating == rhs.isAggregating && lhs.twitterUsername == rhs.twitterUsername && lhs.githubUsername == rhs.githubUsername && lhs.telegramUsername == rhs.telegramUsername diff --git a/Planet/Views/Sidebar/PlanetSidebarView.swift b/Planet/Views/Sidebar/PlanetSidebarView.swift index 689a2f46..9fbd3e6a 100644 --- a/Planet/Views/Sidebar/PlanetSidebarView.swift +++ b/Planet/Views/Sidebar/PlanetSidebarView.swift @@ -13,6 +13,7 @@ struct PlanetSidebarView: View { let timer1m = Timer.publish(every: 60, on: .current, in: .common).autoconnect() let timer3m = Timer.publish(every: 180, on: .current, in: .common).autoconnect() + let timer5m = Timer.publish(every: 300, on: .current, in: .common).autoconnect() var body: some View { VStack(alignment: .leading, spacing: 0) { @@ -200,15 +201,23 @@ struct PlanetSidebarView: View { } } .onReceive(timer1m) { _ in + // Run every 60 seconds (1 minute) Task { await planetStore.checkPinnable() } } .onReceive(timer3m) { _ in + // Run every 180 seconds (3 minutes) Task { await planetStore.pin() } } + .onReceive(timer5m) { _ in + // Run every 300 seconds (5 minutes) + Task { + await planetStore.aggregate() + } + } } private func toggleSidebar() { diff --git a/Planet/versioning.xcconfig b/Planet/versioning.xcconfig index 741f87c9..f32a564a 100644 --- a/Planet/versioning.xcconfig +++ b/Planet/versioning.xcconfig @@ -1 +1 @@ -CURRENT_PROJECT_VERSION = 2140 +CURRENT_PROJECT_VERSION = 2141