Moves user msg Synchronization to it's own object.

This commit is contained in:
Vitor Pamplona
2023-06-25 16:11:21 -04:00
parent 4731575b87
commit ebe1bcedd0
@@ -187,34 +187,34 @@ class User(val pubkeyHex: String) {
} }
@Synchronized @Synchronized
private fun getOrCreatePrivateChatroom(user: User): Chatroom { private fun getOrCreatePrivateChatroomSync(user: User): Chatroom {
checkNotInMainThread() checkNotInMainThread()
return privateChatrooms[user] ?: run { return privateChatrooms[user] ?: run {
val privateChatroom = Chatroom(setOf<Note>()) val privateChatroom = Chatroom()
privateChatrooms = privateChatrooms + Pair(user, privateChatroom) privateChatrooms = privateChatrooms + Pair(user, privateChatroom)
privateChatroom privateChatroom
} }
} }
@Synchronized private fun getOrCreatePrivateChatroom(user: User): Chatroom {
fun addMessage(user: User, msg: Note) { return privateChatrooms[user] ?: getOrCreatePrivateChatroomSync(user)
checkNotInMainThread() }
fun addMessage(user: User, msg: Note) {
val privateChatroom = getOrCreatePrivateChatroom(user) val privateChatroom = getOrCreatePrivateChatroom(user)
if (msg !in privateChatroom.roomMessages) { if (msg !in privateChatroom.roomMessages) {
privateChatroom.roomMessages = privateChatroom.roomMessages + msg privateChatroom.addMessageSync(msg)
liveSet?.messages?.invalidateData() liveSet?.messages?.invalidateData()
} }
} }
@Synchronized
fun removeMessage(user: User, msg: Note) { fun removeMessage(user: User, msg: Note) {
checkNotInMainThread() checkNotInMainThread()
val privateChatroom = getOrCreatePrivateChatroom(user) val privateChatroom = getOrCreatePrivateChatroom(user)
if (msg in privateChatroom.roomMessages) { if (msg in privateChatroom.roomMessages) {
privateChatroom.roomMessages = privateChatroom.roomMessages - msg privateChatroom.removeMessageSync(msg)
liveSet?.messages?.invalidateData() liveSet?.messages?.invalidateData()
} }
} }
@@ -373,7 +373,27 @@ data class RelayInfo(
var counter: Long var counter: Long
) )
data class Chatroom(var roomMessages: Set<Note>) class Chatroom() {
var roomMessages: Set<Note> = setOf()
@Synchronized
fun addMessageSync(msg: Note) {
checkNotInMainThread()
if (msg !in roomMessages) {
roomMessages = roomMessages + msg
}
}
@Synchronized
fun removeMessageSync(msg: Note) {
checkNotInMainThread()
if (msg !in roomMessages) {
roomMessages = roomMessages + msg
}
}
}
@Stable @Stable
class UserMetadata { class UserMetadata {