-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewController.swift
128 lines (97 loc) · 4.37 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// ViewController.swift
// NamesToFaces
//
// Created by Julian Moorhouse on 06/08/2019.
// Copyright © 2019 Mindwarp Consultancy Ltd. All rights reserved.
//
import UIKit
class ViewController: UICollectionViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var people = [Person]()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addNewPerson))
let defaults = UserDefaults.standard
if let savedpeople = defaults.object(forKey: "people") as? Data {
let jsonDecoder = JSONDecoder()
do {
people = try jsonDecoder.decode([Person].self, from: savedpeople)
} catch {
print("Failed to load people.")
}
}
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return people.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Person", for: indexPath) as? PersonCell else {
fatalError("Unable to dequeue PersonCell")
}
let person = people[indexPath.item]
cell.Name.text = person.name
let path = getDocumentsDirectory().appendingPathComponent(person.image)
cell.ImageView.image = UIImage(contentsOfFile: path.path)
cell.ImageView.layer.borderColor = UIColor(white: 0, alpha: 0.3).cgColor
cell.ImageView.layer.borderWidth = 2
cell.ImageView.layer.cornerRadius = 3
cell.layer.cornerRadius = 7
return cell
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let person = people[indexPath.item]
let ac = UIAlertController(title: "Person", message: "Would you like to rename or delete this person?", preferredStyle: .alert)
ac.addTextField()
ac.addAction(UIAlertAction(title: "Rename", style: .default) {
[weak self, weak ac] _ in
guard let newName = ac?.textFields?[0].text else { return }
person.name = newName
self?.save()
self?.collectionView.reloadData()
})
ac.addAction(UIAlertAction(title: "Delete", style: .default) {
[weak self] _ in
self?.people.remove(at: indexPath.item)
self?.save()
self?.collectionView.reloadData()
})
ac.addAction(UIAlertAction(title: "Cancel", style: .cancel))
present(ac, animated: true)
}
@objc func addNewPerson() {
let picker = UIImagePickerController()
picker.allowsEditing = true
picker.delegate = self
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerController.SourceType.camera))
{
picker.sourceType = UIImagePickerController.SourceType.camera
}
present(picker, animated: true)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[.editedImage] as? UIImage else { return }
let imageName = UUID().uuidString
let imagePath = getDocumentsDirectory().appendingPathComponent(imageName)
if let jpegData = image.jpegData(compressionQuality: 0.8) {
try? jpegData.write(to: imagePath)
}
let person = Person(name: "Unknown", image: imageName)
people.append(person)
save()
collectionView.reloadData()
dismiss(animated: true)
}
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}
func save() {
let jsonEncoder = JSONEncoder()
if let savedDate = try? jsonEncoder.encode(people) {
let defaults = UserDefaults.standard
defaults.set(savedDate, forKey: "people")
} else {
print("Failed to save people")
}
}
}