Skip to content

Commit 81bd4fd

Browse files
committed
Dedup poll() code, make main view refreshable
1 parent afa7bb5 commit 81bd4fd

File tree

4 files changed

+21
-32
lines changed

4 files changed

+21
-32
lines changed

ntfy/App/AppMain.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ import Firebase
44
// Must have before release:
55
// TODO: Verify whether model version needs to be specified
66
// TODO: Make AppDelegate prettier
7-
// TODO: Remove duplicate code for poll()
87

98
// Nice to have
109
// TODO: Make notification click open detail view
1110
// TODO: Slide up dialog for "add topic"
12-
// TODO: Pull down "refresh all"
1311

1412
@main
1513
struct AppMain: App {

ntfy/Persistence/SubscriptionManager.swift

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,21 @@ struct SubscriptionManager {
1111
Log.d(tag, "Subscribing to \(topicUrl(baseUrl: baseUrl, topic: topic))")
1212
Messaging.messaging().subscribe(toTopic: topic)
1313
let subscription = store.saveSubscription(baseUrl: baseUrl, topic: topic)
14-
15-
// FIXME: Duplicate code!
14+
poll(subscription)
15+
}
16+
17+
func unsubscribe(_ subscription: Subscription) {
18+
Log.d(tag, "Unsubscribing from \(subscription.urlString())")
19+
DispatchQueue.main.async {
20+
if let topic = subscription.topic {
21+
Messaging.messaging().unsubscribe(fromTopic: topic)
22+
}
23+
store.delete(subscription: subscription)
24+
}
25+
}
26+
27+
func poll(_ subscription: Subscription) {
28+
Log.d(tag, "Polling from \(subscription.urlString())")
1629
ApiService.shared.poll(subscription: subscription) { messages, error in
1730
guard let messages = messages else {
1831
Log.e(tag, "Polling failed", error)
@@ -28,14 +41,4 @@ struct SubscriptionManager {
2841
}
2942
}
3043
}
31-
32-
func unsubscribe(_ subscription: Subscription) {
33-
Log.d(tag, "Unsubscribing from \(subscription.urlString())")
34-
DispatchQueue.main.async {
35-
if let topic = subscription.topic {
36-
Messaging.messaging().unsubscribe(fromTopic: topic)
37-
}
38-
store.delete(subscription: subscription)
39-
}
40-
}
4144
}

ntfy/Views/NotificationListView.swift

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ struct NotificationListView: View {
120120
}
121121
})
122122
.refreshable {
123-
poll()
123+
subscriptionManager.poll(subscription)
124124
}
125125
}
126126

@@ -177,23 +177,6 @@ struct NotificationListView: View {
177177
}
178178
editMode = .inactive
179179
}
180-
181-
private func poll() {
182-
ApiService.shared.poll(subscription: subscription) { messages, error in
183-
guard let messages = messages else {
184-
Log.e(tag, "Polling failed", error)
185-
return
186-
}
187-
Log.d(tag, "Polling success, \(messages.count) new message(s)", messages)
188-
if !messages.isEmpty {
189-
DispatchQueue.main.async {
190-
for message in messages {
191-
store.save(notificationFromMessage: message, withSubscription: subscription)
192-
}
193-
}
194-
}
195-
}
196-
}
197180
}
198181

199182
struct NotificationRowView: View {

ntfy/Views/SubscriptionListView.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ struct SubscriptionListView: View {
4343
.padding(40)
4444
}
4545
})
46+
.refreshable {
47+
subscriptions.forEach { subscription in
48+
subscriptionManager.poll(subscription)
49+
}
50+
}
4651
}
4752
.navigationViewStyle(StackNavigationViewStyle())
4853
}

0 commit comments

Comments
 (0)