Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Vandcarlos/f3a2491f36c4f9b4eaa1819962aa8b55 to your computer and use it in GitHub Desktop.

Select an option

Save Vandcarlos/f3a2491f36c4f9b4eaa1819962aa8b55 to your computer and use it in GitHub Desktop.
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