<commit_before>//
//  MHEditViewController.swift
//  Hello
//
//  Created by <NAME> on 11/8/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import UIKit
class MHEditViewController: UITableViewController, UITextFieldDelegate, NSFetchedResultsControllerDelegate{
    weak var mainViewController: MHViewController?
    var fetchedResultsController: NSFetchedResultsController?
    var editButton: UIBarButtonItem?
    @IBAction func dismiss(sender: UIBarButtonItem) -> Void{
        if tableView.editing{
            tableView.setEditing(false, animated: true)
            navigationItem.leftBarButtonItem = editButton
        }
        else{
            MHCoreDataStack.defaultStack()?.saveContext()
            dismissViewControllerAnimated(true, completion: nil)
        }
    }
    @IBAction func edit(sender: UIBarButtonItem) -> Void{
        tableView.setEditing(true, animated: true)
        navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "addEntry")
        navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
    }
    func addEntry() -> Void{
        let new = NSEntityDescription.insertNewObjectForEntityForName("Display", inManagedObjectContext: MHCoreDataStack.defaultStack()!.managedObjectContext) as! Display
        new.phrase = "Hello"
        MHCoreDataStack.defaultStack()?.saveContextWithCompletionHandler({ (success: Bool, error: NSError?) -> Void in
            guard success && error == nil else{
                print("You're a failure. Here's why: \(error?.localizedDescription)")
                return
            }
//            let path = self.fetchedResultsController!.indexPathForObject(new)!
//            self.tableView.insertRowsAtIndexPaths([path], withRowAnimation: .Automatic)
//            let cell = self.tableView.cellForRowAtIndexPath(path) as! MHEditViewCell
//            cell.textField.text = new.phrase
//            cell.textField.becomeFirstResponder()
        })
    }
    override func viewDidLoad() -> Void{
        super.viewDidLoad()
        editButton = navigationItem.leftBarButtonItem
        tableView.backgroundColor = UIColor.blackColor()
        let request = NSFetchRequest(entityName: "Display")
        request.sortDescriptors = [NSSortDescriptor(key: "phrase", ascending: true)]
        fetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: MHCoreDataStack.defaultStack()!.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
        fetchedResultsController?.delegate = self
        do{
            try fetchedResultsController!.performFetch()
        }
        catch let error as NSError{
            print("The whole damn thing crashed. Here's why: \(error.localizedDescription)")
        }
        tableView.setEditing(false, animated: false)
    }
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int{
        guard let count = fetchedResultsController?.sections?.count else{
            return 0
        }
        return count
    }
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        guard let count = fetchedResultsController?.sections?[section] else{
            return 0
        }
        return count.numberOfObjects
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! MHEditViewCell
        cell.backgroundColor = UIColor.blackColor()
        cell.textField.text = (fetchedResultsController?.objectAtIndexPath(indexPath) as! Display).phrase
        cell.textField.textColor = UIColor.whiteColor()
        cell.textField.delegate = self
        if tableView.editing{
            print("The table view is editing")
            cell.textField.userInteractionEnabled = true
        }<commit_msg>Post-demo cleanups before an overhaul<commit_after>//
//  MHEditViewController.swift
//  Hello
//
//  Created by <NAME> on 11/8/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import UIKit
class MHEditViewController: UITableViewController, UITextFieldDelegate, NSFetchedResultsControllerDelegate{
    weak var mainViewController: MHViewController?
    var fetchedResultsController: NSFetchedResultsController?
    var editButton: UIBarButtonItem?
    @IBAction func dismiss(sender: UIBarButtonItem) -> Void{
        if tableView.editing{
            tableView.setEditing(false, animated: true)
            navigationItem.leftBarButtonItem = editButton
        }
        else{
            MHCoreDataStack.defaultStack()?.saveContext()
            dismissViewControllerAnimated(true, completion: nil)
        }
    }
    @IBAction func edit(sender: UIBarButtonItem) -> Void{
        tableView.setEditing(true, animated: true)
        navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "addEntry")
        navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
    }
    func addEntry() -> Void{
        let new = NSEntityDescription.insertNewObjectForEntityForName("Display", inManagedObjectContext: MHCoreDataStack.defaultStack()!.managedObjectContext) as! Display
        new.phrase = "Hello"
        MHCoreDataStack.defaultStack()?.saveContextWithCompletionHandler({ (success: Bool, error: NSError?) -> Void in
            guard success && error == nil else{
                print("You're a failure. Here's why: \(error?.localizedDescription)")
                return
            }
            let cell = self.tableView.cellForRowAtIndexPath(self.fetchedResultsController!.indexPathForObject(new)!) as! MHEditViewCell
            cell.textField.text = new.phrase
            cell.textField.becomeFirstResponder()
        })
    }
    override func viewDidLoad() -> Void{
        super.viewDidLoad()
        editButton = navigationItem.leftBarButtonItem
        tableView.backgroundColor = UIColor.blackColor()
        let request = NSFetchRequest(entityName: "Display")
        request.sortDescriptors = [NSSortDescriptor(key: "phrase", ascending: true)]
        fetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: MHCoreDataStack.defaultStack()!.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
        fetchedResultsController?.delegate = self
        do{
            try fetchedResultsController!.performFetch()
        }
        catch let error as NSError{
            print("The whole damn thing crashed. Here's why: \(error.localizedDescription)")
        }
        tableView.setEditing(false, animated: false)
    }
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int{
        guard let count = fetchedResultsController?.sections?.count else{
            return 0
        }
        return count
    }
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        guard let count = fetchedResultsController?.sections?[section] else{
            return 0
        }
        return count.numberOfObjects
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! MHEditViewCell
        cell.backgroundColor = UIColor.blackColor()
        cell.textField.text = (fetchedResultsController?.objectAtIndexPath(indexPath) as! Display).phrase
        cell.textField.textColor = UIColor.whiteColor()
        cell.textField.delegate = self
        if tableView.editing{
            cell.textField.userInteractionEnabled = true
        }
{'fraction_non_alphanumeric': 0.09006331567385967, 'fraction_numerical': 0.0028427445406383253, 'mean_word_length': 3.101748807631161, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, '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': 2, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4100118', 'n_tokens_mistral': 2119, 'n_tokens_neox': 2017, 'n_words': 441}