Skip to content

Commit

Permalink
feat: add service list move function
Browse files Browse the repository at this point in the history
  • Loading branch information
phlpsong committed Jan 7, 2024
1 parent f04e659 commit f5b6bb1
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 2 deletions.
12 changes: 12 additions & 0 deletions Easydict.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -695,6 +696,7 @@
0A057D6C2B499A000025C51D /* ServiceTab.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ServiceTab.swift; sourceTree = "<group>"; };
0A057D6E2B499A0B0025C51D /* ServiceItemView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ServiceItemView.swift; sourceTree = "<group>"; };
0A2BA95F2B49A989002872A4 /* Binding+DidSet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Binding+DidSet.swift"; sourceTree = "<group>"; };
0A2BA9632B4A3CCD002872A4 /* Notification+Name.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Notification+Name.swift"; sourceTree = "<group>"; };
17BCAEF32B0DFF9000A7D372 /* EZNiuTransTranslateResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EZNiuTransTranslateResponse.h; sourceTree = "<group>"; };
17BCAEF42B0DFF9000A7D372 /* EZNiuTransTranslate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EZNiuTransTranslate.h; sourceTree = "<group>"; };
17BCAEF52B0DFF9000A7D372 /* EZNiuTransTranslateResponse.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EZNiuTransTranslateResponse.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1798,6 +1800,7 @@
03CF88602B137ECB0030C199 /* Swift */ = {
isa = PBXGroup;
children = (
0A2BA9622B4A3CBB002872A4 /* Notification */,
0A2BA95E2B49A967002872A4 /* Binding */,
03FD68BC2B1E14B500FD388E /* String */,
03CF88612B137ED60030C199 /* Array */,
Expand Down Expand Up @@ -1985,6 +1988,14 @@
path = Binding;
sourceTree = "<group>";
};
0A2BA9622B4A3CBB002872A4 /* Notification */ = {
isa = PBXGroup;
children = (
0A2BA9632B4A3CCD002872A4 /* Notification+Name.swift */,
);
path = Notification;
sourceTree = "<group>";
};
17BCAEF22B0DFF9000A7D372 /* Niutrans */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -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 */,
Expand Down
3 changes: 3 additions & 0 deletions Easydict/App/Localizable.xcstrings
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"sourceLanguage" : "en",
"strings" : {
"" : {

},
"about" : {
"comment" : "about",
"localizations" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ extension Binding {
)
}
}

Original file line number Diff line number Diff line change
@@ -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
}
1 change: 0 additions & 1 deletion Easydict/NewApp/View/ServiceItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ struct ServiceItemView: View {
}
}


#Preview {
let service = EZLocalStorage.shared().allServices(.mini).first
return ServiceItemView(service: .constant(service ?? QueryService())) { val in
Expand Down
39 changes: 39 additions & 0 deletions Easydict/NewApp/View/SettingView/Tabs/ServiceTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit f5b6bb1

Please sign in to comment.