decent place but still buggy

This commit is contained in:
talksik
2022-01-03 19:29:59 -08:00
parent 98c551bc33
commit 4f4e5ba397
2 changed files with 34 additions and 34 deletions
+30 -29
View File
@@ -380,42 +380,43 @@ extension AuthSessionStore {
//optimize this? but also saving on memory and same db reads
self.relevantMessagesByUserDict.removeAll()
self.messagesArr = documents.compactMap { (queryDocumentSnapshot) -> Message? in
do {
let currMessage = try queryDocumentSnapshot.data(as: Message.self)
// print("new message received! \(currMessage!.sentTimestamp)")
DispatchQueue.main.async {
if currMessage != nil { // not really possible but just check
// if the user doesn't exist for the dictionary, then add it
// this means it's most likely someone new (never had user_friend relationship before) messaging for the user's inbox
// TODO: prolly want to make a call to get this sender user details for the inbox, but they should either be in the friendsDict or their are not a friend so won't be there
// also add in any messages where I am the sender
if currMessage!.senderId == currUserId { // if I am the sender
if self.relevantMessagesByUserDict[currMessage!.receiverId] == nil {
self.relevantMessagesByUserDict[currMessage!.receiverId] = [currMessage!]
} else {
self.relevantMessagesByUserDict[currMessage!.receiverId]?.append(currMessage!)
DispatchQueue.main.async {
self.messagesArr = documents.compactMap { (queryDocumentSnapshot) -> Message? in
do {
let currMessage = try queryDocumentSnapshot.data(as: Message.self)
// print("new message received! \(currMessage!.sentTimestamp)")
DispatchQueue.main.async {
if currMessage != nil { // not really possible but just check
// if the user doesn't exist for the dictionary, then add it
// this means it's most likely someone new (never had user_friend relationship before) messaging for the user's inbox
// TODO: prolly want to make a call to get this sender user details for the inbox, but they should either be in the friendsDict or their are not a friend so won't be there
// also add in any messages where I am the sender
if currMessage!.senderId == currUserId { // if I am the sender
if self.relevantMessagesByUserDict[currMessage!.receiverId] == nil {
self.relevantMessagesByUserDict[currMessage!.receiverId] = [currMessage!]
} else {
self.relevantMessagesByUserDict[currMessage!.receiverId]?.append(currMessage!)
}
}
}
else { // if I am receiving
if self.relevantMessagesByUserDict[currMessage!.senderId] == nil {
self.relevantMessagesByUserDict[currMessage!.senderId] = [currMessage!]
} else {
self.relevantMessagesByUserDict[currMessage!.senderId]?.append(currMessage!)
else { // if I am receiving
if self.relevantMessagesByUserDict[currMessage!.senderId] == nil {
self.relevantMessagesByUserDict[currMessage!.senderId] = [currMessage!]
} else {
self.relevantMessagesByUserDict[currMessage!.senderId]?.append(currMessage!)
}
}
}
}
self.objectWillChange.send()
return currMessage
} catch {
print(error)
}
return currMessage
} catch {
print(error)
return nil
}
return nil
}
}
}
self.listenersActive = true
@@ -79,7 +79,7 @@ class InnerCircleViewModel: NSObject, ObservableObject {
@Published var isRecording : Bool = false
@Published var messagesListeningProgress: Float = 1.0
static let multiplier: Float64 = 0.05
static let multiplier: Float64 = 0.01
let audioSession = AVAudioSession.sharedInstance()
@@ -259,7 +259,7 @@ extension InnerCircleViewModel {
self.stopPlayingAnyAudio()
// reset progress
self.messagesListeningProgress = Float(Self.multiplier * 2)
self.messagesListeningProgress = Float(0)
var AVPlayerItems: [AVPlayerItem] = []
for url in audioUrls {
@@ -319,8 +319,6 @@ extension InnerCircleViewModel {
currentTime = currentTime + interval
times.append(NSValue(time: currentTime))
}
// this last one to make sure we get a full loop
times.append(NSValue(time: totalDuration))
// Add time observer. Observe boundary time changes on the main queue.
// TODO: not hitting the last one/100%
@@ -369,7 +367,7 @@ extension InnerCircleViewModel {
// }
func cacheIncomingMessages(friendMessagesDict: [String: [Message]]) {
// need way of making this get called whenever there are messages that come in but do all this in the background...onreceive of new messages, check if we already have it cached, and if not, then download
// TODO: not firing for some reason onreceive of new data or not including new data
guard let userId = AuthSessionStore.getCurrentUserId() else {return}
DispatchQueue.global(qos: .background).async {
@@ -392,6 +390,7 @@ extension InnerCircleViewModel {
if let audioUrl = URL(string: message.audioDataUrl) { // check if it's a valid url
if !self.cachedPlayerItemsDict.keys.contains(message.audioDataUrl) {
// save to play from mem later
let task = URLSession.shared.dataTask(with: audioUrl) {[weak self] (data, response, error) in
guard let data = data else { return }
print(data)