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 {
AddProfileView.ProfileNameSection(
profileName: $viewModel.profileName,
initialName: url.normalizedFilename,
errorMessage: viewModel.errorMessage
) {
processProfile(replacingExisting: false)
}.onAppear {
viewModel.presetName(withURL: url)
}
if viewModel.requiresPassphrase {
encryptionSection

View File

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

View File

@ -34,8 +34,6 @@ enum AddProfileView {
struct ProfileNameSection: View {
@Binding var profileName: String
let initialName: String
let errorMessage: String?
let onCommit: () -> Void
@ -46,12 +44,6 @@ enum AddProfileView {
footer: themeErrorMessage(errorMessage)
) {
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 {
AddProfileView.ProfileNameSection(
profileName: $viewModel.profileName,
initialName: providerMetadata.fullName,
errorMessage: viewModel.errorMessage
) {
saveProfile(replacingExisting: false)
}.onAppear {
viewModel.presetName(withMetadata: providerMetadata)
}
let headers = profileManager.headers.sorted()
if !headers.isEmpty {

View File

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