-
Notifications
You must be signed in to change notification settings - Fork 1
/
AddTripsViewController.swift
158 lines (121 loc) · 5.54 KB
/
AddTripsViewController.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// AddTripsViewController.swift
// Tripy
//
// Created by Chris on 11/19/18.
// Copyright © 2018 Chris . All rights reserved.
//
import UIKit
import Photos
import Firebase
class AddTripsViewController: UIViewController {
var ref: DatabaseReference! //Firebase reference
@IBOutlet weak var TripTextField: UITextField!
@IBOutlet weak var titleLabel: UILabel!
var doneSaving: (() -> ())?
var tripIndexToEdit: Int?
var tripfunctions = TripFunctions()
override func viewDidLoad() {
super.viewDidLoad()
//set the Firebase reference
ref = Database.database().reference().child("plans");
if let index = tripIndexToEdit {
let trip = Data.tripModels[index]
TripTextField.text = trip.name
Userimage.image = trip.image
}
// Do any additional setup after loading the view.
}
@IBOutlet weak var imageView: UIButton! //the camera icon
@IBAction func addPhoto(_ sender: Any) { //choose a background picture from photo library
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
PHPhotoLibrary.requestAuthorization { (status) in
switch status {
case .authorized:
let myPickerController = UIImagePickerController()
myPickerController.delegate = self
myPickerController.sourceType = .photoLibrary
self.present(myPickerController, animated: true)
case .notDetermined:
break
case .restricted:
let alert = UIAlertController(title: "Photo Library Restricted", message: "Photo library access is restricted and cannot be accessed.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "ok", style: .default)
case .denied:
let alert = UIAlertController(title: "Photo Library Denied", message: "Photo library access is restricted and cannot be accessed.", preferredStyle: .alert)
let gotoSettingsAction = UIAlertAction(title: "Go to settings", style: .default) {(action) in
DispatchQueue.main.async {
let url = URL(string: UIApplication.openSettingsURLString)!
UIApplication.shared.open(url, options: [:])
}
}
let cancalAction = UIAlertAction(title: "cancel", style: .cancel)
alert.addAction(gotoSettingsAction)
alert.addAction(cancalAction)
}
}
}
}
@IBOutlet weak var Userimage: UIImageView! //the background image that user chooses
@IBAction func cancelButton(_ sender: Any) {
dismiss(animated: true)
}
@IBAction func saveButton(_ sender: Any) {
TripTextField.rightViewMode = .never
guard TripTextField.text != "", let newTripName = TripTextField.text else {
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 20))
imageView.image = UIImage(named: "error")
imageView.contentMode = .scaleAspectFit
TripTextField.rightView = imageView
TripTextField.rightViewMode = .always
return
}
if let index = tripIndexToEdit{
tripfunctions.updateTrip(at: index , title: newTripName, image: Userimage.image)
} else {
let existed = ExistedPlansTableViewController()
existed.plans.append(TripModel(name: "Berkeley", image: #imageLiteral(resourceName: "yosemite")))
existed.mytableView?.reloadData()
// tripfunctions.createTrip(tripModel: TripModel(name: TripTextField.text!))
}
if let doneSaving = doneSaving {
doneSaving()
}
dismiss(animated: true)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
extension AddTripsViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate{
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
self.Userimage.image = image
}
dismiss(animated: true)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
dismiss(animated:true )
}
}
fileprivate func convertFromUIImagePickerControllerInfoKeyDictionary(_ input: [UIImagePickerController.InfoKey: Any])-> [String:Any]{
return Dictionary(uniqueKeysWithValues: input.map{key,value in (key.rawValue, value)})
}
fileprivate func convertFromUIImagePickerControllerInfoKey (_ input: UIImagePickerController.InfoKey) -> String{
return input.rawValue
}
// {
// guard let selectedImage = info[.originalImage] as? UIImage else {
// print("Error: \(info)")
// return
// }
// self.Userimage.image = selectedImage
// self.Userimage.contentMode = .scaleAspectFill
//
// dismiss(animated:true)
// }}