Add `Person` model, support upload images, support updating name
This commit is contained in:
parent
3ad8014d3c
commit
efb3dc7802
|
@ -7,6 +7,7 @@
|
|||
objects = {
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
F3048AC921F9B2C300ECADB1 /* Person.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3048AC821F9B2C300ECADB1 /* Person.swift */; };
|
||||
F35A33C221F8F29C0025CFE0 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F35A33C121F8F29C0025CFE0 /* AppDelegate.swift */; };
|
||||
F35A33C421F8F29C0025CFE0 /* HomeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F35A33C321F8F29C0025CFE0 /* HomeViewController.swift */; };
|
||||
F35A33C721F8F29C0025CFE0 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F35A33C521F8F29C0025CFE0 /* Main.storyboard */; };
|
||||
|
@ -16,6 +17,7 @@
|
|||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
F3048AC821F9B2C300ECADB1 /* Person.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Person.swift; sourceTree = "<group>"; };
|
||||
F35A33BE21F8F29C0025CFE0 /* Names And Faces.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Names And Faces.app"; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
F35A33C121F8F29C0025CFE0 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
|
||||
F35A33C321F8F29C0025CFE0 /* HomeViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeViewController.swift; sourceTree = "<group>"; };
|
||||
|
@ -58,6 +60,7 @@
|
|||
children = (
|
||||
F35A33C121F8F29C0025CFE0 /* AppDelegate.swift */,
|
||||
F35A33C321F8F29C0025CFE0 /* HomeViewController.swift */,
|
||||
F3048AC821F9B2C300ECADB1 /* Person.swift */,
|
||||
F35A33C521F8F29C0025CFE0 /* Main.storyboard */,
|
||||
F3BA7EF421F9956E001FD18E /* PersonCell.swift */,
|
||||
F35A33C821F8F29D0025CFE0 /* Assets.xcassets */,
|
||||
|
@ -140,6 +143,7 @@
|
|||
files = (
|
||||
F3BA7EF521F9956E001FD18E /* PersonCell.swift in Sources */,
|
||||
F35A33C421F8F29C0025CFE0 /* HomeViewController.swift in Sources */,
|
||||
F3048AC921F9B2C300ECADB1 /* Person.swift in Sources */,
|
||||
F35A33C221F8F29C0025CFE0 /* AppDelegate.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
|
|
|
@ -9,6 +9,9 @@
|
|||
import UIKit
|
||||
|
||||
class HomeViewController: UICollectionViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
|
||||
var people = [Person]()
|
||||
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
// Do any additional setup after loading the view, typically from a nib.
|
||||
|
@ -18,17 +21,30 @@ class HomeViewController: UICollectionViewController, UIImagePickerControllerDel
|
|||
|
||||
|
||||
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
||||
return 10
|
||||
return people.count
|
||||
}
|
||||
|
||||
|
||||
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
||||
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Person", for: indexPath) as! PersonCell
|
||||
let person = people[indexPath.item]
|
||||
|
||||
cell.personImageView.image = UIImage(contentsOfFile: getURL(forFile: person.imageName).path)
|
||||
cell.personNameLabel.text = person.name
|
||||
|
||||
setStyles(forCell: cell)
|
||||
|
||||
return cell
|
||||
}
|
||||
|
||||
|
||||
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
||||
let person = people[indexPath.item]
|
||||
|
||||
promptForName(of: person)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Handles the completion of adding an image to the picker. Our flow:
|
||||
- Extract the image from the dictionary that is passed as a parameter.
|
||||
|
@ -41,13 +57,17 @@ class HomeViewController: UICollectionViewController, UIImagePickerControllerDel
|
|||
guard let imagePicked = info[.editedImage] as? UIImage else { return }
|
||||
|
||||
let fileName = UUID().uuidString
|
||||
let imagePath = getDocumentsDirectoryURL().appendingPathComponent(fileName)
|
||||
let imageURL = getURL(forFile: fileName)
|
||||
|
||||
if let jpegData = imagePicked.jpegData(compressionQuality: 0.8) {
|
||||
try? jpegData.write(to: imagePath)
|
||||
try? jpegData.write(to: imageURL)
|
||||
}
|
||||
|
||||
people.append(Person(name: "Unknown", imageName: fileName))
|
||||
collectionView?.reloadData()
|
||||
|
||||
picker.dismiss(animated: true)
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -57,6 +77,11 @@ class HomeViewController: UICollectionViewController, UIImagePickerControllerDel
|
|||
imagePicker.allowsEditing = true
|
||||
imagePicker.delegate = self
|
||||
|
||||
if UIImagePickerController.isSourceTypeAvailable(.camera) {
|
||||
imagePicker.sourceType = .camera
|
||||
}
|
||||
|
||||
|
||||
present(imagePicker, animated: true)
|
||||
}
|
||||
|
||||
|
@ -66,5 +91,37 @@ class HomeViewController: UICollectionViewController, UIImagePickerControllerDel
|
|||
|
||||
return paths[0]
|
||||
}
|
||||
|
||||
|
||||
func getURL(forFile fileName: String) -> URL {
|
||||
return getDocumentsDirectoryURL().appendingPathComponent(fileName)
|
||||
}
|
||||
|
||||
|
||||
func setStyles(forCell cell: PersonCell) {
|
||||
cell.personImageView.layer.borderColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.3).cgColor
|
||||
cell.personImageView.layer.borderWidth = 2
|
||||
cell.personImageView.layer.cornerRadius = 3
|
||||
cell.layer.cornerRadius = 7
|
||||
}
|
||||
|
||||
|
||||
func promptForName(of person: Person) {
|
||||
let alertController = UIAlertController(title: "Who is this?", message: nil, preferredStyle: .alert)
|
||||
|
||||
alertController.addTextField()
|
||||
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel))
|
||||
|
||||
alertController.addAction(
|
||||
UIAlertAction(title: "OK", style: .default) { [unowned self, alertController] _ in
|
||||
let newName = alertController.textFields![0].text!
|
||||
|
||||
person.name = newName
|
||||
self.collectionView?.reloadData()
|
||||
}
|
||||
)
|
||||
|
||||
present(alertController, animated: true)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
//
|
||||
// Person.swift
|
||||
// Names And Faces
|
||||
//
|
||||
// Created by Brian Sipple on 1/24/19.
|
||||
// Copyright © 2019 Brian Sipple. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
class Person: NSObject {
|
||||
var name: String
|
||||
var imageName: String
|
||||
|
||||
init(name: String, imageName: String) {
|
||||
self.name = name
|
||||
self.imageName = imageName
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue