-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathReaderSubscriptionNotificationSettingsButton.swift
More file actions
76 lines (68 loc) · 2.32 KB
/
Copy pathReaderSubscriptionNotificationSettingsButton.swift
File metadata and controls
76 lines (68 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import SwiftUI
import WordPressData
import WordPressUI
struct ReaderSubscriptionNotificationSettingsButton: View {
@ObservedObject var site: ReaderSiteTopic
@State private var isShowingSettings = false
@State private var status: ReaderSubscriptionNotificationsStatus = .none
var body: some View {
Button {
isShowingSettings = true
} label: {
Group {
switch status {
case .notify:
Image(systemName: "bell.and.waves.left.and.right")
.foregroundStyle(AppColor.primary)
case .personalized:
Image(systemName: "bell")
.foregroundStyle(AppColor.primary)
case .none:
Image(systemName: "bell.slash")
.foregroundStyle(.secondary)
.opacity(0.6)
}
}
.font(.subheadline)
.frame(width: 34, alignment: .center)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.sheet(isPresented: $isShowingSettings) {
ReaderSubscriptionNotificationSettingsView(siteID: site.siteID.intValue)
.presentationDetents([.medium, .large])
.edgesIgnoringSafeArea(.bottom)
}
.onReceive(site.emailSubscription?.objectWillChange ?? .init()) {
refresh()
}
.onReceive(site.postSubscription?.objectWillChange ?? .init()) {
refresh()
}
.onAppear { refresh() }
}
private func refresh() {
status = ReaderSubscriptionNotificationsStatus(site: site)
}
}
private enum ReaderSubscriptionNotificationsStatus {
/// Receive push notifications.
case notify
/// Receives emails notifications.
case personalized
/// Receives none.
case none
init(site: ReaderSiteTopic) {
let notifications = site.postSubscription
let emails = site.emailSubscription
let sendNotifications = notifications?.sendPosts ?? false
let sendEmails = (emails?.sendPosts ?? false) || (emails?.sendComments ?? false)
if sendNotifications {
self = .notify
} else if sendEmails {
self = .personalized
} else {
self = .none
}
}
}