doing my best with the listeners and populating data at the basic core

This commit is contained in:
talksik
2021-12-21 03:41:23 -08:00
parent 3e48b4b3bb
commit 2972fd60e4
3 changed files with 88 additions and 37 deletions
+4 -4
View File
@@ -51,7 +51,7 @@
89BDCDE72769616600577829 /* UIImage+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89BDCDE62769616600577829 /* UIImage+Extension.swift */; }; 89BDCDE72769616600577829 /* UIImage+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89BDCDE62769616600577829 /* UIImage+Extension.swift */; };
89BDCDEA27697F9C00577829 /* UserMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89BDCDE927697F9C00577829 /* UserMenu.swift */; }; 89BDCDEA27697F9C00577829 /* UserMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89BDCDE927697F9C00577829 /* UserMenu.swift */; };
89BDCDEF2769B50C00577829 /* ContactsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89BDCDEE2769B50C00577829 /* ContactsView.swift */; }; 89BDCDEF2769B50C00577829 /* ContactsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89BDCDEE2769B50C00577829 /* ContactsView.swift */; };
89BDCDF12769E64200577829 /* Messages.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89BDCDF02769E64200577829 /* Messages.swift */; }; 89BDCDF12769E64200577829 /* Message.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89BDCDF02769E64200577829 /* Message.swift */; };
89F05B9B276A87920002E9C7 /* ContactsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89F05B9A276A87920002E9C7 /* ContactsViewModel.swift */; }; 89F05B9B276A87920002E9C7 /* ContactsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89F05B9A276A87920002E9C7 /* ContactsViewModel.swift */; };
89F05BA4276ACC060002E9C7 /* ContactService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89F05BA3276ACC060002E9C7 /* ContactService.swift */; }; 89F05BA4276ACC060002E9C7 /* ContactService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89F05BA3276ACC060002E9C7 /* ContactService.swift */; };
89F05BA6276ACD830002E9C7 /* ContactsPickerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89F05BA5276ACD830002E9C7 /* ContactsPickerView.swift */; }; 89F05BA6276ACD830002E9C7 /* ContactsPickerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89F05BA5276ACD830002E9C7 /* ContactsPickerView.swift */; };
@@ -124,7 +124,7 @@
89BDCDE62769616600577829 /* UIImage+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImage+Extension.swift"; sourceTree = "<group>"; }; 89BDCDE62769616600577829 /* UIImage+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImage+Extension.swift"; sourceTree = "<group>"; };
89BDCDE927697F9C00577829 /* UserMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserMenu.swift; sourceTree = "<group>"; }; 89BDCDE927697F9C00577829 /* UserMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserMenu.swift; sourceTree = "<group>"; };
89BDCDEE2769B50C00577829 /* ContactsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactsView.swift; sourceTree = "<group>"; }; 89BDCDEE2769B50C00577829 /* ContactsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactsView.swift; sourceTree = "<group>"; };
89BDCDF02769E64200577829 /* Messages.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Messages.swift; sourceTree = "<group>"; }; 89BDCDF02769E64200577829 /* Message.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Message.swift; sourceTree = "<group>"; };
89F05B9A276A87920002E9C7 /* ContactsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactsViewModel.swift; sourceTree = "<group>"; }; 89F05B9A276A87920002E9C7 /* ContactsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactsViewModel.swift; sourceTree = "<group>"; };
89F05BA3276ACC060002E9C7 /* ContactService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactService.swift; sourceTree = "<group>"; }; 89F05BA3276ACC060002E9C7 /* ContactService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactService.swift; sourceTree = "<group>"; };
89F05BA5276ACD830002E9C7 /* ContactsPickerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactsPickerView.swift; sourceTree = "<group>"; }; 89F05BA5276ACD830002E9C7 /* ContactsPickerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactsPickerView.swift; sourceTree = "<group>"; };
@@ -282,7 +282,7 @@
891A160C27657CD600B26659 /* Models */ = { 891A160C27657CD600B26659 /* Models */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
89BDCDF02769E64200577829 /* Messages.swift */, 89BDCDF02769E64200577829 /* Message.swift */,
891A160D27657CEA00B26659 /* User.swift */, 891A160D27657CEA00B26659 /* User.swift */,
89110713277166D6005797FA /* UserFriends.swift */, 89110713277166D6005797FA /* UserFriends.swift */,
); );
@@ -573,7 +573,7 @@
89BCABF0276D53C4009E9B10 /* PhoneVerificationViewModel.swift in Sources */, 89BCABF0276D53C4009E9B10 /* PhoneVerificationViewModel.swift in Sources */,
89BDCDE72769616600577829 /* UIImage+Extension.swift in Sources */, 89BDCDE72769616600577829 /* UIImage+Extension.swift in Sources */,
89F1386D2767EB19006680ED /* SignInView.swift in Sources */, 89F1386D2767EB19006680ED /* SignInView.swift in Sources */,
89BDCDF12769E64200577829 /* Messages.swift in Sources */, 89BDCDF12769E64200577829 /* Message.swift in Sources */,
891A160E27657CEA00B26659 /* User.swift in Sources */, 891A160E27657CEA00B26659 /* User.swift in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
@@ -8,7 +8,7 @@
import Foundation import Foundation
import FirebaseFirestoreSwift import FirebaseFirestoreSwift
struct Messages: Identifiable, Codable { struct Message: Identifiable, Codable {
@DocumentID var id: String? @DocumentID var id: String?
var senderId: String var senderId: String
var listenCount: Int var listenCount: Int
+83 -32
View File
@@ -10,6 +10,7 @@ import Firebase
import Combine import Combine
import FirebaseAuth import FirebaseAuth
import GoogleSignIn import GoogleSignIn
import SwiftUI
enum SessionState { enum SessionState {
case isAuthenticated case isAuthenticated
@@ -31,12 +32,11 @@ final class AuthSessionStore: ObservableObject, SessionStore {
@Published var sessionState: SessionState = SessionState.isLoggedOut @Published var sessionState: SessionState = SessionState.isLoggedOut
// TODO: figure out which ones to publish // TODO: figure out which ones to publish
var friends: [User] = [] var friendsArr: [User] = []
var messages: [Messages] = [] var messagesArr: [Message] = []
// transformed data for the views // transformed data for the views
var friendsDict: [String:User] = [:] var friendMessagesDict: [String: [Message]] = [:]
var friendMessagesDict: [String: [Messages]] = [:]
// TODO: temporary...should not have this here // TODO: temporary...should not have this here
private var db = Firestore.firestore() private var db = Firestore.firestore()
@@ -213,27 +213,56 @@ extension AuthSessionStore {
// order: when the relationship was created...also easy for user // order: when the relationship was created...also easy for user
// limit: for my protection of db costs lol // limit: for my protection of db costs lol
db.collection("user_friends").whereField("userId", isEqualTo: userId).order(by: "created").limit(to: 100) // TODO: use the indexes I created
db.collection("user_friends").whereField("userId", isEqualTo: userId).limit(to: 100)
.addSnapshotListener { querySnapshot, error in .addSnapshotListener { querySnapshot, error in
guard let snapshot = querySnapshot else { guard let documents = querySnapshot?.documents else {
print("Error fetching user's friends: \(error!)") print("Error fetching user's friends: \(error!)")
return return
} }
snapshot.documentChanges.forEach { diff in
if (diff.type == .added) { for document in querySnapshot!.documents {
print("New friend in circle: \(diff.document.data())") let userFriend:UserFriends? = try? document.data(as: UserFriends.self)
// re-sort my circle alphabetically or by some other means // get user and initialize this user in dict
// need to keep it looking the same everytime self.db.collection("users").document(userFriend!.friendId).getDocument { (document, error) in
} if let document = document, document.exists {
if (diff.type == .modified) { let returnedUser = try? document.data(as: User.self)
// maybe it was deactivated or activated if returnedUser != nil {
print("Modified relationship: \(diff.document.data())") self.friendsArr.append(returnedUser!)
}
if (diff.type == .removed) { // if this friend do not exist in the dict, add it to show up in my circle
print("Removed city: \(diff.document.data())") if self.friendMessagesDict[returnedUser!.id!] == nil {
self.friendMessagesDict[returnedUser!.id!] = []
}
print("added this user to the array of users for user's circle")
}
} else {
print("user doesn't exist from user friend relationship")
// should not happen
}
} }
} }
// TODO: optimize later with the conditionals
// guard let snapshot = querySnapshot else {
// print("Error fetching user's friends: \(error!)")
// return
// }
// snapshot.documentChanges.forEach { diff in
//
// if (diff.type == .added) {
// print("New friend in circle: \(diff.document.data())")
// }
// if (diff.type == .modified) {
// // maybe it was deactivated or activated
// print("Modified relationship: \(diff.document.data())")
// }
// if (diff.type == .removed) {
// print("Removed city: \(diff.document.data())")
// }
// }
} }
@@ -244,26 +273,48 @@ extension AuthSessionStore {
// order: sent time should make it easy to add to dict // order: sent time should make it easy to add to dict
// limit: because each user should have most 12 friends and so would at most need 24 messages to show turns and all that...save myself from hackers here // limit: because each user should have most 12 friends and so would at most need 24 messages to show turns and all that...save myself from hackers here
// TODO: use the indexes I created
db.collection("messages").whereField("receiverId", isEqualTo: userId).limit(to: 100) db.collection("messages").whereField("receiverId", isEqualTo: userId).limit(to: 100)
.addSnapshotListener { querySnapshot, error in .addSnapshotListener { querySnapshot, error in
guard let snapshot = querySnapshot else { guard let documents = querySnapshot?.documents else {
print("Error fetching messages: \(error!)") print("Error fetching messages: \(error!)")
return return
} }
snapshot.documentChanges.forEach { diff in for document in querySnapshot!.documents {
if (diff.type == .added) { // decode and add to arr and dic
print("New message: \(diff.document.data())") let currMessage:Message? = try? document.data(as: Message.self)
// keep array of messages sorted, but this should automatically be sorted? if currMessage != nil { // not really possible but just check
} // if the user doesn't exist for the dictionary, then add it
if (diff.type == .modified) { // this means it's most likely someone new (never had user_friend relationship before) messaging for the user's inbox
// nothing to really do here // TODO: prolly want to make a call to get this sender user details for the inbox
print("Modified message: \(diff.document.data())") if self.friendMessagesDict[currMessage!.senderId] == nil {
} self.friendMessagesDict[currMessage!.senderId] = [currMessage!]
if (diff.type == .removed) { } else {
print("Removed message: \(diff.document.data())") self.friendMessagesDict[currMessage!.senderId]?.append(currMessage!)
}
} }
} }
// TODO: optimize later
// guard let snapshot = querySnapshot else {
// print("Error fetching messages: \(error!)")
// return
// }
// snapshot.documentChanges.forEach { diff in
// if (diff.type == .added) {
// print("New message: \(diff.document.data())")
//
// // keep array of messages sorted, but this should automatically be sorted?
// }
// if (diff.type == .modified) {
// // nothing to really do here
// print("Modified message: \(diff.document.data())")
// }
// if (diff.type == .removed) {
// print("Removed message: \(diff.document.data())")
// }
// }
} }
} }