passepartout-apple/Passepartout/App/Views/AddProviderView+Name.swift

145 lines
4.4 KiB
Swift
Raw Normal View History

2022-04-12 13:09:14 +00:00
//
// AddProviderView+Name.swift
// Passepartout
//
// Created by Davide De Rosa on 3/19/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 AddProviderView {
struct NameView: View {
@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
@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 {
hiddenAccountLink
2022-04-12 13:09:14 +00:00
List {
AddProfileView.ProfileNameSection(
profileName: $viewModel.profileName,
focusedField: $focusedField,
2022-04-12 13:09:14 +00:00
errorMessage: viewModel.errorMessage
) {
saveProfile(replacingExisting: false)
}.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
)
}
}
}.toolbar {
Button {
saveProfile(replacingExisting: false)
} label: {
themeSaveButtonLabel()
}
}.alert(
L10n.AddProfile.Shared.title,
isPresented: $viewModel.isAskingOverwrite,
actions: alertOverwriteActions,
message: alertOverwriteMessage
).navigationTitle(providerMetadata.fullName)
2022-04-12 13:09:14 +00:00
}
}
}
// MARK: -
2023-03-17 20:55:47 +00:00
private extension AddProviderView.NameView {
var hiddenAccountLink: some View {
NavigationLink("", isActive: $isEnteringCredentials) {
AddProfileView.AccountWrapperView(
profile: $profile,
bindings: bindings
)
}
}
2022-04-12 13:09:14 +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)
}
}
func alertOverwriteMessage() -> some View {
Text(L10n.AddProfile.Shared.Alerts.Overwrite.message)
}
}
// MARK: -
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
}
profile = addedProfile
2022-04-12 13:09:14 +00:00
if profile.requiresCredentials {
isEnteringCredentials = true
} else {
bindings.isPresented = false
profileManager.didCreateProfile.send(profile)
2022-04-12 13:09:14 +00:00
}
}
}