Last active
September 26, 2021 17:35
-
-
Save a914-gowtham/fb3719eeb2a38a93fef0d52ec8f84dce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GroupHandler(){ | |
private val messagesList = mutableListOf<GroupMessage>() | |
private val groupIds = ArrayList<String>() | |
fun initListener(){ | |
val db = FirebaseFirestore.getInstance() | |
val groupMsgCollection= db.collectionGroup("group_messages") | |
groupMsgCollection.whereArrayContains("to", userId!!) | |
.addSnapshotListener { snapshots, error -> | |
if (error != null || snapshots == null || snapshots.metadata.isFromCache) { | |
LogMessage.v("Error ${error?.localizedMessage}") | |
return@addSnapshotListener | |
} | |
onSnapShotChanged(snapshots) | |
} | |
} | |
fun onSnapShotChanged(snapshots: QuerySnapshot){ | |
messageList.clear() | |
groupIds.clear() | |
for(snapShot in snapshots){ | |
val message = snapShot.document.data.toDataClass<GroupMessage>() | |
messagesList.add(message) | |
if(!groupsIds.contains()) | |
groupIds.add(message.groupId) | |
} | |
groupMessageDao.insertMessages(messageList) | |
updateUnReadCount() | |
} | |
fun updateUnReadCount(){ | |
val groupsWithMsgs = dbRepository.getGroupWithMessagesList() | |
for (groupWithMsg in groupsWithMsgs) { | |
val unreadCount = groupWithMsg.messages.filter { | |
val myMessageStatus = myMsgStatus(userId.toString(), it) | |
it.from != userId && myMessageStatus < 3 | |
}.size | |
groupWithMsg.group.unRead = unreadCount | |
} | |
val groups = groupsWithMsgs.map { | |
it.group | |
} | |
dbRepository.insertMultipleGroup(groups) | |
updateMessageStatus() | |
} | |
fun updateMessageStatus(){ | |
val messageUpdater= MessageStatusUpdater() | |
messageUpdater.updateToDeliveryOrSeen(myUserId,messageList,true,groupIds) | |
} | |
fun myMsgStatus(myUserId: String, msg: GroupMessage): Int{ | |
val indexOfMyStatus=msg.to.indexOf(myUserId) | |
return msg.status[indexOfMyStatus] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment