This my code, when a user types in customTextField, username in ObservedObject viewmodel updates, but line if text.isEmpty
in CustomTextField does not run.
If I try to use testUsername by @State => it works. But I want to use ObservedObject, Can you help me? Thanks!
struct CustomTextField: View {
var placeholder: Text
@Binding var text: String
var editingChanged: (Bool)->() = { _ in }
var commit: ()->() = { }
var body: some View {
ZStack(alignment: .leading) {
if text.isEmpty {
placeholder
.customPlaceHolderStyle()
.padding()
}
TextField("", text: $text, onEditingChanged: editingChanged, onCommit: commit)
.customTextFieldStyle()
.padding()
}.background(Color.colorPrimaryDark)
}
}
struct LoginView: View {
@ObservedObject var viewModel : LoginViewModel
@State var testUsername : String = ""
var body: some View {
BaseView(content: VStack(content: {
Image("logo_header")
.padding()
CustomTextField(placeholder: Text(NSLocalizedString("lb_username", comment: "")), text: $viewModel.username, commit: {
UIApplication.shared.endEditing()
})
}).padding() , viewModel: viewModel)
}
}
class LoginViewModel : BaseViewModel {
private var authRepository: AuthRepository? = nil
@Published var username : String = ""
@Published var password : String = ""
private init(authRepository : AuthRepository) {
self.authRepository = authRepository
}
static let share : LoginViewModel = LoginViewModel(authRepository: AuthRepositoryImpl.share)
}