Skip to content

Commit

Permalink
coreoptions swift ui values view update on change
Browse files Browse the repository at this point in the history
Signed-off-by: Joseph Mattiello <git@joemattiello.com>
  • Loading branch information
JoeMatt committed Dec 4, 2024
1 parent dd7ed1e commit dab67bc
Showing 1 changed file with 68 additions and 12 deletions.
80 changes: 68 additions & 12 deletions PVUI/Sources/PVSwiftUI/Settings/Views/CoreOptionsDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ struct CoreOptionsDetailView: View {
let coreClass: CoreOptional.Type
let title: String

/// State to track current values of options
@State private var optionValues: [String: Any] = [:]

private var groupedOptions: [(title: String, options: [CoreOption])] {
var rootOptions = [CoreOption]()
var groups = [(title: String, options: [CoreOption])]()
Expand Down Expand Up @@ -42,15 +45,68 @@ struct CoreOptionsDetailView: View {
}
}
.navigationTitle(title)
.onAppear {
// Load initial values
loadOptionValues()
}
}

private func loadOptionValues() {
for group in groupedOptions {
for option in group.options {
let value = getCurrentValue(for: option)
if let value = value {
optionValues[option.key] = value
}
}
}
}

private func getCurrentValue(for option: CoreOption) -> Any? {
switch option {
case .bool(_, let defaultValue):
return coreClass.storedValueForOption(Bool.self, option.key) ?? defaultValue
case .string(_, let defaultValue):
return coreClass.storedValueForOption(String.self, option.key) ?? defaultValue
case .enumeration(_, _, let defaultValue):
return coreClass.storedValueForOption(Int.self, option.key) ?? defaultValue
case .range(_, _, let defaultValue):
return coreClass.storedValueForOption(Int.self, option.key) ?? defaultValue
case .rangef(_, _, let defaultValue):
return coreClass.storedValueForOption(Float.self, option.key) ?? defaultValue
case .multi(_, let values):
return coreClass.storedValueForOption(String.self, option.key) ?? values.first?.title
case .group(_, _):
return nil
@unknown default:
return nil
}
}

private func setValue(_ value: Any, for option: CoreOption) {
optionValues[option.key] = value

switch value {
case let boolValue as Bool:
coreClass.setValue(boolValue, forOption: option)
case let stringValue as String:
coreClass.setValue(stringValue, forOption: option)
case let intValue as Int:
coreClass.setValue(intValue, forOption: option)
case let floatValue as Float:
coreClass.setValue(floatValue, forOption: option)
default:
break
}
}

@ViewBuilder
private func optionView(for option: CoreOption) -> some View {
switch option {
case let .bool(display, defaultValue):
Toggle(isOn: Binding(
get: { coreClass.storedValueForOption(Bool.self, option.key) ?? defaultValue },
set: { coreClass.setValue($0, forOption: option) }
get: { optionValues[option.key] as? Bool ?? defaultValue },
set: { setValue($0, for: option) }
)) {
VStack(alignment: .leading) {
Text(display.title)
Expand All @@ -64,8 +120,8 @@ struct CoreOptionsDetailView: View {

case let .enumeration(display, values, defaultValue):
let selection = Binding(
get: { coreClass.storedValueForOption(Int.self, option.key) ?? defaultValue },
set: { coreClass.setValue($0, forOption: option) }
get: { optionValues[option.key] as? Int ?? defaultValue },
set: { setValue($0, for: option) }
)

NavigationLink {
Expand Down Expand Up @@ -115,8 +171,8 @@ struct CoreOptionsDetailView: View {
}
Slider(
value: Binding(
get: { Double(coreClass.storedValueForOption(Int.self, option.key) ?? defaultValue) },
set: { coreClass.setValue(Int($0), forOption: option) }
get: { Double(optionValues[option.key] as? Int ?? defaultValue) },
set: { setValue(Int($0), for: option) }
),
in: Double(range.min)...Double(range.max),
step: 1
Expand All @@ -139,8 +195,8 @@ struct CoreOptionsDetailView: View {
}
Slider(
value: Binding(
get: { Double(coreClass.storedValueForOption(Float.self, option.key) ?? defaultValue) },
set: { coreClass.setValue(Float($0), forOption: option) }
get: { Double(optionValues[option.key] as? Float ?? defaultValue) },
set: { setValue(Float($0), for: option) }
),
in: Double(range.min)...Double(range.max),
step: 0.1
Expand All @@ -155,8 +211,8 @@ struct CoreOptionsDetailView: View {

case let .multi(display, values):
let selection = Binding(
get: { coreClass.storedValueForOption(String.self, option.key) ?? values.first?.title ?? "" },
set: { coreClass.setValue($0, forOption: option) }
get: { optionValues[option.key] as? String ?? values.first?.title ?? "" },
set: { setValue($0, for: option) }
)

NavigationLink {
Expand Down Expand Up @@ -198,8 +254,8 @@ struct CoreOptionsDetailView: View {

case let .string(display, defaultValue):
let text = Binding(
get: { coreClass.storedValueForOption(String.self, option.key) ?? defaultValue },
set: { coreClass.setValue($0, forOption: option) }
get: { optionValues[option.key] as? String ?? defaultValue },
set: { setValue($0, for: option) }
)

VStack(alignment: .leading) {
Expand Down

0 comments on commit dab67bc

Please sign in to comment.