-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spot.swift
117 lines (90 loc) · 3.83 KB
/
Spot.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
//
// Spot.swift
// Bio
//
// Created by Ann McDonough on 10/15/20.
// Copyright © 2020 Patrick McDonough. All rights reserved.
//
import Foundation
import CoreLocation
import Firebase
import MapKit
import FirebaseFirestore
class Spot: NSObject, MKAnnotation {
var name: String
var address: String
var coordinate: CLLocationCoordinate2D
var averageRating: Double
var numberOfReviews: Int
var postingUserID: String
var documentID: String
var longitude: CLLocationDegrees {
return coordinate.longitude
}
var latitude: CLLocationDegrees {
return coordinate.latitude
}
var location: CLLocation {
return CLLocation(latitude: latitude, longitude: longitude)
}
var title: String? {
return name
}
var sutitle: String? {
return address
}
var dictionary: [String: Any] {
return ["name": name, "address": address, "longitude": longitude, "latitude": latitude, "averageRating": averageRating, "numberOfReviews": numberOfReviews, "postingUserID": postingUserID]
}
init(name: String, address: String, coordinate: CLLocationCoordinate2D, averageRating: Double, numberOfReviews: Int, postingUserID: String, documentID: String ) {
self.name = name
self.address = address
self.coordinate = coordinate
self.averageRating = averageRating
self.numberOfReviews = numberOfReviews
self.postingUserID = postingUserID
self.documentID = documentID
}
convenience override init() {
self.init(name: "", address: "", coordinate: CLLocationCoordinate2D(), averageRating: 0.0, numberOfReviews: 0, postingUserID: "", documentID: "")
}
convenience init(dictionary: [String: Any]) {
let name = dictionary["name"] as! String? ?? ""
let address = dictionary["address"] as! String? ?? ""
let latitude = dictionary["latitude"] as! CLLocationDegrees? ?? 0.0
let longitude = dictionary["longitude"] as! CLLocationDegrees? ?? 0.0
let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let averageRating = dictionary["averageRating"] as! Double? ?? 0.0
let numberOfReviews = dictionary["numberOfReviews"] as! Int? ?? 0
let postingUserID = dictionary["postingUserID"] as! String? ?? ""
self.init(name: name, address: address, coordinate: coordinate, averageRating: averageRating, numberOfReviews: numberOfReviews, postingUserID: postingUserID, documentID: "")
}
func saveData(completed: @escaping (Bool) -> ()) {
let db = Firestore.firestore()
//grab the userID
guard let postingUserID = (Auth.auth().currentUser?.uid) else {
print("Error: could not save data because we don't have a valid postingUserID")
return completed(false)
}
self.postingUserID = postingUserID
//create the dictionary represnting the data we want to save
let dataToSave = self.dictionary
//if we HAVE saved a record, we''ll have a document ID
if self.documentID != "" {
let ref = db.collection("spots").document(self.documentID)
ref.setData(dataToSave) { (error) in
if let error = error {
print("Error: creating new document \(self.documentID) \(error.localizedDescription)")
completed(false)
} else {
print("new document created with ref ID \(ref.documentID )")
completed(true)
}
}
} else {
db.collection("spots").addDocument(data: dataToSave) { error in
}
}
completed(true)
}
}