Skip to content

Commit

Permalink
Add a 5min timer in Planet sidebar for aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
livid committed Jul 25, 2024
1 parent 2d54f43 commit 222a930
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Planet/Entities/MyPlanetModel+Aggregate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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..."
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Planet/Entities/MyPlanetModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions Planet/Views/Sidebar/PlanetSidebarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion Planet/versioning.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CURRENT_PROJECT_VERSION = 2140
CURRENT_PROJECT_VERSION = 2141

0 comments on commit 222a930

Please sign in to comment.