Add duplicate to profile menu

Contextually switch current profile to duplicate.
This commit is contained in:
Davide De Rosa 2022-04-27 16:47:25 +02:00
parent 496abc6c89
commit 02b2e3194e
3 changed files with 26 additions and 10 deletions

View File

@ -109,7 +109,10 @@ extension OrganizerView {
isActive: profileManager.isActiveProfile(header.id)
)
}.contextMenu {
ProfileView.DuplicateButton(header: header)
ProfileView.DuplicateButton(
header: header,
switchCurrentProfile: false
)
}.themeTextButtonStyle()
}

View File

@ -74,10 +74,10 @@ extension ProfileView {
RenameButton(
modalType: $modalType
)
// should contextually set duplicate as current profile
// DuplicateButton(
// header: currentProfile.value.header
// )
DuplicateButton(
header: currentProfile.value.header,
switchCurrentProfile: true
)
uninstallVPNButton
Divider()
deleteProfileButton
@ -187,11 +187,14 @@ extension ProfileView {
struct DuplicateButton: View {
@ObservedObject private var profileManager: ProfileManager
let header: Profile.Header
private let header: Profile.Header
init(header: Profile.Header) {
private let switchCurrentProfile: Bool
init(header: Profile.Header, switchCurrentProfile: Bool) {
profileManager = .shared
self.header = header
self.switchCurrentProfile = switchCurrentProfile
}
var body: some View {
@ -203,7 +206,16 @@ extension ProfileView {
}
private func duplicateProfile(withId id: UUID) {
profileManager.duplicateProfile(withId: id)
guard let copy = profileManager.duplicateProfile(withId: id) else {
return
}
if switchCurrentProfile {
do {
try profileManager.loadCurrentProfile(withId: copy.id, makeReady: true)
} catch {
pp_log.warning("Unable to load profile duplicate: \(error)")
}
}
}
}
}

View File

@ -238,15 +238,16 @@ extension ProfileManager {
removeProfiles(withIds: ids)
}
public func duplicateProfile(withId id: UUID) {
public func duplicateProfile(withId id: UUID) -> Profile? {
guard let source = profile(withId: id) else {
return
return nil
}
let copy = source
.renamedUniquely(withLastUpdate: false)
.withNewId()
saveProfile(copy, isActive: nil)
return copy
}
public func persist() {