<commit_before>//
//  NewTaskViewController.swift
//  TODOApp
//
//  Created by <NAME> on 7/12/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import UIKit

protocol ModalTaskCapturingDelegateProtocol: class
{
    func taskCapturingModalDidFinish(taskTitle: String)
    func taskCapturingModalDidCancel()
}

class NewTaskViewController: UIViewController
{
    @IBOutlet weak var taskTitleTextField: UITextField!
    weak var taskCapturingDelegate: ModalTaskCapturingDelegateProtocol?
    
    deinit
    {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
}

// MARK: Factory methods
extension NewTaskViewController
{
    class func storyboard() -> UIStoryboard
    {
        let storyboard = UIStoryboard(name: "NewTaskViewController", bundle: nil)
        return storyboard
    }
    
    class func instantiateFromStoryboard() -> NewTaskViewController
    {
        let storyboard = self.storyboard()
        let vc = storyboard.instantiateViewControllerWithIdentifier("NewTaskViewController") as! NewTaskViewController
        return vc
    }
}

// MARK: View Lifecycle
extension NewTaskViewController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()
        title = "New Task"
        _setupNavBarButtons()
        _setupTextField()
    }

    override func viewWillAppear(animated: Bool)
    {
        super.viewWillAppear(animated)
        _subscribeToTextFieldNotifications()
        _updateNavBarDoneButtonEnabledness()
    }

    override func viewDidDisappear(animated: Bool)
    {
        super.viewDidDisappear(animated)
        _unsubscribeFromTextFieldNotifications()
    }
}

extension NewTaskViewController: UITextFieldDelegate
{
    func textFieldShouldReturn(textField: UITextField) -> Bool
    {
        _textFieldDoneButtonWillGetTapped()
        return true
    }
}

// MARK: UITextField methods
extension NewTaskViewController
{
    private func _subscribeToTextFieldNotifications()
    {
        let action: Selector = "textFieldDidChangeNotificationHandler:"
        NSNotificationCenter.defaultCenter().addObserver(self,
            selector: action,
            name: UITextFieldTextDidChangeNotification,
            object: taskTitleTextField)
        
    }
    
    private func _unsubscribeFromTextFieldNotifications()
    {
        NSNotificationCenter.defaultCenter().removeObserver(self,
            name: UITextFieldTextDidChangeNotification,
            object: taskTitleTextField)
    }
    
    func textFieldDidChangeNotificationHandler(notification: NSNotification)
    {
        _updateNavBarDoneButtonEnabledness()
    }
}

// MARK: buttons
extension NewTaskViewController
{
    private func _addDoneBarButtonItemToNavBar()
    {
        navigationItem.rightBarButtonItem = _createDoneBarButtonItem()
    }
    
    private func _createDoneBarButtonItem() -> UIBarButtonItem
    {
        let action: Selector = "doneButtonDidGetTapped"
        let doneBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Done,
            target: self,
            action: action)
        return doneBarButtonItem
    }
    
    func doneButtonDidGetTapped()
    {
        taskTitleTextField.resignFirstResponder()
        _finishIfAppropriate()
    }
    
    private func _addCancelBarButtonItemToNavBar()
    {
        navigationItem.leftBarButtonItem = _createCancelBarButtonItem()
    }
    
    private func _createCancelBarButtonItem() -> UIBarButtonItem
    {
        let action: Selector = "cancelButtonDidGetTapped"
        let cancelBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Cancel,
            target: self,
            action: action)
        return cancelBarButtonItem
    }
    
    func cancelButtonDidGetTapped()
    {
        taskCapturingDelegate?.taskCapturingModalDidCancel()
    }
    
    private func _updateNavBarDoneButtonEnabledness()
    {
        let shouldEnable = count(taskTitleTextField.text) > 0
        navigationItem.rightBarButtonItem?.enabled = shouldEnable
    }
}

// MARK: private helpers
extension NewTaskViewController
{
    private func _setupNavBarButtons()
    {
        _addCancelBarButtonItemToNavBar()
        _addDoneBarButtonItemToNavBar()
        
    }
    private func _setupTextField()
    {
        taskTitleTextField.returnKeyType = .Done
        taskTitleTextField.delegate = self
    }
    
    private func _textFieldDoneButtonWillGetTapped()
    {
        doneButtonDidGetTapped()
    }
    <commit_msg>episode 3: tap recognizer on background view to dismiss the keyboard<commit_after>//
//  NewTaskViewController.swift
//  TODOApp
//
//  Created by <NAME> on 7/12/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import UIKit

protocol ModalTaskCapturingDelegateProtocol: class
{
    func taskCapturingModalDidFinish(taskTitle: String)
    func taskCapturingModalDidCancel()
}

class NewTaskViewController: UIViewController
{
    @IBOutlet weak var taskTitleTextField: UITextField!
    weak var taskCapturingDelegate: ModalTaskCapturingDelegateProtocol?
    
