Refactor profile management

This commit is contained in:
世界
2025-12-03 11:15:56 +08:00
parent adf4e5c75c
commit 727136f187
22 changed files with 1635 additions and 787 deletions
+2 -1
View File
@@ -52,7 +52,8 @@ public extension LibboxProfileContent {
if lastUpdated > 0 {
lastUpdatedAt = Date(timeIntervalSince1970: Double(lastUpdated))
}
try await ProfileManager.create(Profile(name: name, type: ProfileType(rawValue: Int(type))!, path: profileConfig.relativePath, remoteURL: remotePath, autoUpdate: autoUpdate, autoUpdateInterval: autoUpdateInterval, lastUpdated: lastUpdatedAt))
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))
}
func generateShareFile() throws -> URL {
+16
View File
@@ -95,4 +95,20 @@ public enum ProfileManager {
try UInt32(Profile.fetchCount(db))
}
}
public nonisolated static func uniqueName(_ baseName: String) async throws -> String {
let profiles = try await list()
let existingNames = Set(profiles.map(\.name))
if !existingNames.contains(baseName) {
return baseName
}
var counter = 1
while true {
let candidate = "\(baseName) (\(counter))"
if !existingNames.contains(candidate) {
return candidate
}
counter += 1
}
}
}