SwiftUI:如何刷新视图以及@Published ObservableObject属性为何随机起作用

时间:2020-03-23 07:53:11

标签: validation swiftui textfield combine observableobject

在我看来,SwiftUI的工作方式越来越混乱。 乍看之下,它接缝快速且易于掌握。但是,如果您添加越来越多的视图 看起来很简单的事情开始表现得很奇怪,需要很多时间来解决。

我有Input个带有验证的字段。这是定制的输入,可以在很多地方重复使用。但是,在不同的屏幕上,此操作可能完全不同且完全不可靠。

使用表单查看

struct LoginView { 

@ObservedObject private var viewModel = LoginViewModel()

var body: some View { 
VStack(spacing: 32) {

                    Spacer()

                    LabeledInput(label: "Email", input: self.$viewModel.email, isNuemorphic: true, rules: LoginFormRules.email, validation: self.$viewModel.emailValidation)
                    .textContentType(.emailAddress)
                    .keyboardType(.emailAddress)
                    .autocapitalization(.none)
                    .frame(height: 50)

                    LabeledInput(label: "Password", isSecure: true, input: self.$viewModel.password, isNuemorphic: true, rules: LoginFormRules.password, validation: self.$viewModel.passwordValidation)
                    .textContentType(.password)
                    .keyboardType(.asciiCapable)
                    .autocapitalization(.none)
                    .frame(height: 50)

                    self.makeSubmitButton()

                    Spacer()

                }
}

LabeledInput-具有验证支持的可恢复自定义输入视图

struct LabeledInput: View {

    // MARK: - Properties
    let label: String?
    let isSecure: Bool

    // MARK: - Binding
    @Binding var input: String
    var isEditing: Binding<Bool>?

    // MARK: - Actions
    private let onEditingChanged: (Bool) -> Void
    private let onCommit: () -> Void

    // MARK: - Validation
    @ObservedObject var validator: FieldValidator<String>

    // MARK: - Init
    init(label: String? = nil,
         isSecure: Bool = false,
         input: Binding<String>,
         isEditing: Binding<Bool>? = nil,

         // validation
         rules: [Rule<String>] = [],
         validation: Binding<Validation>? = nil,

         // actions
         onEditingChanged: @escaping (Bool) -> Void = { _ in },
         onCommit: @escaping () -> Void = { }) {

        self.label = label
        self.isSecure = isSecure
        self._input = input
        self.isEditing = isEditing


        self.onEditingChanged = onEditingChanged
        self.onCommit = onCommit


        self.validator = FieldValidator(input: input, rules: rules, validation: validation ?? .constant(Validation()))
    }

    var useUIKit: Bool {
        self.isEditing != nil
    }

    var body: some View {

        GeometryReader { geometry in
            ZStack {


                    RoundedRectangle(cornerRadius: 4.0)
                    .stroke(lineWidth: 1)
                .foregroundColor(!self.validator.validation.isEdited ? Color("LightGray")
                    : self.validator.validation.isValid ? Color("Green") : Color("Red"))
                    .frame(maxHeight: geometry.size.height)
                    .offset(x: 0, y: 16)


                VStack {
                    HStack {
                        self.makeLabel()
                            .offset(x: self.isNuemorphic ? 0 : 16,
                                    y: self.isNuemorphic ? 0 : 8)
                        Spacer()
                    }
                    Spacer()
                }

                self.makeField()
                .frame(maxHeight: geometry.size.height)
                    .offset(x: 0, y: self.isNuemorphic ? 20 : 16)
                .padding(10)
            }
        }

    }

    private func makeField() -> some View {
        Group {
            if useUIKit {
                self.makeUIKitTextField(secure: self.isSecure)
            } else {
                if self.isSecure {
                    self.makeSecureField()
                } else {
                    self.makeTextField()
                }
            }
        }
    }

