Skip to content

Commit 71ee1d7

Browse files
committed
Filter out feature flag user defaults
Additionally simplify the code and extract strings to constants
1 parent 94a74c8 commit 71ee1d7

2 files changed

Lines changed: 25 additions & 18 deletions

File tree

WordPress/Classes/ViewRelated/Me/App Settings/Boolean User Defaults/BooleanUserDefaultsDebugView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ struct BooleanUserDefaultsDebugView: View {
66
var body: some View {
77
List {
88
ForEach(viewModel.userDefaultsSections.keys.sorted(), id: \.self) { sectionKey in
9-
let userDefaultsSection = viewModel.userDefaultsSections[sectionKey] ?? BooleanUserDefaults()
9+
let userDefaultsSection = viewModel.userDefaultsSections[sectionKey] ?? BooleanUserDefaultEntries()
1010

1111
Section(header: Text(sectionKey)
1212
.font(.caption)) {

WordPress/Classes/ViewRelated/Me/App Settings/Boolean User Defaults/BooleanUserDefaultsDebugViewModel.swift

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
import SwiftUI
22

3+
typealias BooleanUserDefaultsSections = [String: BooleanUserDefaultEntries]
4+
typealias BooleanUserDefaultEntries = [String: BooleanUserDefault]
5+
36
final class BooleanUserDefaultsDebugViewModel: ObservableObject {
47
@Published private var allUserDefaultsSections = BooleanUserDefaultsSections()
58
@Published var searchQuery: String = ""
69

710
private var persistentRepository: UserPersistentRepository
811

912
var userDefaultsSections: BooleanUserDefaultsSections {
10-
return if searchQuery.isEmpty {
11-
allUserDefaultsSections
12-
} else {
13-
filterUserDefaults(by: searchQuery)
14-
}
13+
return filterUserDefaults(by: searchQuery)
1514
}
1615

1716
let coreDataStack: CoreDataStack
1817

1918
private func filterUserDefaults(by query: String) -> BooleanUserDefaultsSections {
19+
guard !query.isEmpty else {
20+
return allUserDefaultsSections
21+
}
22+
2023
var filteredSections = BooleanUserDefaultsSections()
2124
allUserDefaultsSections.forEach { sectionKey, userDefaults in
2225
let filteredUserDefaults = userDefaults.filter { key, userDefault in
@@ -38,20 +41,21 @@ final class BooleanUserDefaultsDebugViewModel: ObservableObject {
3841
func load() {
3942
let allUserDefaults = persistentRepository.dictionaryRepresentation()
4043
var loadedUserDefaultsSections = BooleanUserDefaultsSections()
44+
var otherSection = BooleanUserDefaultEntries()
4145

4246
for (entryKey, entryValue) in allUserDefaults {
43-
if let groupedUserDefaults = entryValue as? [String: Bool] {
47+
if let groupedUserDefaults = entryValue as? [String: Bool], !isFeatureFlagsSection(entryKey) {
4448
loadedUserDefaultsSections[entryKey] = processGroupedUserDefaults(groupedUserDefaults)
45-
} else if let booleanUserDefault = entryValue as? Bool, !isSystemUserDefault(entryKey) {
46-
loadedUserDefaultsSections[Strings.otherBooleanUserDefaultsSectionID, default: [:]][entryKey] = BooleanUserDefault(title: entryKey, value: booleanUserDefault)
49+
} else if let booleanUserDefault = entryValue as? Bool, !isGutenbergUserDefault(entryKey) {
50+
otherSection[entryKey] = BooleanUserDefault(title: entryKey, value: booleanUserDefault)
4751
}
4852
}
49-
53+
loadedUserDefaultsSections[Strings.otherBooleanUserDefaultsSectionID] = otherSection
5054
allUserDefaultsSections = loadedUserDefaultsSections
5155
}
5256

53-
private func processGroupedUserDefaults(_ userDefaults: [String: Bool]) -> BooleanUserDefaults {
54-
userDefaults.reduce(into: BooleanUserDefaults()) { result, keyValue in
57+
private func processGroupedUserDefaults(_ userDefaults: [String: Bool]) -> BooleanUserDefaultEntries {
58+
userDefaults.reduce(into: BooleanUserDefaultEntries()) { result, keyValue in
5559
let (key, value) = keyValue
5660
result[key] = processSingleUserDefault(key: key, value: value)
5761
}
@@ -70,19 +74,20 @@ final class BooleanUserDefaultsDebugViewModel: ObservableObject {
7074
persistentRepository.set(value, forKey: userDefaultID)
7175
} else if var section = allUserDefaultsSections[sectionID] {
7276
section[userDefaultID] = BooleanUserDefault(title: userDefaultID, value: value)
73-
var sectionValues = section.mapValues { $0.value }
77+
let sectionValues = section.mapValues { $0.value }
7478
persistentRepository.set(sectionValues, forKey: sectionID)
7579
}
7680
load()
7781
}
7882

79-
private func isSystemUserDefault(_ key: String) -> Bool {
80-
key.starts(with: "com.wordpress.")
83+
private func isGutenbergUserDefault(_ key: String) -> Bool {
84+
key.starts(with: Strings.gutenbergUserDefaultPrefix)
8185
}
82-
}
8386

84-
typealias BooleanUserDefaultsSections = [String: BooleanUserDefaults]
85-
typealias BooleanUserDefaults = [String: BooleanUserDefault]
87+
private func isFeatureFlagsSection(_ key: String) -> Bool {
88+
key.isEqual(to: Strings.featureFlagSectionKey)
89+
}
90+
}
8691

8792
struct BooleanUserDefault {
8893
var title: String
@@ -91,4 +96,6 @@ struct BooleanUserDefault {
9196

9297
private enum Strings {
9398
static let otherBooleanUserDefaultsSectionID = "Other"
99+
static let gutenbergUserDefaultPrefix = "com.wordpress.gutenberg-"
100+
static let featureFlagSectionKey = "FeatureFlagStoreCache"
94101
}

0 commit comments

Comments
 (0)