I could do something like the following.
class ValidateLogin {
var good: Bool = false
let user: String
let pass: String
init(user: String, pass: String) {
self.user = user
self.pass = pass
}
func validateMe() -> Bool {
if user.count > 3 && pass.count > 3 {
good = true
}
return good
}
}
struct ContentView: View {
@State var userText: String = ""
@State var passText: String = ""
@State var canSave: Bool = false
var body: some View {
ZStack {
VStack {
TextField("Username", text: $userText) {
}.onChange(of: userText) { newValue in
let validateLogin = ValidateLogin(user: userText, pass: passText)
canSave = validateLogin.validateMe()
}
SecureField("Password", text: $passText) {
}.onChange(of: passText) { newValue in
let validateLogin = ValidateLogin(user: userText, pass: passText)
canSave = validateLogin.validateMe()
}
}.padding(.horizontal, 20.0)
}.onAppear {
//Publishers.CombineLatest($userText, $passText)
}
}
}
The code above doesn't involve Combine at all. I want to do it in a Combine way.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: