SHARE
TWEET

Untitled

a guest Sep 23rd, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class UpdateProfilePresenter {
  2.    
  3.     var user: User?
  4.    
  5.     weak var updateProfileViewController: UpdateProfileViewController!
  6.    
  7.     init(user: User?, viewController: UpdateProfileViewController) {
  8.         self.user = user
  9.         self.updateProfileViewController = viewController
  10.        
  11.         if let user  = self.user {
  12.             self.setUserDate(user)
  13.         }
  14.        
  15.     }
  16.    
  17.     func setUserDate(_ user: User){
  18.         self.updateProfileViewController.setName(user.name)
  19.         self.updateProfileViewController.setMobile(user.mobileNumber)
  20.         self.updateProfileViewController.setEmail(user.email)
  21.         self.updateProfileViewController.setAge(user.age)
  22.         self.updateProfileViewController.setGender(atIndex: user.gender.rawValue)
  23.     }
  24.    
  25.     func validateInput(name: String, mobileNumber: String, age: String, email: String) -> UpdateProfileFormError? {
  26.        
  27.         if name.isEmpty {
  28.             return .emptyName
  29.            
  30.         } else if !validate(mobileNumber: mobileNumber) {
  31.             return .invalidMobileNumber
  32.            
  33.         } else if !validate(email: email){
  34.             return .invalidEmailAddress
  35.        
  36.         } else if age < 18 {
  37.             return .veryYoung
  38.         }
  39.        
  40.         return nil
  41.     }
  42.    
  43.     func validate(mobileNumber: String) -> Bool {
  44.         let alphaNumericRegex = "^[0-9]{11}$"
  45.         let alphaNumericTest = NSPredicate(format: "SELF MATCHES %@", alphaNumericRegex)
  46.         return alphaNumericTest.evaluate(with: mobileNumber)
  47.     }
  48.    
  49.     func validate(email: String) -> Bool {
  50.         let regex = "([\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})"
  51.         let emailTest = NSPredicate(format: "SELF MATCHES %@", regex)
  52.         return emailTest.evaluate(with: email)
  53.     }
  54.    
  55.     func saveButtonClicked() {
  56.         if let error = self.validateInput(name: updateProfileViewController.getName(),
  57.                                           mobileNumber: updateProfileViewController.getMobileNumber(),
  58.                                           age: updateProfileViewController.getAge(),
  59.                                           email: updateProfileViewController.getEmail()) {
  60.            
  61.             self.updateProfileViewController.showAlert(withTitle: "Error", andErrorMessage: error.localizedDescription)
  62.         } else {
  63.             self.saveProfileData(name: updateProfileViewController.getName(),
  64.                                  mobileNumber: updateProfileViewController.getMobileNumber(),
  65.                                  age: updateProfileViewController.getAge(),
  66.                                  email: updateProfileViewController.getEmail(),
  67.                                  gender: Gender(rawValue: updateProfileViewController.getGenderIndex())! )
  68.            
  69.             self.updateProfileViewController.showAlert(withTitle: "Success", andErrorMessage: "The profile updated successfully")
  70.         }
  71.     }
  72.    
  73.      func saveProfileData(name: String, mobileNumber: String, email: String, age: Int, gender: Gender) {
  74.        
  75.         self.updateLocalStoreWithUserData(name: name, mobileNumber: mobileNumber, email: email, age: age, gender: gender)
  76.         self.updateUserModel(name: name, mobileNumber: mobileNumber, email: email, age: age, gender: gender)
  77.     }
  78.    
  79.     func updateLocalStoreWithUserData(name: String, mobileNumber: String,email: String, age: Int, gender: Gender) {
  80.         // update local data
  81.         LocalStorage.default.user?.name = name
  82.         LocalStorage.default.user?.mobileNumber = mobileNumber
  83.         LocalStorage.default.user?.gender = gender
  84.         LocalStorage.default.user?.email = email
  85.         LocalStorage.default.user?.age = age
  86.     }
  87.    
  88.     func updateUserModel(name: String, mobileNumber: String, email: String, age: Int, gender: Gender) {
  89.         // update the model
  90.         self.user?.name = name
  91.         self.user?.mobileNumber = mobileNumber
  92.         self.user?.gender = gender
  93.         self.user?.email = email
  94.         self.user?.age = age
  95.     }
  96.    
  97. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top