Skip to content

Commit

Permalink
Register default prefs in app delegate (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
pakerwreah authored Aug 8, 2024
1 parent 434d827 commit 30659e3
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 32 deletions.
4 changes: 2 additions & 2 deletions Calendr.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.11.6;
MARKETING_VERSION = 1.11.7;
PRODUCT_BUNDLE_IDENTIFIER = br.paker.Calendr;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Calendr/Config/Calendr-Bridging-Header.h";
Expand All @@ -1340,7 +1340,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.11.6;
MARKETING_VERSION = 1.11.7;
PRODUCT_BUNDLE_IDENTIFIER = br.paker.Calendr;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Calendr/Config/Calendr-Bridging-Header.h";
Expand Down
27 changes: 27 additions & 0 deletions Calendr/Main/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let notificationCenter = NotificationCenter.default
let workspace = NSWorkspace.shared

registerDefaultPrefs(in: userDefaults)

let dateProvider = DateProvider(notificationCenter: notificationCenter, userDefaults: userDefaults)

viewController = MainViewController(
Expand All @@ -42,3 +44,28 @@ class AppDelegate: NSObject, NSApplicationDelegate {
setUpResignFocus()
}
}

func registerDefaultPrefs(in userDefaults: UserDefaults, calendar: Calendar = .current) {

userDefaults.register(defaults: [
Prefs.statusItemIconEnabled: true,
Prefs.statusItemDateEnabled: true,
Prefs.statusItemBackgroundEnabled: false,
Prefs.statusItemIconStyle: StatusItemIconStyle.calendar.rawValue,
Prefs.statusItemDateStyle: StatusItemDateStyle.short.rawValue,
Prefs.statusItemDateFormat: "E d MMM yyyy",
Prefs.showEventStatusItem: false,
Prefs.eventStatusItemFontSize: 12,
Prefs.eventStatusItemCheckRange: 6,
Prefs.eventStatusItemLength: 18,
Prefs.eventStatusItemDetectNotch: false,
Prefs.calendarScaling: 1,
Prefs.firstWeekday: calendar.firstWeekday,
Prefs.highlightedWeekdays: [0, 6],
Prefs.showWeekNumbers: false,
Prefs.showDeclinedEvents: false,
Prefs.preserveSelectedDate: false,
Prefs.showPastEvents: true,
Prefs.transparencyLevel: 2
])
}
2 changes: 2 additions & 0 deletions Calendr/Main/MockMainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class MockMainViewController: MainViewController {
userDefaults.setVolatileDomain([:], forName: UserDefaults.registrationDomain)
userDefaults.removePersistentDomain(forName: Self.className())

registerDefaultPrefs(in: userDefaults)

userDefaults.setValuesForKeys([
Prefs.firstWeekday: dateProvider.calendar.firstWeekday,
Prefs.statusItemDateStyle: DateFormatter.Style.full.rawValue,
Expand Down
2 changes: 1 addition & 1 deletion Calendr/Settings/Prefs+UserDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ extension UserDefaults {
}

@objc dynamic var highlightedWeekdays: [Int] {
get { array(forKey: Prefs.highlightedWeekdays) as! [Int] }
get { array(forKey: Prefs.highlightedWeekdays) as? [Int] ?? [] }
set { set(newValue, forKey: Prefs.highlightedWeekdays) }
}
}
Expand Down
23 changes: 0 additions & 23 deletions Calendr/Settings/SettingsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,29 +143,6 @@ class SettingsViewModel: StatusItemSettings, NextEventSettings, CalendarSettings
userDefaults: UserDefaults,
notificationCenter: NotificationCenter
) {

userDefaults.register(defaults: [
Prefs.statusItemIconEnabled: true,
Prefs.statusItemDateEnabled: true,
Prefs.statusItemBackgroundEnabled: false,
Prefs.statusItemIconStyle: StatusItemIconStyle.calendar.rawValue,
Prefs.statusItemDateStyle: StatusItemDateStyle.short.rawValue,
Prefs.statusItemDateFormat: dateFormatPlaceholder,
Prefs.showEventStatusItem: false,
Prefs.eventStatusItemFontSize: 12,
Prefs.eventStatusItemCheckRange: 6,
Prefs.eventStatusItemLength: 18,
Prefs.eventStatusItemDetectNotch: false,
Prefs.calendarScaling: 1,
Prefs.firstWeekday: dateProvider.calendar.firstWeekday,
Prefs.highlightedWeekdays: [0, 6],
Prefs.showWeekNumbers: false,
Prefs.showDeclinedEvents: false,
Prefs.preserveSelectedDate: false,
Prefs.showPastEvents: true,
Prefs.transparencyLevel: 2
])

// MARK: - Observers

toggleAutoLaunch = autoLauncher.rx.observer(for: \.isEnabled)
Expand Down
12 changes: 6 additions & 6 deletions CalendrTests/SettingsViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class SettingsViewModelTests: XCTestCase {
var userDefaultsStatusItemBackgroundEnabled: Bool? { userDefaults.object(forKey: Prefs.statusItemBackgroundEnabled) as! Bool? }
var userDefaultsStatusItemDateStyle: Int? { userDefaults.object(forKey: Prefs.statusItemDateStyle) as! Int? }
var userDefaultsShowEventStatusItem: Bool? { userDefaults.object(forKey: Prefs.showEventStatusItem) as! Bool? }
var userDefaultsEventStatusItemFontSize: Float? { userDefaults.object(forKey: Prefs.eventStatusItemFontSize) as! Float? }
var userDefaultsEventStatusItemCheckRange: Int? { userDefaults.object(forKey: Prefs.eventStatusItemCheckRange) as! Int? }
var userDefaultsEventStatusItemFontSize: Float? { userDefaults.object(forKey: Prefs.eventStatusItemFontSize) as! Float? }
var userDefaultsEventStatusItemLength: Int? { userDefaults.object(forKey: Prefs.eventStatusItemLength) as! Int? }
var userDefaultsEventStatusItemDetectNotch: Bool? { userDefaults.object(forKey: Prefs.eventStatusItemDetectNotch) as! Bool? }
var userDefaultsCalendarScaling: Double? { userDefaults.object(forKey: Prefs.calendarScaling) as! Double? }
Expand All @@ -46,19 +46,16 @@ class SettingsViewModelTests: XCTestCase {
override func setUp() {
userDefaults.setVolatileDomain([:], forName: UserDefaults.registrationDomain)
userDefaults.removePersistentDomain(forName: className)
}

func testDefaultSettings() {

XCTAssertNil(userDefaultsStatusItemIconEnabled)
XCTAssertNil(userDefaultsStatusItemDateEnabled)
XCTAssertNil(userDefaultsStatusItemBackgroundEnabled)
XCTAssertNil(userDefaultsStatusItemDateStyle)
XCTAssertNil(userDefaultsShowEventStatusItem)
XCTAssertNil(userDefaultsEventStatusItemCheckRange)
XCTAssertNil(userDefaultsEventStatusItemFontSize)
XCTAssertNil(userDefaultsEventStatusItemLength)
XCTAssertNil(userDefaultsEventStatusItemDetectNotch)
XCTAssertNil(userDefaultsEventStatusItemFontSize)
XCTAssertNil(userDefaultsCalendarScaling)
XCTAssertNil(userDefaultsFirstWeekday)
XCTAssertNil(userDefaultsHighlightedWeekdays)
Expand All @@ -68,7 +65,10 @@ class SettingsViewModelTests: XCTestCase {
XCTAssertNil(userDefaultsShowPastEvents)
XCTAssertNil(userDefaultsTransparency)

dateProvider.m_calendar.firstWeekday = 3
registerDefaultPrefs(in: userDefaults, calendar: .gregorian.with(firstWeekday: 3))
}

func testDefaultSettings() {

var showStatusItemIcon: Bool?
var showStatusItemDate: Bool?
Expand Down

0 comments on commit 30659e3

Please sign in to comment.