    private func makeLabel() -> some View {
        Group {
            if label != nil {
                Text("\(self.label!.uppercased())")
                    .font(.custom("AvenirNext-Regular", size: self.isNuemorphic ? 13 : 11))
                    .foregroundColor(!self.validator.validation.isEdited ? Color("DarkBody")
                        : self.validator.validation.isValid ? Color("Green") : Color("Red"))
                    .padding(.horizontal, 8)

            } else {
                EmptyView()
            }
        }
    }

    private func makeSecureField() -> some View {

        SecureField("", text: self.$input, onCommit: {
            self.validator.onCommit()
            self.onCommit()
        })
        .font(.custom("AvenirNext-Regular", size: 15))
            .foregroundColor(Color("DarkBody"))
        .frame(maxWidth: .infinity)
    }

    private func makeTextField() -> some View {

        TextField("", text: self.$input, onEditingChanged: { editing in

            self.onEditingChanged(editing)
            self.validator.onEditing(editing)

            if !editing { self.onCommit() }

        }, onCommit: {
            self.validator.onCommit()
            self.onCommit()

        })
        .font(.custom("AvenirNext-Regular", size: 15))
            .foregroundColor(Color("DarkBody"))
        .frame(maxWidth: .infinity)
    }

    private func makeUIKitTextField(secure: Bool) -> some View {

        let firstResponderBinding = Binding<Bool>(get: {
            self.isEditing?.wrappedValue ?? false //?? self.isFirstResponder
        }, set: {
            //self.isFirstResponder = $0
            self.isEditing?.wrappedValue = $0
        })

        return UIKitTextField(text: self.$input, isEditing: firstResponderBinding, font: UIFont(name: "AvenirNext-Regular", size: 15)!, textColor: UIColor(named: "DarkBody")!, placeholder: "", onEditingChanged: { editing in

            self.onEditingChanged(editing)
            self.validator.onEditing(editing)

        }, onCommit: {

            self.validator.onCommit()
            self.onCommit()
        })
    }
}

这是我在ObservableObject中存储模型(输入值和验证)的方式,即LoginViewModel。

final class LoginViewModel: ObservableObject {

    // MARK: - Published
    @Published var email: String = ""
    @Published var password: String = ""

    @Published var emailValidation: Validation = Validation(onEditing: true)
    @Published var passwordValidation: Validation = Validation(onEditing: true)

    @Published var validationErrors: [String]? = nil
    @Published var error: DescribableError? = nil

}

当我根据视图父视图(屏幕)根据我如何创建ViewModel(在LoginView属性中或注入到LoginView构造函数中)使用此代码时,它嵌入的工作方式可能完全不同,可能会导致数小时的调试和意外行为。

  1. 有时似乎有1个ViewModel实例,有时似乎每次刷新View时都会创建该实例
  2. 有时LabeledInput主体正在刷新,并且验证标签的着色正确地起作用。有时候似乎根本没有刷新,什么也没发生
  3. 有时会刷新,因此经常会立即隐藏键盘
  4. 其他时候根本没有验证
  5. 其他时间,退出场或将手机横向旋转为纵向时,输入会丢失
  6. 如果发生某些事件导致父视图刷新,则可能导致输入丢失数据和验证。
  7. 有时刷新到其他时间,有时根本没有刷新。

我尝试添加.id(UUID),自定义.id(refreshId)或其他Equatable协议实现,但是它不能像预期的那样可重用的自定义输入起作用,并且可以在多个屏幕上的多个表单之间重用验证。

这是简单的验证结构

struct Validation {

    let onEditing: Bool

    init(onEditing: Bool = false) {
        self.onEditing = onEditing
    }

    var isEdited: Bool = false
    var errors: [String] = []
}

这里是FieldValidator ObservableObject

class FieldValidator<T>: ObservableObject {

    // MARK: - Properties
    private let rules: [Rule<T>]

    // MARK: - Binding
    @Binding private var input: T
    @Binding var validation: Validation

