Fix groups

This commit is contained in:
世界
2025-12-01 16:27:24 +08:00
parent 22de256d13
commit 30492c1191
10 changed files with 199 additions and 161 deletions
@@ -6,6 +6,9 @@ import SwiftUI
public class GroupListViewModel: ObservableObject {
@Published public var isLoading = true
@Published public var groups: [OutboundGroup] = []
@Published public var alert: Alert?
private var pendingSelections: [String: String] = [:]
public init() {}
@@ -28,17 +31,100 @@ public class GroupListViewModel: ObservableObject {
public func setGroups(_ goGroups: [LibboxOutboundGroup]?) {
guard let goGroups else { return }
var groups = [OutboundGroup]()
let existingGroups = Dictionary(uniqueKeysWithValues: groups.map { ($0.tag, $0) })
var newGroups = [OutboundGroup]()
for goGroup in goGroups {
var items = [OutboundGroupItem]()
let itemIterator = goGroup.getItems()!
while itemIterator.hasNext() {
let goItem = itemIterator.next()!
items.append(OutboundGroupItem(tag: goItem.tag, type: goItem.type, urlTestTime: Date(timeIntervalSince1970: Double(goItem.urlTestTime)), urlTestDelay: UInt16(goItem.urlTestDelay)))
items.append(OutboundGroupItem(
tag: goItem.tag,
type: goItem.type,
urlTestTime: Date(timeIntervalSince1970: Double(goItem.urlTestTime)),
urlTestDelay: UInt16(goItem.urlTestDelay)
))
}
groups.append(OutboundGroup(tag: goGroup.tag, type: goGroup.type, selected: goGroup.selected, selectable: goGroup.selectable, isExpand: goGroup.isExpand, items: items))
var selected = goGroup.selected
if let pending = pendingSelections[goGroup.tag] {
if goGroup.selected == pending {
pendingSelections.removeValue(forKey: goGroup.tag)
} else {
selected = pending
}
}
let isExpand = existingGroups[goGroup.tag]?.isExpand ?? goGroup.isExpand
newGroups.append(OutboundGroup(
tag: goGroup.tag,
type: goGroup.type,
selected: selected,
selectable: goGroup.selectable,
isExpand: isExpand,
items: items
))
}
self.groups = groups
groups = newGroups
isLoading = false
}
public func selectOutbound(groupTag: String, outboundTag: String) {
if let index = groups.firstIndex(where: { $0.tag == groupTag }) {
groups[index].selected = outboundTag
}
pendingSelections[groupTag] = outboundTag
Task {
await doSelectOutbound(groupTag: groupTag, outboundTag: outboundTag)
}
}
private nonisolated func doSelectOutbound(groupTag: String, outboundTag: String) async {
do {
try await LibboxNewStandaloneCommandClient()!.selectOutbound(groupTag, outboundTag: outboundTag)
} catch {
await MainActor.run {
alert = Alert(error)
}
}
}
public func toggleExpand(groupTag: String) {
guard let index = groups.firstIndex(where: { $0.tag == groupTag }) else { return }
groups[index].isExpand.toggle()
let isExpand = groups[index].isExpand
Task {
await setGroupExpand(tag: groupTag, isExpand: isExpand)
}
}
private nonisolated func setGroupExpand(tag: String, isExpand: Bool) async {
do {
try await LibboxNewStandaloneCommandClient()!.setGroupExpand(tag, isExpand: isExpand)
} catch {
await MainActor.run {
alert = Alert(error)
}
}
}
public func performURLTest(_ tag: String) {
Task {
await doURLTest(tag: tag)
}
}
private nonisolated func doURLTest(tag: String) async {
do {
try await LibboxNewStandaloneCommandClient()!.urlTest(tag)
} catch {
await MainActor.run {
alert = Alert(error)
}
}
}
}