Created
June 12, 2020 00:18
-
-
Save Vandcarlos/f3a2491f36c4f9b4eaa1819962aa8b55 to your computer and use it in GitHub Desktop.
It's a gist of file https://github.com/Vandcarlos/Pet-Stuffs-MVC/blob/master/Pet%20Stuffs%20MVC/Features/Stuff%20Form/StuffFormViewController.swift to use in medium
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
| import UIKit | |
| final class StuffFormViewController: UIViewController { | |
| @IBOutlet weak var nameLabel: UITextField! | |
| @IBOutlet weak var typeLabel: UITextField! | |
| @IBOutlet weak var priceLabel: UITextField! | |
| var petUuid: String! | |
| var stuff: StuffModel? { | |
| willSet(value) { | |
| self.title = value == nil ? "Adicionar Stuff" : "Editar Stuff" | |
| } | |
| } | |
| private let priceDelegate = PriceDelagate() | |
| private let errorMessage = """ | |
| Verifique as condições | |
| O nome possuí no mínimo: \(StuffValidation.minNameSize) caracteres | |
| O tipo possuí no mínimo: \(StuffValidation.minTypeSize) caracteres | |
| O preço é valido | |
| """ | |
| override func viewDidLoad() { | |
| self.priceLabel.delegate = priceDelegate | |
| self.checkIfHasStuff() | |
| } | |
| } | |
| // MARK: View actions | |
| extension StuffFormViewController { | |
| @IBAction func saveButtonTapped(_ sender: UIBarButtonItem) { | |
| if let stuffForm = self.getValidForm() { | |
| self.saveStuff(with: stuffForm) | |
| self.navigationController?.popViewController(animated: true) | |
| } else { | |
| self.showError() | |
| } | |
| } | |
| } | |
| // MARK: Private methods | |
| extension StuffFormViewController { | |
| private func checkIfHasStuff() { | |
| if let stuff = self.stuff { | |
| self.fullFillForm(with: stuff) | |
| } | |
| } | |
| private func fullFillForm(with stuff: StuffModel) { | |
| self.nameLabel.text = stuff.name | |
| self.typeLabel.text = stuff.type | |
| self.priceLabel.text = String(format: "%.2f", stuff.price) | |
| } | |
| private func getValidForm() -> StuffForm? { | |
| guard let name = self.getValidName(), | |
| let type = self.getValidType(), | |
| let price = self.getValidPrice() | |
| else { return nil } | |
| return StuffForm(uuid: self.stuff?.uuid, petUuid: self.petUuid, name: name, type: type, price: price) | |
| } | |
| private func getValidName() -> String? { | |
| if let name = nameLabel.text, name.count > StuffValidation.minNameSize { | |
| return name | |
| } else { | |
| return nil | |
| } | |
| } | |
| private func getValidType() -> String? { | |
| if let type = typeLabel.text, type.count > StuffValidation.minTypeSize { | |
| return type | |
| } else { | |
| return nil | |
| } | |
| } | |
| private func getValidPrice() -> Double? { | |
| return priceLabel.text?.toDoubleWithLocale() | |
| } | |
| private func saveStuff(with stuffForm: StuffForm) { | |
| if let uuid = stuffForm.uuid, var stuff = StuffModel.get(byUuid: uuid) { | |
| stuff.name = stuffForm.name | |
| stuff.type = stuffForm.type | |
| stuff.price = stuffForm.price | |
| stuff.saveOrUpdate() | |
| } else { | |
| let stuff = StuffModel(petUuid: self.petUuid, name: stuffForm.name, type: stuffForm.type, price: stuffForm.price) | |
| stuff.saveOrUpdate() | |
| } | |
| } | |
| private func showError() { | |
| let alertController = UIAlertController( | |
| title: "Erro ao salvar", | |
| message: self.errorMessage, | |
| preferredStyle: .alert | |
| ) | |
| let closeButton = UIAlertAction(title: "Fechar", style: .cancel) | |
| alertController.addAction(closeButton) | |
| self.present(alertController, animated: true) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment