Refactor task usage and profile auto update

This commit is contained in:
世界
2023-09-22 11:32:51 +08:00
parent 390e063f20
commit 3374f8e727
49 changed files with 823 additions and 636 deletions
@@ -2,6 +2,7 @@ import Libbox
import Library
import SwiftUI
@MainActor
public struct GroupItemView: View {
private let _group: Binding<OutboundGroup>
private var group: OutboundGroup {
@@ -17,13 +18,13 @@ public struct GroupItemView: View {
@State private var alert: Alert?
public var body: some View {
Button(action: {
Button {
if group.selectable, group.selected != item.tag {
Task.detached {
selectOutbound()
Task {
await selectOutbound()
}
}
}, label: {
} label: {
HStack {
VStack {
HStack {
@@ -56,7 +57,7 @@ public struct GroupItemView: View {
}
}
}
})
}
#if !os(tvOS)
.buttonStyle(.borderless)
.padding(EdgeInsets(top: 10, leading: 13, bottom: 10, trailing: 13))
@@ -66,15 +67,18 @@ public struct GroupItemView: View {
.alertBinding($alert)
}
private func selectOutbound() {
private nonisolated func selectOutbound() async {
do {
try LibboxNewStandaloneCommandClient()!.selectOutbound(group.tag, outboundTag: item.tag)
var newGroup = group
try await LibboxNewStandaloneCommandClient()!.selectOutbound(group.tag, outboundTag: item.tag)
var newGroup = await group
newGroup.selected = item.tag
_group.wrappedValue = newGroup
await MainActor.run { [newGroup] in
_group.wrappedValue = newGroup
}
} catch {
alert = Alert(error)
return
await MainActor.run {
alert = Alert(error)
}
}
}
+15 -10
View File
@@ -2,6 +2,7 @@ import Libbox
import Library
import SwiftUI
@MainActor
public struct GroupView: View {
@State private var group: OutboundGroup
@State private var geometryWidth: CGFloat = 300
@@ -25,8 +26,8 @@ public struct GroupView: View {
.cornerRadius(4)
Button {
group.isExpand = !group.isExpand
Task.detached {
setGroupExpand()
Task {
await setGroupExpand()
}
} label: {
if group.isExpand {
@@ -39,8 +40,8 @@ public struct GroupView: View {
.buttonStyle(.plain)
#endif
Button {
Task.detached {
doURLTest()
Task {
await doURLTest()
}
} label: {
Image(systemName: "bolt.fill")
@@ -137,19 +138,23 @@ public struct GroupView: View {
#endif
}
private func doURLTest() {
private nonisolated func doURLTest() async {
do {
try LibboxNewStandaloneCommandClient()!.urlTest(group.tag)
try await LibboxNewStandaloneCommandClient()!.urlTest(group.tag)
} catch {
alert = Alert(error)
await MainActor.run {
alert = Alert(error)
}
}
}
private func setGroupExpand() {
private nonisolated func setGroupExpand() async {
do {
try LibboxNewStandaloneCommandClient()!.setGroupExpand(group.tag, isExpand: group.isExpand)
try await LibboxNewStandaloneCommandClient()!.setGroupExpand(group.tag, isExpand: group.isExpand)
} catch {
alert = Alert(error)
await MainActor.run {
alert = Alert(error)
}
}
}
}