tutanota/app-ios/TutanotaSharedFramework/Notifications/NotificationStorage.swift

129 lines
5.2 KiB
Swift
Raw Normal View History

2021-11-03 17:02:45 +01:00
import DictionaryCoding
import Foundation
2021-09-17 11:56:43 +02:00
2024-01-31 14:16:40 +01:00
private let SSE_INFO_KEY = "sseInfo"
private let ALARMS_KEY = "repeatingAlarmNotification"
private let LAST_PROCESSED_NOTIFICAION_ID_KEY = "lastProcessedNotificationId"
private let LAST_MISSED_NOTIFICATION_CHECK_TIME = "lastMissedNotificationCheckTime"
private let EXTENDED_NOTIFICATION_MODE = "extendedNotificationMode"
private let RECEIVE_CALENDAR_NOTIFICATION_CONFIG = "receiveCalendarNotificationConfig"
private let NOTIFICAITON_COUNT_KEY = "notificationCount"
2021-09-17 11:56:43 +02:00
public class NotificationStorage {
private let userPreferencesProvider: UserPreferencesProvider
2024-01-31 14:16:40 +01:00
public init(userPreferencesProvider: UserPreferencesProvider) { self.userPreferencesProvider = userPreferencesProvider }
2024-01-31 14:16:40 +01:00
public var sseInfo: SSEInfo? {
get {
let dict = self.userPreferencesProvider.getObject(forKey: SSE_INFO_KEY)
return dict.map { try! DictionaryDecoder().decode(SSEInfo.self, from: $0 as! NSDictionary) }
}
}
2024-01-31 14:16:40 +01:00
public func store(pushIdentifier: String, userId: String, sseOrigin: String) throws {
// Provide right defaults for extended notification mode.
// - Start with "nothing" as a conservative default
// - If notifications were not used before, enable extended notifications
if var sseInfo = self.sseInfo {
sseInfo.pushIdentifier = pushIdentifier
sseInfo.sseOrigin = sseOrigin
var userIds = sseInfo.userIds
if !userIds.contains(userId) {
userIds.append(userId)
try self.setExtendedNotificationConfig(userId, .sender_and_subject)
}
sseInfo.userIds = userIds
self.put(sseInfo: sseInfo)
} else {
let sseInfo = SSEInfo(pushIdentifier: pushIdentifier, sseOrigin: sseOrigin, userIds: [userId])
try self.setExtendedNotificationConfig(userId, .sender_and_subject)
self.put(sseInfo: sseInfo)
}
}
2024-01-31 14:16:40 +01:00
public func store(alarms: [EncryptedAlarmNotification]) {
let jsonData = try! JSONEncoder().encode(alarms)
self.userPreferencesProvider.setValue(jsonData, forKey: ALARMS_KEY)
}
2024-01-31 14:16:40 +01:00
public var alarms: [EncryptedAlarmNotification] {
get {
let notificationsJsonData = self.userPreferencesProvider.getObject(forKey: ALARMS_KEY)
if let notificationsJsonData {
2025-04-29 18:04:33 +02:00
do { return try JSONDecoder().decode(Array<EncryptedAlarmNotification>.self, from: notificationsJsonData as! Data) } catch {
TUTSLog("Could not schedule alarms")
}
}
2025-04-29 18:04:33 +02:00
return []
}
}
2024-01-31 14:16:40 +01:00
public func removeUser(_ userId: String) {
guard var sseInfo = self.sseInfo else {
TUTSLog("Removing userId but there's no SSEInfo stored")
return
}
var userIds = sseInfo.userIds
if let index = userIds.firstIndex(of: userId) { userIds.remove(at: index) }
sseInfo.userIds = userIds
self.put(sseInfo: sseInfo)
}
2024-01-31 14:16:40 +01:00
public var lastProcessedNotificationId: String? {
get { self.userPreferencesProvider.getObject(forKey: LAST_PROCESSED_NOTIFICAION_ID_KEY) as! String? }
set { return self.userPreferencesProvider.setValue(newValue, forKey: LAST_PROCESSED_NOTIFICAION_ID_KEY) }
}
2024-01-31 14:16:40 +01:00
public var lastMissedNotificationCheckTime: Date? {
get { self.userPreferencesProvider.getObject(forKey: LAST_MISSED_NOTIFICATION_CHECK_TIME) as! Date? }
set { return self.userPreferencesProvider.setValue(newValue, forKey: LAST_MISSED_NOTIFICATION_CHECK_TIME) }
}
2024-01-31 14:16:40 +01:00
/// Count of email notifications received but not viewed
public var notificationCount: Int { get { self.userPreferencesProvider.getObject(forKey: NOTIFICAITON_COUNT_KEY) as! Int? ?? 0 } }
public func incrementNotificationCount() { self.userPreferencesProvider.setValue(self.notificationCount + 1, forKey: NOTIFICAITON_COUNT_KEY) }
public func resetNotificaitonCount() { self.userPreferencesProvider.setValue(0, forKey: NOTIFICAITON_COUNT_KEY) }
public func clear() {
TUTSLog("UserPreference clear")
let sseInfo = self.sseInfo
if var sseInfo {
sseInfo.userIds = []
self.put(sseInfo: nil)
self.lastMissedNotificationCheckTime = nil
self.store(alarms: [])
}
}
2024-01-31 14:16:40 +01:00
public func setExtendedNotificationConfig(_ userId: String, _ mode: TutanotaSharedFramework.ExtendedNotificationMode) throws {
self.userPreferencesProvider.setValue(mode.rawValue, forKey: "\(EXTENDED_NOTIFICATION_MODE):\(userId)")
}
public func getExtendedNotificationConfig(_ userId: String) throws -> TutanotaSharedFramework.ExtendedNotificationMode {
// This default gets overwritten later when we store the pushIdentifier
self.userPreferencesProvider.getObject(forKey: "\(EXTENDED_NOTIFICATION_MODE):\(userId)")
.map { mode in ExtendedNotificationMode(rawValue: mode as! String)! } ?? .sender_and_subject
}
public func setReceiveCalendarNotificationConfig(_ pushIdentifier: String, _ value: Bool) {
self.userPreferencesProvider.setValue(value, forKey: "\(RECEIVE_CALENDAR_NOTIFICATION_CONFIG):\(pushIdentifier)")
}
public func getReceiveCalendarNotificationConfig(_ pushIdentifier: String) -> Bool {
self.userPreferencesProvider.getObject(forKey: "\(RECEIVE_CALENDAR_NOTIFICATION_CONFIG):\(pushIdentifier)").map { enabled in enabled as! Bool == true }
?? true
}
private func put(sseInfo: SSEInfo?) {
if let sseInfo {
let dict: NSDictionary = try! DictionaryEncoder().encode(sseInfo)
self.userPreferencesProvider.setValue(dict, forKey: SSE_INFO_KEY)
} else {
self.userPreferencesProvider.setValue(nil, forKey: SSE_INFO_KEY)
}
}
2021-09-17 11:56:43 +02:00
}