2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// AddProviderView+Name.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 3/19/22.
|
2024-01-14 13:34:21 +00:00
|
|
|
// Copyright (c) 2024 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 AddProviderView {
|
|
|
|
struct NameView: View {
|
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
|
|
|
@Binding private var profile: Profile
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private let providerMetadata: ProviderMetadata
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private let bindings: AddProfileView.Bindings
|
|
|
|
|
|
|
|
@State private var viewModel = ViewModel()
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
@State private var isEnteringCredentials = false
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-07-23 11:28:47 +00:00
|
|
|
@FocusState private var focusedField: AddProfileView.Field?
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
init(
|
|
|
|
profile: Binding<Profile>,
|
|
|
|
providerMetadata: ProviderMetadata,
|
|
|
|
bindings: AddProfileView.Bindings
|
|
|
|
) {
|
|
|
|
profileManager = .shared
|
|
|
|
_profile = profile
|
|
|
|
self.providerMetadata = providerMetadata
|
|
|
|
self.bindings = bindings
|
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
ZStack {
|
2022-04-22 17:02:54 +00:00
|
|
|
hiddenAccountLink
|
2022-04-12 13:09:14 +00:00
|
|
|
List {
|
|
|
|
AddProfileView.ProfileNameSection(
|
|
|
|
profileName: $viewModel.profileName,
|
2023-07-23 11:28:47 +00:00
|
|
|
focusedField: $focusedField,
|
2022-04-12 13:09:14 +00:00
|
|
|
errorMessage: viewModel.errorMessage
|
|
|
|
) {
|
|
|
|
saveProfile(replacingExisting: false)
|
2022-04-21 10:13:29 +00:00
|
|
|
}.onAppear {
|
|
|
|
viewModel.presetName(withMetadata: providerMetadata)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
let headers = profileManager.headers.sorted()
|
|
|
|
if !headers.isEmpty {
|
|
|
|
AddProfileView.ExistingProfilesSection(
|
|
|
|
headers: headers,
|
|
|
|
profileName: $viewModel.profileName
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2022-04-21 13:56:26 +00:00
|
|
|
}.toolbar {
|
|
|
|
Button {
|
|
|
|
saveProfile(replacingExisting: false)
|
|
|
|
} label: {
|
|
|
|
themeSaveButtonLabel()
|
|
|
|
}
|
2023-07-03 14:41:49 +00:00
|
|
|
}.alert(
|
|
|
|
L10n.AddProfile.Shared.title,
|
|
|
|
isPresented: $viewModel.isAskingOverwrite,
|
|
|
|
actions: alertOverwriteActions,
|
|
|
|
message: alertOverwriteMessage
|
|
|
|
).navigationTitle(providerMetadata.fullName)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: -
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
private extension AddProviderView.NameView {
|
|
|
|
var hiddenAccountLink: some View {
|
|
|
|
NavigationLink("", isActive: $isEnteringCredentials) {
|
|
|
|
AddProfileView.AccountWrapperView(
|
|
|
|
profile: $profile,
|
|
|
|
bindings: bindings
|
|
|
|
)
|
2022-04-22 17:02:54 +00:00
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
@ViewBuilder
|
|
|
|
func alertOverwriteActions() -> some View {
|
|
|
|
Button(role: .destructive) {
|
|
|
|
saveProfile(replacingExisting: true)
|
|
|
|
} label: {
|
|
|
|
Text(L10n.Global.Strings.ok)
|
|
|
|
}
|
|
|
|
Button(role: .cancel) {
|
|
|
|
} label: {
|
|
|
|
Text(L10n.Global.Strings.cancel)
|
2023-07-03 14:41:49 +00:00
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func alertOverwriteMessage() -> some View {
|
|
|
|
Text(L10n.AddProfile.Shared.Alerts.Overwrite.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: -
|
2023-07-03 14:41:49 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
private extension AddProviderView.NameView {
|
|
|
|
func saveProfile(replacingExisting: Bool) {
|
|
|
|
let addedProfile = viewModel.addProfile(
|
|
|
|
profile,
|
|
|
|
to: profileManager,
|
|
|
|
replacingExisting: replacingExisting
|
|
|
|
)
|
|
|
|
guard let addedProfile = addedProfile else {
|
|
|
|
return
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
profile = addedProfile
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
if profile.requiresCredentials {
|
|
|
|
isEnteringCredentials = true
|
|
|
|
} else {
|
|
|
|
bindings.isPresented = false
|
|
|
|
profileManager.didCreateProfile.send(profile)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|