Browse Source

Move constants to an enum.

master
Abhishek Banthia 9 months ago
parent
commit
efba5a7d8b
  1. 18
      Clocker/AppDelegate.swift
  2. 8
      Clocker/ClockerUnitTests/AppDelegateTests.swift
  3. 8
      Clocker/ClockerUnitTests/ClockerUnitTests.swift
  4. 2
      Clocker/ClockerUnitTests/SearchDataSourceTests.swift
  5. 6
      Clocker/ClockerUnitTests/StandardMenubarHandlerTests.swift
  6. 2
      Clocker/ClockerUnitTests/ThemerTests.swift
  7. 2
      Clocker/Dependencies/Date Additions/Date+TimeAgo.swift
  8. 14
      Clocker/Events and Reminders/CalendarHandler.swift
  9. 12
      Clocker/Onboarding/OnboardingParentViewController.swift
  10. 38
      Clocker/Onboarding/OnboardingSearchController.swift
  11. 40
      Clocker/Overall App/AppDefaults.swift
  12. 46
      Clocker/Overall App/ConfigExport.swift
  13. 52
      Clocker/Overall App/DataStore.swift
  14. 79
      Clocker/Overall App/Strings.swift
  15. 4
      Clocker/Overall App/Themer.swift

18
Clocker/AppDelegate.swift

