11import SwiftUI
22
3+ typealias BooleanUserDefaultsSections = [ String : BooleanUserDefaultEntries ]
4+ typealias BooleanUserDefaultEntries = [ String : BooleanUserDefault ]
5+
36final 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
8792struct BooleanUserDefault {
8893 var title : String
@@ -91,4 +96,6 @@ struct BooleanUserDefault {
9196
9297private enum Strings {
9398 static let otherBooleanUserDefaultsSectionID = " Other "
99+ static let gutenbergUserDefaultPrefix = " com.wordpress.gutenberg- "
100+ static let featureFlagSectionKey = " FeatureFlagStoreCache "
94101}
0 commit comments