swiftmvp

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


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?


Solution

  • 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)
    }
    
    }