Refactor task usage and profile auto update
This commit is contained in:
@@ -9,3 +9,21 @@ public extension Binding {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
public extension Binding where Value == Int32 {
|
||||
func stringBinding(defaultValue: Int32) -> Binding<String> {
|
||||
return Binding<String> {
|
||||
var intValue = wrappedValue
|
||||
if intValue == 0 {
|
||||
intValue = defaultValue
|
||||
}
|
||||
return String(intValue)
|
||||
} set: { newValue in
|
||||
var newIntValue = Int32(newValue) ?? defaultValue
|
||||
if newIntValue == 0 {
|
||||
newIntValue = defaultValue
|
||||
}
|
||||
wrappedValue = newIntValue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,8 @@ public func FormItem(_ title: String, @ViewBuilder content: () -> some View) ->
|
||||
#if os(iOS) || os(tvOS)
|
||||
HStack {
|
||||
Text(title)
|
||||
.lineLimit(1)
|
||||
.layoutPriority(1)
|
||||
Spacer()
|
||||
Spacer()
|
||||
content()
|
||||
|
||||
@@ -7,6 +7,7 @@ import SwiftUI
|
||||
import AppKit
|
||||
#endif
|
||||
|
||||
@MainActor
|
||||
public struct ProfileShareButton<Label>: View where Label: View {
|
||||
private let alert: Binding<Alert?>
|
||||
private let profile: Profile
|
||||
@@ -62,8 +63,8 @@ public struct ShareButtonCompat<Label>: View where Label: View {
|
||||
|
||||
private func shareItem() {
|
||||
#if os(iOS)
|
||||
Task.detached {
|
||||
shareItem0()
|
||||
Task {
|
||||
await shareItem0()
|
||||
}
|
||||
#elseif os(macOS)
|
||||
sharePresented = true
|
||||
@@ -71,14 +72,14 @@ public struct ShareButtonCompat<Label>: View where Label: View {
|
||||
}
|
||||
|
||||
#if os(iOS)
|
||||
private func shareItem0() {
|
||||
private nonisolated func shareItem0() async {
|
||||
do {
|
||||
let shareItem = try itemURL()
|
||||
DispatchQueue.main.async {
|
||||
await MainActor.run {
|
||||
shareItem1(shareItem)
|
||||
}
|
||||
} catch {
|
||||
DispatchQueue.main.async {
|
||||
await MainActor.run {
|
||||
alert = Alert(error)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import Libbox
|
||||
import Library
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
public struct ActiveDashboardView: View {
|
||||
@Environment(\.scenePhase) var scenePhase
|
||||
@Environment(\.selection) private var parentSelection
|
||||
@@ -19,7 +20,7 @@ public struct ActiveDashboardView: View {
|
||||
public var body: some View {
|
||||
if isLoading {
|
||||
ProgressView().onAppear {
|
||||
Task.detached {
|
||||
Task {
|
||||
await doReload()
|
||||
}
|
||||
}
|
||||
@@ -28,9 +29,14 @@ public struct ActiveDashboardView: View {
|
||||
body1
|
||||
} else {
|
||||
body1
|
||||
.onAppear {
|
||||
Task {
|
||||
await doReloadSystemProxy()
|
||||
}
|
||||
}
|
||||
.onChangeCompat(of: profile.status) { newStatus in
|
||||
if newStatus == .connected {
|
||||
Task.detached {
|
||||
Task {
|
||||
await doReloadSystemProxy()
|
||||
}
|
||||
}
|
||||
@@ -70,14 +76,14 @@ public struct ActiveDashboardView: View {
|
||||
#if os(iOS) || os(tvOS)
|
||||
.onChangeCompat(of: scenePhase) { newValue in
|
||||
if newValue == .active {
|
||||
Task.detached {
|
||||
Task {
|
||||
await doReload()
|
||||
}
|
||||
}
|
||||
}
|
||||
.onChangeCompat(of: parentSelection.wrappedValue) { newValue in
|
||||
if newValue == .dashboard {
|
||||
Task.detached {
|
||||
Task {
|
||||
await doReload()
|
||||
}
|
||||
}
|
||||
@@ -86,7 +92,7 @@ public struct ActiveDashboardView: View {
|
||||
.alertBinding($alert)
|
||||
}
|
||||
|
||||
private func doReload() {
|
||||
private func doReload() async {
|
||||
defer {
|
||||
isLoading = false
|
||||
}
|
||||
@@ -98,35 +104,39 @@ public struct ActiveDashboardView: View {
|
||||
systemProxyAvailable = true
|
||||
systemProxyEnabled = true
|
||||
selectedProfileID = 0
|
||||
|
||||
} else {
|
||||
do {
|
||||
profileList = try ProfileManager.list()
|
||||
profileList = try await ProfileManager.list()
|
||||
if profileList.isEmpty {
|
||||
return
|
||||
}
|
||||
selectedProfileID = await SharedPreferences.selectedProfileID.get()
|
||||
if profileList.filter({ profile in
|
||||
profile.id == selectedProfileID
|
||||
})
|
||||
.isEmpty {
|
||||
selectedProfileID = profileList[0].id!
|
||||
await SharedPreferences.selectedProfileID.set(selectedProfileID)
|
||||
}
|
||||
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
return
|
||||
}
|
||||
if profileList.isEmpty {
|
||||
return
|
||||
}
|
||||
|
||||
selectedProfileID = SharedPreferences.selectedProfileID
|
||||
if profileList.filter({ profile in
|
||||
profile.id == selectedProfileID
|
||||
})
|
||||
.isEmpty {
|
||||
selectedProfileID = profileList[0].id!
|
||||
SharedPreferences.selectedProfileID = selectedProfileID
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func doReloadSystemProxy() {
|
||||
private nonisolated func doReloadSystemProxy() async {
|
||||
do {
|
||||
let status = try LibboxNewStandaloneCommandClient()!.getSystemProxyStatus()
|
||||
systemProxyAvailable = status.available
|
||||
systemProxyEnabled = status.enabled
|
||||
await MainActor.run {
|
||||
systemProxyAvailable = status.available
|
||||
systemProxyEnabled = status.enabled
|
||||
}
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
await MainActor.run {
|
||||
alert = Alert(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import Libbox
|
||||
import Library
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
public struct ClashModeView: View {
|
||||
@Environment(\.scenePhase) private var scenePhase
|
||||
@StateObject private var commandClient = CommandClient(.clashMode)
|
||||
@@ -16,8 +17,8 @@ public struct ClashModeView: View {
|
||||
clashMode
|
||||
}, set: { newMode in
|
||||
clashMode = newMode
|
||||
Task.detached {
|
||||
await setMode(newMode)
|
||||
Task {
|
||||
await setClashMode(newMode)
|
||||
}
|
||||
}), content: {
|
||||
ForEach(commandClient.clashModeList, id: \.self) { it in
|
||||
@@ -48,11 +49,13 @@ public struct ClashModeView: View {
|
||||
.alertBinding($alert)
|
||||
}
|
||||
|
||||
private func setMode(_ newMode: String) {
|
||||
private nonisolated func setClashMode(_ newMode: String) async {
|
||||
do {
|
||||
try LibboxNewStandaloneCommandClient()!.setClashMode(newMode)
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
await MainActor.run {
|
||||
alert = Alert(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ public extension DashboardPage {
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
func contentView(_ profileList: Binding<[Profile]>, _ selectedProfileID: Binding<Int64?>, _ systemProxyAvailable: Binding<Bool>, _ systemProxyEnabled: Binding<Bool>) -> some View {
|
||||
viewBuilder {
|
||||
switch self {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import Library
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
public struct DashboardView: View {
|
||||
#if os(macOS)
|
||||
@Environment(\.controlActiveState) private var controlActiveState
|
||||
@@ -16,12 +17,18 @@ public struct DashboardView: View {
|
||||
viewBuilder {
|
||||
if !systemExtensionInstalled {
|
||||
FormView {
|
||||
InstallSystemExtensionButton(reload)
|
||||
InstallSystemExtensionButton {
|
||||
await reload()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
DashboardView0()
|
||||
}
|
||||
}.onAppear(perform: reload)
|
||||
}.onAppear {
|
||||
Task {
|
||||
await reload()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
DashboardView0()
|
||||
}
|
||||
@@ -34,7 +41,9 @@ public struct DashboardView: View {
|
||||
if newValue != .inactive {
|
||||
if Variant.useSystemExtension {
|
||||
if !isLoading {
|
||||
reload()
|
||||
Task {
|
||||
await reload()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,9 +52,10 @@ public struct DashboardView: View {
|
||||
}
|
||||
|
||||
#if os(macOS)
|
||||
private func reload() {
|
||||
Task {
|
||||
systemExtensionInstalled = await SystemExtension.isInstalled()
|
||||
private nonisolated func reload() async {
|
||||
let systemExtensionInstalled = await SystemExtension.isInstalled()
|
||||
await MainActor.run {
|
||||
self.systemExtensionInstalled = systemExtensionInstalled
|
||||
isLoading = false
|
||||
}
|
||||
}
|
||||
@@ -81,16 +91,20 @@ public struct DashboardView: View {
|
||||
.alertBinding($alert)
|
||||
.onChangeCompat(of: profile.status) { newValue in
|
||||
if newValue == .disconnecting || newValue == .connected {
|
||||
Task.detached {
|
||||
if let serviceError = try? String(contentsOf: ExtensionProvider.errorFile) {
|
||||
DispatchQueue.main.async {
|
||||
alert = Alert(title: Text("Service Error"), message: Text(serviceError))
|
||||
}
|
||||
try? FileManager.default.removeItem(at: ExtensionProvider.errorFile)
|
||||
}
|
||||
Task {
|
||||
await checkServiceError()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private nonisolated func checkServiceError() async {
|
||||
if let serviceError = try? String(contentsOf: ExtensionProvider.errorFile) {
|
||||
await MainActor.run {
|
||||
alert = Alert(title: Text("Service Error"), message: Text(serviceError))
|
||||
}
|
||||
try? FileManager.default.removeItem(at: ExtensionProvider.errorFile)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,14 +113,6 @@ public struct ExtensionStatusView: View {
|
||||
}
|
||||
}
|
||||
|
||||
private func closeConnections() {
|
||||
do {
|
||||
try LibboxNewStandaloneCommandClient()!.closeConnections()
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
}
|
||||
}
|
||||
|
||||
private struct StatusItem<T>: View where T: View {
|
||||
private let title: String
|
||||
@ViewBuilder private let content: () -> T
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import Library
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
public struct InstallProfileButton: View {
|
||||
@State private var alert: Alert?
|
||||
|
||||
|
||||
@@ -3,10 +3,11 @@
|
||||
import Library
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
public struct InstallSystemExtensionButton: View {
|
||||
@State private var alert: Alert?
|
||||
private let callback: () -> Void
|
||||
public init(_ callback: @escaping () -> Void) {
|
||||
private let callback: () async -> Void
|
||||
public init(_ callback: @escaping () async -> Void) {
|
||||
self.callback = callback
|
||||
}
|
||||
|
||||
@@ -26,7 +27,7 @@
|
||||
alert = Alert(errorMessage: "Need Reboot")
|
||||
}
|
||||
}
|
||||
callback()
|
||||
await callback()
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import Libbox
|
||||
import Library
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
public struct OverviewView: View {
|
||||
public static let NotificationUpdateSelectedProfile = Notification.Name("update-selected-profile")
|
||||
|
||||
@@ -38,7 +39,7 @@ public struct OverviewView: View {
|
||||
if ApplicationLibrary.inPreview || profile.status.isConnectedStrict, systemProxyAvailable {
|
||||
Toggle("HTTP Proxy", isOn: $systemProxyEnabled)
|
||||
.onChangeCompat(of: systemProxyEnabled) { newValue in
|
||||
Task.detached {
|
||||
Task {
|
||||
await setSystemProxyEnabled(newValue)
|
||||
}
|
||||
}
|
||||
@@ -55,7 +56,7 @@ public struct OverviewView: View {
|
||||
if ApplicationLibrary.inPreview || profile.status.isConnectedStrict, systemProxyAvailable {
|
||||
Toggle("HTTP Proxy", isOn: $systemProxyEnabled)
|
||||
.onChangeCompat(of: systemProxyEnabled) { newValue in
|
||||
Task.detached {
|
||||
Task {
|
||||
await setSystemProxyEnabled(newValue)
|
||||
}
|
||||
}
|
||||
@@ -75,7 +76,7 @@ public struct OverviewView: View {
|
||||
.alertBinding($alert)
|
||||
.onChangeCompat(of: selectedProfileID) {
|
||||
reasserting = true
|
||||
Task.detached {
|
||||
Task {
|
||||
await switchProfile(selectedProfileID!)
|
||||
}
|
||||
}
|
||||
@@ -96,25 +97,31 @@ public struct OverviewView: View {
|
||||
#endif
|
||||
}
|
||||
|
||||
private func switchProfile(_ newProfileID: Int64) {
|
||||
SharedPreferences.selectedProfileID = newProfileID
|
||||
private nonisolated func switchProfile(_ newProfileID: Int64) async {
|
||||
await SharedPreferences.selectedProfileID.set(newProfileID)
|
||||
NotificationCenter.default.post(name: OverviewView.NotificationUpdateSelectedProfile, object: newProfileID)
|
||||
if profile.status.isConnected {
|
||||
if await profile.status.isConnected {
|
||||
do {
|
||||
try LibboxNewStandaloneCommandClient()!.serviceReload()
|
||||
} catch {
|
||||
await MainActor.run {
|
||||
alert = Alert(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
await MainActor.run {
|
||||
reasserting = false
|
||||
}
|
||||
}
|
||||
|
||||
private nonisolated func setSystemProxyEnabled(_ isEnabled: Bool) async {
|
||||
do {
|
||||
try LibboxNewStandaloneCommandClient()!.setSystemProxyEnabled(isEnabled)
|
||||
await SharedPreferences.systemProxyEnabled.set(isEnabled)
|
||||
} catch {
|
||||
await MainActor.run {
|
||||
alert = Alert(error)
|
||||
}
|
||||
}
|
||||
reasserting = false
|
||||
}
|
||||
|
||||
private func setSystemProxyEnabled(_ isEnabled: Bool) {
|
||||
do {
|
||||
try LibboxNewStandaloneCommandClient()!.setSystemProxyEnabled(isEnabled)
|
||||
SharedPreferences.systemProxyEnabled = isEnabled
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import Library
|
||||
import NetworkExtension
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
public struct StartStopButton: View {
|
||||
@EnvironmentObject private var environments: ExtensionEnvironments
|
||||
|
||||
@@ -15,9 +16,9 @@ public struct StartStopButton: View {
|
||||
Text("Enabled")
|
||||
}
|
||||
#elseif os(macOS)
|
||||
Button(action: {}, label: {
|
||||
Button {} label: {
|
||||
Label("Stop", systemImage: "stop.fill")
|
||||
})
|
||||
}
|
||||
#endif
|
||||
|
||||
} else if let profile = environments.extensionProfile {
|
||||
@@ -28,10 +29,9 @@ public struct StartStopButton: View {
|
||||
Text("Enabled")
|
||||
}
|
||||
#elseif os(macOS)
|
||||
|
||||
Button(action: {}, label: {
|
||||
Button {} label: {
|
||||
Label("Start", systemImage: "play.fill")
|
||||
})
|
||||
}
|
||||
.disabled(true)
|
||||
#endif
|
||||
}
|
||||
@@ -49,41 +49,42 @@ public struct StartStopButton: View {
|
||||
Toggle(isOn: Binding(get: {
|
||||
profile.status.isConnected
|
||||
}, set: { newValue, _ in
|
||||
Task.detached {
|
||||
Task {
|
||||
await switchProfile(newValue)
|
||||
}
|
||||
})) {
|
||||
Text("Enabled")
|
||||
}
|
||||
#elseif os(macOS)
|
||||
Button(action: {
|
||||
Task.detached {
|
||||
Button {
|
||||
Task {
|
||||
await switchProfile(!profile.status.isConnected)
|
||||
}
|
||||
}, label: {
|
||||
} label: {
|
||||
if !profile.status.isConnected {
|
||||
Label("Start", systemImage: "play.fill")
|
||||
} else {
|
||||
Label("Stop", systemImage: "stop.fill")
|
||||
}
|
||||
})
|
||||
}
|
||||
#endif
|
||||
}
|
||||
.disabled(!profile.status.isEnabled)
|
||||
.alertBinding($alert)
|
||||
}
|
||||
|
||||
private func switchProfile(_ isEnabled: Bool) async {
|
||||
private nonisolated func switchProfile(_ isEnabled: Bool) async {
|
||||
do {
|
||||
if isEnabled {
|
||||
try await profile.start()
|
||||
environments.logClient.connect()
|
||||
await environments.logClient.connect()
|
||||
} else {
|
||||
profile.stop()
|
||||
await profile.stop()
|
||||
}
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
return
|
||||
await MainActor.run {
|
||||
alert = Alert(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,6 +59,7 @@ public extension NavigationPage {
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
var contentView: some View {
|
||||
viewBuilder {
|
||||
switch self {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
import Library
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
public struct EditProfileContentView: View {
|
||||
#if os(macOS)
|
||||
public static let windowID = "edit-profile-content"
|
||||
@@ -33,8 +34,8 @@
|
||||
viewBuilder {
|
||||
if isLoading {
|
||||
ProgressView().onAppear {
|
||||
Task.detached {
|
||||
loadContent()
|
||||
Task {
|
||||
await loadContent()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -64,13 +65,13 @@
|
||||
.toolbar {
|
||||
ToolbarItemGroup(placement: .navigation) {
|
||||
if !readOnly {
|
||||
Button(action: {
|
||||
Task.detached {
|
||||
saveContent()
|
||||
Button {
|
||||
Task {
|
||||
await saveContent()
|
||||
}
|
||||
}, label: {
|
||||
} label: {
|
||||
Image("save", label: Text("Save"))
|
||||
})
|
||||
}
|
||||
.disabled(!isChanged)
|
||||
}
|
||||
}
|
||||
@@ -80,8 +81,8 @@
|
||||
ToolbarItem(placement: .navigationBarTrailing) {
|
||||
if !readOnly {
|
||||
Button("Save") {
|
||||
Task.detached {
|
||||
saveContent()
|
||||
Task {
|
||||
await saveContent()
|
||||
}
|
||||
}.disabled(!isChanged)
|
||||
}
|
||||
@@ -99,38 +100,45 @@
|
||||
}
|
||||
}
|
||||
|
||||
private func loadContent() {
|
||||
private func loadContent() async {
|
||||
do {
|
||||
try loadContent0()
|
||||
try await loadContentBackground()
|
||||
} catch {
|
||||
alert = Alert(error, dismiss.callAsFunction)
|
||||
alert = Alert(error)
|
||||
}
|
||||
}
|
||||
|
||||
private func loadContent0() throws {
|
||||
guard let profileID else {
|
||||
throw NSError(domain: "Context destroyed", code: 0)
|
||||
}
|
||||
guard let profile = try ProfileManager.get(profileID) else {
|
||||
throw NSError(domain: "Profile missing", code: 0)
|
||||
}
|
||||
profileContent = try profile.read()
|
||||
self.profile = profile
|
||||
isLoading = false
|
||||
}
|
||||
|
||||
private func saveContent() {
|
||||
private nonisolated func loadContentBackground() async throws {
|
||||
guard let profileID else {
|
||||
throw NSError(domain: "Context destroyed", code: 0)
|
||||
}
|
||||
guard let profile = try await ProfileManager.get(profileID) else {
|
||||
throw NSError(domain: "Profile missing", code: 0)
|
||||
}
|
||||
let profileContent = try profile.read()
|
||||
await MainActor.run {
|
||||
self.profile = profile
|
||||
self.profileContent = profileContent
|
||||
}
|
||||
}
|
||||
|
||||
private func saveContent() async {
|
||||
guard let profile else {
|
||||
return
|
||||
}
|
||||
do {
|
||||
try profile.write(profileContent)
|
||||
try await saveContentBackground(profile)
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
return
|
||||
}
|
||||
isChanged = false
|
||||
}
|
||||
|
||||
private nonisolated func saveContentBackground(_ profile: Profile) async throws {
|
||||
try await profile.write(profileContent)
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import Library
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
public struct EditProfileView: View {
|
||||
#if os(macOS)
|
||||
@Environment(\.openWindow) private var openWindow
|
||||
@@ -43,6 +44,13 @@ public struct EditProfileView: View {
|
||||
.multilineTextAlignment(.trailing)
|
||||
}
|
||||
Toggle("Auto Update", isOn: $profile.autoUpdate)
|
||||
FormItem("Auto Update Interval") {
|
||||
TextField("Auto Update Interval", text: $profile.autoUpdateInterval.stringBinding(defaultValue: 60), prompt: Text("In Minutes"))
|
||||
.multilineTextAlignment(.trailing)
|
||||
#if os(iOS)
|
||||
.keyboardType(.numberPad)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
if profile.type == .remote {
|
||||
Section("Status") {
|
||||
@@ -77,14 +85,14 @@ public struct EditProfileView: View {
|
||||
}
|
||||
Button("Update") {
|
||||
isLoading = true
|
||||
Task.detached {
|
||||
Task {
|
||||
await updateProfile()
|
||||
}
|
||||
}
|
||||
.disabled(isLoading)
|
||||
}
|
||||
Button("Delete", role: .destructive) {
|
||||
Task.detached {
|
||||
Task {
|
||||
await deleteProfile()
|
||||
}
|
||||
}
|
||||
@@ -104,37 +112,37 @@ public struct EditProfileView: View {
|
||||
#if os(macOS)
|
||||
.toolbar {
|
||||
ToolbarItemGroup(placement: .navigation) {
|
||||
Button(action: {
|
||||
Button {
|
||||
isLoading = true
|
||||
Task.detached {
|
||||
Task {
|
||||
await saveProfile()
|
||||
}
|
||||
}, label: {
|
||||
} label: {
|
||||
Image("save", bundle: ApplicationLibrary.bundle, label: Text("Save"))
|
||||
})
|
||||
}
|
||||
.disabled(isLoading || !isChanged)
|
||||
if profile.type != .remote {
|
||||
Button(action: {
|
||||
Button {
|
||||
openWindow(id: EditProfileContentView.windowID, value: EditProfileContentView.Context(profileID: profile.id!, readOnly: false))
|
||||
}, label: {
|
||||
} label: {
|
||||
Label("Edit Content", systemImage: "pencil")
|
||||
})
|
||||
}
|
||||
.disabled(isLoading)
|
||||
} else {
|
||||
Button(action: {
|
||||
Button {
|
||||
isLoading = true
|
||||
Task.detached {
|
||||
Task {
|
||||
await updateProfile()
|
||||
}
|
||||
}, label: {
|
||||
} label: {
|
||||
Label("Update", systemImage: "arrow.clockwise")
|
||||
})
|
||||
}
|
||||
.disabled(isLoading)
|
||||
Button(action: {
|
||||
Button {
|
||||
openWindow(id: EditProfileContentView.windowID, value: EditProfileContentView.Context(profileID: profile.id!, readOnly: true))
|
||||
}, label: {
|
||||
} label: {
|
||||
Label("View Content", systemImage: "doc.text.fill")
|
||||
})
|
||||
}
|
||||
.disabled(isLoading)
|
||||
}
|
||||
}
|
||||
@@ -144,7 +152,7 @@ public struct EditProfileView: View {
|
||||
ToolbarItem(placement: .navigationBarTrailing) {
|
||||
Button("Save") {
|
||||
isLoading = true
|
||||
Task.detached {
|
||||
Task {
|
||||
await saveProfile()
|
||||
}
|
||||
}.disabled(!isChanged)
|
||||
@@ -161,7 +169,12 @@ public struct EditProfileView: View {
|
||||
}
|
||||
do {
|
||||
try await Task.sleep(nanoseconds: UInt64(100 * Double(NSEC_PER_MSEC)))
|
||||
try profile.updateRemoteProfile()
|
||||
try await profile.updateRemoteProfile()
|
||||
#if os(iOS) || os(tvOS)
|
||||
try await UIProfileUpdateTask.configure()
|
||||
#else
|
||||
try await ProfileUpdateTask.configure()
|
||||
#endif
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
}
|
||||
@@ -169,7 +182,7 @@ public struct EditProfileView: View {
|
||||
|
||||
private func deleteProfile() async {
|
||||
do {
|
||||
try ProfileManager.delete(profile)
|
||||
try await ProfileManager.delete(profile)
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
return
|
||||
@@ -180,7 +193,7 @@ public struct EditProfileView: View {
|
||||
|
||||
private func saveProfile() async {
|
||||
do {
|
||||
_ = try ProfileManager.update(profile)
|
||||
_ = try await ProfileManager.update(profile)
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
return
|
||||
@@ -194,9 +207,7 @@ public struct EditProfileView: View {
|
||||
if let updateCallback {
|
||||
updateCallback()
|
||||
} else {
|
||||
await MainActor.run {
|
||||
NotificationCenter.default.post(name: ProfileView.notificationName, object: nil)
|
||||
}
|
||||
NotificationCenter.default.post(name: ProfileView.notificationName, object: nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import Library
|
||||
import SwiftUI
|
||||
|
||||
#if os(macOS)
|
||||
@MainActor
|
||||
public struct EditProfileWindowView: View {
|
||||
public static let windowID = "edit-profile"
|
||||
|
||||
@@ -21,7 +22,7 @@ import SwiftUI
|
||||
viewBuilder {
|
||||
if isLoading {
|
||||
ProgressView().onAppear {
|
||||
Task.detached {
|
||||
Task {
|
||||
await doReload()
|
||||
}
|
||||
}
|
||||
@@ -41,7 +42,7 @@ import SwiftUI
|
||||
return
|
||||
}
|
||||
do {
|
||||
profile = try ProfileManager.get(profileID)
|
||||
profile = try await ProfileManager.get(profileID)
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
return
|
||||
@@ -53,4 +54,5 @@ import SwiftUI
|
||||
isLoading = false
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
import Library
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
public struct ImportProfileView: View {
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
@@ -13,9 +14,9 @@
|
||||
@State private var alert: Alert?
|
||||
@State private var connection: NWSocket?
|
||||
@State private var profiles: [LibboxProfilePreview]?
|
||||
private let callback: () -> Void
|
||||
private let callback: () async -> Void
|
||||
|
||||
public init(callback: @escaping () -> Void) {
|
||||
public init(callback: @escaping () async -> Void) {
|
||||
self.callback = callback
|
||||
}
|
||||
|
||||
@@ -26,7 +27,7 @@
|
||||
.applicationService(name: "sing-box:profile"))
|
||||
{ endpoint in
|
||||
selected = true
|
||||
Task.detached {
|
||||
Task {
|
||||
await handleEndpoint(endpoint)
|
||||
}
|
||||
} label: {
|
||||
@@ -42,7 +43,7 @@
|
||||
ForEach(profiles, id: \.profileID) { profile in
|
||||
Button(profile.name) {
|
||||
isLoading = true
|
||||
Task.detached {
|
||||
Task {
|
||||
selectProfile(profileID: profile.profileID)
|
||||
isLoading = false
|
||||
}
|
||||
@@ -68,14 +69,14 @@
|
||||
self.connection = NWSocket(connection)
|
||||
connection.start(queue: .global())
|
||||
do {
|
||||
try loopMessages()
|
||||
try await loopMessages()
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
reset()
|
||||
}
|
||||
}
|
||||
|
||||
private func loopMessages() throws {
|
||||
private func loopMessages() async throws {
|
||||
guard let connection else {
|
||||
return
|
||||
}
|
||||
@@ -110,7 +111,7 @@
|
||||
if let error {
|
||||
throw error
|
||||
}
|
||||
try importProfile(content!)
|
||||
try await importProfile(content!)
|
||||
default:
|
||||
throw NSError(domain: "unknown message type \(message[0])", code: 0)
|
||||
}
|
||||
@@ -131,7 +132,7 @@
|
||||
}
|
||||
}
|
||||
|
||||
private func importProfile(_ content: LibboxProfileContent) throws {
|
||||
private nonisolated func importProfile(_ content: LibboxProfileContent) async throws {
|
||||
var type: ProfileType = .local
|
||||
switch content.type {
|
||||
case LibboxProfileTypeLocal:
|
||||
@@ -143,8 +144,7 @@
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
let nextProfileID = try ProfileManager.nextID()
|
||||
let nextProfileID = try await ProfileManager.nextID()
|
||||
let profileConfigDirectory = FilePath.sharedDirectory.appendingPathComponent("configs", isDirectory: true)
|
||||
try FileManager.default.createDirectory(at: profileConfigDirectory, withIntermediateDirectories: true)
|
||||
let profileConfig = profileConfigDirectory.appendingPathComponent("config_\(nextProfileID).json")
|
||||
@@ -153,10 +153,10 @@
|
||||
if content.lastUpdated > 0 {
|
||||
lastUpdated = Date(timeIntervalSince1970: Double(content.lastUpdated))
|
||||
}
|
||||
try ProfileManager.create(Profile(name: content.name, type: type, path: profileConfig.relativePath, remoteURL: content.remotePath, autoUpdate: content.autoUpdate, lastUpdated: lastUpdated))
|
||||
DispatchQueue.main.async {
|
||||
try await ProfileManager.create(Profile(name: content.name, type: type, path: profileConfig.relativePath, remoteURL: content.remotePath, autoUpdate: content.autoUpdate, lastUpdated: lastUpdated))
|
||||
await callback()
|
||||
await MainActor.run {
|
||||
dismiss()
|
||||
callback()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import Libbox
|
||||
import Library
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
public struct NewProfileView: View {
|
||||
#if os(macOS)
|
||||
public static let windowID = "new-profile"
|
||||
@@ -16,6 +17,8 @@ public struct NewProfileView: View {
|
||||
@State private var fileImport = false
|
||||
@State private var fileURL: URL!
|
||||
@State private var remotePath = ""
|
||||
@State private var autoUpdate = true
|
||||
@State private var autoUpdateInterval: Int32 = 60
|
||||
@State private var pickerPresented = false
|
||||
@State private var alert: Alert?
|
||||
|
||||
@@ -24,8 +27,8 @@ public struct NewProfileView: View {
|
||||
public let url: String
|
||||
}
|
||||
|
||||
private let callback: (() -> Void)?
|
||||
public init(_ importRequest: ImportRequest? = nil, _ callback: (() -> Void)? = nil) {
|
||||
private let callback: (() async -> Void)?
|
||||
public init(_ importRequest: ImportRequest? = nil, _ callback: (() async -> Void)? = nil) {
|
||||
self.callback = callback
|
||||
if let importRequest {
|
||||
_profileName = .init(initialValue: importRequest.name)
|
||||
@@ -87,12 +90,20 @@ public struct NewProfileView: View {
|
||||
TextField("URL", text: $remotePath, prompt: Text("Required"))
|
||||
.multilineTextAlignment(.trailing)
|
||||
}
|
||||
Toggle("Auto Update", isOn: $autoUpdate)
|
||||
FormItem("Auto Update Interval") {
|
||||
TextField("Auto Update Interval", text: $autoUpdateInterval.stringBinding(defaultValue: 60), prompt: Text("In Minutes"))
|
||||
.multilineTextAlignment(.trailing)
|
||||
#if os(iOS)
|
||||
.keyboardType(.numberPad)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
Section {
|
||||
if !isSaving {
|
||||
Button("Create") {
|
||||
isSaving = true
|
||||
Task.detached {
|
||||
Task {
|
||||
await createProfile()
|
||||
}
|
||||
}
|
||||
@@ -140,21 +151,19 @@ public struct NewProfileView: View {
|
||||
}
|
||||
}
|
||||
do {
|
||||
try createProfile0()
|
||||
try await createProfileBackground()
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
return
|
||||
}
|
||||
await MainActor.run {
|
||||
dismiss()
|
||||
if let callback {
|
||||
callback()
|
||||
}
|
||||
#if os(macOS)
|
||||
NotificationCenter.default.post(name: ProfileView.notificationName, object: nil)
|
||||
resetFields()
|
||||
#endif
|
||||
if let callback {
|
||||
await callback()
|
||||
}
|
||||
dismiss()
|
||||
#if os(macOS)
|
||||
NotificationCenter.default.post(name: ProfileView.notificationName, object: nil)
|
||||
resetFields()
|
||||
#endif
|
||||
}
|
||||
|
||||
private func resetFields() {
|
||||
@@ -165,25 +174,31 @@ public struct NewProfileView: View {
|
||||
remotePath = ""
|
||||
}
|
||||
|
||||
private func createProfile0() throws {
|
||||
let nextProfileID = try ProfileManager.nextID()
|
||||
private nonisolated func createProfileBackground() async throws {
|
||||
let nextProfileID = try await ProfileManager.nextID()
|
||||
|
||||
var savePath = ""
|
||||
var remoteURL: String? = nil
|
||||
var lastUpdated: Date? = nil
|
||||
|
||||
let profileName = await profileName
|
||||
let profileType = await profileType
|
||||
let fileImport = await fileImport
|
||||
let fileURL = await fileURL
|
||||
let remotePath = await remotePath
|
||||
let autoUpdate = await autoUpdate
|
||||
let autoUpdateInterval = await autoUpdateInterval
|
||||
|
||||
if profileType == .local {
|
||||
let profileConfigDirectory = FilePath.sharedDirectory.appendingPathComponent("configs", isDirectory: true)
|
||||
try FileManager.default.createDirectory(at: profileConfigDirectory, withIntermediateDirectories: true)
|
||||
let profileConfig = profileConfigDirectory.appendingPathComponent("config_\(nextProfileID).json")
|
||||
if fileImport {
|
||||
guard let fileURL else {
|
||||
alert = Alert(errorMessage: "Missing file")
|
||||
return
|
||||
throw NSError(domain: "Missing file", code: 0)
|
||||
}
|
||||
if !fileURL.startAccessingSecurityScopedResource() {
|
||||
alert = Alert(errorMessage: "Missing access to selected file")
|
||||
return
|
||||
throw NSError(domain: "Missing access to selected file", code: 0)
|
||||
}
|
||||
defer {
|
||||
fileURL.stopAccessingSecurityScopedResource()
|
||||
@@ -223,6 +238,14 @@ public struct NewProfileView: View {
|
||||
remoteURL = remotePath
|
||||
lastUpdated = .now
|
||||
}
|
||||
try ProfileManager.create(Profile(name: profileName, type: profileType, path: savePath, remoteURL: remoteURL, lastUpdated: lastUpdated))
|
||||
try await ProfileManager.create(Profile(
|
||||
name: profileName,
|
||||
type: profileType,
|
||||
path: savePath,
|
||||
remoteURL: remoteURL,
|
||||
autoUpdate: autoUpdate,
|
||||
autoUpdateInterval: autoUpdateInterval,
|
||||
lastUpdated: lastUpdated
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import Library
|
||||
import Network
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
public struct ProfileView: View {
|
||||
public static let notificationName = Notification.Name("\(FilePath.packageName).update-profile")
|
||||
|
||||
@@ -36,8 +37,8 @@ public struct ProfileView: View {
|
||||
viewBuilder {
|
||||
if isLoading {
|
||||
ProgressView().onAppear {
|
||||
Task.detached {
|
||||
doReload()
|
||||
Task {
|
||||
await doReload()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -46,9 +47,7 @@ public struct ProfileView: View {
|
||||
if let importRemoteProfileRequest {
|
||||
NavigationDestinationCompat(isPresented: $importRemoteProfilePresented) {
|
||||
NewProfileView(importRemoteProfileRequest) {
|
||||
Task.detached {
|
||||
doReload()
|
||||
}
|
||||
await doReload()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -56,9 +55,7 @@ public struct ProfileView: View {
|
||||
#if os(iOS)
|
||||
NavigationLink {
|
||||
NewProfileView {
|
||||
Task.detached {
|
||||
doReload()
|
||||
}
|
||||
await doReload()
|
||||
}
|
||||
} label: {
|
||||
Text("New Profile").foregroundColor(.accentColor)
|
||||
@@ -68,9 +65,7 @@ public struct ProfileView: View {
|
||||
Section {
|
||||
NavigationLink {
|
||||
NewProfileView {
|
||||
Task.detached {
|
||||
doReload()
|
||||
}
|
||||
await doReload()
|
||||
}
|
||||
} label: {
|
||||
Text("New Profile").foregroundColor(.accentColor)
|
||||
@@ -78,9 +73,7 @@ public struct ProfileView: View {
|
||||
if ApplicationLibrary.inPreview || devicePickerSupports(.applicationService(name: "sing-box"), parameters: { .applicationService }) {
|
||||
NavigationLink {
|
||||
ImportProfileView {
|
||||
Task.detached {
|
||||
doReload()
|
||||
}
|
||||
await doReload()
|
||||
}
|
||||
} label: {
|
||||
Text("Import Profile").foregroundColor(.accentColor)
|
||||
@@ -138,8 +131,8 @@ public struct ProfileView: View {
|
||||
#if os(macOS)
|
||||
if observer == nil {
|
||||
observer = NotificationCenter.default.addObserver(forName: ProfileView.notificationName, object: nil, queue: .main) { _ in
|
||||
Task.detached {
|
||||
doReload()
|
||||
Task {
|
||||
await doReload()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -166,11 +159,11 @@ public struct ProfileView: View {
|
||||
}
|
||||
.toolbar {
|
||||
ToolbarItem {
|
||||
Button(action: {
|
||||
Button {
|
||||
openWindow(id: NewProfileView.windowID)
|
||||
}, label: {
|
||||
} label: {
|
||||
Label("New Profile", systemImage: "plus.square.fill")
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
#elseif os(iOS)
|
||||
@@ -188,14 +181,14 @@ public struct ProfileView: View {
|
||||
title: Text("Import Profile"),
|
||||
message: Text("Are you sure to import profile \(profile.name)?"),
|
||||
primaryButton: .default(Text("Import")) {
|
||||
do {
|
||||
try profile.importProfile()
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
return
|
||||
}
|
||||
Task.detached {
|
||||
doReload()
|
||||
Task {
|
||||
do {
|
||||
try await profile.importProfile()
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
return
|
||||
}
|
||||
await doReload()
|
||||
}
|
||||
},
|
||||
secondaryButton: .cancel()
|
||||
@@ -218,28 +211,18 @@ public struct ProfileView: View {
|
||||
)
|
||||
}
|
||||
|
||||
private func deleteSelectedProfiles(_ profileID: [Int64]) {
|
||||
do {
|
||||
if try ProfileManager.delete(by: profileID) > 0 {
|
||||
isLoading = true
|
||||
}
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
}
|
||||
}
|
||||
|
||||
private func doReload() {
|
||||
defer {
|
||||
isLoading = false
|
||||
}
|
||||
private func doReload() async {
|
||||
if ApplicationLibrary.inPreview {
|
||||
profileList = [
|
||||
Profile(id: 0, name: "profile local", type: .local, path: ""),
|
||||
Profile(id: 1, name: "profile remote", type: .remote, path: "", lastUpdated: Date(timeIntervalSince1970: 0)),
|
||||
]
|
||||
} else {
|
||||
defer {
|
||||
isLoading = false
|
||||
}
|
||||
do {
|
||||
profileList = try ProfileManager.list()
|
||||
profileList = try await ProfileManager.list()
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
return
|
||||
@@ -247,37 +230,42 @@ public struct ProfileView: View {
|
||||
}
|
||||
}
|
||||
|
||||
private func updateProfile(_ profile: Profile) {
|
||||
do {
|
||||
_ = try profile.updateRemoteProfile()
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
}
|
||||
private func updateProfile(_ profile: Profile) async {
|
||||
await updateProfileBackground(profile)
|
||||
isUpdating = false
|
||||
}
|
||||
|
||||
private func deleteProfile(_ profile: Profile) {
|
||||
Task.detached {
|
||||
do {
|
||||
_ = try ProfileManager.delete(profile)
|
||||
} catch {
|
||||
private nonisolated func updateProfileBackground(_ profile: Profile) async {
|
||||
do {
|
||||
_ = try await profile.updateRemoteProfile()
|
||||
} catch {
|
||||
await MainActor.run {
|
||||
alert = Alert(error)
|
||||
return
|
||||
}
|
||||
doReload()
|
||||
}
|
||||
}
|
||||
|
||||
private func deleteProfile(_ profile: Profile) async {
|
||||
do {
|
||||
_ = try await ProfileManager.delete(profile)
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
return
|
||||
}
|
||||
await doReload()
|
||||
}
|
||||
|
||||
private func moveProfile(from source: IndexSet, to destination: Int) {
|
||||
profileList.move(fromOffsets: source, toOffset: destination)
|
||||
for (index, profile) in profileList.enumerated() {
|
||||
profile.order = UInt32(index)
|
||||
}
|
||||
do {
|
||||
try ProfileManager.update(profileList)
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
return
|
||||
Task {
|
||||
do {
|
||||
try await ProfileManager.update(profileList)
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -286,9 +274,9 @@ public struct ProfileView: View {
|
||||
profileList[index]
|
||||
}
|
||||
profileList.remove(atOffsets: profileIndex)
|
||||
Task.detached {
|
||||
Task {
|
||||
do {
|
||||
_ = try ProfileManager.delete(profileToDelete)
|
||||
_ = try await ProfileManager.delete(profileToDelete)
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
}
|
||||
@@ -315,13 +303,14 @@ public struct ProfileView: View {
|
||||
#endif
|
||||
}
|
||||
|
||||
@MainActor
|
||||
private var body0: some View {
|
||||
viewBuilder {
|
||||
#if !os(macOS)
|
||||
NavigationLink {
|
||||
EditProfileView {
|
||||
Task.detached {
|
||||
parent.doReload()
|
||||
Task {
|
||||
await parent.doReload()
|
||||
}
|
||||
}.environmentObject(profile)
|
||||
} label: {
|
||||
@@ -334,15 +323,17 @@ public struct ProfileView: View {
|
||||
if profile.type == .remote {
|
||||
Button {
|
||||
parent.isUpdating = true
|
||||
Task.detached {
|
||||
parent.updateProfile(profile)
|
||||
Task {
|
||||
await parent.updateProfile(profile)
|
||||
}
|
||||
} label: {
|
||||
Label("Update", systemImage: "arrow.clockwise")
|
||||
}
|
||||
}
|
||||
Button(role: .destructive) {
|
||||
parent.deleteProfile(profile)
|
||||
Task {
|
||||
await parent.deleteProfile(profile)
|
||||
}
|
||||
} label: {
|
||||
Label("Delete", systemImage: "trash.fill")
|
||||
}
|
||||
@@ -358,28 +349,30 @@ public struct ProfileView: View {
|
||||
}
|
||||
HStack {
|
||||
if profile.type == .remote {
|
||||
Button(action: {
|
||||
Button {
|
||||
parent.isUpdating = true
|
||||
Task.detached {
|
||||
parent.updateProfile(profile)
|
||||
Task {
|
||||
await parent.updateProfile(profile)
|
||||
}
|
||||
}, label: {
|
||||
} label: {
|
||||
Image(systemName: "arrow.clockwise")
|
||||
})
|
||||
}
|
||||
}
|
||||
ProfileShareButton(parent.$alert, profile) {
|
||||
Image(systemName: "square.and.arrow.up.fill")
|
||||
}
|
||||
Button(action: {
|
||||
Button {
|
||||
parent.openWindow(id: EditProfileWindowView.windowID, value: profile.mustID)
|
||||
}, label: {
|
||||
} label: {
|
||||
Image(systemName: "pencil")
|
||||
})
|
||||
Button(action: {
|
||||
parent.deleteProfile(profile)
|
||||
}, label: {
|
||||
}
|
||||
Button {
|
||||
Task {
|
||||
await parent.deleteProfile(profile)
|
||||
}
|
||||
} label: {
|
||||
Image(systemName: "trash.fill")
|
||||
})
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity, alignment: .trailing)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import Library
|
||||
import SwiftUI
|
||||
import UniformTypeIdentifiers
|
||||
|
||||
@MainActor
|
||||
public struct ServiceLogView: View {
|
||||
#if os(macOS)
|
||||
public static let windowID = "service-log"
|
||||
@@ -20,8 +21,8 @@ public struct ServiceLogView: View {
|
||||
viewBuilder {
|
||||
if isLoading {
|
||||
ProgressView().onAppear {
|
||||
Task.detached {
|
||||
loadContent()
|
||||
Task {
|
||||
await loadContent()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -44,8 +45,8 @@ public struct ServiceLogView: View {
|
||||
fileExporterPresented = true
|
||||
}
|
||||
Button("Delete", role: .destructive) {
|
||||
Task.detached {
|
||||
deleteContent()
|
||||
Task {
|
||||
await deleteContent()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,7 +67,8 @@ public struct ServiceLogView: View {
|
||||
#endif
|
||||
}
|
||||
|
||||
private func loadContent() {
|
||||
private nonisolated func loadContent() async {
|
||||
var content = ""
|
||||
do {
|
||||
content = try String(contentsOf: FilePath.cacheDirectory.appendingPathComponent("stderr.log"))
|
||||
} catch {}
|
||||
@@ -75,13 +77,16 @@ public struct ServiceLogView: View {
|
||||
content = try String(contentsOf: FilePath.cacheDirectory.appendingPathComponent("stderr.log.old"))
|
||||
} catch {}
|
||||
}
|
||||
isLoading = false
|
||||
await MainActor.run { [content] in
|
||||
self.content = content
|
||||
isLoading = false
|
||||
}
|
||||
}
|
||||
|
||||
private func deleteContent() {
|
||||
private nonisolated func deleteContent() async {
|
||||
try? FileManager.default.removeItem(at: FilePath.cacheDirectory.appendingPathComponent("stderr.log"))
|
||||
try? FileManager.default.removeItem(at: FilePath.cacheDirectory.appendingPathComponent("stderr.log.old"))
|
||||
DispatchQueue.main.async {
|
||||
await MainActor.run {
|
||||
dismiss()
|
||||
isLoading = true
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import SwiftUI
|
||||
import ServiceManagement
|
||||
#endif
|
||||
|
||||
@MainActor
|
||||
public struct SettingView: View {
|
||||
#if os(macOS)
|
||||
@Environment(\.openWindow) private var openWindow
|
||||
@@ -38,7 +39,7 @@ public struct SettingView: View {
|
||||
viewBuilder {
|
||||
if isLoading {
|
||||
ProgressView().onAppear {
|
||||
Task.detached {
|
||||
Task {
|
||||
await loadSettings()
|
||||
}
|
||||
}
|
||||
@@ -48,14 +49,14 @@ public struct SettingView: View {
|
||||
Section("MacOS") {
|
||||
Toggle("Start At Login", isOn: $startAtLogin)
|
||||
.onChangeCompat(of: startAtLogin) { newValue in
|
||||
Task.detached {
|
||||
Task {
|
||||
updateLoginItems(newValue)
|
||||
}
|
||||
}
|
||||
Toggle("Show in Menu Bar", isOn: showMenuBarExtra)
|
||||
.onChange(of: showMenuBarExtra.wrappedValue) { newValue in
|
||||
Task.detached {
|
||||
SharedPreferences.showMenuBarExtra = newValue
|
||||
Task {
|
||||
await SharedPreferences.showMenuBarExtra.set(newValue)
|
||||
if !newValue {
|
||||
keepMenuBarInBackground = false
|
||||
}
|
||||
@@ -64,8 +65,8 @@ public struct SettingView: View {
|
||||
if showMenuBarExtra.wrappedValue {
|
||||
Toggle("Keep Menu Bar in Background", isOn: $keepMenuBarInBackground)
|
||||
.onChangeCompat(of: keepMenuBarInBackground) { newValue in
|
||||
Task.detached {
|
||||
SharedPreferences.menuBarExtraInBackground = newValue
|
||||
Task {
|
||||
await SharedPreferences.menuBarExtraInBackground.set(newValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,22 +75,22 @@ public struct SettingView: View {
|
||||
Section("Packet Tunnel") {
|
||||
Toggle("Always On", isOn: $alwaysOn)
|
||||
.onChangeCompat(of: alwaysOn) { newValue in
|
||||
Task.detached {
|
||||
SharedPreferences.alwaysOn = newValue
|
||||
Task {
|
||||
await SharedPreferences.alwaysOn.set(newValue)
|
||||
await updateAlwaysOn(newValue)
|
||||
}
|
||||
}
|
||||
Toggle("Disable Memory Limit", isOn: $disableMemoryLimit)
|
||||
.onChangeCompat(of: disableMemoryLimit) { newValue in
|
||||
Task.detached {
|
||||
SharedPreferences.disableMemoryLimit = newValue
|
||||
Task {
|
||||
await SharedPreferences.disableMemoryLimit.set(newValue)
|
||||
}
|
||||
}
|
||||
#if !os(tvOS)
|
||||
Toggle("Include All Networks", isOn: $includeAllNetworks)
|
||||
.onChangeCompat(of: includeAllNetworks) { newValue in
|
||||
Task.detached {
|
||||
SharedPreferences.includeAllNetworks = newValue
|
||||
Task {
|
||||
await SharedPreferences.includeAllNetworks.set(newValue)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -121,8 +122,8 @@ public struct SettingView: View {
|
||||
Text("View Service Log")
|
||||
}
|
||||
Button("Clear Working Directory") {
|
||||
Task.detached {
|
||||
clearWorkingDirectory()
|
||||
Task {
|
||||
await clearWorkingDirectory()
|
||||
}
|
||||
}
|
||||
.foregroundColor(.red)
|
||||
@@ -135,8 +136,8 @@ public struct SettingView: View {
|
||||
NSWorkspace.shared.selectFile(nil, inFileViewerRootedAtPath: FilePath.workingDirectory.relativePath)
|
||||
}
|
||||
Button {
|
||||
Task.detached {
|
||||
clearWorkingDirectory()
|
||||
Task {
|
||||
await clearWorkingDirectory()
|
||||
}
|
||||
} label: {
|
||||
Text("Clear Working Directory").foregroundColor(.red)
|
||||
@@ -174,12 +175,12 @@ public struct SettingView: View {
|
||||
private func loadSettings() async {
|
||||
#if os(macOS)
|
||||
startAtLogin = SMAppService.mainApp.status == .enabled
|
||||
keepMenuBarInBackground = SharedPreferences.menuBarExtraInBackground
|
||||
keepMenuBarInBackground = await SharedPreferences.menuBarExtraInBackground.get()
|
||||
#endif
|
||||
alwaysOn = SharedPreferences.alwaysOn
|
||||
disableMemoryLimit = SharedPreferences.disableMemoryLimit
|
||||
alwaysOn = await SharedPreferences.alwaysOn.get()
|
||||
disableMemoryLimit = await SharedPreferences.disableMemoryLimit.get()
|
||||
#if !os(tvOS)
|
||||
includeAllNetworks = SharedPreferences.includeAllNetworks
|
||||
includeAllNetworks = await SharedPreferences.includeAllNetworks.get()
|
||||
#endif
|
||||
if ApplicationLibrary.inPreview {
|
||||
version = "<redacted>"
|
||||
@@ -191,13 +192,22 @@ public struct SettingView: View {
|
||||
dataSize = "Loading..."
|
||||
taiwanFlagAvailable = !DeviceCensorship.isChinaDevice()
|
||||
isLoading = false
|
||||
dataSize = (try? FilePath.workingDirectory.formattedSize()) ?? "Unknown"
|
||||
await loadSettingsBackground()
|
||||
}
|
||||
}
|
||||
|
||||
private func clearWorkingDirectory() {
|
||||
private nonisolated func loadSettingsBackground() async {
|
||||
let dataSize = (try? FilePath.workingDirectory.formattedSize()) ?? "Unknown"
|
||||
await MainActor.run {
|
||||
self.dataSize = dataSize
|
||||
}
|
||||
}
|
||||
|
||||
private nonisolated func clearWorkingDirectory() async {
|
||||
try? FileManager.default.removeItem(at: FilePath.workingDirectory)
|
||||
isLoading = true
|
||||
await MainActor.run {
|
||||
isLoading = true
|
||||
}
|
||||
}
|
||||
|
||||
private func updateAlwaysOn(_ newState: Bool) async {
|
||||
|
||||
Reference in New Issue
Block a user