如何修改函数以在MVP架构中使用?

huangapple go评论49阅读模式
英文:

How do I modify function to be used in MVP architecture?

问题

我有下面的函数。它正常工作。

当用户输入任何字符时,它会验证用户输入并根据输入隐藏一些imageView。

@IBAction func onEmailValueChanged(_ sender: UITextField) {
    let hasMinimumLength = TextValidationHelper.validateHasMinimumLength(password: sender.text!)
    passLengthCheckmarkImageView.isHidden = hasMinimumLength ? false : true
    let hasCapitalLetter = TextValidationHelper.validateHasCapitalLetter(password: sender.text!)
    passHasUppercaseCheckmarkImageView.isHidden = hasCapitalLetter ? false : true
    let hasNumber = TextValidationHelper.validateHasNumber(password: sender.text!)
    passHasNumberCheckmarkImageView.isHidden = hasNumber ? false : true
    let hasSpecialCharacter = TextValidationHelper.validateHasSpecialCharacter(password: sender.text!)
    passHasSymbolCheckmarkImageView.isHidden = hasSpecialCharacter ? false : true
    
    resetButton.isHidden = hasMinimumLength && hasCapitalLetter && hasNumber && hasSpecialCharacter ? false : true 
}

但现在我想在这个函数上应用一个MVP模型,以将函数从ViewController文件中移除。我应该如何做?

我需要发布更多的代码来使得回答这个问题成为可能吗?

英文:

I have the function below. It works properly.

When a user types any character it validates the user input and hides some imageView based on the input.

@IBAction func onEmailValueChanged(_ sender: UITextField) {
    let hasMinimumLength = TextValidationHelper.validateHasMinimumLength(password: sender.text!)
    passLengthCheckmarkImageView.isHidden = hasMinimumLength ? false : true
    let hasCapitalLetter = TextValidationHelper.validateHasCapitalLetter(password: sender.text!)
    passHasUppercaseCheckmarkImageView.isHidden = hasCapitalLetter ? false : true
    let hasNumber = TextValidationHelper.validateHasNumber(password: sender.text!)
    passHasNumberCheckmarkImageView.isHidden = hasNumber ? false : true
    let hasSpecialCharacter = TextValidationHelper.validateHasSpecialCharacter(password: sender.text!)
    passHasSymbolCheckmarkImageView.isHidden = hasSpecialCharacter ? false : true
    
    resetButton.isHidden = hasMinimumLength && hasCapitalLetter && hasNumber && hasSpecialCharacter ? false : true 
}

But now I want to apply an MVP model on this function to remove the function from the ViewController file.
How can I do that?

Do I need to publish more code to make it possible to create an answer for this question?

答案1

得分: 1

不建议仅为一个方法使用任何架构模式。因此,假设您有一个包含许多类或文件的完整应用程序。

重要的是,并没有固定/强制使用任何特定模式。实际上,这取决于代码,有时您最终会编写大量代码来处理一个方法。因此,尝试考虑最佳方法,使代码更具可测试性和可扩展性。

但是供您参考,您可以查看以下代码:

在ViewController中:

lazy var presenter: Presenter = Presenter(view: self)

@IBAction func onEmailValueChanged(_ sender: UITextField) {
    presenter.validateHasMinimumLength(password: sender.text!)
    presenter.validateHasCapitalLetter(password: sender.text!)
    presenter.validateHasNumber(password: sender.text!)
    presenter.validateHasSpecialCharacter(password: sender.text!)
}

采用ViewController:PrensenterViewProtocol在ViewController中:

extension ViewController: PrensenterViewProtocol {

    func updateLengthCheckmarkImageView(isHidden: Bool) {
        passLengthCheckmarkImageView.isHidden = isHidden
    }
    
    func updateUpperCaseCheckmarkImageView(isHidden: Bool) {
        passHasUppercaseCheckmarkImageView.isHidden = isHidden
    }
    
    func updateNumberCheckmarkImageView(isHidden: Bool) {
        passHasNumberCheckmarkImageView.isHidden = isHidden
    }
    
    func updateSymbolCheckmarkImageView(isHidden: Bool) {
        passHasSymbolCheckmarkImageView.isHidden = isHidden
    }
    
    func updateResetButton(isHidden: Bool) {
        resetButton.isHidden = isHidden
    }
}

PresenterView协议如下:

protocol PrensenterViewProtocol: NSObjectProtocol {
    func updateLengthCheckmarkImageView(isHidden: Bool)
    func updateUpperCaseCheckmarkImageView(isHidden: Bool)
    func updateNumberCheckmarkImageView(isHidden: Bool)
    func updateSymbolCheckmarkImageView(isHidden: Bool)
    func updateResetButton(isHidden: Bool)
}

Presenter类如下:

class Presenter {
    weak var view: PrensenterViewProtocol!
    private var hasMinimumLength: Bool = false
    private var hasCapitalLetter: Bool = false
    private var hasNumber: Bool = false
    private var hasSpecialCharacter: Bool = false
    
