-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathSessionDefaults.swift
More file actions
354 lines (300 loc) · 13 KB
/
SessionDefaults.swift
File metadata and controls
354 lines (300 loc) · 13 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//
// This file is part of Canvas.
// Copyright (C) 2019-present Instructure, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
import UIKit
public struct SessionDefaults: Equatable {
// MARK: - Public Interface
/**
This is a shared session storage with an empty string as `sessionID`.
Can be used for testing/preview/fallback purposes.
*/
public static let fallback = SessionDefaults(sessionID: "")
public let sessionID: String
/// The underlying UserDefaults instance used for storage.
/// Automatically configured to use the app group suite for sharing data between app and extensions.
public var userDefaults: UserDefaults {
UserDefaults(suiteName: Bundle.main.appGroupID()) ?? .standard
}
/// The session-specific storage dictionary, keyed by the current session ID.
/// All session data is stored under this dictionary to ensure proper user isolation.
public var sessionDefaults: [String: Any]? {
get { userDefaults.dictionary(forKey: sessionID) }
set { userDefaults.set(newValue, forKey: sessionID) }
}
public mutating func reset() {
sessionDefaults = nil
}
/// Provides direct access to session-specific storage using a key-value pattern.
/// Values are automatically scoped to the current user session.
public subscript(key: String) -> Any? {
get { return sessionDefaults?[key] }
set {
var defaults = sessionDefaults ?? [:]
if let value = newValue {
defaults[key] = value
} else {
defaults.removeValue(forKey: key)
}
sessionDefaults = defaults
}
}
// MARK: - Mixed Feature Settings
/** This property is used by the file share extension to automatically select the course of the last viewed file in the app. The use-case is that the user views the assignment's file in the app, saves it to iOS Photos app, annotates it there and shares it back to the assignment. */
public var submitAssignmentCourseID: String? {
get { return self["submitAssignmentCourseID"] as? String }
set { self["submitAssignmentCourseID"] = newValue }
}
/** This property is used by the file share extension to automatically select the assignment of the last viewed file in the app. The use-case is that the user views the assignment's file in the app, saves it to iOS Photos app, annotates it there and shares it back to the assignment. */
public var submitAssignmentID: String? {
get { return self["submitAssignmentID"] as? String }
set { self["submitAssignmentID"] = newValue }
}
public var tokenExpires: Bool? {
get { return self["tokenExpires"] as? Bool }
set { self["tokenExpires"] = newValue }
}
public var showGradesOnDashboard: Bool? {
get { return self["showGradesOnDashboard"] as? Bool }
set {
self["showGradesOnDashboard"] = newValue
NotificationCenter.default.post(name: .showGradesOnDashboardDidChange, object: nil)
}
}
public var isDashboardLayoutGrid: Bool {
get { (self["isDashboardLayoutGrid"] as? Bool) ?? false }
set { self["isDashboardLayoutGrid"] = newValue }
}
public var interfaceStyle: UIUserInterfaceStyle? {
get {
guard let styleInt = self["interfaceStyle"] else { return nil }
return UIUserInterfaceStyle(rawValue: styleInt as? Int ?? -1)
}
set {
guard let newValue = newValue else { return self["interfaceStyle"] = nil}
self["interfaceStyle"] = newValue.rawValue
}
}
// We are using it to preserve Student Academic interface style. It is used when switching between the Academic and Career experience.
public var academicInterfaceStyle: UIUserInterfaceStyle? {
get {
guard let styleInt = self["academicInterfaceStyle"] else { return nil }
return UIUserInterfaceStyle(rawValue: styleInt as? Int ?? -1)
}
set {
guard let newValue = newValue else { return self["academicInterfaceStyle"] = nil}
self["academicInterfaceStyle"] = newValue.rawValue
}
}
public var isMissingItemsSectionOpenOnK5Schedule: Bool? {
get { return self["isMissingItemsSectionOpenOnK5Schedule"] as? Bool }
set { self["isMissingItemsSectionOpenOnK5Schedule"] = newValue }
}
public var isElementaryViewEnabled: Bool {
get { (self["isElementaryViewEnabled"] as? Bool) ?? true }
set { self["isElementaryViewEnabled"] = newValue }
}
public var isK5StudentView: Bool {
get { (self["isK5StudentView"] as? Bool) ?? false }
set { self["isK5StudentView"] = newValue }
}
public var landingPath: String? {
mutating get {
if let landingPath = self["landingPath"] as? String {
return landingPath
}
if let legacy = (UserDefaults.standard.object(forKey: "landingPageSettings") as? [String: String])?.first?.value {
let map = [
"Courses": "/",
"Calendar": "/calendar",
"To-Do List": "/to-do",
"Notifications": "/notifications",
"Messages": "/conversations"
]
if let path = map[legacy] {
self.landingPath = path
return path
}
}
return nil
}
set { self["landingPath"] = newValue }
}
public var limitWebAccess: Bool? {
get { self["limitWebAccess"] as? Bool }
set { self["limitWebAccess"] = newValue }
}
public var parentCurrentStudentID: String? {
get { self["parentCurrentStudentID"] as? String }
set { self["parentCurrentStudentID"] = newValue }
}
public var parentColorScheme: [String: Int]? {
get { self["parentColorScheme"] as? [String: Int] }
set { self["parentColorScheme"] = newValue }
}
public var hasSetPSPDFKitLastUsedValues: Bool {
get { return self["hasSetPSPDFKitLastUsedValues"] as? Bool ?? false }
set { self["hasSetPSPDFKitLastUsedValues"] = newValue }
}
public var collapsedModules: [String: [String]]? {
get { self["collapsedModules"] as? [String: [String]] }
set { self["collapsedModules"] = newValue }
}
public var appExperience: Experience? {
get {
if let rawValue = self["appExperience"] as? String {
return Experience(rawValue: rawValue)
}
return nil
}
set { self["appExperience"] = newValue?.rawValue }
}
// MARK: - Calendar Settings
private typealias ObservedStudentID = String
private typealias CanvasContextId = String
private let calendarSelectedContextsContainerKey = "calendarSelectedContexts"
public mutating func setCalendarSelectedContexts(
_ selectedContexts: Set<Context>,
observedStudentId: String?
) {
let observedStudentId = observedStudentId ?? ""
var container = self[calendarSelectedContextsContainerKey] as? [ObservedStudentID: [CanvasContextId]] ?? [:]
container[observedStudentId] = Array(selectedContexts.map { $0.canvasContextID })
self[calendarSelectedContextsContainerKey] = container
}
/**
- returns: Nil if the user has never selected calendars.
*/
public func calendarSelectedContexts(observedStudentId: String?) -> Set<Context>? {
let observedStudentId = observedStudentId ?? ""
guard
let container = self[calendarSelectedContextsContainerKey] as? [ObservedStudentID: [CanvasContextId]],
let selectedContextCodes = container[observedStudentId]
else {
return nil
}
let selectedContexts = selectedContextCodes.compactMap { Context(canvasContextID: $0) }
return Set(selectedContexts)
}
// MARK: - Offline Settings
public var isOfflineAutoSyncEnabled: Bool? {
get { self["isOfflineAutoSyncEnabled"] as? Bool }
set { self["isOfflineAutoSyncEnabled"] = newValue }
}
public var offlineSyncFrequency: CourseSyncFrequency? {
get {
guard let raw = self["offlineSyncFrequency"] as? Int,
let syncFrequency = CourseSyncFrequency(rawValue: raw) else {
return nil
}
return syncFrequency
}
set { self["offlineSyncFrequency"] = newValue?.rawValue }
}
public var offlineSyncNextDate: Date? {
get { self["offlineSyncNextDate"] as? Date }
set { self["offlineSyncNextDate"] = newValue }
}
public var isOfflineWifiOnlySyncEnabled: Bool? {
get { self["isOfflineWifiOnlySyncEnabled"] as? Bool }
set { self["isOfflineWifiOnlySyncEnabled"] = newValue }
}
public var offlineSyncSelections: [CourseSyncItemSelection] {
get {
self["offlineSyncSelections"] as? [String] ?? []
}
set {
self["offlineSyncSelections"] = newValue
}
}
// MARK: - Todo List Settings
public var todoFilterOptions: TodoFilterOptions? {
get {
guard let data = self["todoFilterOptions"] as? Data else {
return nil
}
return try? JSONDecoder().decode(TodoFilterOptions.self, from: data)
}
set {
if let newValue, let data = try? JSONEncoder().encode(newValue) {
self["todoFilterOptions"] = data
} else {
self["todoFilterOptions"] = nil
}
}
}
// MARK: - Grades
public var selectedSortByOptionIDs: [String: String]? {
get { self["selectedSortByOptionIDs"] as? [String: String] }
set { self["selectedSortByOptionIDs"] = newValue }
}
// MARK: - Assignments
public var assignmentListGroupBySettingByCourseId: [String: String]? {
get { self["assignmentListGroupBySettingByCourseId"] as? [String: String] }
set { self["assignmentListGroupBySettingByCourseId"] = newValue }
}
public var assignmentListStudentFilterSettingsByCourseId: [String: [String]]? {
get { self["assignmentListStudentFilterSettingsByCourseId"] as? [String: [String]] }
set { self["assignmentListStudentFilterSettingsByCourseId"] = newValue }
}
public var assignmentListTeacherFilterSettingByCourseId: [String: String]? {
get { self["assignmentListTeacherFilterSettingByCourseId"] as? [String: String] }
set { self["assignmentListTeacherFilterSettingByCourseId"] = newValue }
}
public var assignmentListTeacherStatusFilterSettingByCourseId: [String: String]? {
get { self["assignmentListTeacherStatusFilterSettingByCourseId"] as? [String: String] }
set { self["assignmentListTeacherStatusFilterSettingByCourseId"] = newValue }
}
// MARK: - Horizon
public var assignmentSubmissionTextEntry: [String: String]? {
get { self["assignmentSubmissionTextEntry"] as? [String: String] }
set { self["assignmentSubmissionTextEntry"] = newValue }
}
// MARK: - SpeedGrader
public var isSpeedGraderAnnotationToolbarVisible: Bool? {
get { self["isSpeedGraderAnnotationToolbarVisible"] as? Bool }
set { self["isSpeedGraderAnnotationToolbarVisible"] = newValue }
}
// MARK: - Learner Dashboard
public var learnerDashboardEnabledOnInstance: Bool {
get {
(self["learnerDashboardEnabledOnInstance"] as? Bool) ?? false
}
set {
self["learnerDashboardEnabledOnInstance"] = newValue
}
}
public var preferNewLearnerDashboard: Bool {
get {
(self["preferNewLearnerDashboard"] as? Bool) ?? true
}
set {
self["preferNewLearnerDashboard"] = newValue
}
}
/// Indicates whether the dashboard feedback alert should be shown when the classic dashboard appears.
/// This flag is set to `true` when the user switches away from the new learner dashboard.
/// The classic dashboard checks this flag and presents a feedback survey if `true`.
/// After presenting the survey, the flag is automatically reset to `false`.
public var shouldShowDashboardFeedback: Bool {
get {
(self["shouldShowDashboardFeedback"] as? Bool) ?? false
}
set {
self["shouldShowDashboardFeedback"] = newValue
}
}
}