@ -10,21 +10,12 @@ import LeStorage
struct UserCreationFormView : View {
struct UserCreationFormView : View {
@ FocusState private var focusedField : Field ?
@ AppStorage ( " didCreateAccount " ) var didCreateAccount : Bool = false
fileprivate enum Field : Int , Hashable {
case username
case password1
case password2
case email
case firstName
case lastName
case phone
}
@ EnvironmentObject var networkMonitor : NetworkMonitor
@ EnvironmentObject var networkMonitor : NetworkMonitor
@ EnvironmentObject var dataStore : DataStore
@ Binding var isPresented : Bool
@ Binding var isPresented : Bool
@ Binding var showEmailValidationMessage : Bool
@ Binding var credentials : Credentials ?
@ State var username : String = " "
@ State var username : String = " "
@ State var email : String = " "
@ State var email : String = " "
@ -51,6 +42,18 @@ struct UserCreationFormView: View {
@ State var isLoading = false
@ State var isLoading = false
@ FocusState var focusedField : UserCreationFormField ?
enum UserCreationFormField {
case username
case email
case password
case confirmPassword
case firstName
case lastName
case phoneNumber
}
var body : some View {
var body : some View {
NavigationStack {
NavigationStack {
@ -58,46 +61,68 @@ struct UserCreationFormView: View {
Section {
Section {
TextField ( " Nom d'utilisateur " , text : self . $ username )
TextField ( " Nom d'utilisateur " , text : self . $ username )
. autocorrectionDisabled ( )
. textContentType ( . init ( rawValue : " " ) )
. keyboardType ( . asciiCapable )
. autocorrectionDisabled ( true )
. textInputAutocapitalization ( . never )
. textInputAutocapitalization ( . never )
. focused ( $ focusedField , equals : . username )
. submitLabel ( . next )
. submitLabel ( . next )
. focused ( $ focusedField , equals : Field . username )
. onSubmit ( of : . text ) {
. onSubmit { self . focusedField = Field . email }
focusedField = . email
}
TextField ( " Email " , text : self . $ email )
TextField ( " Email " , text : self . $ email )
. keyboardType ( . emailAddress )
. keyboardType ( . emailAddress )
. textContentType ( . emailAddress )
. textInputAutocapitalization ( . never )
. textInputAutocapitalization ( . never )
. autocorrectionDisabled ( )
. focused ( $ focusedField , equals : . email )
. submitLabel ( . next )
. submitLabel ( . next )
. focused ( $ focusedField , equals : Field . email )
. onSubmit ( of : . text ) {
. onSubmit { self . focusedField = Field . password1 }
focusedField = . password
}
}
}
Section {
Section {
SecureField ( " Mot de passe " , text : self . $ password1 )
SecureField ( " Mot de passe " , text : self . $ password1 )
. focused ( $ focusedField , equals : . password )
. submitLabel ( . next )
. submitLabel ( . next )
. focused ( $ focusedField , equals : Field . password1 )
. onSubmit ( of : . text ) {
. onSubmit { self . focusedField = Field . password2 }
focusedField = . confirmPassword
}
SecureField ( " Confirmez le mot de passe " , text : self . $ password2 )
SecureField ( " Confirmez le mot de passe " , text : self . $ password2 )
. focused ( $ focusedField , equals : . confirmPassword )
. submitLabel ( . next )
. submitLabel ( . next )
. focused ( $ focusedField , equals : Field . password2 )
. onSubmit ( of : . text ) {
. onSubmit { self . focusedField = Field . firstName }
focusedField = . firstName
}
}
}
Section {
Section {
TextField ( " Prénom " , text : self . $ firstName )
TextField ( " Prénom " , text : self . $ firstName )
. focused ( $ focusedField , equals : . firstName )
. textContentType ( . init ( rawValue : " " ) )
. keyboardType ( . asciiCapable )
. autocorrectionDisabled ( true )
. submitLabel ( . next )
. submitLabel ( . next )
. autocorrectionDisabled ( )
. onSubmit ( of : . text ) {
. focused ( $ focusedField , equals : Field . firstName )
focusedField = . lastName
. onSubmit { self . focusedField = Field . lastName }
}
TextField ( " Nom " , text : self . $ lastName )
TextField ( " Nom " , text : self . $ lastName )
. focused ( $ focusedField , equals : . lastName )
. textContentType ( . init ( rawValue : " " ) )
. keyboardType ( . asciiCapable )
. autocorrectionDisabled ( true )
. submitLabel ( . next )
. submitLabel ( . next )
. autocorrectionDisabled ( )
. onSubmit ( of : . text ) {
. focused ( $ focusedField , equals : Field . lastName )
focusedField = . phoneNumber
. onSubmit { self . focusedField = Field . phone }
}
TextField ( " Téléphone " , text : self . $ phone )
TextField ( " Téléphone " , text : self . $ phone )
. submitLabel ( . next )
. focused ( $ focusedField , equals : . phoneNumber )
. autocorrectionDisabled ( )
. keyboardType ( . default )
. focused ( $ focusedField , equals : Field . phone )
. textContentType ( . telephoneNumber )
// . o n S u b m i t { s e l f . f o c u s N e x t F i e l d ( $ f o c u s e d F i e l d ) }
. autocorrectionDisabled ( true )
. submitLabel ( . done )
LabeledContent {
LabeledContent {
Picker ( selection : $ selectedCountryIndex ) {
Picker ( selection : $ selectedCountryIndex ) {
@ -125,9 +150,13 @@ struct UserCreationFormView: View {
}
}
. disabled ( ! self . dataCollectAuthorized )
. disabled ( ! self . dataCollectAuthorized )
}
}
} . navigationTitle ( " Créez votre compte " )
}
. navigationTitle ( " Créez votre compte " )
. navigationBarTitleDisplayMode ( . inline )
. toolbarBackground ( . visible , for : . navigationBar )
}
}
. onAppear {
. onAppear {
self . focusedField = . username
self . _selectCountry ( )
self . _selectCountry ( )
}
}
. alert ( self . alertMessage , isPresented : self . $ showAlertView , actions : {
. alert ( self . alertMessage , isPresented : self . $ showAlertView , actions : {
@ -197,8 +226,10 @@ struct UserCreationFormView: View {
DispatchQueue . main . async {
DispatchQueue . main . async {
self . isLoading = false
self . isLoading = false
self . showEmailValidationMessage = true
self . credentials = Credentials ( username : username , password : password1 )
self . isPresented = false
self . isPresented = false
dataStore . appSettings . didCreateAccount = true
dataStore . appSettingsStorage . write ( )
}
}
} catch {
} catch {