Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// ProfileViewController.swift
// Bg-Mon
//
// Created by Keaton Burleson on 4/4/16.
// Copyright © 2016 Keaton Burleson. All rights reserved.
//
import Foundation
import UIKit
import MBProgressHUD
class ProfileViewController: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate {
//MARK: - Properties
@IBOutlet var sensitivity: UITextField?
@IBOutlet var targetBG: UITextField?
@IBOutlet var carbRatio: UITextField?
@IBOutlet var doctorName: UITextField?
@IBOutlet var doctorEmail: UITextField?
@IBOutlet var longBrandField: UITextField?
@IBOutlet var longBrandLabel: UILabel?
@IBOutlet var fastBrandField: UITextField?
@IBOutlet var fastBrandLabel: UILabel?
@IBOutlet var fastUnits: UITextField?
@IBOutlet var longUnits: UITextField?
@IBOutlet var switchReminders: UISwitch?
@IBOutlet var switchAmerica: UISwitch?
@IBOutlet var switchRound: UISwitch?
fileprivate var profileManager: ProfileManager?
fileprivate var hud: MBProgressHUD?
//MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
setDelegates()
setNextFields()
profileManager = ProfileManager()
}
//MARK - UITextFieldDelegate Methods
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.nextField?.becomeFirstResponder()
return true
}
//MARK: - Updater Methods
@IBAction func updateMetric(_ sender: UISwitch) {
profileManager?.useMetric = sender.isOn
}
@IBAction func updateReminders(_ sender: UISwitch) {
profileManager?.useReminders = sender.isOn
}
@IBAction func updateRounding(_ sender: UISwitch) {
profileManager?.roundInsulin = sender.isOn
}
//MARK - Helper Methods
@IBAction func saveAll() {
resignAllSubviews()
if sensitivity?.text != "" {
profileManager?.sensitivity = sensitivity?.text
}
if carbRatio?.text != "" {
profileManager?.ratio = carbRatio?.text
}
if targetBG?.text != "" {
profileManager?.targetBloodSugar = targetBG?.text
}
if doctorEmail?.text != "" {
profileManager?.drEmail = doctorEmail?.text
}
if doctorName?.text != "" {
profileManager?.drName = doctorName?.text
}
if fastBrandField?.text != nil {
profileManager?.standardBrand = fastBrandField?.text
}
if longBrandField?.text != "" {
profileManager?.longLastingBrand = longBrandField?.text
}
if longUnits?.text != "" {
profileManager?.longLastingUnits = longUnits?.text
}
if fastUnits?.text != "" {
profileManager?.standardUnits = fastUnits?.text
}
}
fileprivate func setDelegates() {
targetBG?.delegate = self
sensitivity?.delegate = self
carbRatio?.delegate = self
doctorName?.delegate = self
fastUnits?.delegate = self
longUnits?.delegate = self
longBrandField?.delegate = self
fastBrandField?.delegate = self
}
fileprivate func setNextFields() {
targetBG?.nextField = sensitivity
sensitivity?.nextField = carbRatio
carbRatio?.nextField = doctorName
doctorName?.nextField = doctorEmail
doctorEmail?.nextField = longBrandField
longBrandField?.nextField = fastBrandField
fastBrandField?.nextField = longUnits
longUnits?.nextField = fastUnits
}
fileprivate func resignAllSubviews() {
for view in self.view.subviews {
if view is UITextField {
let field = view as! UITextField
field.resignFirstResponder()
}
}
self.view.endEditing(true)
}
//MARK: - UITableViewDelegate Methods
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView.deselectRow(at: indexPath, animated: true)
}
}
extension ProfileViewController: ProfileManagerDelegate {
func updatedMetric(metric: Bool!) {
if metric == true {
targetBG?.placeholder = "7.8 mmol/L"
doctorEmail?.placeholder = "plumbottom@doctor.co.uk"
} else {
targetBG?.placeholder = "120 mg/dL"
doctorEmail?.placeholder = "doctor@dr-office.org"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.