    init(view: PrensenterViewProtocol) {
        self.view = view
    }
        
    func validateHasMinimumLength(password: String?) {
        hasMinimumLength = TextValidationHelper.validateHasMinimumLength(password: password)
        self.view.updateLengthCheckmarkImageView(isHidden: hasMinimumLength)
        checkAllValidations()
    }

    func validateHasCapitalLetter(password: String?) {
        hasCapitalLetter = TextValidationHelper.validateHasCapitalLetter(password: password)
        self.view.updateUpperCaseCheckmarkImageView(isHidden: hasCapitalLetter)
        checkAllValidations()
    }
    
    func validateHasNumber(password: String?) {
        hasNumber = TextValidationHelper.validateHasNumber(password: password)
        self.view.updateNumberCheckmarkImageView(isHidden: hasNumber)
        checkAllValidations()
    }
    
    func validateHasSpecialCharacter(password: String?) {
        hasSpecialCharacter = TextValidationHelper.validateHasSpecialCharacter(password: password)
        self.view.updateSymbolCheckmarkImageView(isHidden: hasSpecialCharacter)
        checkAllValidations()
    }
    
    func checkAllValidations() {
        let areAllValid: Bool = hasMinimumLength && hasCapitalLetter && hasNumber && hasSpecialCharacter ? false : true
        self.view.updateResetButton(isHidden: areAllValid)
    }
}
英文:

It is not a good practice to use any architectural pattern only for method. So assuming you are having a complete app with many classes or files.

An important thing is that it is not fixed/compulsory to use any specific pattern. It actually depends on the code, sometimes you end up writing much code just to handle a method. So try to think the optimal approach to make the code more testable and scalable.

But for your reference, you can check the following code:

On ViewController:

lazy var presenter:Presenter = Presenter(view:self)


 @IBAction func onEmailValueChanged(_ sender: UITextField) {
    presenter.validateHasMinimumLength(password: sender.text!)
    presenter.validateHasCapitalLetter(password: sender.text!)
    presenter.validateHasNumber(password: sender.text!)
    presenter.validateHasSpecialCharacter(password: sender.text!)
}

//Adopting ViewController:PrensenterViewProtocol on ViewController

extension ViewController:PrensenterViewProtocol {

func updateLengthCheckmarkImageView(isHidden:Bool) {
    passLengthCheckmarkImageView.isHidden = isHidden
}

func updateUpperCaseCheckmarkImageView(isHidden:Bool) {
    passHasUppercaseCheckmarkImageView.isHidden = isHidden
}

func updateNumberCheckmarkImageView(isHidden:Bool) {
    passHasNumberCheckmarkImageView.isHidden = isHidden
}

func updateSymbolCheckmarkImageView(isHidden:Bool) {
    passHasSymbolCheckmarkImageView.isHidden = isHidden
}

func updateResetButton(isHidden:Bool) {
    resetButton.isHidden = isHidden
}

}

PresenterView protocol as:

protocol PrensenterViewProtocol:NSObjectProtocol {
    func updateLengthCheckmarkImageView(isHidden:Bool)
    func updateUpperCaseCheckmarkImageView(isHidden:Bool)
    func updateNumberCheckmarkImageView(isHidden:Bool)
    func updateSymbolCheckmarkImageView(isHidden:Bool)
    func updateResetButton(isHidden:Bool)
}

Presenter as:

class Presenter {
weak var view:PrensenterViewProtocol!
private var hasMinimumLength:Bool = false
private var hasCapitalLetter:Bool = false
private var hasNumber:Bool = false
private var hasSpecialCharacter:Bool = false

init(view:PrensenterViewProtocol) {
    self.view = view
}
    
func validateHasMinimumLength(password:String?) {
    hasMinimumLength = TextValidationHelper.validateHasMinimumLength(password: password)
    self.view.updateLengthCheckmarkImageView(isHidden: hasMinimumLength)
    checkAllValidations()
}

func validateHasCapitalLetter(password:String?) {
    hasCapitalLetter = TextValidationHelper.validateHasCapitalLetter(password: password)
    self.view.updateUpperCaseCheckmarkImageView(isHidden:hasCapitalLetter )
    checkAllValidations()
}

func validateHasNumber(password:String?) {
    hasNumber = TextValidationHelper.validateHasNumber(password: password)
    self.view.updateNumberCheckmarkImageView(isHidden: hasNumber)
    checkAllValidations()
}

func validateHasSpecialCharacter(password:String?) {
    hasSpecialCharacter = TextValidationHelper.validateHasSpecialCharacter(password: password)
    self.view.updateSymbolCheckmarkImageView(isHidden: hasSpecialCharacter)
    checkAllValidations()
}

func checkAllValidations() {
    let areAllValid:Bool = hasMinimumLength && hasCapitalLetter && hasNumber && hasSpecialCharacter ? false : true
    self.view.updateResetButton(isHidden: areAllValid)
}

}

huangapple
  • 本文由 发表于 2023年1月8日 23:46:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定