2021-10-09 13:01:08 +02:00
|
|
|
//
|
|
|
|
// UserDefaults+Notification.swift
|
2022-10-09 14:07:57 +02:00
|
|
|
// MastodonCommon
|
2021-10-09 13:01:08 +02:00
|
|
|
//
|
|
|
|
// Created by Cirno MainasuK on 2021-10-9.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import CryptoKit
|
2022-09-30 13:28:09 +02:00
|
|
|
import MastodonExtension
|
2021-10-09 13:01:08 +02:00
|
|
|
|
|
|
|
extension UserDefaults {
|
|
|
|
// always use hash value (SHA256) from accessToken as key
|
|
|
|
private static func deriveKey(from accessToken: String, prefix: String) -> String {
|
|
|
|
let digest = SHA256.hash(data: Data(accessToken.utf8))
|
|
|
|
let bytes = [UInt8](digest)
|
|
|
|
let hex = bytes.toHexString()
|
|
|
|
let key = prefix + "@" + hex
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
|
|
|
private static let notificationCountKeyPrefix = "notification_count"
|
2023-05-15 14:12:41 +02:00
|
|
|
private static let notificationsLastTabIndexKeyPrefix = "last_notification_tab_index"
|
|
|
|
|
2021-10-09 13:01:08 +02:00
|
|
|
public func getNotificationCountWithAccessToken(accessToken: String) -> Int {
|
|
|
|
let prefix = UserDefaults.notificationCountKeyPrefix
|
|
|
|
let key = UserDefaults.deriveKey(from: accessToken, prefix: prefix)
|
|
|
|
return integer(forKey: key)
|
|
|
|
}
|
|
|
|
|
|
|
|
public func setNotificationCountWithAccessToken(accessToken: String, value: Int) {
|
|
|
|
let prefix = UserDefaults.notificationCountKeyPrefix
|
|
|
|
let key = UserDefaults.deriveKey(from: accessToken, prefix: prefix)
|
|
|
|
setValue(value, forKey: key)
|
|
|
|
}
|
|
|
|
|
|
|
|
public func increaseNotificationCount(accessToken: String) {
|
|
|
|
let count = getNotificationCountWithAccessToken(accessToken: accessToken)
|
|
|
|
setNotificationCountWithAccessToken(accessToken: accessToken, value: count + 1)
|
|
|
|
}
|
2024-03-14 07:19:46 +01:00
|
|
|
|
|
|
|
@objc public func getLastSelectedNotificationsTabName(accessToken: String) -> String? {
|
2023-05-15 14:12:41 +02:00
|
|
|
let prefix = UserDefaults.notificationsLastTabIndexKeyPrefix
|
|
|
|
let key = UserDefaults.deriveKey(from: accessToken, prefix: prefix)
|
2024-03-14 07:19:46 +01:00
|
|
|
return object(forKey: key) as? String
|
2023-05-15 14:12:41 +02:00
|
|
|
}
|
|
|
|
|
2024-03-14 07:19:46 +01:00
|
|
|
@objc public func setLastSelectedNotificationsTabName(accessToken: String, value: String?) {
|
2023-05-15 14:12:41 +02:00
|
|
|
let prefix = UserDefaults.notificationsLastTabIndexKeyPrefix
|
|
|
|
let key = UserDefaults.deriveKey(from: accessToken, prefix: prefix)
|
|
|
|
setValue(value, forKey: key)
|
|
|
|
}
|
2021-10-09 13:01:08 +02:00
|
|
|
}
|
2022-09-30 13:28:09 +02:00
|
|
|
|
|
|
|
extension UserDefaults {
|
|
|
|
|
|
|
|
@objc public dynamic var notificationBadgeCount: Int {
|
|
|
|
get {
|
|
|
|
register(defaults: [#function: 0])
|
|
|
|
return integer(forKey: #function)
|
|
|
|
}
|
|
|
|
set { self[#function] = newValue }
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|