    // MARK: - Init
    init(input: Binding<T>, rules: [Rule<T>], validation: Binding<Validation>) {
        #if DEBUG
        print("[FieldValidator] init: \(input.wrappedValue)")
        #endif

        self._input = input
        self.rules = rules
        self._validation = validation 
    }

     private var disposables = Set<AnyCancellable>()
}

// MARK: - Public API
extension FieldValidator {

    func validateField() {

        validation.errors = rules
            .filter { !$0.isAsync }
            .filter { !$0.validate(input) }
            .map { $0.errorMessage() }
    }

    func validateFieldAsync() {

        rules
            .filter { $0.isAsync }
            .forEach { rule in

                rule.validateAsync(input)
                .filter { valid  in
                    !valid
                }.sink(receiveValue: { _ in
                    self.validation.errors.append(rule.errorMessage())
                })
                .store(in: &disposables)
            }
    }
}

// MARK: - Helper Public API
extension FieldValidator {

    func onEditing(_ editing: Bool) {

        self.validation.isEdited = true

        if editing {
            if self.validation.onEditing {
                self.validateField()
            }
        } else {
            // on end editing
            self.validateField()
            self.validateFieldAsync()
        }
    }

    func onCommit() {
        self.validateField()
        self.validateFieldAsync()
    }
}

规则只是

的子类
class Rule<T>  {

    var isAsync: Bool { return false }

    func validate(_ value: T) -> Bool { return false }
    func errorMessage() -> String { return "" }

    func validateAsync(_ value: T) -> AnyPublisher<Bool, Never> {
        fatalError("Async validation is not implemented!")
    }
}

更新

完整的UIKitTextField示例

@available(iOS 13.0, *)
struct UIKitTextField: UIViewRepresentable {

    // MARK: - Observed
    @ObservedObject private var keyboardEvents = KeyboardEvents()

    // MARK: - Binding
    @Binding var text: String
    var isEditing: Binding<Bool>?

    // MARK: - Actions
    let onBeginEditing: () -> Void
    let onEndEditing: () -> Void
    let onEditingChanged: (Bool) -> Void
    let onCommit: () -> Void

    // MARK: - Proprerties
    private let keyboardOffset: CGFloat

    private let textAlignment: NSTextAlignment
    private let font: UIFont
    private let textColor: UIColor
    private let backgroundColor: UIColor
    private let contentType: UITextContentType?
    private let keyboardType: UIKeyboardType
    private let autocorrection: UITextAutocorrectionType
    private let autocapitalization: UITextAutocapitalizationType
    private let isSecure: Bool
    private let isUserInteractionEnabled: Bool
    private let placeholder: String?

    public static let defaultFont = UIFont.preferredFont(forTextStyle: .body)

    private var hasDoneToolbar: Bool = false

