-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventStore.swift
More file actions
55 lines (52 loc) · 1.4 KB
/
EventStore.swift
File metadata and controls
55 lines (52 loc) · 1.4 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
//
// EventStore.swift
// DavisFriends
//
// Created by Jerry Au on 4/27/25.
//
import SwiftUI
import MapKit
import Foundation
import FirebaseFirestore
struct Pin: Identifiable, Codable{
@DocumentID var id: String?
var lat: Double
var long: Double
var locationName: String
var description: String?
var mkAnnotation: MKPointAnnotation{
let ann = MKPointAnnotation()
ann.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
ann.title = locationName
ann.subtitle = description
return ann
}
}
class EventStore: ObservableObject{
@Published var pins: [Pin] = []
private var database = Firestore.firestore()
private var listener: ListenerRegistration?
init(){
subscribe()
}
deinit{
listener?.remove()
}
func subscribe(){
listener = database.collection("pins")
.addSnapshotListener{snapshot, error in guard let docs = snapshot?.documents else {return}
self.pins = docs.compactMap{try? $0.data(as: Pin.self)}
}
}
func append(_ pin: Pin){
do {
_ = try database.collection("pins").addDocument(from: pin)
}catch{
print("Error in appending", error)
}
}
func remove(_ pin: Pin){
guard let id = pin.id else {return}
database.collection("pins").document(id).delete()
}
}