Fix profile name reset to initial on error

This commit is contained in:
Davide De Rosa 2022-04-21 12:13:29 +02:00
parent ed16617288
commit ab7e6359ae
5 changed files with 24 additions and 10 deletions

View File

@ -95,10 +95,11 @@ struct AddHostView: View {
private var processingView: some View { private var processingView: some View {
AddProfileView.ProfileNameSection( AddProfileView.ProfileNameSection(
profileName: $viewModel.profileName, profileName: $viewModel.profileName,
initialName: url.normalizedFilename,
errorMessage: viewModel.errorMessage errorMessage: viewModel.errorMessage
) { ) {
processProfile(replacingExisting: false) processProfile(replacingExisting: false)
}.onAppear {
viewModel.presetName(withURL: url)
} }
if viewModel.requiresPassphrase { if viewModel.requiresPassphrase {
encryptionSection encryptionSection

View File

@ -30,6 +30,8 @@ import TunnelKitWireGuard
extension AddHostView { extension AddHostView {
struct ViewModel { struct ViewModel {
private var isNamePreset = false
var profileName = "" var profileName = ""
private(set) var requiresPassphrase = false private(set) var requiresPassphrase = false
@ -41,6 +43,14 @@ extension AddHostView {
private(set) var errorMessage: String? private(set) var errorMessage: String?
var isAskingOverwrite = false var isAskingOverwrite = false
mutating func presetName(withURL url: URL) {
guard !isNamePreset else {
return
}
isNamePreset = true
profileName = url.normalizedFilename
}
@MainActor @MainActor
mutating func processURL( mutating func processURL(

View File

@ -34,8 +34,6 @@ enum AddProfileView {
struct ProfileNameSection: View { struct ProfileNameSection: View {
@Binding var profileName: String @Binding var profileName: String
let initialName: String
let errorMessage: String? let errorMessage: String?
let onCommit: () -> Void let onCommit: () -> Void
@ -46,12 +44,6 @@ enum AddProfileView {
footer: themeErrorMessage(errorMessage) footer: themeErrorMessage(errorMessage)
) { ) {
TextField(L10n.Global.Placeholders.profileName, text: $profileName, onCommit: onCommit) TextField(L10n.Global.Placeholders.profileName, text: $profileName, onCommit: onCommit)
.onAppear {
// XXX: this is reset on the way back, but:
// host: there is no back button after processing profile
// host/provider: back button is hidden after going to credentials
profileName = initialName
}
} }
} }
} }

View File

@ -56,10 +56,11 @@ extension AddProviderView {
List { List {
AddProfileView.ProfileNameSection( AddProfileView.ProfileNameSection(
profileName: $viewModel.profileName, profileName: $viewModel.profileName,
initialName: providerMetadata.fullName,
errorMessage: viewModel.errorMessage errorMessage: viewModel.errorMessage
) { ) {
saveProfile(replacingExisting: false) saveProfile(replacingExisting: false)
}.onAppear {
viewModel.presetName(withMetadata: providerMetadata)
} }
let headers = profileManager.headers.sorted() let headers = profileManager.headers.sorted()
if !headers.isEmpty { if !headers.isEmpty {

View File

@ -124,11 +124,21 @@ extension AddProviderView {
extension AddProviderView.NameView { extension AddProviderView.NameView {
struct ViewModel { struct ViewModel {
private var isNamePreset = false
var profileName = "" var profileName = ""
var isAskingOverwrite = false var isAskingOverwrite = false
private(set) var errorMessage: String? private(set) var errorMessage: String?
mutating func presetName(withMetadata metadata: ProviderMetadata) {
guard !isNamePreset else {
return
}
isNamePreset = true
profileName = metadata.fullName
}
@MainActor @MainActor
mutating func addProfile( mutating func addProfile(