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
|
2023-04-04 07:50:45 +00:00
|
|
|
import SwiftUI
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
extension ProfileView {
|
|
|
|
struct RenameView: View {
|
2023-07-23 11:28:47 +00:00
|
|
|
enum Field {
|
|
|
|
case name
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
@Environment(\.presentationMode) private var presentationMode
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-08-28 07:19:15 +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
|
|
|
|
2023-07-23 11:28:47 +00:00
|
|
|
@FocusState private var focusedField: Field?
|
|
|
|
|
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 {
|
2022-05-01 17:43:33 +00:00
|
|
|
Section {
|
2022-04-12 13:09:14 +00:00
|
|
|
TextField(L10n.Global.Placeholders.profileName, text: $newName, onCommit: commitRenaming)
|
2022-04-25 20:44:24 +00:00
|
|
|
.themeValidProfileName()
|
2023-07-23 11:28:47 +00:00
|
|
|
.focused($focusedField, equals: .name)
|
2022-04-12 13:09:14 +00:00
|
|
|
.onAppear(perform: loadCurrentName)
|
2022-05-01 17:43:33 +00:00
|
|
|
} header: {
|
|
|
|
Text(L10n.Profile.Alerts.Rename.title)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}.themeSecondaryView()
|
|
|
|
.navigationTitle(currentProfile.value.header.name)
|
|
|
|
.toolbar {
|
2022-04-19 08:10:06 +00:00
|
|
|
themeCloseItem(presentationMode: presentationMode)
|
|
|
|
ToolbarItem(placement: .primaryAction) {
|
2022-04-20 06:49:19 +00:00
|
|
|
Button(action: commitRenaming, label: themeSaveButtonLabel)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-07-03 14:41:49 +00:00
|
|
|
}.alert(
|
|
|
|
L10n.Profile.Alerts.Rename.title,
|
|
|
|
isPresented: $isOverwritingExistingProfile,
|
|
|
|
actions: alertOverwriteActions,
|
|
|
|
message: alertOverwriteMessage
|
|
|
|
)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
// MARK: -
|
2023-07-03 14:41:49 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
private extension ProfileView.RenameView {
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-07-03 14:54:43 +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
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
Button(role: .cancel) {
|
|
|
|
} label: {
|
|
|
|
Text(L10n.Global.Strings.cancel)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func alertOverwriteMessage() -> some View {
|
|
|
|
Text(L10n.AddProfile.Shared.Alerts.Overwrite.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: -
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
private extension ProfileView.RenameView {
|
|
|
|
func loadCurrentName() {
|
|
|
|
newName = currentProfile.value.header.name
|
2023-07-23 11:28:47 +00:00
|
|
|
focusedField = .name
|
2023-07-03 14:54:43 +00:00
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
func commitRenaming() {
|
|
|
|
commitRenaming(force: false)
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
func commitRenaming(force: Bool) {
|
|
|
|
let name = newName.stripped
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2023-07-03 14:54:43 +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()
|
2023-07-03 14:54:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
guard force || !profileManager.isExistingProfile(withName: name) else {
|
|
|
|
isOverwritingExistingProfile = true
|
|
|
|
return
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-07-03 14:54:43 +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
|
|
|
}
|
|
|
|
}
|