    deinit
    {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
}

// MARK: Factory methods
extension NewTaskViewController
{
    class func storyboard() -> UIStoryboard
    {
        let storyboard = UIStoryboard(name: "NewTaskViewController", bundle: nil)
        return storyboard
    }
    
    class func instantiateFromStoryboard() -> NewTaskViewController
    {
        let storyboard = self.storyboard()
        let vc = storyboard.instantiateViewControllerWithIdentifier("NewTaskViewController") as! NewTaskViewController
        return vc
    }
}

// MARK: View Lifecycle
extension NewTaskViewController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()
        title = "New Task"
        _setupNavBarButtons()
        _setupTextField()
        _setupBackgroundView()
    }
    
    override func viewWillAppear(animated: Bool)
    {
        super.viewWillAppear(animated)
        _subscribeToTextFieldNotifications()
        _updateNavBarDoneButtonEnabledness()
    }

    override func viewDidDisappear(animated: Bool)
    {
        super.viewDidDisappear(animated)
        _unsubscribeFromTextFieldNotifications()
    }
}

extension NewTaskViewController: UITextFieldDelegate
{
    func textFieldShouldReturn(textField: UITextField) -> Bool
    {
        _textFieldDoneButtonWillGetTapped()
        return true
    }
}

// MARK: UITextField methods
extension NewTaskViewController
{
    private func _subscribeToTextFieldNotifications()
    {
        let action: Selector = "textFieldDidChangeNotificationHandler:"
        NSNotificationCenter.defaultCenter().addObserver(self,
            selector: action,
            name: UITextFieldTextDidChangeNotification,
            object: taskTitleTextField)
        
    }
    
    private func _unsubscribeFromTextFieldNotifications()
    {
        NSNotificationCenter.defaultCenter().removeObserver(self,
            name: UITextFieldTextDidChangeNotification,
            object: taskTitleTextField)
    }
    
    func textFieldDidChangeNotificationHandler(notification: NSNotification)
    {
        _updateNavBarDoneButtonEnabledness()
    }

    private func _setupTextField()
    {
        taskTitleTextField.returnKeyType = .Done
        taskTitleTextField.delegate = self
    }
    
    private func _textFieldDoneButtonWillGetTapped()
    {
        doneButtonDidGetTapped()
    }
}

// MARK: buttons
extension NewTaskViewController
{
    private func _setupNavBarButtons()
    {
        _addCancelBarButtonItemToNavBar()
        _addDoneBarButtonItemToNavBar()
    }

    private func _addDoneBarButtonItemToNavBar()
    {
        navigationItem.rightBarButtonItem = _createDoneBarButtonItem()
    }
    
    private func _createDoneBarButtonItem() -> UIBarButtonItem
    {
        let action: Selector = "doneButtonDidGetTapped"
        let doneBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Done,
            target: self,
            action: action)
        return doneBarButtonItem
    }
    
    func doneButtonDidGetTapped()
    {
        taskTitleTextField.resignFirstResponder()
        _finishIfAppropriate()
    }
    
    private func _addCancelBarButtonItemToNavBar()
    {
        navigationItem.leftBarButtonItem = _createCancelBarButtonItem()
    }
    
    private func _createCancelBarButtonItem() -> UIBarButtonItem
    {
        let action: Selector = "cancelButtonDidGetTapped"
        let cancelBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Cancel,
            target: self,
            action: action)
        return cancelBarButtonItem
    }
    
    func cancelButtonDidGetTapped()
    {
        taskCapturingDelegate?.taskCapturingModalDidCancel()
    }
    
    private func _updateNavBarDoneButtonEnabledness()
    {
        let shouldEnable = count(taskTitleTextField.text) > 0
        navigationItem.rightBarButtonItem?.enabled = shouldEnable
    }
}

// MARK: background view
extension NewTaskViewController
{
    private func _setupBackgroundView()
    {
        let action: Selector = "tapDidGetRecognized:"
        let recognizer = UITapGestureRecognizer(target: self, action: action)
        view.addGestureRecognizer(recognizer)
    }
    
    func tapDidGetRecognized(recognizer: UITapGestureRecognizer)
    {
        taskTitleTextField.resignFirstResponder()
    }
}

// MARK: private helpers
extension NewTaskViewController
{
{'fraction_non_alphanumeric': 0.06837969844977702, 'fraction_numerical': 0.0022297727755362074, 'mean_word_length': 2.9476110645431683, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2345647', 'n_tokens_mistral': 2619, 'n_tokens_neox': 2503, 'n_words': 484}