Refactor profile management
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user