    init(text: Binding<String>,
         isEditing: Binding<Bool>? = nil,
         keyboardOffset: CGFloat = 0,

         textAlignment: NSTextAlignment = .left,
         font: UIFont = UIKitTextField.defaultFont,
         textColor: UIColor = .black,
         backgroundColor: UIColor = .white,
         contentType: UITextContentType? = nil,
         keyboardType: UIKeyboardType = .default,
         autocorrection: UITextAutocorrectionType = .default,
         autocapitalization: UITextAutocapitalizationType = .none,
         isSecure: Bool = false,
         isUserInteractionEnabled: Bool = true,
         placeholder: String? = nil,

         hasDoneToolbar: Bool = false,

         onBeginEditing: @escaping () -> Void = { },
         onEndEditing: @escaping () -> Void = { },
         onEditingChanged: @escaping (Bool) -> Void = { _ in },
         onCommit: @escaping () -> Void = { }) {

        self._text = text
        self.isEditing = isEditing

        self.keyboardOffset = keyboardOffset

        self.onBeginEditing = onBeginEditing
        self.onEndEditing = onEndEditing
        self.onEditingChanged = onEditingChanged
        self.onCommit = onCommit

        self.textAlignment = textAlignment
        self.font = font
        self.textColor = textColor
        self.backgroundColor = backgroundColor
        self.contentType = contentType
        self.keyboardType = keyboardType
        self.autocorrection = autocorrection
        self.autocapitalization = autocapitalization
        self.isSecure = isSecure
        self.isUserInteractionEnabled = isUserInteractionEnabled
        self.placeholder = placeholder

        self.hasDoneToolbar = hasDoneToolbar
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func makeUIView(context: Context) -> UITextField {

        let textField = UITextField()
        textField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        textField.delegate = context.coordinator
        textField.keyboardType = keyboardType
        textField.textAlignment = textAlignment
        textField.font = font
        textField.textColor = textColor
        textField.backgroundColor = backgroundColor
        textField.textContentType = contentType
        textField.autocorrectionType = autocorrection
        textField.autocapitalizationType = autocapitalization
        textField.isSecureTextEntry = isSecure
        textField.isUserInteractionEnabled = isUserInteractionEnabled
        //textField.placeholder = placeholder
        if let placeholder = placeholder {
            textField.attributedPlaceholder = NSAttributedString(
                                    string: placeholder,
                                    attributes: [
                                        NSAttributedString.Key.foregroundColor: UIColor.lightGray
                                    ])
        }

        textField.addTarget(context.coordinator, action: #selector(Coordinator.valueChanged(_:)), for: .editingChanged)

        keyboardEvents.didShow = {
            if textField.isFirstResponder {
                DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(350)) {
                    textField.adjustScrollView(offset: self.keyboardOffset, animated: true)
                }
            }
        }

        if hasDoneToolbar {
            textField.addDoneButton {
                print("Did tap Done Toolbar button")
                textField.resignFirstResponder()
            }
        }

        return textField
    }

    func updateUIView(_ textField: UITextField, context: Context) {

        textField.text = text

        if let isEditing = isEditing {
            if isEditing.wrappedValue {
                textField.becomeFirstResponder()
            } else {
                textField.resignFirstResponder()
            }
        }
    }

    final class Coordinator: NSObject, UITextFieldDelegate {

        let parent: UIKitTextField

        init(_ parent: UIKitTextField) {
            self.parent = parent
        }

        @objc func valueChanged(_ textField: UITextField) {
            parent.text = textField.text ?? ""
            parent.onEditingChanged(true)
        }

        func textFieldDidBeginEditing(_ textField: UITextField) {
            parent.onBeginEditing()
            parent.onEditingChanged(true)
        }

        func textFieldDidEndEditing(_ textField: UITextField, reason: UITextField.DidEndEditingReason) {

            //guard textField.text != "" || parent.shouldCommitIfEmpty else { return }

            DispatchQueue.main.async {
               self.parent.isEditing?.wrappedValue = false
            }
            parent.text = textField.text ?? ""
            parent.onEditingChanged(false)
            parent.onEndEditing()
            parent.onCommit()
        }

        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            parent.isEditing?.wrappedValue = false
            textField.resignFirstResponder()
            parent.onCommit()
            return true
        }
    }

}

extension UIView {

    func adjustScrollView(offset: CGFloat, animated: Bool = false) {

        if let scrollView = findParent(of: UIScrollView.self) {
            let contentOffset = CGPoint(x: scrollView.contentOffset.x, y: scrollView.contentOffset.y + offset)
            scrollView.setContentOffset(contentOffset, animated: animated)
        } else {
            print("View is not in ScrollView - do not adjust content offset")
        }
    }
}

这是示例EmailRule实现

class EmailRule : RegexRule {

    static let regex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"

    public convenience init(message : String = "Email address is invalid"){
        self.init(regex: EmailRule.regex, message: message)
    }

    override func validate(_ value: String) -> Bool {
        guard value.count > 0 else { return true }

        return super.validate(value)
    }
}

0 个答案:

没有答案