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

6
Clocker/ClockerUnitTests/StandardMenubarHandlerTests.swift

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

2
Clocker/Overall App/AppDefaults.swift

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

9
Clocker/Overall App/DataStore.swift

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

Loading…
Cancel
Save