forked from Automattic/simplenote-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Options.swift
82 lines (66 loc) · 1.76 KB
/
Options.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
// Options.swift
// Simplenote
//
// Copyright © 2019 Automattic. All rights reserved.
//
import Foundation
// MARK: - Wraps access to all of the UserDefault Values
//
class Options: NSObject {
/// Shared Instance
///
@objc
static let shared = Options()
/// User Defaults: Convenience
///
private var defaults: UserDefaults {
return UserDefaults.standard
}
/// Designated Initializer
///
override private init() {
super.init()
migrateLegacyOptions()
}
}
// MARK: - Actual Options!
//
extension Options {
/// Returns the target Sort Mode for the Notes List
///
@objc
var listSortMode: SortMode {
get {
let payload = defaults.integer(forKey: .listSortMode)
return SortMode(rawValue: payload) ?? .alphabeticallyAscending
}
set {
defaults.set(newValue.rawValue, forKey: .listSortMode)
SPTracker.trackSettingsNoteListSortMode(newValue.description)
NotificationCenter.default.post(name: .SPNotesListSortModeChanged, object: nil)
}
}
}
// MARK: - ObjC Convenience Methods
//
extension Options {
/// Returns the *Description* for the current List's Sort Mode
///
@objc
var listSortModeDescription: String {
return listSortMode.description
}
}
// MARK: - Private
//
private extension Options {
func migrateLegacyOptions() {
guard let legacySortAscending: Bool = defaults[.listSortModeLegacy] else {
return
}
let newMode: SortMode = legacySortAscending ? .alphabeticallyAscending : .alphabeticallyDescending
defaults.set(newMode.rawValue, forKey: .listSortMode)
defaults.removeObject(forKey: .listSortModeLegacy)
}
}