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.

130 lines
3.9 KiB

// Copyright © 2015 Abhishek Banthia
import Cocoa
enum ViewType {
case futureSlider
case upcomingEventView
case twelveHour
case sunrise
case seconds
case showMeetingInMenubar
case showAllDayEventsInMenubar
case showAppInForeground
case dateInMenubar
case placeInMenubar
case dayInMenubar
case menubarCompactMode
}
class DataStore: NSObject {
private static var sharedStore = DataStore(with: UserDefaults.standard)
private var userDefaults: UserDefaults!
6 years ago
// Since this pref can accessed every second, let's cache this
private var shouldDisplayDateInMenubar: Bool = false
@objc class func shared() -> DataStore {
return sharedStore
}
init(with defaults: UserDefaults) {
super.init()
userDefaults = defaults
shouldDisplayDateInMenubar = shouldDisplay(.dayInMenubar)
}
@objc func timezones() -> [Data] {
guard let preferences = userDefaults.object(forKey: CLDefaultPreferenceKey) as? [Data] else {
return []
}
return preferences
}
6 years ago
func updateDayPreference() {
shouldDisplayDateInMenubar = shouldDisplay(.dayInMenubar)
}
func shouldShowDateInMenubar() -> Bool {
return shouldDisplayDateInMenubar
}
6 years ago
func setTimezones(_ timezones: [Data]) {
userDefaults.set(timezones, forKey: CLDefaultPreferenceKey)
}
func retrieve(key: String) -> Any? {
return userDefaults.object(forKey: key)
}
func addTimezone(_ timezone: TimezoneData) {
let encodedTimezone = NSKeyedArchiver.archivedData(withRootObject: timezone)
var defaults: [Data] = (userDefaults.object(forKey: CLDefaultPreferenceKey) as? [Data]) ?? []
defaults.append(encodedTimezone)
userDefaults.set(defaults, forKey: CLDefaultPreferenceKey)
}
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
userDefaults.set(currentLineup, forKey: CLDefaultPreferenceKey)
}
6 years ago
private func shouldDisplayHelper(_ key: String) -> Bool {
guard let value = retrieve(key: key) as? NSNumber else {
return false
}
return value.isEqual(to: NSNumber(value: 0))
}
func shouldDisplay(_ type: ViewType) -> Bool {
switch type {
case .futureSlider:
6 years ago
return shouldDisplayHelper(CLDisplayFutureSliderKey)
case .upcomingEventView:
guard let value = retrieve(key: CLShowUpcomingEventView) as? NSString else {
return false
}
return value == "YES"
case .twelveHour:
6 years ago
return shouldDisplayHelper(CL24hourFormatSelectedKey)
case .showAllDayEventsInMenubar:
6 years ago
return shouldDisplayHelper(CLShowAllDayEventsInUpcomingView)
case .sunrise:
6 years ago
return shouldDisplayHelper(CLSunriseSunsetTime)
case .seconds:
6 years ago
return shouldDisplayHelper(CLShowSecondsInMenubar)
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)
case .menubarCompactMode:
guard let value = retrieve(key: CLMenubarCompactMode) as? Int else {
return false
}
6 years ago
return value == 0
}
}
}