Browse Source

UserDefaults consolidation.

pull/113/head
Abhishek Banthia 3 years ago
parent
commit
a61da91db2
  1. 21
      Clocker/ClockerUnitTests/ClockerUnitTests.swift
  2. 6
      Clocker/ClockerUnitTests/StandardMenubarHandlerTests.swift
  3. 2
      Clocker/Overall App/AppDefaults.swift
  4. 9
      Clocker/Overall App/DataStore.swift

21
Clocker/ClockerUnitTests/ClockerUnitTests.swift

@ -94,18 +94,16 @@ class ClockerUnitTests: XCTestCase {
func testAddingATimezoneToDefaults() { func testAddingATimezoneToDefaults() {
let timezoneData = TimezoneData(with: california) let timezoneData = TimezoneData(with: california)
let currentFavourites = DataStore.shared().timezones()
let defaults = UserDefaults.standard
let currentFavourites = (defaults.object(forKey: CLDefaultPreferenceKey) as? [Data]) ?? []
let oldCount = currentFavourites.count let oldCount = currentFavourites.count
let operationsObject = TimezoneDataOperations(with: timezoneData) let operationsObject = TimezoneDataOperations(with: timezoneData)
operationsObject.saveObject() operationsObject.saveObject()
let newDefaults = UserDefaults.standard.object(forKey: CLDefaultPreferenceKey) as? [Data] let newDefaults = DataStore.shared().timezones()
XCTAssert(newDefaults != nil) XCTAssert(newDefaults.isEmpty == false)
XCTAssert(newDefaults?.count == oldCount + 1) XCTAssert(newDefaults.count == oldCount + 1)
} }
func testDecoding() { func testDecoding() {
@ -143,12 +141,7 @@ class ClockerUnitTests: XCTestCase {
} }
func testDeletingATimezone() { func testDeletingATimezone() {
let defaults = UserDefaults.standard var currentFavourites = DataStore.shared().timezones()
guard var currentFavourites = defaults.object(forKey: CLDefaultPreferenceKey) as? [Data] else {
XCTFail("Default preferences aren't in the correct format")
return
}
// Check if timezone with test identifier is present. // Check if timezone with test identifier is present.
let filteredCount = currentFavourites.filter { let filteredCount = currentFavourites.filter {
let timezone = TimezoneData.customObject(from: $0) let timezone = TimezoneData.customObject(from: $0)
@ -162,14 +155,14 @@ class ClockerUnitTests: XCTestCase {
operationsObject.saveObject() operationsObject.saveObject()
} }
let oldCount = (defaults.object(forKey: CLDefaultPreferenceKey) as? [Data])?.count ?? 0 let oldCount = DataStore.shared().timezones().count
currentFavourites = currentFavourites.filter { currentFavourites = currentFavourites.filter {
let timezone = TimezoneData.customObject(from: $0) let timezone = TimezoneData.customObject(from: $0)
return timezone?.placeID != "TestIdentifier" return timezone?.placeID != "TestIdentifier"
} }
defaults.set(currentFavourites, forKey: CLDefaultPreferenceKey) DataStore.shared().setTimezones(currentFavourites)
XCTAssertTrue(currentFavourites.count == oldCount - 1) XCTAssertTrue(currentFavourites.count == oldCount - 1)
} }

6
Clocker/ClockerUnitTests/StandardMenubarHandlerTests.swift

@ -16,7 +16,7 @@ class StandardMenubarHandlerTests: XCTestCase {
func testValidStandardMenubarHandler_returnMenubarTitle() { func testValidStandardMenubarHandler_returnMenubarTitle() {
// Wipe all timezones from UserDefaults // Wipe all timezones from UserDefaults
UserDefaults.standard.setValue(nil, forKey: CLDefaultPreferenceKey) DataStore.shared().setTimezones(nil)
// Save a menubar selected timezone // Save a menubar selected timezone
let dataObject = TimezoneData(with: mumbai) let dataObject = TimezoneData(with: mumbai)
@ -43,7 +43,7 @@ class StandardMenubarHandlerTests: XCTestCase {
func testUnfavouritedTimezone_returnEmptyMenubarTimezoneCount() { func testUnfavouritedTimezone_returnEmptyMenubarTimezoneCount() {
// Wipe all timezones from UserDefaults // Wipe all timezones from UserDefaults
UserDefaults.standard.setValue(nil, forKey: CLDefaultPreferenceKey) DataStore.shared().setTimezones(nil)
// Save a menubar selected timezone // Save a menubar selected timezone
let dataObject = TimezoneData(with: mumbai) let dataObject = TimezoneData(with: mumbai)
@ -57,7 +57,7 @@ class StandardMenubarHandlerTests: XCTestCase {
func testUnfavouritedTimezone_returnNilMenubarString() { func testUnfavouritedTimezone_returnNilMenubarString() {
// Wipe all timezones from UserDefaults // Wipe all timezones from UserDefaults
UserDefaults.standard.setValue(nil, forKey: CLDefaultPreferenceKey) DataStore.shared().setTimezones(nil)
let menubarHandler = MenubarHandler() let menubarHandler = MenubarHandler()
let emptyMenubarString = menubarHandler.titleForMenubar() let emptyMenubarString = menubarHandler.titleForMenubar()
// Returns early because DataStore.menubarTimezones is nil // Returns early because DataStore.menubarTimezones is nil

2
Clocker/Overall App/AppDefaults.swift

@ -31,7 +31,7 @@ class AppDefaults {
// Register the usual suspects // Register the usual suspects
userDefaults.register(defaults: defaultsDictionary()) userDefaults.register(defaults: defaultsDictionary())
userDefaults.set(timezones, forKey: CLDefaultPreferenceKey) dataStore.setTimezones(timezones)
userDefaults.set(selectedCalendars, forKey: CLSelectedCalendars) userDefaults.set(selectedCalendars, forKey: CLSelectedCalendars)
// Set the theme default as Light! // Set the theme default as Light!

9
Clocker/Overall App/DataStore.swift

@ -70,7 +70,7 @@ class DataStore: NSObject {
return shouldDisplayDateInMenubar return shouldDisplayDateInMenubar
} }
func setTimezones(_ timezones: [Data]) { func setTimezones(_ timezones: [Data]?) {
userDefaults.set(timezones, forKey: CLDefaultPreferenceKey) userDefaults.set(timezones, forKey: CLDefaultPreferenceKey)
} }
@ -81,10 +81,9 @@ class DataStore: NSObject {
func addTimezone(_ timezone: TimezoneData) { func addTimezone(_ timezone: TimezoneData) {
let encodedTimezone = NSKeyedArchiver.archivedData(withRootObject: timezone) let encodedTimezone = NSKeyedArchiver.archivedData(withRootObject: timezone)
var defaults: [Data] = (userDefaults.object(forKey: CLDefaultPreferenceKey) as? [Data]) ?? [] var defaults: [Data] = timezones()
defaults.append(encodedTimezone) defaults.append(encodedTimezone)
setTimezones(defaults)
userDefaults.set(defaults, forKey: CLDefaultPreferenceKey)
} }
func removeLastTimezone() { func removeLastTimezone() {
@ -98,7 +97,7 @@ class DataStore: NSObject {
Logger.log(object: [:], for: "Undo Action Executed during Onboarding") Logger.log(object: [:], for: "Undo Action Executed during Onboarding")
userDefaults.set(currentLineup, forKey: CLDefaultPreferenceKey) setTimezones(currentLineup)
} }
private func shouldDisplayHelper(_ key: String) -> Bool { private func shouldDisplayHelper(_ key: String) -> Bool {

Loading…
Cancel
Save