-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathWeeklyRoundupDebugScreen.swift
More file actions
235 lines (186 loc) · 7.57 KB
/
Copy pathWeeklyRoundupDebugScreen.swift
File metadata and controls
235 lines (186 loc) · 7.57 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import BackgroundTasks
import SwiftUI
import WordPressShared
struct BlueButton: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.padding()
.background(configuration.isPressed ? Color.white : Color(red: 0.5, green: 0.5, blue: 0.8))
.foregroundColor(.white)
.clipShape(Capsule())
}
}
struct WeeklyRoundupDebugScreen: View {
class Settings {
private let weeklyRoundupEnabledForA8cP2sKey = "weekly_roundup.debug.enabled_for_a8c_p2s"
let defaultPadding = CGFloat(16)
let spacerHeight = CGFloat(16)
var isEnabledForA8cP2s: Bool {
get {
(UserPersistentStoreFactory.instance().object(forKey: weeklyRoundupEnabledForA8cP2sKey) as? Bool) ?? false
}
set {
UserPersistentStoreFactory.instance().set(newValue, forKey: weeklyRoundupEnabledForA8cP2sKey)
}
}
}
@Environment(\.presentationMode) var presentationMode
@State private var scheduledDate: Date? = nil
@State private var running: Bool = false
@State private var errorScheduling: Bool = false
private let settings = Settings()
var body: some View {
VStack(alignment: .center) {
if errorScheduling {
Group {
Text(verbatim: "Error scheduling Weekly Roundup!")
.foregroundColor(.red)
}
Spacer()
.frame(height: 16)
}
Group {
Toggle("Include A8c P2s", isOn: Binding(get: {
settings.isEnabledForA8cP2s
}, set: { isOn in
settings.isEnabledForA8cP2s = isOn
}))
.padding()
Spacer()
.frame(height: 16)
}
Group {
scheduleDetailsView()
Spacer()
.frame(height: 16)
}
Group {
HStack {
Spacer()
Button("Schedule immediately") {
self.scheduleImmediately()
}
.buttonStyle(BlueButton())
.frame(width: 350)
Spacer()
}
Spacer()
.frame(height: settings.spacerHeight)
HStack {
Spacer()
Button("Schedule in 10 sec") {
self.scheduleDelayed(taskRunDelay: 10, staticNotificationDelay: 5 * 60)
}
.buttonStyle(BlueButton())
.frame(width: 350)
Spacer()
}
Spacer()
.frame(height: settings.spacerHeight)
HStack {
Spacer()
Button("Schedule in 10 sec") {
self.scheduleDelayed(taskRunDelay: 10, staticNotificationDelay: 30 * 60)
}
.buttonStyle(BlueButton())
.frame(width: 350)
Spacer()
}
Spacer()
.frame(height: settings.spacerHeight)
HStack {
Spacer()
Button("Schedule in 10 sec") {
self.scheduleDelayed(taskRunDelay: 10, staticNotificationDelay: 60 * 60)
}
.buttonStyle(BlueButton())
.frame(width: 350)
Spacer()
}
Spacer()
.frame(height: settings.spacerHeight)
}
Text(verbatim: "The values represent when the dynamic notification is scheduled at the earliest. It can take a lot more time to be sent since iOS basically decides when to deliver it.")
.fixedSize(horizontal: false, vertical: true)
.padding(settings.defaultPadding)
Spacer()
}
.navigationBarTitle("Weekly Roundup", displayMode: .inline)
.onAppear {
self.updateBackgroundTaskDate()
}
}
func updateBackgroundTaskDate() {
DispatchQueue.main.async {
WordPressAppDelegate.shared?.backgroundTasksCoordinator.getScheduledExecutionDate(taskIdentifier: WeeklyRoundupBackgroundTask.identifier, completion: { date in
self.scheduledDate = date
})
}
}
func scheduleDetailsView() -> some View {
guard !running else {
return AnyView(Text(verbatim: "Running..."))
}
if let scheduledDate = self.scheduledDate {
return AnyView(HStack {
Text(verbatim: "Earliest begin date:")
Spacer()
Text(verbatim: "\(scheduledDate.shortStringWithTime())")
})
} else {
return AnyView(HStack {
Text(verbatim: "Not scheduled.")
})
}
}
func scheduleImmediately() {
running = true
InteractiveNotificationsManager.shared.requestAuthorization { authorized in
guard authorized else {
self.running = false
return
}
typealias LaunchTaskWithIdentifier = @convention(c) (NSObject, Selector, NSString) -> Void
let selector = Selector(("_simulateLaunchForTaskWithIdentifier:"))
let methodImp = BGTaskScheduler.shared.method(for: selector)
let method = unsafeBitCast(methodImp, to: LaunchTaskWithIdentifier.self)
method(BGTaskScheduler.shared, selector, WeeklyRoundupBackgroundTask.identifier as NSString)
self.errorScheduling = false
DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
self.updateBackgroundTaskDate()
self.running = false
}
}
}
func scheduleDelayed(taskRunDelay: TimeInterval, staticNotificationDelay: TimeInterval) {
updateBackgroundTaskDate()
InteractiveNotificationsManager.shared.requestAuthorization { authorized in
if authorized {
DispatchQueue.main.async {
let taskRunDate = Date(timeIntervalSinceNow: taskRunDelay)
let staticNotificationDate = Date(timeIntervalSinceNow: staticNotificationDelay)
let calendar = Calendar.current
let runDateComponents = calendar.dateComponents([.hour, .minute, .second], from: taskRunDate)
let staticNotificationDateComponents = calendar.dateComponents([.hour, .minute, .second], from: staticNotificationDate)
let backgroundTask = WeeklyRoundupBackgroundTask(
runDateComponents: runDateComponents,
staticNotificationDateComponents: staticNotificationDateComponents)
WordPressAppDelegate.shared?.backgroundTasksCoordinator.schedule(backgroundTask) { result in
switch result {
case .success:
errorScheduling = false
case .failure:
errorScheduling = true
}
self.updateBackgroundTaskDate()
}
}
}
}
}
}
struct WeeklyRoundupDebugScreen_Preview: PreviewProvider {
static var previews: some View {
StoreSandboxSecretScreen(cookieJar: HTTPCookieStorage.shared)
}
}