-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirebaseManager.swift
58 lines (52 loc) · 1.87 KB
/
FirebaseManager.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
//
// FirebaseManager.swift
// Arkan
//
// Created by mac on 2/3/25.
//
import FirebaseDatabase
class FirebaseManager {
private var ref: DatabaseReference
init() {
self.ref = Database.database().reference()
}
// Post a new question to Firebase
func postQuestion(author: String, content: String, completion: @escaping (Bool) -> Void) {
let newPostRef = ref.child("communityPosts").childByAutoId()
newPostRef.setValue([
"author": author,
"content": content,
"timestamp": ServerValue.timestamp()
]) { error, _ in
if let error = error {
print("Error posting question: \(error.localizedDescription)")
completion(false)
} else {
print("Question posted successfully")
completion(true)
}
}
}
// Observe new community posts
func observeCommunityPosts(completion: @escaping ([CommunityPost]) -> Void) {
ref.child("communityPosts").observe(.value) { snapshot in
var posts: [CommunityPost] = []
for child in snapshot.children {
if let snapshot = child as? DataSnapshot,
let postData = snapshot.value as? [String: Any],
let author = postData["author"] as? String,
let content = postData["content"] as? String,
let timestamp = postData["timestamp"] as? Double {
let post = CommunityPost(author: author, content: content, timestamp: Date())
posts.append(post)
}
}
completion(posts)
}
}
// Post the answer from Gemini
func postAnswer(postId: String, answer: String) {
let postRef = ref.child("communityPosts").child(postId)
postRef.updateChildValues(["answer": answer])
}
}