@ -10,7 +10,7 @@ open class AppDelegate: NSObject, NSApplicationDelegate {
private lazy var floatingWindow = FloatingWindowController.shared()
internal lazy var panelController = PanelController(windowNibName: .panel)
private var statusBarHandler: StatusItemHandler!
private let store: VersionUpdateHandler = VersionUpdateHandler(with: DataStore.shared())
private let versionUpdateHandler: VersionUpdateHandler = VersionUpdateHandler(with: DataStore.shared())
override open func observeValue(forKeyPath keyPath: String?, of object: Any?, change _: [NSKeyValueChangeKey: Any]?, context _: UnsafeMutableRawPointer?) {
if let path = keyPath, path == PreferencesConstants.hotKeyPathIdentifier {
@ -82,16 +82,16 @@ open class AppDelegate: NSObject, NSApplicationDelegate {
}
@objc func hideFromDock() {
UserDefaults.standard.set(0, forKey: CLAppDisplayOptions)
UserDefaults.standard.set(0, forKey: UserDefaultKeys.appDisplayOptions)
NSApp.setActivationPolicy(.accessory)
}
private var controller: OnboardingController?
private func showOnboardingFlowIfEligible() {
let isTestInProgress = ProcessInfo.processInfo.arguments.contains(CLOnboardingTestsLaunchArgument)
let isTestInProgress = ProcessInfo.processInfo.arguments.contains(UserDefaultKeys.onboardingTestsLaunchArgument)
let shouldLaunchOnboarding =
(DataStore.shared().retrieve(key: CLShowOnboardingFlow) == nil
(DataStore.shared().retrieve(key: UserDefaultKeys.showOnboardingFlow) == nil
&& DataStore.shared().timezones().isEmpty)
|| isTestInProgress
@ -116,7 +116,7 @@ open class AppDelegate: NSObject, NSApplicationDelegate {
// Install the menubar item!
statusBarHandler = StatusItemHandler(with: DataStore.shared())
if ProcessInfo.processInfo.arguments.contains(CLUITestingLaunchArgument) {
if ProcessInfo.processInfo.arguments.contains(UserDefaultKeys.testingLaunchArgument) {
FirebaseApp.configure()
ReviewController.setPreviewMode(true)
}
@ -130,9 +130,9 @@ open class AppDelegate: NSObject, NSApplicationDelegate {
setActivationPolicy()
// Set the display mode default as panel!
if let displayMode = defaults.object(forKey: CLShowAppInForeground) as? NSNumber, displayMode.intValue == 1 {
if let displayMode = defaults.object(forKey: UserDefaultKeys.showAppInForeground) as? NSNumber, displayMode.intValue == 1 {
showFloatingWindow()
} else if let displayMode = defaults.object(forKey: CLShowAppInForeground) as? Int, displayMode == 1 {
} else if let displayMode = defaults.object(forKey: UserDefaultKeys.showAppInForeground) as? Int, displayMode == 1 {
showFloatingWindow()
}
}
@ -142,7 +142,7 @@ open class AppDelegate: NSObject, NSApplicationDelegate {
let defaults = UserDefaults.standard
let currentActivationPolicy = NSRunningApplication.current.activationPolicy
let activationPolicy: NSApplication.ActivationPolicy = defaults.integer(forKey: CLAppDisplayOptions) == 0 ? .accessory : .regular
let activationPolicy: NSApplication.ActivationPolicy = defaults.integer(forKey: UserDefaultKeys.appDisplayOptions) == 0 ? .accessory : .regular
if currentActivationPolicy != activationPolicy {
NSApp.setActivationPolicy(activationPolicy)
@ -234,7 +234,7 @@ open class AppDelegate: NSObject, NSApplicationDelegate {
@IBAction open func togglePanel(_ sender: NSButton) {
Logger.info("Toggle Panel called with sender state \(sender.state.rawValue)")
let displayMode = UserDefaults.standard.integer(forKey: CLShowAppInForeground)
let displayMode = UserDefaults.standard.integer(forKey: UserDefaultKeys.showAppInForeground)
if displayMode == 1 {
// No need to call NSApp.activate here since `showFloatingWindow` takes care of this

8
Clocker/ClockerUnitTests/AppDelegateTests.swift

@ -71,7 +71,7 @@ class AppDelegateTests: XCTestCase {
func testActivationPolicy() {
let subject = NSApplication.shared.delegate as? AppDelegate
let previousOption = UserDefaults.standard.integer(forKey: CLAppDisplayOptions)
let previousOption = UserDefaults.standard.integer(forKey: UserDefaultKeys.appDisplayOptions)
if previousOption == 0 {
XCTAssertEqual(NSApp.activationPolicy(), .accessory)
} else {
@ -87,7 +87,7 @@ class AppDelegateTests: XCTestCase {
subject?.invalidateMenubarTimer(true)
let statusItemHandler = subject?.statusItemForPanel()
XCTAssertEqual(statusItemHandler?.statusItem.button?.subviews, [])
XCTAssertEqual(statusItemHandler?.statusItem.button?.title, CLEmptyString)
XCTAssertEqual(statusItemHandler?.statusItem.button?.title, UserDefaultKeys.emptyString)
XCTAssertEqual(statusItemHandler?.statusItem.button?.image?.name(), "LightModeIcon")
XCTAssertEqual(statusItemHandler?.statusItem.button?.imagePosition, .imageOnly)
XCTAssertEqual(statusItemHandler?.statusItem.button?.toolTip, "Clocker")
@ -116,7 +116,7 @@ class AppDelegateTests: XCTestCase {
func testStandardModeMenubarSetup() {
let olderTimezones = DataStore.shared().timezones()
UserDefaults.standard.set(1, forKey: CLMenubarCompactMode) // Set the menubar mode to standard
UserDefaults.standard.set(1, forKey: UserDefaultKeys.menubarCompactMode) // Set the menubar mode to standard
let subject = NSApplication.shared.delegate as? AppDelegate
let statusItemHandler = subject?.statusItemForPanel()
@ -142,6 +142,6 @@ class AppDelegateTests: XCTestCase {
XCTAssertEqual(subject?.statusItemForPanel().statusItem.button?.subviews.isEmpty, true) // This will be nil for standard mode
UserDefaults.standard.set(0, forKey: CLMenubarCompactMode) // Set the menubar mode back to compact
UserDefaults.standard.set(0, forKey: UserDefaultKeys.menubarCompactMode) // Set the menubar mode back to compact
}
}

8
Clocker/ClockerUnitTests/ClockerUnitTests.swift

@ -74,7 +74,7 @@ class ClockerUnitTests: XCTestCase {
func testOverridingSecondsComponent_shouldHideSeconds() {
let dummyDefaults = UserDefaults.standard
dummyDefaults.set(NSNumber(value: 4), forKey: CLSelectedTimeZoneFormatKey) // 4 is 12 hour with seconds
dummyDefaults.set(NSNumber(value: 4), forKey: UserDefaultKeys.selectedTimeZoneFormatKey) // 4 is 12 hour with seconds
let timezoneObjects = [TimezoneData(with: mumbai),
TimezoneData(with: auckland),
@ -207,7 +207,7 @@ class ClockerUnitTests: XCTestCase {
func testTimezoneFormat() {
let dataObject = TimezoneData(with: mumbai)
UserDefaults.standard.set(NSNumber(value: 0), forKey: CLSelectedTimeZoneFormatKey) // Set to 12 hour format
UserDefaults.standard.set(NSNumber(value: 0), forKey: UserDefaultKeys.selectedTimeZoneFormatKey) // Set to 12 hour format
dataObject.setShouldOverrideGlobalTimeFormat(0) // Respect Global Preference
XCTAssertTrue(dataObject.timezoneFormat(DataStore.shared().timezoneFormat()) == "h:mm a")
@ -246,7 +246,7 @@ class ClockerUnitTests: XCTestCase {
func testTimezoneFormatWithDefaultSetAs24HourFormat() {
let dataObject = TimezoneData(with: california)
UserDefaults.standard.set(NSNumber(value: 1), forKey: CLSelectedTimeZoneFormatKey) // Set to 24-Hour Format
UserDefaults.standard.set(NSNumber(value: 1), forKey: UserDefaultKeys.selectedTimeZoneFormatKey) // Set to 24-Hour Format
dataObject.setShouldOverrideGlobalTimeFormat(0)
XCTAssertTrue(dataObject.timezoneFormat(DataStore.shared().timezoneFormat()) == "HH:mm",
@ -285,7 +285,7 @@ class ClockerUnitTests: XCTestCase {
func testSecondsDisplayForOverridenTimezone() {
let dataObject = TimezoneData(with: california)
UserDefaults.standard.set(NSNumber(value: 1), forKey: CLSelectedTimeZoneFormatKey) // Set to 24-Hour Format
UserDefaults.standard.set(NSNumber(value: 1), forKey: UserDefaultKeys.selectedTimeZoneFormatKey) // Set to 24-Hour Format
// Test default behaviour
let timezoneWithSecondsKeys = [4, 5, 8, 11]

2
Clocker/ClockerUnitTests/SearchDataSourceTests.swift

@ -113,7 +113,7 @@ class SearchDataSourceTests: XCTestCase {
func testRetrieveSelectedTimezoneWithEmptySearchField() {
// Setup subject with an empty search field
setupSubject(searchText: CLEmptyString)
setupSubject(searchText: UserDefaultKeys.emptyString)
subject.searchTimezones("los")
XCTAssertFalse(subject.calculateChangesets())

6
Clocker/ClockerUnitTests/StandardMenubarHandlerTests.swift

@ -20,8 +20,8 @@ class StandardMenubarHandlerTests: XCTestCase {
private func makeMockStore(with menubarMode: Int = 1) -> DataStore {
// Wipe all timezones from UserDefaults
let defaults = UserDefaults(suiteName: "com.abhishek.Clocker.StandardMenubarHandlerTests")!
defaults.set(menubarMode, forKey: CLMenubarCompactMode)
defaults.set(0, forKey: CLShowMeetingInMenubar)
defaults.set(menubarMode, forKey: UserDefaultKeys.menubarCompactMode)
defaults.set(0, forKey: UserDefaultKeys.showMeetingInMenubar)
XCTAssertNotEqual(defaults, UserDefaults.standard)
return DataStore(with: defaults)
}
@ -163,7 +163,7 @@ class StandardMenubarHandlerTests: XCTestCase {
mockEvent.startDate = Date().add(futureChunk)
let menubarHandler = MenubarTitleProvider(with: store, eventStore: EventCenter.sharedCenter())
XCTAssert(menubarHandler.format(event: mockEvent) == CLEmptyString,
XCTAssert(menubarHandler.format(event: mockEvent) == UserDefaultKeys.emptyString,
"Suffix \(menubarHandler.format(event: mockEvent)) doesn't match expectation")
}

2
Clocker/ClockerUnitTests/ThemerTests.swift

@ -122,7 +122,7 @@ class ThemerTests: XCTestCase {
func testSystemTheme() throws {
let currentSystemTheme =
UserDefaults.standard.string(forKey: CLAppleInterfaceStyleKey)?.lowercased().contains("dark") ?? false ? Themer.Theme.dark : Themer.Theme.light
UserDefaults.standard.string(forKey: UserDefaultKeys.appleInterfaceStyleKey)?.lowercased().contains("dark") ?? false ? Themer.Theme.dark : Themer.Theme.light
let subject = Themer(index: 2) // 2 is for system theme
let expectedSliderKnobColor = currentSystemTheme == .light ? NSColor(deviceRed: 255.0, green: 255.0, blue: 255, alpha: 0.9) : NSColor(deviceRed: 0.0, green: 0.0, blue: 0, alpha: 0.9)
let expectedSliderRightColor = currentSystemTheme == .dark ? NSColor.white : NSColor.gray

2
Clocker/Dependencies/Date Additions/Date+TimeAgo.swift

@ -131,7 +131,7 @@ public extension Date {
// Instead of returning "Just now" or the equivalent localized version; let's return an empty string
// Previously, we returned DateToolsLocalizedStrings("Just now")
return CLEmptyString
return UserDefaultKeys.emptyString
}
}

14
Clocker/Events and Reminders/CalendarHandler.swift

@ -36,7 +36,7 @@ extension EventCenter {
setOfCalendars = Set(userCalendars)
}
var currentSourceTitle = CLEmptyString
var currentSourceTitle = UserDefaultKeys.emptyString
for calendar in calendars {
if !(calendar.source.title == currentSourceTitle) {
@ -81,12 +81,12 @@ extension EventCenter {
Returns a tuple with 0 as the header string and 1 as the subtitle string
*/
func separateFormat(event: EKEvent) -> (String, String)? {
guard let truncateLength = DataStore.shared().retrieve(key: CLTruncateTextLength) as? NSNumber, let eventTitle = event.title else {
guard let truncateLength = DataStore.shared().retrieve(key: UserDefaultKeys.truncateTextLength) as? NSNumber, let eventTitle = event.title else {
return nil
}
let seconds = event.startDate.timeIntervalSinceNow
var formattedTitle: String = CLEmptyString
var formattedTitle: String = UserDefaultKeys.emptyString
if eventTitle.count > truncateLength.intValue {
let truncateIndex = eventTitle.index(eventTitle.startIndex, offsetBy: truncateLength.intValue)
@ -98,7 +98,7 @@ extension EventCenter {
formattedTitle.append(eventTitle)
}
var menubarText: String = CLEmptyString
var menubarText: String = UserDefaultKeys.emptyString
let minutes = seconds / 60
if minutes > 2 {
@ -224,7 +224,7 @@ extension EventCenter {
let allCalendars = self.retrieveAllCalendarIdentifiers()
if !allCalendars.isEmpty {
UserDefaults.standard.set(allCalendars, forKey: CLSelectedCalendars)
UserDefaults.standard.set(allCalendars, forKey: UserDefaultKeys.selectedCalendars)
Logger.info("Finished saving all calendar identifiers in default")
self.filterEvents()
}
@ -485,8 +485,8 @@ struct EventInfo {
return "started +\(event.startDate.shortTimeAgoSinceNow)."
} else if event.startDate.isToday, timeIntervalSinceNowForMeeting > 0 {
let timeSince = Date().timeAgo(since: event.startDate).lowercased()
let withoutAn = timeSince.replacingOccurrences(of: "an", with: CLEmptyString)
var withoutAgo = withoutAn.replacingOccurrences(of: "ago", with: CLEmptyString)
let withoutAn = timeSince.replacingOccurrences(of: "an", with: UserDefaultKeys.emptyString)
var withoutAgo = withoutAn.replacingOccurrences(of: "ago", with: UserDefaultKeys.emptyString)
// If the user has not turned on seconds granularity for one of the timezones,
// we return "in 12 seconds" which looks weird.

12
Clocker/Onboarding/OnboardingParentViewController.swift

@ -194,9 +194,9 @@ class OnboardingParentViewController: NSViewController {
}
private func navigateToFinalStage() {
if UserDefaults.standard.object(forKey: CLInstallHomeIndicatorObject) == nil, DataStore.shared().timezones().isEmpty {
if UserDefaults.standard.object(forKey: UserDefaultKeys.installHomeIndicatorObject) == nil, DataStore.shared().timezones().isEmpty {
fetchLocalTimezone()
UserDefaults.standard.set(1, forKey: CLInstallHomeIndicatorObject)
UserDefaults.standard.set(1, forKey: UserDefaultKeys.installHomeIndicatorObject)
}
guard let fromViewController = onboardingSearchVC, let toViewController = finalOnboardingVC else {
@ -220,8 +220,8 @@ class OnboardingParentViewController: NSViewController {
view.window?.close()
if ProcessInfo.processInfo.arguments.contains(CLOnboardingTestsLaunchArgument) == false {
UserDefaults.standard.set(true, forKey: CLShowOnboardingFlow)
if ProcessInfo.processInfo.arguments.contains(UserDefaultKeys.onboardingTestsLaunchArgument) == false {
UserDefaults.standard.set(true, forKey: UserDefaultKeys.showOnboardingFlow)
}
// Install the menubar option!
@ -317,11 +317,11 @@ class OnboardingParentViewController: NSViewController {
private func shouldStartAtLogin(_ shouldStart: Bool) {
// If tests are going on, we don't want to enable/disable launch at login!
if ProcessInfo.processInfo.arguments.contains(CLOnboardingTestsLaunchArgument) {
if ProcessInfo.processInfo.arguments.contains(UserDefaultKeys.onboardingTestsLaunchArgument) {
return
}
UserDefaults.standard.set(shouldStart ? 1 : 0, forKey: CLStartAtLogin)
UserDefaults.standard.set(shouldStart ? 1 : 0, forKey: UserDefaultKeys.startAtLogin)
startupManager.toggleLogin(shouldStart)
shouldStart ?
Logger.log(object: nil, for: "Enable Launch at Login while Onboarding") :

38
Clocker/Onboarding/OnboardingSearchController.swift

@ -100,7 +100,7 @@ class OnboardingSearchController: NSViewController {
private func cleanupAfterInstallingTimezone() {
let data = TimezoneData()
data.setLabel(CLEmptyString)
data.setLabel(UserDefaultKeys.emptyString)
if let currentSelection = searchResultsDataSource?.retrieveSelectedTimezone(resultsTableView.selectedRow) {
let metaInfo = metadata(for: currentSelection)
@ -115,7 +115,7 @@ class OnboardingSearchController: NSViewController {
searchResultsDataSource?.cleanupFilterArray()
searchResultsDataSource?.timezoneFilteredArray = []
searchResultsDataSource?.calculateChangesets()
searchBar.stringValue = CLEmptyString
searchBar.stringValue = UserDefaultKeys.emptyString
accessoryLabel.stringValue = "Added \(metaInfo.1.formattedName)."
undoButton.isHidden = false
@ -141,7 +141,7 @@ class OnboardingSearchController: NSViewController {
repeats: false)
{ _ in
OperationQueue.main.addOperation {
self.setInfoLabel(CLEmptyString)
self.setInfoLabel(UserDefaultKeys.emptyString)
}
}
}
@ -225,13 +225,13 @@ class OnboardingSearchController: NSViewController {
}
let newTimeZone = [
CLTimezoneID: response.timeZoneId,
CLTimezoneName: filteredAddress,
CLPlaceIdentifier: dataObject.placeID!,
UserDefaultKeys.timezoneID: response.timeZoneId,
UserDefaultKeys.timezoneName: filteredAddress,
UserDefaultKeys.placeIdentifier: dataObject.placeID!,
"latitude": latitude,
"longitude": longitude,
"nextUpdate": CLEmptyString,
CLCustomLabel: filteredAddress,
"nextUpdate": UserDefaultKeys.emptyString,
UserDefaultKeys.customLabel: filteredAddress,
] as [String: Any]
DataStore.shared().addTimezone(TimezoneData(with: newTimeZone))
@ -271,7 +271,7 @@ class OnboardingSearchController: NSViewController {
private func setup() {
appName.stringValue = "Quick Add Locations".localized()
onboardingTypeLabel.stringValue = "More search options in Clocker Preferences.".localized()
setInfoLabel(CLEmptyString)
setInfoLabel(UserDefaultKeys.emptyString)
searchBar.bezelStyle = .roundedBezel
searchBar.placeholderString = "Press Enter to Search!"
searchBar.delegate = self
@ -291,7 +291,7 @@ class OnboardingSearchController: NSViewController {
if searchString.isEmpty {
resetSearchView()
setInfoLabel(CLEmptyString)
setInfoLabel(UserDefaultKeys.emptyString)
return
}
@ -308,7 +308,7 @@ class OnboardingSearchController: NSViewController {
fileprivate func resetIfNeccesary(_ searchString: String) {
if searchString.isEmpty {
resetSearchView()
setInfoLabel(CLEmptyString)
setInfoLabel(UserDefaultKeys.emptyString)
}
}
@ -324,7 +324,7 @@ class OnboardingSearchController: NSViewController {
let words = searchString.components(separatedBy: CharacterSet.whitespacesAndNewlines)
searchString = words.joined(separator: CLEmptyString)
searchString = words.joined(separator: UserDefaultKeys.emptyString)
if searchString.count < 3 {
resetIfNeccesary(searchString)
@ -343,7 +343,7 @@ class OnboardingSearchController: NSViewController {
let words = currentSearchBarValue.components(separatedBy: CharacterSet.whitespacesAndNewlines)
if words.joined(separator: CLEmptyString) != searchString {
if words.joined(separator: UserDefaultKeys.emptyString) != searchString {
return
}
@ -397,7 +397,7 @@ class OnboardingSearchController: NSViewController {
}
private func prepareUIForPresentingResults() {
setInfoLabel(CLEmptyString)
setInfoLabel(UserDefaultKeys.emptyString)
if let dataSource = searchResultsDataSource, dataSource.calculateChangesets() {
resultsTableView.isHidden = false
resultsTableView.reloadData()
@ -414,10 +414,10 @@ class OnboardingSearchController: NSViewController {
let totalPackage = [
"latitude": latitude,
"longitude": longitude,
CLTimezoneName: formattedAddress,
CLCustomLabel: formattedAddress,
CLTimezoneID: CLEmptyString,
CLPlaceIdentifier: result.placeId,
UserDefaultKeys.timezoneName: formattedAddress,
UserDefaultKeys.customLabel: formattedAddress,
UserDefaultKeys.timezoneID: UserDefaultKeys.emptyString,
UserDefaultKeys.placeIdentifier: result.placeId,
] as [String: Any]
return TimezoneData(with: totalPackage)
@ -431,7 +431,7 @@ class OnboardingSearchController: NSViewController {
searchResultsDataSource?.timezoneFilteredArray = []
searchResultsDataSource?.calculateChangesets()
resultsTableView.reloadData()
searchBar.stringValue = CLEmptyString
searchBar.stringValue = UserDefaultKeys.emptyString
searchBar.placeholderString = "Press Enter to Search"
}

40
Clocker/Overall App/AppDefaults.swift

@ -16,30 +16,30 @@ class AppDefaults {
defaults.register(defaults: defaultsDictionary())
store.setTimezones(timezones)
defaults.set(selectedCalendars, forKey: CLSelectedCalendars)
defaults.set(selectedCalendars, forKey: UserDefaultKeys.selectedCalendars)
}
private class func defaultsDictionary() -> [String: Any] {
let calendars: [String] = []
return [CLThemeKey: 0,
CLDisplayFutureSliderKey: 0,
CLSelectedTimeZoneFormatKey: 0, // 12-hour format
CLRelativeDateKey: 0,
CLShowDayInMenu: 0,
CLShowDateInMenu: 1,
CLShowPlaceInMenu: 0,
CLStartAtLogin: 0,
CLSunriseSunsetTime: 1,
CLUserFontSizePreference: 4,
CLShowUpcomingEventView: "YES",
CLShowAppInForeground: 0,
CLFutureSliderRange: 0,
CLShowAllDayEventsInUpcomingView: 1,
CLShowMeetingInMenubar: 0,
CLTruncateTextLength: 30,
CLSelectedCalendars: calendars,
CLAppDisplayOptions: 0,
CLMenubarCompactMode: 1]
return [UserDefaultKeys.themeKey: 0,
UserDefaultKeys.displayFutureSliderKey: 0,
UserDefaultKeys.selectedTimeZoneFormatKey: 0, // 12-hour format
UserDefaultKeys.relativeDateKey: 0,
UserDefaultKeys.showDayInMenu: 0,
UserDefaultKeys.showDateInMenu: 1,
UserDefaultKeys.showPlaceInMenu: 0,
UserDefaultKeys.startAtLogin: 0,
UserDefaultKeys.sunriseSunsetTime: 1,
UserDefaultKeys.userFontSizePreference: 4,
UserDefaultKeys.showUpcomingEventView: "YES",
UserDefaultKeys.showAppInForeground: 0,
UserDefaultKeys.futureSliderRange: 0,
UserDefaultKeys.showAllDayEventsInUpcomingView: 1,
UserDefaultKeys.showMeetingInMenubar: 0,
UserDefaultKeys.truncateTextLength: 30,
UserDefaultKeys.selectedCalendars: calendars,
UserDefaultKeys.appDisplayOptions: 0,
UserDefaultKeys.menubarCompactMode: 1]
}
}

46
Clocker/Overall App/ConfigExport.swift

@ -7,29 +7,29 @@ import Foundation
struct ConfigExport {
private func generateJSON(from store: DataStore) {
let selectedKeys: Set<String> = Set([
CLShowOnboardingFlow,
CLSelectedTimeZoneFormatKey,
CLThemeKey,
CLShowDayInMenu,
CLShowDateInMenu,
CLShowPlaceInMenu,
CLDisplayFutureSliderKey,
CLStartAtLogin,
CLShowAppInForeground,
CLSunriseSunsetTime,
CLUserFontSizePreference,
CLShowUpcomingEventView,
CLShowAllDayEventsInUpcomingView,
CLShowMeetingInMenubar,
CLTruncateTextLength,
CLFutureSliderRange,
CLSelectedCalendars,
CLAppDisplayOptions,
CLLongStatusBarWarningMessage,
CLMenubarCompactMode,
CLDefaultMenubarMode,
CLInstallHomeIndicatorObject,
CLSwitchToCompactModeAlert,
UserDefaultKeys.showOnboardingFlow,
UserDefaultKeys.selectedTimeZoneFormatKey,
UserDefaultKeys.themeKey,
UserDefaultKeys.showDayInMenu,
UserDefaultKeys.showDateInMenu,
UserDefaultKeys.showPlaceInMenu,
UserDefaultKeys.displayFutureSliderKey,
UserDefaultKeys.startAtLogin,
UserDefaultKeys.showAppInForeground,
UserDefaultKeys.sunriseSunsetTime,
UserDefaultKeys.userFontSizePreference,
UserDefaultKeys.showUpcomingEventView,
UserDefaultKeys.showAllDayEventsInUpcomingView,
UserDefaultKeys.showMeetingInMenubar,
UserDefaultKeys.truncateTextLength,
UserDefaultKeys.futureSliderRange,
UserDefaultKeys.selectedCalendars,
UserDefaultKeys.appDisplayOptions,
UserDefaultKeys.longStatusBarWarningMessage,
UserDefaultKeys.menubarCompactMode,
UserDefaultKeys.defaultMenubarMode,
UserDefaultKeys.installHomeIndicatorObject,
UserDefaultKeys.switchToCompactModeAlert,
])
let dictionaryRep = UserDefaults.standard.dictionaryRepresentation()
var clockerPrefs: [String: Any] = [:]

52
Clocker/Overall App/DataStore.swift

@ -37,7 +37,7 @@ class DataStore: NSObject {
}
init(with defaults: UserDefaults) {
cachedTimezones = (defaults.object(forKey: CLDefaultPreferenceKey) as? [Data]) ?? []
cachedTimezones = (defaults.object(forKey: UserDefaultKeys.defaultPreferenceKey) as? [Data]) ?? []
cachedMenubarTimezones = cachedTimezones.filter {
let customTimezone = TimezoneData.customObject(from: $0)
return customTimezone?.isFavourite == 1
@ -69,10 +69,10 @@ class DataStore: NSObject {
let userInfo = notification.userInfo ?? [:]
let ubiquitousStore = notification.object as? NSUbiquitousKeyValueStore
Logger.info("Ubiquitous Store Changed: User Info is \(userInfo)")
let currentTimezones = userDefaults.object(forKey: CLDefaultPreferenceKey) as? [Data]
let cloudTimezones = ubiquitousStore?.object(forKey: CLDefaultPreferenceKey) as? [Data]
let cloudLastUpdateDate = (ubiquitousStore?.object(forKey: CLUbiquitousStoreLastUpdateKey) as? Date) ?? Date()
let defaultsLastUpdateDate = (ubiquitousStore?.object(forKey: CLUserDefaultsLastUpdateKey) as? Date) ?? Date()
let currentTimezones = userDefaults.object(forKey: UserDefaultKeys.defaultPreferenceKey) as? [Data]
let cloudTimezones = ubiquitousStore?.object(forKey: UserDefaultKeys.defaultPreferenceKey) as? [Data]
let cloudLastUpdateDate = (ubiquitousStore?.object(forKey: UserDefaultKeys.ubiquitousStoreLastUpdateKey) as? Date) ?? Date()
let defaultsLastUpdateDate = (ubiquitousStore?.object(forKey: UserDefaultKeys.userDefaultsLastUpdateKey) as? Date) ?? Date()
if cloudTimezones == currentTimezones {
Logger.info("Ubiquitous Store timezones aren't equal to current timezones")
@ -84,8 +84,8 @@ class DataStore: NSObject {
if cloudTimezones != currentTimezones, cloudLastUpdateDate.isLaterThanOrEqual(to: defaultsLastUpdateDate) {
Logger.info("Syncing local timezones with data from the ☁. ☁ last update timestamp is recent")
userDefaults.set(cloudTimezones, forKey: CLDefaultPreferenceKey)
userDefaults.set(Date(), forKey: CLUserDefaultsLastUpdateKey)
userDefaults.set(cloudTimezones, forKey: UserDefaultKeys.defaultPreferenceKey)
userDefaults.set(Date(), forKey: UserDefaultKeys.userDefaultsLastUpdateKey)
NotificationCenter.default.post(name: DataStore.didSyncFromExternalSourceNotification,
object: self)
return
@ -97,16 +97,16 @@ class DataStore: NSObject {
}
func setTimezones(_ timezones: [Data]?) {
userDefaults.set(timezones, forKey: CLDefaultPreferenceKey)
userDefaults.set(Date(), forKey: CLUserDefaultsLastUpdateKey)
userDefaults.set(timezones, forKey: UserDefaultKeys.defaultPreferenceKey)
userDefaults.set(Date(), forKey: UserDefaultKeys.userDefaultsLastUpdateKey)
cachedTimezones = timezones ?? []
cachedMenubarTimezones = cachedTimezones.filter {
let customTimezone = TimezoneData.customObject(from: $0)
return customTimezone?.isFavourite == 1
}
// iCloud sync
ubiquitousStore?.set(timezones, forKey: CLDefaultPreferenceKey)
ubiquitousStore?.set(Date(), forKey: CLUbiquitousStoreLastUpdateKey)
ubiquitousStore?.set(timezones, forKey: UserDefaultKeys.defaultPreferenceKey)
ubiquitousStore?.set(Date(), forKey: UserDefaultKeys.ubiquitousStoreLastUpdateKey)
}
func menubarTimezones() -> [Data]? {
@ -114,7 +114,7 @@ class DataStore: NSObject {
}
func selectedCalendars() -> [String]? {
return userDefaults.array(forKey: CLSelectedCalendars) as? [String]
return userDefaults.array(forKey: UserDefaultKeys.selectedCalendars) as? [String]
}
// MARK: Date (May 8th) in Compact Menubar
@ -158,7 +158,7 @@ class DataStore: NSObject {
}
func timezoneFormat() -> NSNumber {
return userDefaults.object(forKey: CLSelectedTimeZoneFormatKey) as? NSNumber ?? NSNumber(integerLiteral: 0)
return userDefaults.object(forKey: UserDefaultKeys.selectedTimeZoneFormatKey) as? NSNumber ?? NSNumber(integerLiteral: 0)
}
func isBufferRequiredForTwelveHourFormats() -> Bool {
@ -168,44 +168,44 @@ class DataStore: NSObject {
func shouldDisplay(_ type: ViewType) -> Bool {
switch type {
case .futureSlider:
guard let value = retrieve(key: CLDisplayFutureSliderKey) as? NSNumber else {
guard let value = retrieve(key: UserDefaultKeys.displayFutureSliderKey) as? NSNumber else {
return false
}
return value != 1 // Display slider is 0 and Hide is 1.
case .upcomingEventView:
guard let value = retrieve(key: CLShowUpcomingEventView) as? NSString else {
guard let value = retrieve(key: UserDefaultKeys.showUpcomingEventView) as? NSString else {
return false
}
return value == "YES"
case .twelveHour:
return shouldDisplayHelper(CLSelectedTimeZoneFormatKey)
return shouldDisplayHelper(UserDefaultKeys.selectedTimeZoneFormatKey)
case .showAllDayEventsInMenubar:
return shouldDisplayHelper(CLShowAllDayEventsInUpcomingView)
return shouldDisplayHelper(UserDefaultKeys.showAllDayEventsInUpcomingView)
case .sunrise:
return shouldDisplayHelper(CLSunriseSunsetTime)
return shouldDisplayHelper(UserDefaultKeys.sunriseSunsetTime)
case .showMeetingInMenubar:
return shouldDisplayHelper(CLShowMeetingInMenubar)
return shouldDisplayHelper(UserDefaultKeys.showMeetingInMenubar)
case .showAppInForeground:
guard let value = retrieve(key: CLShowAppInForeground) as? NSNumber else {
guard let value = retrieve(key: UserDefaultKeys.showAppInForeground) as? NSNumber else {
return false
}
return value.isEqual(to: NSNumber(value: 1))
case .dateInMenubar:
return shouldDisplayNonObjectHelper(CLShowDateInMenu)
return shouldDisplayNonObjectHelper(UserDefaultKeys.showDateInMenu)
case .placeInMenubar:
return shouldDisplayHelper(CLShowPlaceInMenu)
return shouldDisplayHelper(UserDefaultKeys.showPlaceInMenu)
case .dayInMenubar:
return shouldDisplayNonObjectHelper(CLShowDayInMenu)
return shouldDisplayNonObjectHelper(UserDefaultKeys.showDayInMenu)
case .appDisplayOptions:
return shouldDisplayHelper(CLAppDisplayOptions)
return shouldDisplayHelper(UserDefaultKeys.appDisplayOptions)
case .menubarCompactMode:
guard let value = retrieve(key: CLMenubarCompactMode) as? Int else {
guard let value = retrieve(key: UserDefaultKeys.menubarCompactMode) as? Int else {
return false
}
return value == 0
case .sync:
return shouldDisplayHelper(CLEnableSyncKey)
return shouldDisplayHelper(UserDefaultKeys.enableSyncKey)
}
}

79
Clocker/Overall App/Strings.swift

@ -2,42 +2,45 @@
import Cocoa
let CLEmptyString = ""
let CLDefaultPreferenceKey = "defaultPreferences"
let CLTimezoneName = "formattedAddress"
let CLCustomLabel = "customLabel"
let CLSelectedTimeZoneFormatKey = "is24HourFormatSelected"
let CLDragSessionKey = "public.text"
let CLTimezoneID = "timezoneID"
let CLPlaceIdentifier = "place_id"
let CLRelativeDateKey = "relativeDate"
let CLThemeKey = "defaultTheme"
let CLShowDayInMenu = "showDay"
let CLShowDateInMenu = "showDate"
let CLShowPlaceInMenu = "showPlaceName"
let CLDisplayFutureSliderKey = "displayFutureSlider"
let CLStartAtLogin = "startAtLogin"
let CLShowAppInForeground = "displayAppAsForegroundApp"
let CLSunriseSunsetTime = "showSunriseSetTime"
let CLUserFontSizePreference = "userFontSize"
let CLShowUpcomingEventView = "ShowUpcomingEventView"
let CLShowAllDayEventsInUpcomingView = "showAllDayEventsInUpcomingView"
let CLShowMeetingInMenubar = "showMeetingInfoInMenubar"
let CLTruncateTextLength = "truncateTextLength"
let CLFutureSliderRange = "sliderDayRange"
let CLSelectedCalendars = "SelectedCalendars"
let CLShowOnboardingFlow = "com.abhishek.showOnboardingFlow"
let CLAppDisplayOptions = "com.abhishek.appDisplayOptions"
let CLLongStatusBarWarningMessage = "com.abhishek.longStatusBarWarning"
let CLUITestingLaunchArgument = "isUITesting"
let CLOnboardingTestsLaunchArgument = "isTestingTheOnboardingFlow"
let CLMenubarCompactMode = "com.abhishek.menubarCompactMode"
let CLDefaultMenubarMode = "com.abhishek.shouldDefaultToCompactMode"
let CLInstallHomeIndicatorObject = "installHomeIndicatorObject"
let CLSwitchToCompactModeAlert = "com.abhishek.switchToCompactMode"
let CLAppleInterfaceStyleKey = "AppleInterfaceStyle"
public enum UserDefaultKeys {
static let emptyString = ""
static let defaultPreferenceKey = "defaultPreferences"
static let timezoneName = "formattedAddress"
static let customLabel = "customLabel"
static let selectedTimeZoneFormatKey = "is24HourFormatSelected"
static let dragSessionKey = "public.text"
static let timezoneID = "timezoneID"
static let placeIdentifier = "place_id"
static let relativeDateKey = "relativeDate"
static let themeKey = "defaultTheme"
static let showDayInMenu = "showDay"
static let showDateInMenu = "showDate"
static let showPlaceInMenu = "showPlaceName"
static let displayFutureSliderKey = "displayFutureSlider"
static let startAtLogin = "startAtLogin"
static let showAppInForeground = "displayAppAsForegroundApp"
static let sunriseSunsetTime = "showSunriseSetTime"
static let userFontSizePreference = "userFontSize"
static let showUpcomingEventView = "ShowUpcomingEventView"
static let showAllDayEventsInUpcomingView = "showAllDayEventsInUpcomingView"
static let showMeetingInMenubar = "showMeetingInfoInMenubar"
static let truncateTextLength = "truncateTextLength"
static let futureSliderRange = "sliderDayRange"
static let selectedCalendars = "SelectedCalendars"
static let showOnboardingFlow = "com.abhishek.showOnboardingFlow"
static let appDisplayOptions = "com.abhishek.appDisplayOptions"
static let longStatusBarWarningMessage = "com.abhishek.longStatusBarWarning"
static let testingLaunchArgument = "isUITesting"
static let onboardingTestsLaunchArgument = "isTestingTheOnboardingFlow"
static let menubarCompactMode = "com.abhishek.menubarCompactMode"
static let defaultMenubarMode = "com.abhishek.shouldDefaultToCompactMode"
static let installHomeIndicatorObject = "installHomeIndicatorObject"
static let switchToCompactModeAlert = "com.abhishek.switchToCompactMode"
static let appleInterfaceStyleKey = "AppleInterfaceStyle"
// Sync Keys
let CLEnableSyncKey = "com.abhishek.enableSync"
let CLUbiquitousStoreLastUpdateKey = "com.abhishek.ubiquitousLastUpdateKey"
let CLUserDefaultsLastUpdateKey = "com.abhishek.defaultsLastUpdateKey"
static let enableSyncKey = "com.abhishek.enableSync"
static let ubiquitousStoreLastUpdateKey = "com.abhishek.ubiquitousLastUpdateKey"
static let userDefaultsLastUpdateKey = "com.abhishek.defaultsLastUpdateKey"
}

4
Clocker/Overall App/Themer.swift

@ -16,7 +16,7 @@ class Themer: NSObject {
case solarizedDark
}
private static var sharedInstance = Themer(index: UserDefaults.standard.integer(forKey: CLThemeKey))
private static var sharedInstance = Themer(index: UserDefaults.standard.integer(forKey: UserDefaultKeys.themeKey))
private var effectiveApperanceObserver: NSKeyValueObservation?
private var themeIndex: Theme {
didSet {
@ -447,7 +447,7 @@ extension Themer {
private func retrieveCurrentSystem() -> Theme {
if #available(OSX 10.14, *) {
if let appleInterfaceStyle = UserDefaults.standard.object(forKey: CLAppleInterfaceStyleKey) as? String {
if let appleInterfaceStyle = UserDefaults.standard.object(forKey: UserDefaultKeys.appleInterfaceStyleKey) as? String {
if appleInterfaceStyle.lowercased().contains("dark") {
return .dark
}

Loading…
Cancel
Save