You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
7.4 KiB

// Copyright © 2015 Abhishek Banthia
import Cocoa
import CoreLoggerKit
import CoreModelKit
enum ViewType {
case futureSlider
case upcomingEventView
case twelveHour
case sunrise
case showMeetingInMenubar
case showAllDayEventsInMenubar
case showAppInForeground
4 years ago
case appDisplayOptions
case dateInMenubar
case placeInMenubar
case dayInMenubar
case menubarCompactMode
case dstTransitionInfo
3 years ago
case sync
}
class DataStore: NSObject {
private static var sharedStore = DataStore(with: UserDefaults.standard)
private var userDefaults: UserDefaults!
3 years ago
private var ubiquitousStore: NSUbiquitousKeyValueStore?
6 years ago
// Since these pref can accessed every second, let's cache this
private var shouldDisplayDayInMenubar: Bool = false
private var shouldDisplayDateInMenubar: Bool = false
private static let timeFormatsWithSuffix: Set<NSNumber> = Set([NSNumber(integerLiteral: 0),
NSNumber(integerLiteral: 3),
NSNumber(integerLiteral: 4),
NSNumber(integerLiteral: 6),
NSNumber(integerLiteral: 7)])
class func shared() -> DataStore {
return sharedStore
}
init(with defaults: UserDefaults) {
super.init()
userDefaults = defaults
shouldDisplayDayInMenubar = shouldDisplay(.dayInMenubar)
shouldDisplayDateInMenubar = shouldDisplay(.dateInMenubar)
setupSyncNotification()
}
3 years ago
func setupSyncNotification() {
3 years ago
if shouldDisplay(.sync) {
3 years ago
ubiquitousStore = NSUbiquitousKeyValueStore.default
NotificationCenter.default.addObserver(self,
selector: #selector(ubiquitousKeyValueStoreChanged),
name: NSUbiquitousKeyValueStore.didChangeExternallyNotification,
object: NSUbiquitousKeyValueStore.default)
ubiquitousStore?.synchronize()
} else {
NotificationCenter.default.removeObserver(self,
name: NSUbiquitousKeyValueStore.didChangeExternallyNotification,
object: nil)
3 years ago
}
}
3 years ago
3 years ago
@objc func ubiquitousKeyValueStoreChanged(_ notification: Notification) {
let userInfo = notification.userInfo ?? [:]
let ubiquitousStore = notification.object as? NSUbiquitousKeyValueStore
print("--- User Info is \(userInfo)")
let currentTimezones = userDefaults.object(forKey: CLDefaultPreferenceKey) as? [Data]
let cloudTimezones = ubiquitousStore?.object(forKey: CLDefaultPreferenceKey) as? [Data]
3 years ago
3 years ago
if cloudTimezones != currentTimezones {
Logger.info("Syncing local timezones with data from the ☁")
userDefaults.set(cloudTimezones, forKey: CLDefaultPreferenceKey)
3 years ago
NotificationCenter.default.post(name: DataStore.didSyncFromExternalSourceNotification,
object: self)
3 years ago
}
}
func timezones() -> [Data] {
guard let preferences = userDefaults.object(forKey: CLDefaultPreferenceKey) as? [Data] else {
return []
}
return preferences
}
3 years ago
func setTimezones(_ timezones: [Data]?) {
userDefaults.set(timezones, forKey: CLDefaultPreferenceKey)
// iCloud sync
3 years ago
ubiquitousStore?.set(timezones, forKey: CLDefaultPreferenceKey)
}
6 years ago
func menubarTimezones() -> [Data]? {
return timezones().filter {
let customTimezone = TimezoneData.customObject(from: $0)
return customTimezone?.isFavourite == 1
}
}
func updateDayPreference() {
shouldDisplayDayInMenubar = shouldDisplay(.dayInMenubar)
}
func updateDateInPreference() {
shouldDisplayDateInMenubar = shouldDisplay(.dateInMenubar)
}
func shouldShowDayInMenubar() -> Bool {
return shouldDisplayDayInMenubar
}
func shouldShowDateInMenubar() -> Bool {
return shouldDisplayDateInMenubar
}
6 years ago
func retrieve(key: String) -> Any? {
return userDefaults.object(forKey: key)
}
func addTimezone(_ timezone: TimezoneData) {
let encodedTimezone = NSKeyedArchiver.archivedData(withRootObject: timezone)
var defaults: [Data] = timezones()
defaults.append(encodedTimezone)
setTimezones(defaults)
}
6 years ago
func removeLastTimezone() {
var currentLineup = timezones()
6 years ago
if currentLineup.isEmpty {
return
}
6 years ago
currentLineup.removeLast()
6 years ago
Logger.log(object: [:], for: "Undo Action Executed during Onboarding")
6 years ago
setTimezones(currentLineup)
}
6 years ago
func timezoneFormat() -> NSNumber {
return userDefaults.object(forKey: CLSelectedTimeZoneFormatKey) as? NSNumber ?? NSNumber(integerLiteral: 0)
}
func isBufferRequiredForTwelveHourFormats() -> Bool {
return DataStore.timeFormatsWithSuffix.contains(timezoneFormat())
}
func shouldDisplay(_ type: ViewType) -> Bool {
switch type {
case .futureSlider:
guard let value = retrieve(key: CLDisplayFutureSliderKey) as? NSNumber else {
return false
}
return value != 2 // Modern is 0, Legacy is 1 and Hide is 2.
case .upcomingEventView:
guard let value = retrieve(key: CLShowUpcomingEventView) as? NSString else {
return false
}
return value == "YES"
case .twelveHour:
return shouldDisplayHelper(CLSelectedTimeZoneFormatKey)
case .showAllDayEventsInMenubar:
6 years ago
return shouldDisplayHelper(CLShowAllDayEventsInUpcomingView)
case .sunrise:
6 years ago
return shouldDisplayHelper(CLSunriseSunsetTime)
case .showMeetingInMenubar:
6 years ago
return shouldDisplayHelper(CLShowMeetingInMenubar)
case .showAppInForeground:
guard let value = retrieve(key: CLShowAppInForeground) as? NSNumber else {
return false
}
return value.isEqual(to: NSNumber(value: 1))
case .dateInMenubar:
6 years ago
return shouldDisplayHelper(CLShowDateInMenu)
case .placeInMenubar:
6 years ago
return shouldDisplayHelper(CLShowPlaceInMenu)
case .dayInMenubar:
6 years ago
return shouldDisplayHelper(CLShowDayInMenu)
4 years ago
case .appDisplayOptions:
4 years ago
return shouldDisplayHelper(CLAppDisplayOptions)
case .dstTransitionInfo:
return shouldDisplayHelper(CLDisplayDSTTransitionInfo)
case .menubarCompactMode:
guard let value = retrieve(key: CLMenubarCompactMode) as? Int else {
return false
}
6 years ago
return value == 0
3 years ago
case .sync:
return shouldDisplayHelper(CLEnableSyncKey)
}
}
3 years ago
// MARK: Private
private func shouldDisplayHelper(_ key: String) -> Bool {
guard let value = retrieve(key: key) as? NSNumber else {
return false
}
return value.isEqual(to: NSNumber(value: 0))
}
}
3 years ago
extension DataStore {
public static let didSyncFromExternalSourceNotification: NSNotification.Name = .init("didSyncFromExternalSourceNotification")
}