From f5b6bb13ab68a264687bbcc4fbbb36630d640de9 Mon Sep 17 00:00:00 2001 From: phlpsong Date: Sun, 7 Jan 2024 10:31:35 +0800 Subject: [PATCH] feat: add service list move function --- Easydict.xcodeproj/project.pbxproj | 12 ++++++ Easydict/App/Localizable.xcstrings | 3 ++ .../Swift/Binding/Binding+DidSet.swift | 1 - .../Notification/Notification+Name.swift | 17 ++++++++ Easydict/NewApp/View/ServiceItemView.swift | 1 - .../View/SettingView/Tabs/ServiceTab.swift | 39 +++++++++++++++++++ 6 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 Easydict/Feature/Utility/Swift/Notification/Notification+Name.swift diff --git a/Easydict.xcodeproj/project.pbxproj b/Easydict.xcodeproj/project.pbxproj index c54cea561..aea43950e 100644 --- a/Easydict.xcodeproj/project.pbxproj +++ b/Easydict.xcodeproj/project.pbxproj @@ -231,6 +231,7 @@ 0A057D6D2B499A000025C51D /* ServiceTab.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A057D6C2B499A000025C51D /* ServiceTab.swift */; }; 0A057D6F2B499A0B0025C51D /* ServiceItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A057D6E2B499A0B0025C51D /* ServiceItemView.swift */; }; 0A2BA9602B49A989002872A4 /* Binding+DidSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A2BA95F2B49A989002872A4 /* Binding+DidSet.swift */; }; + 0A2BA9642B4A3CCD002872A4 /* Notification+Name.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A2BA9632B4A3CCD002872A4 /* Notification+Name.swift */; }; 17BCAEF72B0DFF9000A7D372 /* EZNiuTransTranslateResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = 17BCAEF52B0DFF9000A7D372 /* EZNiuTransTranslateResponse.m */; }; 17BCAEF82B0DFF9000A7D372 /* EZNiuTransTranslate.m in Sources */ = {isa = PBXBuildFile; fileRef = 17BCAEF62B0DFF9000A7D372 /* EZNiuTransTranslate.m */; }; 2721E4D02AFE920700A059AC /* Alamofire in Frameworks */ = {isa = PBXBuildFile; productRef = 2721E4CF2AFE920700A059AC /* Alamofire */; }; @@ -695,6 +696,7 @@ 0A057D6C2B499A000025C51D /* ServiceTab.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ServiceTab.swift; sourceTree = ""; }; 0A057D6E2B499A0B0025C51D /* ServiceItemView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ServiceItemView.swift; sourceTree = ""; }; 0A2BA95F2B49A989002872A4 /* Binding+DidSet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Binding+DidSet.swift"; sourceTree = ""; }; + 0A2BA9632B4A3CCD002872A4 /* Notification+Name.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Notification+Name.swift"; sourceTree = ""; }; 17BCAEF32B0DFF9000A7D372 /* EZNiuTransTranslateResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EZNiuTransTranslateResponse.h; sourceTree = ""; }; 17BCAEF42B0DFF9000A7D372 /* EZNiuTransTranslate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EZNiuTransTranslate.h; sourceTree = ""; }; 17BCAEF52B0DFF9000A7D372 /* EZNiuTransTranslateResponse.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EZNiuTransTranslateResponse.m; sourceTree = ""; }; @@ -1798,6 +1800,7 @@ 03CF88602B137ECB0030C199 /* Swift */ = { isa = PBXGroup; children = ( + 0A2BA9622B4A3CBB002872A4 /* Notification */, 0A2BA95E2B49A967002872A4 /* Binding */, 03FD68BC2B1E14B500FD388E /* String */, 03CF88612B137ED60030C199 /* Array */, @@ -1985,6 +1988,14 @@ path = Binding; sourceTree = ""; }; + 0A2BA9622B4A3CBB002872A4 /* Notification */ = { + isa = PBXGroup; + children = ( + 0A2BA9632B4A3CCD002872A4 /* Notification+Name.swift */, + ); + path = Notification; + sourceTree = ""; + }; 17BCAEF22B0DFF9000A7D372 /* Niutrans */ = { isa = PBXGroup; children = ( @@ -2625,6 +2636,7 @@ 039CC90D292F664E0037B91E /* NSObject+EZWindowType.m in Sources */, 03B0232229231FA6001C7E63 /* NSImage+MM.m in Sources */, 03BB2DEF29F59C8A00447EDD /* EZSymbolImageButton.m in Sources */, + 0A2BA9642B4A3CCD002872A4 /* Notification+Name.swift in Sources */, 62A2D03F2A82967F007EEB01 /* EZBingRequest.m in Sources */, 03BDA7BE2A26DA280079D04F /* XPMCountedArgument.m in Sources */, 03D35DAA2AA6C49B00B023FE /* NSString+EZRegex.m in Sources */, diff --git a/Easydict/App/Localizable.xcstrings b/Easydict/App/Localizable.xcstrings index 0ee5bae52..62c8fd48d 100644 --- a/Easydict/App/Localizable.xcstrings +++ b/Easydict/App/Localizable.xcstrings @@ -1,6 +1,9 @@ { "sourceLanguage" : "en", "strings" : { + "" : { + + }, "about" : { "comment" : "about", "localizations" : { diff --git a/Easydict/Feature/Utility/Swift/Binding/Binding+DidSet.swift b/Easydict/Feature/Utility/Swift/Binding/Binding+DidSet.swift index ef20210c0..244b735cb 100644 --- a/Easydict/Feature/Utility/Swift/Binding/Binding+DidSet.swift +++ b/Easydict/Feature/Utility/Swift/Binding/Binding+DidSet.swift @@ -21,4 +21,3 @@ extension Binding { ) } } - diff --git a/Easydict/Feature/Utility/Swift/Notification/Notification+Name.swift b/Easydict/Feature/Utility/Swift/Notification/Notification+Name.swift new file mode 100644 index 000000000..71c773f81 --- /dev/null +++ b/Easydict/Feature/Utility/Swift/Notification/Notification+Name.swift @@ -0,0 +1,17 @@ +// +// Notification+Name.swift +// Easydict +// +// Created by phlpsong on 2024/1/7. +// Copyright © 2024 izual. All rights reserved. +// + +import Foundation + +extension Notification.Name { + static let serviceHasUpdated = Notification.Name(EZServiceHasUpdatedNotification) +} + +@objc public extension NSNotification { + static let serviceHasUpdated = Notification.Name.serviceHasUpdated +} diff --git a/Easydict/NewApp/View/ServiceItemView.swift b/Easydict/NewApp/View/ServiceItemView.swift index 4807fa7c1..e5b0d5eac 100644 --- a/Easydict/NewApp/View/ServiceItemView.swift +++ b/Easydict/NewApp/View/ServiceItemView.swift @@ -31,7 +31,6 @@ struct ServiceItemView: View { } } - #Preview { let service = EZLocalStorage.shared().allServices(.mini).first return ServiceItemView(service: .constant(service ?? QueryService())) { val in diff --git a/Easydict/NewApp/View/SettingView/Tabs/ServiceTab.swift b/Easydict/NewApp/View/SettingView/Tabs/ServiceTab.swift index 50dc50be8..4a5159a60 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/ServiceTab.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/ServiceTab.swift @@ -48,6 +48,9 @@ struct ServiceTab: View { serviceToggled(index: index, isEnable: isEnable) } } + .onMove(perform: { indices, newOffset in + onServiceItemMove(fromOffsets: indices, toOffset: newOffset) + }) } .listStyle(.inset) .clipShape(RoundedRectangle(cornerRadius: 10.0)) @@ -79,6 +82,42 @@ struct ServiceTab: View { } let windowType = EZWindowType(rawValue: windowTypeValue) ?? .none EZLocalStorage.shared().setService(services[index], windowType: windowType) + postUpdateServiceNotification() + } + + func enabledServices(in services: [QueryService]) -> [QueryService] { + services.filter(\.enabled) + } + + func onServiceItemMove(fromOffsets: IndexSet, toOffset: Int) { + services.move(fromOffsets: fromOffsets, toOffset: toOffset) + serviceTypes.move(fromOffsets: fromOffsets, toOffset: toOffset) + + let oldEnabledServices = enabledServices(in: services) + let windowType = EZWindowType(rawValue: windowTypeValue) ?? .none + EZLocalStorage.shared().setAllServiceTypes(serviceTypes, windowType: windowType) + let newServices = EZLocalStorage.shared().allServices(windowType) + let newEnabledServices = enabledServices(in: newServices) + + if isEnabledServicesOrderChanged(source: oldEnabledServices, dest: newEnabledServices) { + postUpdateServiceNotification() + } + } + + func isEnabledServicesOrderChanged( + source: [QueryService], + dest: [QueryService] + ) -> Bool { + !source.elementsEqual(dest) { sItem, dItem in + sItem.serviceType() == dItem.serviceType() && sItem.name() == dItem.name() + } + } + + func postUpdateServiceNotification() { + let windowType = EZWindowType(rawValue: windowTypeValue) ?? .none + let userInfo: [String: Any] = [EZWindowTypeKey: windowType] + let notification = Notification(name: .serviceHasUpdated, object: nil, userInfo: userInfo) + NotificationCenter.default.post(notification) } }