hugh-h
10/18/2017 - 9:00 PM

UserDetailController

storyboards, NotificationCenter, NSLayoutConstraint

class UserDetailController: UIViewController {

    @IBOutlet var photoImage: UIImageView!
    @IBOutlet var fullNameField: UITextField!
    @IBOutlet var doneButton: UIButton!
    
    @IBOutlet weak var photoHeightConstraint: NSLayoutConstraint!
    var user:User?
    
    @IBOutlet weak var photoWidthConstraint: NSLayoutConstraint!
    
    @IBAction func onBeginEditingFullName(_ sender: Any) {
        doneButton.alpha = 1
    }
    
    @IBAction func onFinishEditingFullName(_ sender: Any) {
        doneButton.alpha = 0
    }
    
    @IBAction func onDoneButtonTap(_ sender: Any) {
        fullNameField.resignFirstResponder()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let firstName = user?.firstName ?? ""
        let lastName = user?.lastName ?? ""
        doneButton.tintColor = UIColor.blue
        doneButton.alpha = 0
        
        fullNameField.text = "\(firstName) ]\(lastName)"
        if let url = user?.image128 {
            photoImage.af_setImage(withURL: url)
            photoImage.layer.cornerRadius = 60
        }
    }

    override func viewWillAppear(_ animated: Bool) {
        
        NotificationCenter.default.addObserver(self, selector: #selector(onShowKeyboard(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(onHideKeyboard(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }
    
    @objc(onShowKeyboard:)
    func onShowKeyboard(notification:NSNotification) {
        photoHeightConstraint.constant = 40
        photoWidthConstraint.constant = 40
    }
    
    @objc(onHideKeyboard:)
    func onHideKeyboard(notification:NSNotification) {
        photoHeightConstraint.constant = 120
        photoWidthConstraint.constant = 120
    }
    override func viewWillDisappear(_ animated: Bool) {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}