2022-04-26 20:43:42 +00:00
|
|
|
//
|
|
|
|
// AddHostView+Name.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 3/18/22.
|
2023-03-17 15:56:19 +00:00
|
|
|
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
|
2022-04-26 20:43:42 +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-26 20:43:42 +00:00
|
|
|
import TunnelKitOpenVPN
|
|
|
|
import TunnelKitWireGuard
|
|
|
|
|
|
|
|
extension AddHostView {
|
|
|
|
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-26 20:43:42 +00:00
|
|
|
private let url: URL
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-26 20:43:42 +00:00
|
|
|
private let deletingURLOnSuccess: Bool
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-26 20:43:42 +00:00
|
|
|
private let bindings: AddProfileView.Bindings
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-26 20:43:42 +00:00
|
|
|
@State private var viewModel = ViewModel()
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-26 20:43:42 +00:00
|
|
|
@State private var isEnteringCredentials = false
|
2022-04-26 20:56:41 +00:00
|
|
|
|
2023-07-23 11:28:47 +00:00
|
|
|
@FocusState private var focusedField: AddProfileView.Field?
|
|
|
|
|
2022-04-26 20:43:42 +00:00
|
|
|
init(
|
|
|
|
url: URL,
|
|
|
|
deletingURLOnSuccess: Bool,
|
|
|
|
bindings: AddProfileView.Bindings
|
|
|
|
) {
|
|
|
|
profileManager = .shared
|
|
|
|
self.url = url
|
|
|
|
self.deletingURLOnSuccess = deletingURLOnSuccess
|
|
|
|
self.bindings = bindings
|
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
ZStack {
|
|
|
|
hiddenAccountLink
|
|
|
|
List {
|
2022-04-26 20:56:41 +00:00
|
|
|
mainView
|
2022-04-26 20:43:42 +00:00
|
|
|
}.themeAnimation(on: viewModel)
|
|
|
|
}.toolbar {
|
|
|
|
themeCloseItem(isPresented: bindings.$isPresented)
|
|
|
|
ToolbarItem(placement: .primaryAction) {
|
|
|
|
Button(nextString) {
|
|
|
|
if !viewModel.processedProfile.isPlaceholder {
|
|
|
|
saveProfile()
|
|
|
|
} else {
|
|
|
|
processProfile(replacingExisting: false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-03 14:41:49 +00:00
|
|
|
}.alert(
|
|
|
|
L10n.AddProfile.Shared.title,
|
|
|
|
isPresented: $viewModel.isAskingOverwrite,
|
|
|
|
actions: alertOverwriteActions,
|
|
|
|
message: alertOverwriteMessage
|
2023-07-23 11:28:47 +00:00
|
|
|
).onChange(of: viewModel.requiresPassphrase) {
|
|
|
|
if $0 {
|
|
|
|
focusedField = .passphrase
|
|
|
|
}
|
|
|
|
}.onAppear(perform: requestResourcePermissions)
|
2022-04-26 20:43:42 +00:00
|
|
|
.onDisappear(perform: dropResourcePermissions)
|
|
|
|
.navigationTitle(L10n.AddProfile.Shared.title)
|
|
|
|
.themeSecondaryView()
|
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-26 20:43:42 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
// MARK: -
|
2022-04-26 20:56:41 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
private extension AddHostView.NameView {
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
@ViewBuilder
|
|
|
|
var mainView: some View {
|
|
|
|
AddProfileView.ProfileNameSection(
|
|
|
|
profileName: $viewModel.profileName,
|
2023-07-23 11:28:47 +00:00
|
|
|
focusedField: $focusedField,
|
2023-07-03 14:54:43 +00:00
|
|
|
errorMessage: viewModel.errorMessage
|
|
|
|
) {
|
|
|
|
processProfile(replacingExisting: false)
|
|
|
|
}.onAppear {
|
|
|
|
viewModel.presetName(withURL: url)
|
|
|
|
}.disabled(isComplete)
|
|
|
|
|
|
|
|
if !isComplete {
|
|
|
|
if viewModel.requiresPassphrase {
|
|
|
|
encryptionSection
|
2022-04-26 20:43:42 +00:00
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
let headers = profileManager.headers.sorted()
|
|
|
|
if !headers.isEmpty {
|
|
|
|
AddProfileView.ExistingProfilesSection(
|
|
|
|
headers: headers,
|
|
|
|
profileName: $viewModel.profileName
|
|
|
|
)
|
2022-04-26 20:43:42 +00:00
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
} else {
|
|
|
|
completeSection
|
2022-04-26 20:43:42 +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
|
|
|
var encryptionSection: some View {
|
|
|
|
Section {
|
|
|
|
SecureField(L10n.AddProfile.Host.Sections.Encryption.footer, text: $viewModel.encryptionPassphrase) {
|
|
|
|
processProfile(replacingExisting: false)
|
2023-07-23 11:28:47 +00:00
|
|
|
}.focused($focusedField, equals: .passphrase)
|
2023-07-03 14:54:43 +00:00
|
|
|
} header: {
|
|
|
|
Text(L10n.Global.Strings.encryption)
|
2022-04-26 20:43:42 +00:00
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
}
|
2022-04-26 20:43:42 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
var completeSection: some View {
|
|
|
|
Section {
|
|
|
|
Text(Unlocalized.Network.url)
|
|
|
|
.withTrailingText(url.lastPathComponent)
|
|
|
|
viewModel.processedProfile.vpnProtocols.first.map {
|
|
|
|
Text(L10n.Global.Strings.protocol)
|
|
|
|
.withTrailingText($0.description)
|
2022-04-26 20:43:42 +00:00
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
} header: {
|
|
|
|
Text(L10n.AddProfile.Shared.title)
|
|
|
|
} footer: {
|
|
|
|
themeErrorMessage(viewModel.errorMessage)
|
2022-04-26 20:43:42 +00:00
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
}
|
2022-04-26 20:43:42 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
var hiddenAccountLink: some View {
|
|
|
|
NavigationLink("", isActive: $isEnteringCredentials) {
|
|
|
|
AddProfileView.AccountWrapperView(
|
|
|
|
profile: $viewModel.processedProfile,
|
|
|
|
bindings: bindings
|
|
|
|
)
|
2022-04-26 20:43:42 +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
|
|
|
var nextString: String {
|
|
|
|
if !viewModel.processedProfile.isPlaceholder {
|
|
|
|
return viewModel.processedProfile.requiresCredentials ? L10n.Global.Strings.next : L10n.Global.Strings.save
|
|
|
|
} else {
|
|
|
|
return L10n.Global.Strings.next
|
2022-04-26 20:43:42 +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
|
|
|
@ViewBuilder
|
|
|
|
func alertOverwriteActions() -> some View {
|
|
|
|
Button(role: .destructive) {
|
|
|
|
processProfile(replacingExisting: true)
|
|
|
|
} label: {
|
|
|
|
Text(L10n.Global.Strings.ok)
|
2023-07-03 14:41:49 +00:00
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
Button(role: .cancel) {
|
|
|
|
} label: {
|
|
|
|
Text(L10n.Global.Strings.cancel)
|
2022-04-26 20:43:42 +00:00
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
}
|
2022-04-26 20:43:42 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
func alertOverwriteMessage() -> some View {
|
|
|
|
Text(L10n.AddProfile.Shared.Alerts.Overwrite.message)
|
|
|
|
}
|
2022-04-26 20:43:42 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
var isComplete: Bool {
|
|
|
|
!viewModel.processedProfile.isPlaceholder
|
|
|
|
}
|
|
|
|
}
|
2022-04-26 20:43:42 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
// MARK: -
|
|
|
|
|
|
|
|
private extension AddHostView.NameView {
|
|
|
|
func requestResourcePermissions() {
|
|
|
|
_ = url.startAccessingSecurityScopedResource()
|
|
|
|
}
|
|
|
|
|
|
|
|
func dropResourcePermissions() {
|
|
|
|
url.stopAccessingSecurityScopedResource()
|
|
|
|
}
|
|
|
|
|
|
|
|
func processProfile(replacingExisting: Bool) {
|
|
|
|
viewModel.processURL(
|
|
|
|
url,
|
|
|
|
with: profileManager,
|
|
|
|
replacingExisting: replacingExisting,
|
|
|
|
deletingURLOnSuccess: deletingURLOnSuccess
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveProfile() {
|
|
|
|
let result = viewModel.addProcessedProfile(to: profileManager)
|
|
|
|
guard result else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let profile = viewModel.processedProfile
|
|
|
|
if profile.requiresCredentials {
|
|
|
|
isEnteringCredentials = true
|
|
|
|
} else {
|
|
|
|
bindings.isPresented = false
|
|
|
|
profileManager.didCreateProfile.send(profile)
|
2022-04-26 20:43:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|