@@ -11,11 +11,16 @@ import Combine
1111import Navajo_Swift
1212
1313class UserModel : ObservableObject {
14+ // input
1415 @Published var userName = " "
1516 @Published var password = " "
1617 @Published var passwordAgain = " "
17- @Published var valid = false
1818
19+ // output
20+ @Published var userNameMessage = " "
21+ @Published var passwordMessage = " "
22+ @Published var valid = false
23+
1924 private var cancellableSet : Set < AnyCancellable > = [ ]
2025
2126 private var isUserNameValidPublisher : AnyPublisher < Bool , Never > {
@@ -106,6 +111,31 @@ class UserModel: ObservableObject {
106111 }
107112
108113 init ( ) {
114+ isUserNameValidPublisher
115+ . receive ( on: RunLoop . main)
116+ . map { valid in
117+ valid ? " " : " User name must at leat have 3 characters "
118+ }
119+ . assign ( to: \. userNameMessage, on: self )
120+ . store ( in: & cancellableSet)
121+
122+ isPasswordValidPublisher
123+ . receive ( on: RunLoop . main)
124+ . map { passwordCheck in
125+ switch passwordCheck {
126+ case . empty:
127+ return " Password must not be empty "
128+ case . noMatch:
129+ return " Passwords don't match "
130+ case . notStrongEnough:
131+ return " Password not strong enough "
132+ default :
133+ return " "
134+ }
135+ }
136+ . assign ( to: \. passwordMessage, on: self )
137+ . store ( in: & cancellableSet)
138+
109139 isFormValidPublisher
110140 . receive ( on: RunLoop . main)
111141 . assign ( to: \. valid, on: self )
@@ -120,22 +150,21 @@ struct ContentView: View {
120150
121151 var body : some View {
122152 Form {
123- Section {
153+ Section ( footer : Text ( userModel . userNameMessage ) . foregroundColor ( . red ) ) {
124154 TextField ( " Username " , text: $userModel. userName)
125155 . autocapitalization ( . none)
126156 }
127- Section {
157+ Section ( footer : Text ( userModel . passwordMessage ) . foregroundColor ( . red ) ) {
128158 SecureField ( " Password " , text: $userModel. password)
129159 SecureField ( " Password again " , text: $userModel. passwordAgain)
130160 }
131161 Section {
132162 Button ( action: { } ) {
133- Text ( " Sign up " )
134- } . disabled ( !userModel. valid)
163+ Text ( " Login " )
164+ } . disabled ( !self . userModel. valid)
135165 }
136166 }
137167 }
138-
139168}
140169
141170struct ContentView_Previews : PreviewProvider {
0 commit comments