Skip to content

Commit

Permalink
refactor: ServiceItems
Browse files Browse the repository at this point in the history
  • Loading branch information
CanglongCl committed Jan 16, 2024
1 parent 8ccdcac commit 5cba85f
Showing 1 changed file with 20 additions and 45 deletions.
65 changes: 20 additions & 45 deletions Easydict/NewApp/View/SettingView/Tabs/ServiceTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private struct ServiceItems: View {

var body: some View {
ForEach(servicesWithID, id: \.1) { service, _ in
ServiceItemView(service: service, windowType: viewModel.windowType)
ServiceItemView(service: service)
.tag(service)
}
.onMove(perform: viewModel.onServiceItemMove)
Expand All @@ -135,21 +135,32 @@ private struct ServiceItems: View {

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

@EnvironmentObject private var viewModel: ServiceTabViewModel

init(service: QueryService, windowType: EZWindowType) {
_service = .init(wrappedValue: .init(queryService: service, windowType: windowType))
private var enabled: Binding<Bool> {
.init {
service.enabled
} set: { 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()
}
}

var body: some View {
Toggle(isOn: $service.enabled) {
Toggle(isOn: enabled) {
HStack {
Image(service.inner.serviceType().rawValue)
Image(service.serviceType().rawValue)
.resizable()
.scaledToFit()
.frame(width: 20.0, height: 20.0)
Text(service.inner.name())
Text(service.name())
.lineLimit(1)
.fixedSize()
}
Expand All @@ -160,48 +171,12 @@ private struct ServiceItemView: View {
.listRowSeparator(.hidden)
.listRowInsets(.init())
.padding(10)
.listRowBackground(viewModel.selectedService == service.inner ? Color("service_cell_highlight") : tableColor)
.listRowBackground(viewModel.selectedService == service ? Color("service_cell_highlight") : tableColor)
.overlay {
TapHandler {
viewModel.selectedService = service.inner
}
}
}

private class QueryServiceWrapper: ObservableObject {
let windowType: EZWindowType
var inner: QueryService

var enabled: Bool {
get {
inner.enabled
} set {
guard inner.enabled != newValue else { return }
inner.enabled = newValue
if newValue {
inner.enabledQuery = newValue
}
save()
viewModel.selectedService = service
}
}

private var cancellables: Set<AnyCancellable> = []

init(queryService: QueryService, windowType: EZWindowType) {
inner = queryService
self.windowType = windowType
}

private func save() {
EZLocalStorage.shared().setService(inner, windowType: windowType)
postUpdateServiceNotification()
}

private func postUpdateServiceNotification() {
let userInfo: [String: Any] = [EZWindowTypeKey: windowType.rawValue]
let notification = Notification(name: .serviceHasUpdated, object: nil, userInfo: userInfo)
NotificationCenter.default.post(notification)
}
}

@Environment(\.colorScheme) private var colorScheme
Expand Down

0 comments on commit 5cba85f

Please sign in to comment.