Skip to content

Commit c98be2b

Browse files
committed
Add UserPersistentRepositoryUtility and refactor utility variables
1 parent ad7633a commit c98be2b

13 files changed

Lines changed: 115 additions & 118 deletions

WordPress/Classes/Stores/UserPersistentRepository.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
protocol UserPersistentRepositoryReader {
2-
func object(forKey key: String) -> Any?
32
func string(forKey key: String) -> String?
43
func bool(forKey key: String) -> Bool
54
func integer(forKey key: String) -> Int
@@ -10,7 +9,7 @@ protocol UserPersistentRepositoryReader {
109
func url(forKey key: String) -> URL?
1110
}
1211

13-
protocol UserPersistentRepositoryWriter {
12+
protocol UserPersistentRepositoryWriter: KeyValueDatabase {
1413
func set(_ value: Any?, forKey key: String)
1514
func set(_ value: Int, forKey key: String)
1615
func set(_ value: Float, forKey key: String)
@@ -20,7 +19,7 @@ protocol UserPersistentRepositoryWriter {
2019
func removeObject(forKey key: String)
2120
}
2221

23-
typealias UserPersistentRepository = UserPersistentRepositoryReader & UserPersistentRepositoryWriter
22+
typealias UserPersistentRepository = UserPersistentRepositoryReader & UserPersistentRepositoryWriter & UserPersistentRepositoryUtility
2423

2524
extension UserDefaults: UserPersistentRepository {
2625
private static var isOneOffMigrationCompleteKey: String {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import Foundation
2+
3+
private enum UPRUConstants {
4+
static let promptKey = "onboarding_notifications_prompt_displayed"
5+
static let questionKey = "onboarding_question_selection"
6+
static let notificationPrimerAlertWasDisplayed = "NotificationPrimerAlertWasDisplayed"
7+
static let notificationsTabAccessCount = "NotificationsTabAccessCount"
8+
static let notificationPrimerInlineWasAcknowledged = "notificationPrimerInlineWasAcknowledged"
9+
static let secondNotificationsAlertCount = "secondNotificationsAlertCount"
10+
}
11+
12+
protocol UserPersistentRepositoryUtility: AnyObject {
13+
var onboardingNotificationsPromptDisplayed: Bool { get set }
14+
var onboardingQuestionSelected: OnboardingOption? { get set }
15+
var notificationPrimerAlertWasDisplayed: Bool { get set }
16+
}
17+
18+
extension UserPersistentRepositoryUtility {
19+
var onboardingNotificationsPromptDisplayed: Bool {
20+
get {
21+
UserPersistentStoreFactory.instance().bool(forKey: UPRUConstants.promptKey)
22+
}
23+
set {
24+
UserPersistentStoreFactory.instance().set(newValue, forKey: UPRUConstants.promptKey)
25+
}
26+
}
27+
28+
var onboardingQuestionSelected: OnboardingOption? {
29+
get {
30+
if let str = UserPersistentStoreFactory.instance().string(forKey: UPRUConstants.questionKey) {
31+
return OnboardingOption(rawValue: str)
32+
}
33+
34+
return nil
35+
}
36+
set {
37+
UserPersistentStoreFactory.instance().set(newValue?.rawValue, forKey: UPRUConstants.questionKey)
38+
}
39+
}
40+
41+
var notificationPrimerAlertWasDisplayed: Bool {
42+
get {
43+
UserPersistentStoreFactory.instance().bool(forKey: UPRUConstants.notificationPrimerAlertWasDisplayed)
44+
}
45+
set {
46+
UserPersistentStoreFactory.instance().set(newValue, forKey: UPRUConstants.notificationPrimerAlertWasDisplayed)
47+
}
48+
}
49+
50+
var notificationsTabAccessCount: Int {
51+
get {
52+
UserPersistentStoreFactory.instance().integer(forKey: UPRUConstants.notificationsTabAccessCount)
53+
}
54+
55+
set {
56+
UserPersistentStoreFactory.instance().set(newValue, forKey: UPRUConstants.notificationsTabAccessCount)
57+
}
58+
}
59+
60+
var welcomeNotificationSeenKey: String {
61+
return "welcomeNotificationSeen"
62+
}
63+
64+
var notificationPrimerInlineWasAcknowledged: Bool {
65+
get {
66+
return UserPersistentStoreFactory.instance().bool(forKey: UPRUConstants.notificationPrimerInlineWasAcknowledged)
67+
}
68+
set {
69+
UserPersistentStoreFactory.instance().set(newValue, forKey: UPRUConstants.notificationPrimerInlineWasAcknowledged)
70+
}
71+
}
72+
73+
var secondNotificationsAlertCount: Int {
74+
get {
75+
UserPersistentStoreFactory.instance().integer(forKey: UPRUConstants.secondNotificationsAlertCount)
76+
}
77+
set {
78+
UserPersistentStoreFactory.instance().set(newValue, forKey: UPRUConstants.secondNotificationsAlertCount)
79+
}
80+
}
81+
}

WordPress/Classes/System/WordPressAppDelegate.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ class WordPressAppDelegate: UIResponder, UIApplicationDelegate {
227227

228228
func application(_ application: UIApplication, shouldRestoreApplicationState coder: NSCoder) -> Bool {
229229
let lastSavedStateVersionKey = "lastSavedStateVersionKey"
230-
let defaults = UserDefaults.standard
230+
let defaults = UserPersistentStoreFactory.instance()
231231

232232
var shouldRestoreApplicationState = false
233233

@@ -237,9 +237,8 @@ class WordPressAppDelegate: UIResponder, UIApplicationDelegate {
237237
shouldRestoreApplicationState = self.shouldRestoreApplicationState
238238
}
239239

240-
defaults.setValue(currentVersion, forKey: lastSavedStateVersionKey)
240+
defaults.set(currentVersion, forKey: lastSavedStateVersionKey)
241241
}
242-
243242
return shouldRestoreApplicationState
244243
}
245244

WordPress/Classes/Utility/Migrations/20-21/AccountToAccount20to21.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class AccountToAccount20to21: NSEntityMigrationPolicy {
1818
if let unwrappedAccount = legacyDefaultWordPressAccount(manager.sourceContext) {
1919
let username = unwrappedAccount.value(forKey: "username") as! String
2020

21-
let userDefaults = UserDefaults.standard
22-
userDefaults.setValue(username, forKey: defaultDotcomUsernameKey)
21+
let userDefaults = UserPersistentStoreFactory.instance()
22+
userDefaults.set(username, forKey: defaultDotcomUsernameKey)
2323

2424
DDLogWarn(">> Migration process matched [\(username)] as the default WordPress.com account")
2525
} else {
@@ -29,7 +29,7 @@ class AccountToAccount20to21: NSEntityMigrationPolicy {
2929

3030
override func end(_ mapping: NSEntityMapping, manager: NSMigrationManager) throws {
3131
// Load the default username
32-
let userDefaults = UserDefaults.standard
32+
let userDefaults = UserPersistentStoreFactory.instance()
3333
let defaultUsername = userDefaults.string(forKey: defaultDotcomUsernameKey) ?? String()
3434

3535
// Find the Default Account
@@ -88,7 +88,7 @@ class AccountToAccount20to21: NSEntityMigrationPolicy {
8888

8989
let accountURL = account.objectID.uriRepresentation()
9090

91-
let defaults = UserDefaults.standard
91+
let defaults = UserPersistentStoreFactory.instance()
9292
defaults.set(accountURL, forKey: defaultDotcomKey)
9393
}
9494
}

WordPress/Classes/Utility/Migrations/AccountToAccount22to23.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class AccountToAccount22to23: NSEntityMigrationPolicy {
3131
}
3232

3333
if isDotCom! == true {
34-
let userDefaults = UserDefaults.standard
35-
userDefaults.setValue(username!, forKey: defaultDotcomUsernameKey)
34+
let userDefaults = UserPersistentStoreFactory.instance()
35+
userDefaults.set(username!, forKey: defaultDotcomUsernameKey)
3636

3737
DDLogWarn(">> Migration process matched [\(username!)] as the default WordPress.com account")
3838
} else {
@@ -74,7 +74,7 @@ class AccountToAccount22to23: NSEntityMigrationPolicy {
7474
}
7575

7676
// Set the defaultAccount (if any)
77-
let userDefaults = UserDefaults.standard
77+
let userDefaults = UserPersistentStoreFactory.instance()
7878

7979
if defaultAccount != nil {
8080
let uuid = defaultAccount!.value(forKey: "uuid") as! String
@@ -142,7 +142,7 @@ class AccountToAccount22to23: NSEntityMigrationPolicy {
142142
return
143143
}
144144

145-
let defaults = UserDefaults.standard
145+
let defaults = UserPersistentStoreFactory.instance()
146146
defaults.set(uuid, forKey: defaultDotcomUUIDKey)
147147
}
148148

WordPress/Classes/ViewRelated/Blog/My Site/MySiteSettings.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ protocol DefaultSectionProvider {
99
///
1010
@objc final class MySiteSettings: NSObject, DefaultSectionProvider {
1111

12-
private var userDefaults: UserDefaults {
13-
UserDefaults.standard
12+
private var userDefaults: UserPersistentRepository {
13+
UserPersistentStoreFactory.instance()
1414
}
1515

1616
var defaultSection: MySiteViewController.Section {

WordPress/Classes/ViewRelated/Blog/Onboarding Questions Prompt/OnboardingQuestionsCoordinator.swift

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extension OnboardingQuestionsCoordinator {
5151
}
5252

5353
track(.onboardingQuestionsItemSelected, option: option)
54-
UserDefaults.standard.onboardingQuestionSelected = option
54+
UserPersistentStoreFactory.instance().onboardingQuestionSelected = option
5555

5656
// Check if notification's are already enabled
5757
// If they are just dismiss, if not then prompt
@@ -73,7 +73,7 @@ extension OnboardingQuestionsCoordinator {
7373
extension OnboardingQuestionsCoordinator {
7474
func notificationsDisplayed(option: OnboardingOption) {
7575
track(.onboardingEnableNotificationsDisplayed, option: option)
76-
UserDefaults.standard.onboardingNotificationsPromptDisplayed = true
76+
UserPersistentStoreFactory.instance().onboardingNotificationsPromptDisplayed = true
7777
}
7878

7979
func notificationsEnabledTapped(selection: OnboardingOption) {
@@ -91,31 +91,3 @@ extension OnboardingQuestionsCoordinator {
9191
dismiss(selection: selection)
9292
}
9393
}
94-
95-
96-
extension UserDefaults {
97-
private static let promptKey = "onboarding_notifications_prompt_displayed"
98-
private static let questionKey = "onboarding_question_selection"
99-
100-
var onboardingNotificationsPromptDisplayed: Bool {
101-
get {
102-
bool(forKey: Self.promptKey)
103-
}
104-
set {
105-
set(newValue, forKey: Self.promptKey)
106-
}
107-
}
108-
109-
var onboardingQuestionSelected: OnboardingOption? {
110-
get {
111-
if let str = string(forKey: Self.questionKey) {
112-
return OnboardingOption(rawValue: str)
113-
}
114-
115-
return nil
116-
}
117-
set {
118-
set(newValue?.rawValue, forKey: Self.questionKey)
119-
}
120-
}
121-
}

WordPress/Classes/ViewRelated/Feature Highlight/FeatureHighlightStore.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@ struct FeatureHighlightStore {
66
static let followConversationTooltipCounterKey = "follow-conversation-tooltip-counter"
77
}
88

9-
private let userDefaults: UserDefaults
9+
private let userStore: UserPersistentRepository
1010

11-
init(userDefaults: UserDefaults = UserDefaults.standard) {
12-
self.userDefaults = userDefaults
11+
init(userStore: UserPersistentRepository = UserPersistentStoreFactory.instance()) {
12+
self.userStore = userStore
1313
}
1414

1515
var didDismissTooltip: Bool {
1616
get {
17-
return userDefaults.bool(forKey: Keys.didUserDismissTooltipKey)
17+
return userStore.bool(forKey: Keys.didUserDismissTooltipKey)
1818
}
1919
set {
20-
userDefaults.set(newValue, forKey: Keys.didUserDismissTooltipKey)
20+
userStore.set(newValue, forKey: Keys.didUserDismissTooltipKey)
2121
}
2222
}
2323

2424
var followConversationTooltipCounter: Int {
2525
get {
26-
return userDefaults.integer(forKey: Keys.followConversationTooltipCounterKey)
26+
return userStore.integer(forKey: Keys.followConversationTooltipCounterKey)
2727
}
2828
set {
29-
userDefaults.set(newValue, forKey: Keys.followConversationTooltipCounterKey)
29+
userStore.set(newValue, forKey: Keys.followConversationTooltipCounterKey)
3030
}
3131
}
3232

WordPress/Classes/ViewRelated/Notifications/Controllers/NotificationsViewController+PushPrimer.swift

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -103,34 +103,3 @@ extension NotificationsViewController {
103103
}
104104
}
105105
}
106-
107-
// MARK: - User Defaults for Push Notifications
108-
109-
extension UserDefaults {
110-
private enum Keys: String {
111-
case notificationPrimerInlineWasAcknowledged = "notificationPrimerInlineWasAcknowledged"
112-
case secondNotificationsAlertCount = "secondNotificationsAlertCount"
113-
}
114-
115-
var notificationPrimerInlineWasAcknowledged: Bool {
116-
get {
117-
return bool(forKey: Keys.notificationPrimerInlineWasAcknowledged.rawValue)
118-
}
119-
set {
120-
set(newValue, forKey: Keys.notificationPrimerInlineWasAcknowledged.rawValue)
121-
}
122-
}
123-
124-
var secondNotificationsAlertCount: Int {
125-
get {
126-
integer(forKey: Keys.secondNotificationsAlertCount.rawValue)
127-
}
128-
set {
129-
set(newValue, forKey: Keys.secondNotificationsAlertCount.rawValue)
130-
}
131-
}
132-
133-
@objc var welcomeNotificationSeenKey: String {
134-
return "welcomeNotificationSeen"
135-
}
136-
}

WordPress/Classes/ViewRelated/Notifications/Controllers/NotificationsViewController.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,16 +1847,16 @@ private extension NotificationsViewController {
18471847
return NotificationActionsService(managedObjectContext: mainContext)
18481848
}
18491849

1850-
var userDefaults: UserDefaults {
1851-
return UserDefaults.standard
1850+
var userDefaults: UserPersistentRepository {
1851+
return UserPersistentStoreFactory.instance()
18521852
}
18531853

18541854
var lastSeenTime: String? {
18551855
get {
18561856
return userDefaults.string(forKey: Settings.lastSeenTime)
18571857
}
18581858
set {
1859-
userDefaults.setValue(newValue, forKey: Settings.lastSeenTime)
1859+
userDefaults.set(newValue, forKey: Settings.lastSeenTime)
18601860
}
18611861
}
18621862

@@ -2037,7 +2037,7 @@ extension NotificationsViewController: UIViewControllerTransitioningDelegate {
20372037
return
20382038
}
20392039

2040-
UserDefaults.standard.notificationPrimerAlertWasDisplayed = true
2040+
UserPersistentStoreFactory.instance().notificationPrimerAlertWasDisplayed = true
20412041

20422042
let alert = alertController
20432043
alert.modalPresentationStyle = .custom

0 commit comments

Comments
 (0)