-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLitterReportViewController
More file actions
75 lines (63 loc) · 2.5 KB
/
LitterReportViewController
File metadata and controls
75 lines (63 loc) · 2.5 KB
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
import UIKit
class LitterReportViewController: UIViewController {
// MARK: - Properties
private let imagePickerController = UIImagePickerController()
private var selectedImage: UIImage?
// MARK: - Outlets
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var addPhotoButton: UIButton!
@IBOutlet weak var locationTextField: UITextField!
@IBOutlet weak var submitButton: UIButton!
// MARK: - Actions
@IBAction func addPhotoButtonTapped(_ sender: Any) {
showImagePicker()
}
@IBAction func submitButtonTapped(_ sender: Any) {
guard let location = locationTextField.text, !location.isEmpty else {
showMissingLocationAlert()
return
}
guard let image = selectedImage else {
showMissingImageAlert()
return
}
submitLitterReport(location: location, image: image)
}
// MARK: - Private
private func showImagePicker() {
imagePickerController.sourceType = .photoLibrary
imagePickerController.delegate = self
present(imagePickerController, animated: true)
}
private func showMissingLocationAlert() {
let alertController = UIAlertController(title: "Missing Location",
message: "Please enter the location of the litter.",
preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default)
alertController.addAction(okAction)
present(alertController, animated: true)
}
private func showMissingImageAlert() {
let alertController = UIAlertController(title: "Missing Image",
message: "Please add a photo of the litter.",
preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default)
alertController.addAction(okAction)
present(alertController, animated: true)
}
private func submitLitterReport(location: String, image: UIImage) {
// TODO: Implement code to submit litter report
}
}
// MARK: - UIImagePickerControllerDelegate
extension LitterReportViewController: UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
selectedImage = info[.originalImage] as? UIImage
imageView.image = selectedImage
dismiss(animated: true)
}
}
// MARK: - UINavigationControllerDelegate
extension LitterReportViewController: UINavigationControllerDelegate {
}