-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlansViewModel.swift
228 lines (180 loc) · 7.41 KB
/
PlansViewModel.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//
// PlansViewModel.swift
// WeOut
//
// Created by Jacquese Whitson on 6/1/24.
//
import Foundation
import FirebaseAuth
import FirebaseCore
import Firebase
import FirebaseFirestore
import FirebaseFirestoreSwift
import FirebaseStorage
import PhotosUI
import UIKit
import _PhotosUI_SwiftUI
@MainActor
//MARK:
class PlansViewModel : ObservableObject {
static var shared = PlansViewModel()
init(){
}
// Firebase
let db = Firestore.firestore()
private let userCollection: CollectionReference = Firestore.firestore().collection("users")
@Published var currentPlan = ItineraryModel()
@Published var selectedPlan: ItineraryModel? = nil
@Published var myPlans: [ItineraryModel] = []
// Editing
@Published var editPlan: ItineraryModel? = nil
@Published var editItineraryItem: ItineraryModel? = nil
// Images
@Published var planImageURLString = ""
@Published var planImage : UIImage?
@Published var photoPickerItem : PhotosPickerItem?
//Images Pt.2
@Published var newPhoto = Photo()
@Published var selectedPhoto: PhotosPickerItem?
@Published var uiImageSelected = UIImage()
@Published var didSelectImage = false
}
// Add && update plans
extension PlansViewModel{
// new code
func addItinerary(to trip: TripModel, with plan: ItineraryModel,image: UIImage) async -> Bool {
let db = Firestore.firestore()
try? await ProfileViewModel.shared.loadCurrentUser()
// get the user
guard let userID = ProfileViewModel.shared.currentUser?.uid else {
print("😡 ERROR: Could not load a user")
return false }
// access the games id
guard let tripID = trip.id else {
print("Error: Trip ID == Nil")
return false
}
let _ = await SaveImageForPlan(userID: userID, planID: plan.id ?? "", image: image)
var updatedItem = plan
let collectionString = "users/\(userID)/myTrips/\(tripID)/itinerarys"
updatedItem.itineraryImage = self.planImageURLString
do {
let _: Void = try await db.collection(collectionString).document().setData(updatedItem.dictionary)
print("😎 Added itinerary to trip")
return true
} catch {
print ("🤬ERROR: Could not update data for itineray '\(error.localizedDescription)")
return false
}
}
// new code
func updateItinerary(user: DBUser,trip:TripModel, plan: ItineraryModel,image: UIImage) async -> Bool {
let collectionPath = "users/\(user.uid)/myTrips/\(trip.id ?? "")/itinerarys"
guard let tripID = trip.id else {
return false
}
if let planID = plan.id { // update the data that alrsady here
var updatedPlan = plan
do {
print("Updating Plan")
try await db.collection(collectionPath).document(planID).updateData(updatedPlan.dictionary)
print("Plan updated")
print("Saving Image to storage ")
if planImage != nil {
let _ = await SaveImageForPlan(userID: user.uid, planID: planID, image: self.planImage ?? UIImage())
print("Image Stored")
print("Updatign ItineraryImage to: \(self.planImageURLString)")
updatedPlan.itineraryImage = self.planImageURLString
print("Adding the image to the plans")
try await addImageToPlan(user: user.uid, tripID: tripID , planID: planID, newUrl: self.planImageURLString)
print("Image Added")
self.planImage = nil
self.planImageURLString = ""
}
// imageReseter()
print ("😎 Plans updated successfully!")
return true
} catch {
print ("🤬ERROR: Could not update data in'itinerarys'")
return false
}
} else {
print("Error: Can not find ID for Itinerary")
return false
}
}
// new code
func deletePlan(userId: String, tripID: String,plan: ItineraryModel ) async throws -> Bool {
let db = Firestore.firestore()
let collectionPath = "users/\(userId)/myTrips/\(tripID)/itinerarys"
let documentReference = db.collection(collectionPath)
do {
try await documentReference.document(plan.id ?? "").delete()
print("😎 Document successfully deleted!")
return true
} catch {
print("🤬Error deleting document: \(error)")
return false
}
}
}
// For images
extension PlansViewModel {
func SaveImageForPlan(userID: String, planID: String, image: UIImage) async -> Bool {
//
let photoName = UUID().uuidString
guard !userID.isEmpty else {
print("Error: Can not find userID to save itinerary image to storage")
return false
}
let storage = Storage.storage()
print("Accessing Storage")
print("Plan: \(planID)")
// save this storage to
let storageRef = storage.reference().child("\(planID)/\(photoName).jpeg")
print("New storage reference")
// compressing image
guard let resizedImage = image.jpegData(compressionQuality: 0.2) else {
print("🤬 ERROR: could not resize image")
return false
}
let metadata = StorageMetadata()
// Allowds us to dispaly the image in the storgae database
metadata.contentType = "image/jpg"
// this will hold the url of the image that we saved
// var imageURLString = ""
do {
// adding the image data to storage (resized, metadata)
print("Adding image data to storage")
let _ = try await storageRef.putDataAsync(resizedImage,metadata: metadata)
print("😎 image saved")
do {
// gettin the pictures url from storage
print("trying to dwonload url from storage")
let imageURL = try await storageRef.downloadURL()
//turning URL into a string so i can use it to access the image in the app
self.planImageURLString = "\(imageURL)"
print("UrlLink: \(self.planImageURLString)")
return true
}catch{
print("🤬 ERROR: could not get imageUrl after saving image")
return false
}
}
catch {
print("🤬 ERROR: Uploading image to firebase")
return false
}
}
// New code
func addImageToPlan(user: String, tripID: String,planID:String, newUrl: String) async throws{
guard !user.isEmpty else {
return
}
let db = Firestore.firestore()
let data: [String:Any] = [
"itineraryImage" : newUrl
]
try await db.collection("users/\(user)/myTrips/\(tripID)/itinerarys").document(planID).updateData(data)
}
}