<commit_before>//
//  ViewController.swift
//  NamesToFaces
//
//  Created by <NAME> on 19/10/2015.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate
{
	@IBOutlet weak var collectionView: UICollectionView!
	
	var people = [Person]()
	
	override func viewDidLoad()
	{
		super.viewDidLoad()
		
		// Add new person button
		navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "addNewPerson")
		
		// Load saved data
		let defaults = NSUserDefaults.standardUserDefaults()
		if let savedPeople = defaults.objectForKey("people") as? NSData
		{
			people = NSKeyedUnarchiver.unarchiveObjectWithData(savedPeople) as! [Person]
		}
	}
	
	func addNewPerson()
	{
		let picker = UIImagePickerController()
		picker.allowsEditing = true
		picker.delegate = self
		presentViewController(picker, animated: true, completion: nil)
	}
	
	func getDocumentsDirectory() -> NSString
	{
		let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
		let documentsDirectory = paths[0]
		//print(documentsDirectory)
		return documentsDirectory
	}
	
	func renamePerson(person: Person, indexPath: NSIndexPath)
	{
		let ac = UIAlertController(title: "Rename person", message: nil, preferredStyle: .Alert)
		ac.addTextFieldWithConfigurationHandler
			{
				field in
				if(person.name != "Unknown")
				{
					field.text = person.name
				}
		}
		ac.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
		ac.addAction(UIAlertAction(title: "OK", style: .Default)
			{
				[unowned self, ac] _ in
				let newName = ac.textFields![0]
				person.name = newName.text!
				self.collectionView.reloadItemsAtIndexPaths([indexPath])
				self.save()
			})
		
		self.presentViewController(ac, animated: true, completion: nil)
	}
	
	func deletePerson(person: Person, indexPath: NSIndexPath)
	{
		let ac = UIAlertController(title: "Delete person", message: "Are you sure you want to delete this person?", preferredStyle: .Alert)
		ac.addAction(UIAlertAction(title: "No", style: .Cancel, handler: nil))
		ac.addAction(UIAlertAction(title: "Yes", style: .Default)
			{
				[unowned self] _ in
				self.people.removeAtIndex(indexPath.item)
				self.collectionView.reloadData()
				self.save()
			})
		
		self.presentViewController(ac, animated: true, completion: nil)
	}
	
	func save()
	{
		let savedData = NSKeyedArchiver.archivedDataWithRootObject(people)
		let defaults = NSUserDefaults.standardUserDefaults()
		defaults.setObject(savedData, forKey: "people")
	}
	<commit_msg>Delete associated image when a person is deleted.<commit_after>//
//  ViewController.swift
//  NamesToFaces
//
//  Created by <NAME> on 19/10/2015.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate
{
	@IBOutlet weak var collectionView: UICollectionView!
	
	var people = [Person]()
	
	override func viewDidLoad()
	{
		super.viewDidLoad()
		
		// Add new person button
		navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "addNewPerson")
		
		// Load saved data
		let defaults = NSUserDefaults.standardUserDefaults()
		if let savedPeople = defaults.objectForKey("people") as? NSData
		{
			people = NSKeyedUnarchiver.unarchiveObjectWithData(savedPeople) as! [Person]
		}
	}
	
	func addNewPerson()
	{
		let picker = UIImagePickerController()
		picker.allowsEditing = true
		picker.delegate = self
		presentViewController(picker, animated: true, completion: nil)
	}
	
	func getDocumentsDirectory() -> NSString
	{
		let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
		let documentsDirectory = paths[0]
		//print(documentsDirectory)
		return documentsDirectory
	}
	
	func renamePerson(person: Person, indexPath: NSIndexPath)
	{
		let ac = UIAlertController(title: "Rename person", message: nil, preferredStyle: .Alert)
		ac.addTextFieldWithConfigurationHandler
			{
				field in
				if(person.name != "Unknown")
				{
					field.text = person.name
				}
		}
		ac.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
		ac.addAction(UIAlertAction(title: "OK", style: .Default)
			{
				[unowned self, ac] _ in
				let newName = ac.textFields![0]
				person.name = newName.text!
				self.collectionView.reloadItemsAtIndexPaths([indexPath])
				self.save()
			})
		
		self.presentViewController(ac, animated: true, completion: nil)
	}
	
	func deletePerson(person: Person, indexPath: NSIndexPath)
	{
		let ac = UIAlertController(title: "Delete person", message: "Are you sure you want to delete this person?", preferredStyle: .Alert)
		ac.addAction(UIAlertAction(title: "No", style: .Cancel, handler: nil))
		ac.addAction(UIAlertAction(title: "Yes", style: .Default)
			{
				[unowned self] _ in
				self.people.removeAtIndex(indexPath.item)
                
                // Delete image
                let imagePath = self.getDocumentsDirectory().stringByAppendingPathComponent(person.image)
                let fileMgr = NSFileManager.defaultManager()
                
                do
                {
                    try fileMgr.removeItemAtPath(imagePath)
                }
                catch
                {
                    print("Failed to delete image file")
                }
                
				self.collectionView.reloadData()
				self.save()
			})
		
		self.presentViewController(ac, animated: true, completion: nil)
	}
	
	func save()
	{
		let savedData = NSKeyedArchiver.archivedDataWithRootObject(people)
		let defaults = NSUserDefaults.standardUserDefaults()
		defaults.setObject(savedData, forKey: "people")
	}
	
{'fraction_non_alphanumeric': 0.10421585160202361, 'fraction_numerical': 0.004721753794266442, 'mean_word_length': 3.6664044059795438, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '2483908', 'n_tokens_mistral': 2015, 'n_tokens_neox': 1739, 'n_words': 408}