passepartout-apple/Passepartout/App/Views/ProfileView+Rename.swift

126 lines
3.7 KiB
Swift
Raw Normal View History

2022-04-12 13:09:14 +00:00
//
// ProfileView.swift
// Passepartout
//
// Created by Davide De Rosa on 2/6/22.
2023-03-17 15:56:19 +00:00
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
2022-04-12 13:09:14 +00:00
//
// https://github.com/passepartoutvpn
//
// This file is part of Passepartout.
//
// Passepartout is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Passepartout is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
//
2022-06-23 21:31:01 +00:00
import PassepartoutLibrary
import SwiftUI
2022-04-12 13:09:14 +00:00
extension ProfileView {
struct RenameView: View {
@Environment(\.presentationMode) private var presentationMode
2023-03-17 20:55:47 +00:00
@ObservedObject private var profileManager: ProfileManager
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
@ObservedObject private var currentProfile: ObservableProfile
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
@State private var newName = ""
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
@State private var isOverwritingExistingProfile = false
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
init(currentProfile: ObservableProfile) {
profileManager = .shared
self.currentProfile = currentProfile
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
var body: some View {
List {
Section {
2022-04-12 13:09:14 +00:00
TextField(L10n.Global.Placeholders.profileName, text: $newName, onCommit: commitRenaming)
.themeValidProfileName()
2022-04-12 13:09:14 +00:00
.onAppear(perform: loadCurrentName)
} header: {
Text(L10n.Profile.Alerts.Rename.title)
2022-04-12 13:09:14 +00:00
}
}.themeSecondaryView()
.navigationTitle(currentProfile.value.header.name)
.toolbar {
themeCloseItem(presentationMode: presentationMode)
ToolbarItem(placement: .primaryAction) {
Button(action: commitRenaming, label: themeSaveButtonLabel)
2022-04-12 13:09:14 +00:00
}
}.alert(
L10n.Profile.Alerts.Rename.title,
isPresented: $isOverwritingExistingProfile,
actions: alertOverwriteActions,
message: alertOverwriteMessage
)
2022-04-12 13:09:14 +00:00
}
}
}
2023-03-17 20:55:47 +00:00
// MARK: -
private extension ProfileView.RenameView {
2023-03-17 20:55:47 +00:00
@ViewBuilder
func alertOverwriteActions() -> some View {
Button(role: .destructive) {
commitRenaming(force: true)
} label: {
Text(L10n.Global.Strings.ok)
2022-04-12 13:09:14 +00:00
}
Button(role: .cancel) {
} label: {
Text(L10n.Global.Strings.cancel)
2022-04-12 13:09:14 +00:00
}
}
func alertOverwriteMessage() -> some View {
Text(L10n.AddProfile.Shared.Alerts.Overwrite.message)
}
}
// MARK: -
2022-04-12 13:09:14 +00:00
private extension ProfileView.RenameView {
func loadCurrentName() {
newName = currentProfile.value.header.name
}
2022-04-12 13:09:14 +00:00
func commitRenaming() {
commitRenaming(force: false)
}
2022-04-12 13:09:14 +00:00
func commitRenaming(force: Bool) {
let name = newName.stripped
2022-04-12 13:09:14 +00:00
guard !name.isEmpty else {
return
}
guard name != currentProfile.value.header.name else {
2022-04-12 13:09:14 +00:00
presentationMode.wrappedValue.dismiss()
return
}
guard force || !profileManager.isExistingProfile(withName: name) else {
isOverwritingExistingProfile = true
return
2022-04-12 13:09:14 +00:00
}
let renamed = currentProfile.value.renamed(to: name)
profileManager.saveProfile(renamed, isActive: nil)
presentationMode.wrappedValue.dismiss()
2022-04-12 13:09:14 +00:00
}
}