Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: service toggle issue after switch focus #424

Merged
merged 6 commits into from
Feb 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions Easydict/NewApp/View/SettingView/Tabs/ServiceTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,28 +121,29 @@ private struct ServiceItems: View {
}
}

private class ServiceItemViewModel: ObservableObject {
@Published var isEnable = false

init(isEnable: Bool) {
self.isEnable = isEnable
}
}

@available(macOS 13.0, *)
private struct ServiceItemView: View {
let service: QueryService

@EnvironmentObject private var viewModel: ServiceTabViewModel

private var enabled: Binding<Bool> {
.init {
service.enabled
} set: { newValue in
guard service.enabled != newValue else { return }
phlpsong marked this conversation as resolved.
Show resolved Hide resolved
service.enabled = newValue
if newValue {
service.enabledQuery = newValue
}
EZLocalStorage.shared().setService(service, windowType: viewModel.windowType)
viewModel.postUpdateServiceNotification()
}
@ObservedObject private var serviceItemViewModel: ServiceItemViewModel

init(service: QueryService) {
self.service = service
serviceItemViewModel = ServiceItemViewModel(isEnable: service.enabled)
}

var body: some View {
Toggle(isOn: enabled) {
Toggle(isOn: $serviceItemViewModel.isEnable) {
HStack {
Image(service.serviceType().rawValue)
.resizable()
Expand All @@ -153,6 +154,15 @@ private struct ServiceItemView: View {
.fixedSize()
}
}
.onReceive(serviceItemViewModel.$isEnable) { newValue in
guard service.enabled != newValue else { return }
service.enabled = newValue
if newValue {
service.enabledQuery = newValue
}
EZLocalStorage.shared().setService(service, windowType: viewModel.windowType)
viewModel.postUpdateServiceNotification()
}
.padding(4.0)
.toggleStyle(.switch)
.controlSize(.small)
Expand Down