Auto-select newly created or imported profile

This commit is contained in:
世界
2025-12-07 18:12:47 +08:00
parent 9b25243b6a
commit 2da9397ef6
3 changed files with 28 additions and 11 deletions
@@ -143,7 +143,9 @@
lastUpdated = Date(timeIntervalSince1970: Double(content.lastUpdated)) lastUpdated = Date(timeIntervalSince1970: Double(content.lastUpdated))
} }
let uniqueProfileName = try await ProfileManager.uniqueName(content.name) let uniqueProfileName = try await ProfileManager.uniqueName(content.name)
try await ProfileManager.create(Profile(name: uniqueProfileName, type: type, path: profileConfig.relativePath, remoteURL: content.remotePath, autoUpdate: content.autoUpdate, lastUpdated: lastUpdated)) let profile = Profile(name: uniqueProfileName, type: type, path: profileConfig.relativePath, remoteURL: content.remotePath, autoUpdate: content.autoUpdate, lastUpdated: lastUpdated)
try await ProfileManager.create(profile)
await SharedPreferences.selectedProfileID.set(profile.mustID)
await reset() await reset()
await MainActor.run { await MainActor.run {
environments.profileUpdate.send() environments.profileUpdate.send()
@@ -63,13 +63,15 @@ public struct NewProfileMenuView: View {
handleFileImport(result) handleFileImport(result)
} }
.sheet(isPresented: $showNewProfile) { .sheet(isPresented: $showNewProfile) {
NewProfileView(onSuccess: { _ in NewProfileView(onSuccess: { profile in
await SharedPreferences.selectedProfileID.set(profile.mustID)
dismiss() dismiss()
}) })
.environmentObject(environments) .environmentObject(environments)
} }
.sheet(item: $localImportRequest) { request in .sheet(item: $localImportRequest) { request in
NewProfileView(localImportRequest: request, onSuccess: { _ in NewProfileView(localImportRequest: request, onSuccess: { profile in
await SharedPreferences.selectedProfileID.set(profile.mustID)
dismiss() dismiss()
}) })
.environmentObject(environments) .environmentObject(environments)
@@ -80,11 +82,17 @@ public struct NewProfileMenuView: View {
private var otherBody: some View { private var otherBody: some View {
Group { Group {
if let request = importRequest { if let request = importRequest {
NewProfileView(request) NewProfileView(request, onSuccess: { profile in
.environmentObject(environments) await SharedPreferences.selectedProfileID.set(profile.mustID)
dismiss()
})
.environmentObject(environments)
} else if let request = localImportRequest { } else if let request = localImportRequest {
NewProfileView(localImportRequest: request) NewProfileView(localImportRequest: request, onSuccess: { profile in
.environmentObject(environments) await SharedPreferences.selectedProfileID.set(profile.mustID)
dismiss()
})
.environmentObject(environments)
} else { } else {
menuContent menuContent
} }
@@ -156,8 +164,11 @@ public struct NewProfileMenuView: View {
} }
#else #else
FormNavigationLink { FormNavigationLink {
NewProfileView() NewProfileView(onSuccess: { profile in
.environmentObject(environments) await SharedPreferences.selectedProfileID.set(profile.mustID)
dismiss()
})
.environmentObject(environments)
} label: { } label: {
Label("Create Manually", systemImage: "square.and.pencil") Label("Create Manually", systemImage: "square.and.pencil")
} }
+6 -2
View File
@@ -42,7 +42,8 @@ public extension LibboxProfileContent {
return content! return content!
} }
func importProfile() async throws { @discardableResult
func importProfile() async throws -> Profile {
let nextProfileID = try await ProfileManager.nextID() let nextProfileID = try await ProfileManager.nextID()
let profileConfigDirectory = FilePath.sharedDirectory.appendingPathComponent("configs", isDirectory: true) let profileConfigDirectory = FilePath.sharedDirectory.appendingPathComponent("configs", isDirectory: true)
try FileManager.default.createDirectory(at: profileConfigDirectory, withIntermediateDirectories: true) try FileManager.default.createDirectory(at: profileConfigDirectory, withIntermediateDirectories: true)
@@ -53,7 +54,10 @@ public extension LibboxProfileContent {
lastUpdatedAt = Date(timeIntervalSince1970: Double(lastUpdated)) lastUpdatedAt = Date(timeIntervalSince1970: Double(lastUpdated))
} }
let uniqueProfileName = try await ProfileManager.uniqueName(name) let uniqueProfileName = try await ProfileManager.uniqueName(name)
try await ProfileManager.create(Profile(name: uniqueProfileName, type: ProfileType(rawValue: Int(type))!, path: profileConfig.relativePath, remoteURL: remotePath, autoUpdate: autoUpdate, autoUpdateInterval: autoUpdateInterval, lastUpdated: lastUpdatedAt)) let profile = Profile(name: uniqueProfileName, type: ProfileType(rawValue: Int(type))!, path: profileConfig.relativePath, remoteURL: remotePath, autoUpdate: autoUpdate, autoUpdateInterval: autoUpdateInterval, lastUpdated: lastUpdatedAt)
try await ProfileManager.create(profile)
await SharedPreferences.selectedProfileID.set(profile.mustID)
return profile
} }
func generateShareFile() throws -> URL { func generateShareFile() throws -> URL {