Skip to content

Commit

Permalink
fix EntryView addition
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj committed Dec 15, 2024
1 parent 6173b09 commit 2888151
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/ConfigView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ struct ConfigView: View {
data: child,
onUpdate: { value in
setConfig(uri, child["Option"] as! String, value)
viewModel.refresh(uri)
// Don't call viewModel.refresh here because view may be in a temporary invalid state,
// e.g. having removed key of punctuation map but not put anything yet.
})
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/config/ListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,22 @@ private func serialize(_ value: [Any]) -> [String: Any] {
}

private struct ListSectionHeader: View {
let children: Any?
@Binding var value: Any

var body: some View {
HStack {
Spacer()
Button {
var list = deserialize(value)
list.append("")
if let children = children as? [[String: Any]] {
list.append(
children.reduce(into: [:]) { result, child in
result[child["Option"] as! String] = child["DefaultValue"] ?? ""
} as NSDictionary)
} else {
list.append("")
}
value = serialize(list)
} label: {
Image(systemName: "plus")
Expand All @@ -40,7 +48,7 @@ struct ListSubView: OptionViewProtocol {
let optionViewType = toOptionViewType(["Type": String(type.suffix(type.count - "List|".count))])
var list = deserialize(value)
List {
Section(header: ListSectionHeader(value: $value)) {
Section(header: ListSectionHeader(children: data["Children"], value: $value)) {
ForEach(list.indices, id: \.self) { i in
AnyView(
optionViewType.init(
Expand Down
7 changes: 6 additions & 1 deletion src/config/StringView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ struct StringView: View, OptionViewProtocol {
"",
text: Binding<String>(
get: { value as! String },
set: { value = $0 }
set: {
// Avoid unnecessary write.
if value as! String != $0 {
value = $0
}
}
)
)
// Leading for List item, trailing for String option.
Expand Down

0 comments on commit 2888151

Please sign in to comment.