Last active
September 26, 2021 17:30
-
-
Save a914-gowtham/fe0a75f6ce85e972524567b5ae63e98b 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 GroupMsgStatusUpdater { | |
fun updateToDeliveryOrSeen(myUserId: String, messageList: List<GroupMessage>,isSeen: Boolean, vararg groupId: String){ | |
try { | |
val batch= firestore.batch() | |
val db = FirebaseFirestore.getInstance() | |
val groupCollection= db.collection("Groups") | |
for (id in groupId){ | |
val msgSubCollection=groupCollection.document(id).collection("group_messages") | |
val filterList= messageList | |
.filter { | |
val indexOfMyStatus=it.to.indexOf(myUserId) | |
it.from!=myUserId && it.status[indexOfMyStatus]==0 && it.groupId==id } | |
.map { | |
val myIndex=it.to.indexOf(myUserId) | |
it.status[myIndex]=if(isSeen) 3 else 2 | |
if(isSeen) | |
it.seenTime[myIndex]= Date() | |
it.deliveryTime[myIndex]=if(it.deliveryTime.length<myIndex) | |
it.deliveryTime[myIndex] else Date() | |
it | |
} | |
for (msg in filterList){ | |
batch.update(msgSubCollection | |
.document(msg.id),msg.asMap()) | |
} | |
} | |
batch.commit().addOnSuccessListener { | |
LogMessage.v("Batch update success from group") | |
}.addOnFailureListener { | |
LogMessage.v("Batch update failure ${it.message} from group") | |
} | |
} catch (e: Exception) { | |
e.printStackTrace() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment