Skip to content

Commit b30da69

Browse files
committed
Persist user default setting without needing to reload allUserDefaultsSections
1 parent 5152cdd commit b30da69

2 files changed

Lines changed: 15 additions & 35 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ struct BooleanUserDefaultsDebugView: View {
55

66
var body: some View {
77
List {
8-
ForEach(viewModel.userDefaultsSections, id: \.key) { section in
8+
ForEach(viewModel.userDefaultsSections, id: \.id) { section in
99
Section(header: Text(section.key)
1010
.font(.caption)) {
11-
ForEach(section.rows, id: \.key) { row in
11+
ForEach(section.rows, id: \.id) { row in
1212
let isOn = Binding<Bool>(
1313
get: {
1414
row.value
1515
},
1616
set: { newValue in
1717
viewModel.updateUserDefault(
1818
newValue,
19-
forSection: section.key,
20-
forRow: row.key
19+
section: section,
20+
row: row
2121
)
2222
}
2323
)

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

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -91,39 +91,16 @@ final class BooleanUserDefaultsDebugViewModel: ObservableObject {
9191
return try? Blog.lookup(withID: Int(id) ?? 0, in: coreDataStack.mainContext)
9292
}
9393

94-
func updateUserDefault(_ newValue: Bool, forSection targetSection: String, forRow targetRow: String) {
95-
updateAllUserDefaultsSections(newValue, forSection: targetSection, forRow: targetRow)
96-
if targetSection == Strings.otherBooleanUserDefaultsSectionID {
97-
persistentRepository.set(newValue, forKey: targetRow)
94+
func updateUserDefault(_ newValue: Bool, section: Section, row: Row) {
95+
row.value = newValue
96+
97+
if section.key == Strings.otherBooleanUserDefaultsSectionID {
98+
persistentRepository.set(newValue, forKey: row.key)
9899
} else {
99-
guard let section = allUserDefaultsSections.first(where: { $0.key == targetSection }) else {
100-
return
101-
}
102100
let entries = section.rows.reduce(into: [String: Bool]()) { result, row in
103-
if row.key == targetRow {
104-
result[row.key] = newValue
105-
} else {
106-
result[row.key] = row.value
107-
}
108-
}
109-
persistentRepository.set(entries, forKey: targetSection)
110-
}
111-
}
112-
113-
func updateAllUserDefaultsSections(_ newValue: Bool, forSection targetSection: String, forRow targetRow: String) {
114-
allUserDefaultsSections = allUserDefaultsSections.map { currentSection in
115-
if currentSection.key == targetSection {
116-
let updatedRows = currentSection.rows.map { currentRow in
117-
if currentRow.key == targetRow {
118-
return Row(key: currentRow.key, title: currentRow.title, value: newValue)
119-
} else {
120-
return currentRow
121-
}
122-
}
123-
return Section(key: currentSection.key, rows: updatedRows)
124-
} else {
125-
return currentSection
101+
result[row.key] = row.value
126102
}
103+
persistentRepository.set(entries, forKey: section.key)
127104
}
128105
}
129106

@@ -138,14 +115,17 @@ final class BooleanUserDefaultsDebugViewModel: ObservableObject {
138115
// MARK: - Types
139116

140117
struct Section {
118+
let id: String = UUID().uuidString
141119
let key: String
142120
let rows: [Row]
143121
}
144122

145123
final class Row {
124+
let id: String = UUID().uuidString
146125
let key: String
147126
let title: String
148-
let value: Bool
127+
128+
var value: Bool
149129

150130
init(key: String, title: String, value: Bool) {
151131
self.key = key

0 commit comments

Comments
 (0)