-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddJournalEntryViewController.swift
More file actions
139 lines (117 loc) · 4.89 KB
/
AddJournalEntryViewController.swift
File metadata and controls
139 lines (117 loc) · 4.89 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
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
128
129
130
131
132
133
134
135
136
137
138
139
//
// AddJournalEntryViewController.swift
// JRNL
//
// Created by iOS17Programming on 07/10/2023.
//
import UIKit
import CoreLocation
class AddJournalEntryViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate, CLLocationManagerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// MARK: - Properties
@IBOutlet var titleTextField: UITextField!
@IBOutlet var bodyTextView: UITextView!
@IBOutlet var photoImageView: UIImageView!
@IBOutlet var saveButton: UIBarButtonItem!
@IBOutlet var getLocationSwitch: UISwitch!
@IBOutlet var getLocationSwitchLabel: UILabel!
@IBOutlet var ratingView: RatingView!
var newJournalEntry: JournalEntry?
let locationManager = CLLocationManager()
var currentLocation: CLLocation?
override func viewDidLoad() {
super.viewDidLoad()
titleTextField.delegate = self
bodyTextView.delegate = self
updateSaveButtonState()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
}
// MARK: - UITextFieldDelegate
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textFieldDidBeginEditing(_ textField: UITextField) {
saveButton.isEnabled = false
}
func textFieldDidEndEditing(_ textField: UITextField) {
updateSaveButtonState()
}
// MARK: - UITextViewDelegate
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if (text == "\n") {
textView.resignFirstResponder()
}
return true
}
func textViewDidBeginEditing(_ textView: UITextView) {
saveButton.isEnabled = false
}
func textViewDidEndEditing(_ textView: UITextView) {
updateSaveButtonState()
}
// MARK: - CLLocationManagerDelegate
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let myCurrentLocation = locations.first {
currentLocation = myCurrentLocation
getLocationSwitchLabel.text = "Done"
updateSaveButtonState()
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Failed to find user's location: \(error.localizedDescription)")
}
// MARK: - UIImagePickerControllerDelegate
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
let smallerImage = selectedImage.preparingThumbnail(of: CGSize(width: 300, height: 300))
photoImageView.image = smallerImage
dismiss(animated: true)
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let title = titleTextField.text ?? ""
let body = bodyTextView.text ?? ""
let photo = photoImageView.image
let rating = ratingView.rating
let lat = currentLocation?.coordinate.latitude
let long = currentLocation?.coordinate.longitude
newJournalEntry = JournalEntry(rating: rating, title: title, body: body, photo: photo, latitude: lat, longitude: long)
}
// MARK: - Private methods
private func updateSaveButtonState() {
let textFieldText = titleTextField.text ?? ""
let textViewText = bodyTextView.text ?? ""
if getLocationSwitch.isOn {
saveButton.isEnabled = !textFieldText.isEmpty && !textViewText.isEmpty && !(currentLocation == nil)
} else {
saveButton.isEnabled = !textFieldText.isEmpty && !textViewText.isEmpty
}
}
// MARK: - Actions
@IBAction func getLocationSwitchValueChanged(_ sender: UISwitch) {
if getLocationSwitch.isOn {
getLocationSwitchLabel.text = "Getting location..."
locationManager.requestLocation()
} else {
currentLocation = nil
getLocationSwitchLabel.text = "Get location"
}
}
@IBAction func getPhoto(_ sender: UITapGestureRecognizer) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
#if targetEnvironment(simulator)
imagePickerController.sourceType = .photoLibrary
#else
imagePickerController.sourceType = .camera
imagePickerController.showsCameraControls = true
#endif
present(imagePickerController, animated: true)
}
}