Refactor task usage and profile auto update
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user