This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct FormStateSaver { | |
| static func save(_ state: MementoConvertible, for key: String) { | |
| let defaults = UserDefaults.standard | |
| defaults.set(state.memento, forKey: key) | |
| defaults.synchronize() | |
| } | |
| static func restore(_ key: String) -> Memento? { | |
| let defaults = UserDefaults.standard | |
| return defaults.object(forKey: key) as? Memento |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @IBAction func saveChangesAction(_ sender: UIButton) { | |
| let formState = FormState(name: nameField.text!, surname: surnameField.text!, address: addressField.text!, essay: essayField.text, email: emailField.text!) | |
| FormStateSaver.save(formState, for: "mainForm") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func retriveState() { | |
| if let memento = FormStateSaver.restore("mainForm") { | |
| let state = FormState(memento: memento) | |
| configureView(for: state) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct FormStateSaver { | |
| static func save(_ state: MementoConvertible, for key: String) { | |
| let defaults = UserDefaults.standard | |
| let versions = defaults.array(forKey: key) | |
| if var versions = versions { | |
| versions.append(state.memento) | |
| defaults.set(versions, forKey: key) | |
| } else { | |
| defaults.set([state.memento], forKey: key) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Defaults { | |
| static var mainFormStates: [Memento] { | |
| get { | |
| return UserDefaults.standard.array(forKey: UserDefaultConstants.mainForm.rawValue) as? [Memento] ?? [Memento]() | |
| } | |
| set { | |
| UserDefaults.standard.set(newValue, forKey: UserDefaultConstants.mainForm.rawValue) | |
| UserDefaults.standard.synchronize() | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct FormStateSaver { | |
| static func save(_ state: MementoConvertible) { | |
| Defaults.mainFormStates.append(state.memento) | |
| } | |
| static func restore() -> [FormState] { | |
| return Defaults.mainFormStates.map {FormState(memento: $0)} | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @IBAction func publishAction(_ sender: Any) { | |
| do { | |
| let email = try emailField.validatedText(validationType: ValidatorType.email) | |
| let name = try nameField.validatedText(validationType: ValidatorType.name(field: "Name")) | |
| let surname = try surnameField.validatedText(validationType: ValidatorType.name(field: "Surname")) | |
| let address = try addressField.validatedText(validationType: ValidatorType.requiredField(field: "Address")) | |
| let essay = try self.essayField.validatedText(validationType: ValidatorType.requiredField(field: "Essay")) | |
| handleDataPublished() | |
| } catch(let error) { | |
| alert(with: (error as! ValidationError).message) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension UITextField { | |
| func validatedText(validationType: ValidatorType) throws -> String { | |
| switch validationType { | |
| case .email: //Validation Logic to determine if text is an email | |
| case .name: //Validation Logic to determine if text is allowed to represent a name | |
| case .requiredField: //Validation Logic to determine if text is provided | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum VaildatorFactory { | |
| static func validatorFor(type: ValidatorType) -> ValidatorConvertible { | |
| switch type { | |
| case .email: return EmailValidator() | |
| case .name(let fieldName) : return NameValidator(fieldName) | |
| case .requiredField(let fieldName): return RequiredFieldValidator(fieldName) | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension UITextField { | |
| func validatedText(validationType: ValidatorType) throws -> String { | |
| let validator = VaildatorFactory.validatorFor(type: validationType) | |
| return try validator.validated(self.text!) | |
| } | |
| } |