Skip to content

Commit e1b24c8

Browse files
author
Artyom Vlasov
authored
Avoid reloading user defaults sections on toggle switch (#22534)
2 parents c52d38c + edc32a5 commit e1b24c8

3 files changed

Lines changed: 23 additions & 39 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) { section in
99
Section(header: Text(section.key)
1010
.font(.caption)) {
11-
ForEach(section.rows, id: \.key) { row in
11+
ForEach(section.rows) { 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: 13 additions & 33 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

@@ -137,15 +114,18 @@ final class BooleanUserDefaultsDebugViewModel: ObservableObject {
137114

138115
// MARK: - Types
139116

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

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

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

WordPress/WordPressTest/Me/App Settings/Boolean User Defaults/BooleanUserDefaultsDebugViewModelTests.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ class BooleanUserDefaultsDebugViewModelTests: CoreDataTestCase {
128128
viewModel.load()
129129

130130
// When
131-
viewModel.updateUserDefault(false, forSection: "Other", forRow: "entry1")
131+
let section = viewModel.userDefaultsSections[0]
132+
let row = section.rows[0]
133+
viewModel.updateUserDefault(false, section: section, row: row)
132134

133135
// Then
134136
XCTAssertTrue(viewModel.userDefaultsSections.count == 1)
@@ -145,7 +147,9 @@ class BooleanUserDefaultsDebugViewModelTests: CoreDataTestCase {
145147
viewModel.load()
146148

147149
// When
148-
viewModel.updateUserDefault(false, forSection: "section1", forRow: "entry1")
150+
let section = viewModel.userDefaultsSections[0]
151+
let row = section.rows[0]
152+
viewModel.updateUserDefault(false, section: section, row: row)
149153

150154
// Then
151155
XCTAssertTrue(viewModel.userDefaultsSections.count == 1)

0 commit comments

Comments
 (0)