Skip to content

Commit

Permalink
fix: toggle configuration reset after switch tab issue
Browse files Browse the repository at this point in the history
  • Loading branch information
phlpsong committed Feb 2, 2024
1 parent 586b8ca commit 4d14744
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,27 @@ struct ServiceConfigurationPickerCell<T: RawRepresentable & Hashable & EnumLocal
}
}

class ConfigurationToggleViewModel: ObservableObject {
@Published var isOn = false
}

@available(macOS 13.0, *)
struct ServiceConfigurationToggleCell: View {
@Default var value: String
let titleKey: LocalizedStringKey
@State private var isOn = false

@ObservedObject var viewModel = ConfigurationToggleViewModel()

init(titleKey: LocalizedStringKey, key: Defaults.Key<String>) {
self.titleKey = titleKey
_value = .init(key)
_isOn = State(initialValue: value == "1")
viewModel.isOn = value == "1"
}

var body: some View {
Toggle(titleKey, isOn: $isOn)
Toggle(titleKey, isOn: $viewModel.isOn)
.padding(10.0)
.onChange(of: isOn) { newValue in
.onChange(of: viewModel.isOn) { newValue in
value = newValue ? "1" : "0"
}
}
Expand Down
3 changes: 0 additions & 3 deletions Easydict/NewApp/View/SettingView/Tabs/ServiceTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ struct ServiceTab: View {
}
}
}
.onDisappear {
viewModel.selectedService = nil
}
.layoutPriority(1)
}
.environmentObject(viewModel)
Expand Down

0 comments on commit 4d14744

Please sign in to comment.