Fixed chat bubbles sometimes closing when opened

This commit is contained in:
Sylvain Berfini 2022-01-05 12:17:31 +01:00
parent 0e17da27e8
commit 8c790929c4
2 changed files with 31 additions and 19 deletions

View file

@ -94,10 +94,6 @@ class ChatBubbleActivity : GenericActivity() {
return return
} }
// Workaround for the removed notification when a chat room is marked as read
coreContext.notificationsManager.disableDismissNotificationUponReadForChatRoom(chatRoom)
chatRoom.markAsRead()
viewModel = ViewModelProvider( viewModel = ViewModelProvider(
this, this,
ChatRoomViewModelFactory(chatRoom) ChatRoomViewModelFactory(chatRoom)
@ -158,6 +154,7 @@ class ChatBubbleActivity : GenericActivity() {
binding.setOpenAppClickListener { binding.setOpenAppClickListener {
coreContext.notificationsManager.currentlyDisplayedChatRoomAddress = null coreContext.notificationsManager.currentlyDisplayedChatRoomAddress = null
coreContext.notificationsManager.changeDismissNotificationUponReadForChatRoom(viewModel.chatRoom, false)
val intent = Intent(this, MainActivity::class.java) val intent = Intent(this, MainActivity::class.java)
intent.putExtra("RemoteSipUri", remoteSipUri) intent.putExtra("RemoteSipUri", remoteSipUri)
@ -182,6 +179,10 @@ class ChatBubbleActivity : GenericActivity() {
viewModel.chatRoom.addListener(listener) viewModel.chatRoom.addListener(listener)
// Workaround for the removed notification when a chat room is marked as read
coreContext.notificationsManager.changeDismissNotificationUponReadForChatRoom(viewModel.chatRoom, true)
viewModel.chatRoom.markAsRead()
val peerAddress = viewModel.chatRoom.peerAddress.asStringUriOnly() val peerAddress = viewModel.chatRoom.peerAddress.asStringUriOnly()
coreContext.notificationsManager.currentlyDisplayedChatRoomAddress = peerAddress coreContext.notificationsManager.currentlyDisplayedChatRoomAddress = peerAddress
coreContext.notificationsManager.resetChatNotificationCounterForSipUri(peerAddress) coreContext.notificationsManager.resetChatNotificationCounterForSipUri(peerAddress)
@ -197,6 +198,7 @@ class ChatBubbleActivity : GenericActivity() {
viewModel.chatRoom.removeListener(listener) viewModel.chatRoom.removeListener(listener)
coreContext.notificationsManager.currentlyDisplayedChatRoomAddress = null coreContext.notificationsManager.currentlyDisplayedChatRoomAddress = null
coreContext.notificationsManager.changeDismissNotificationUponReadForChatRoom(viewModel.chatRoom, false)
super.onPause() super.onPause()
} }

View file

@ -63,7 +63,6 @@ class Notifiable(val notificationId: Int) {
var localIdentity: String? = null var localIdentity: String? = null
var myself: String? = null var myself: String? = null
var remoteAddress: String? = null var remoteAddress: String? = null
var dismissNotificationUponReadChatRoom: Boolean = true
} }
class NotifiableMessage( class NotifiableMessage(
@ -102,6 +101,7 @@ class NotificationsManager(private val context: Context) {
private val chatNotificationsMap: HashMap<String, Notifiable> = HashMap() private val chatNotificationsMap: HashMap<String, Notifiable> = HashMap()
private val callNotificationsMap: HashMap<String, Notifiable> = HashMap() private val callNotificationsMap: HashMap<String, Notifiable> = HashMap()
private val previousChatNotifications: ArrayList<Int> = arrayListOf() private val previousChatNotifications: ArrayList<Int> = arrayListOf()
private val chatBubbleNotifications: ArrayList<Int> = arrayListOf()
private var currentForegroundServiceNotificationId: Int = 0 private var currentForegroundServiceNotificationId: Int = 0
private var serviceNotification: Notification? = null private var serviceNotification: Notification? = null
@ -174,18 +174,23 @@ class NotificationsManager(private val context: Context) {
val address = chatRoom.peerAddress.asStringUriOnly() val address = chatRoom.peerAddress.asStringUriOnly()
val notifiable = chatNotificationsMap[address] val notifiable = chatNotificationsMap[address]
if (notifiable != null) { if (notifiable != null) {
if (notifiable.dismissNotificationUponReadChatRoom) { if (chatBubbleNotifications.contains(notifiable.notificationId)) {
Log.i("[Notifications Manager] Chat room [$chatRoom] has been marked as read, not removing notification because of a chat bubble")
} else {
Log.i("[Notifications Manager] Chat room [$chatRoom] has been marked as read, removing notification if any") Log.i("[Notifications Manager] Chat room [$chatRoom] has been marked as read, removing notification if any")
dismissChatNotification(chatRoom) dismissChatNotification(chatRoom)
} else {
Log.i("[Notifications Manager] Chat room [$chatRoom] has been marked as read, not removing notification, maybe because of a chat bubble?")
} }
} else {
val notificationId = chatRoom.creationTime.toInt()
if (chatBubbleNotifications.contains(notificationId)) {
Log.i("[Notifications Manager] Chat room [$chatRoom] has been marked as read but no notifiable found, not removing notification because of a chat bubble")
} else { } else {
Log.i("[Notifications Manager] Chat room [$chatRoom] has been marked as read but no notifiable found, removing notification if any") Log.i("[Notifications Manager] Chat room [$chatRoom] has been marked as read but no notifiable found, removing notification if any")
dismissChatNotification(chatRoom) dismissChatNotification(chatRoom)
} }
} }
} }
}
val chatListener: ChatMessageListener = object : ChatMessageListenerStub() { val chatListener: ChatMessageListener = object : ChatMessageListenerStub() {
override fun onMsgStateChanged(message: ChatMessage, state: ChatMessage.State) { override fun onMsgStateChanged(message: ChatMessage, state: ChatMessage.State) {
@ -696,21 +701,26 @@ class NotificationsManager(private val context: Context) {
} else { } else {
val previousNotificationId = previousChatNotifications.find { id -> id == room.creationTime.toInt() } val previousNotificationId = previousChatNotifications.find { id -> id == room.creationTime.toInt() }
if (previousNotificationId != null) { if (previousNotificationId != null) {
if (chatBubbleNotifications.contains(previousNotificationId)) {
Log.i("[Notifications Manager] Found previous notification with same ID [$previousNotificationId] but not cancelling it as it's ID is in chat bubbles list")
} else {
Log.i("[Notifications Manager] Found previous notification with same ID [$previousNotificationId], canceling it") Log.i("[Notifications Manager] Found previous notification with same ID [$previousNotificationId], canceling it")
cancel(previousNotificationId, CHAT_TAG) cancel(previousNotificationId, CHAT_TAG)
}
return true return true
} }
} }
return false return false
} }
fun disableDismissNotificationUponReadForChatRoom(chatRoom: ChatRoom) { fun changeDismissNotificationUponReadForChatRoom(chatRoom: ChatRoom, dismiss: Boolean) {
val address = chatRoom.peerAddress.asStringUriOnly() val notificationId = chatRoom.creationTime.toInt()
val notifiable: Notifiable? = chatNotificationsMap[address] if (dismiss) {
if (notifiable != null) { Log.i("[Notifications Manager] Allow notification with id [$notificationId] to be dismissed when chat room will be marked as read, used for chat bubble")
Log.i("[Notifications Manager] Prevent notification with id [${notifiable.notificationId}] from being dismissed when chat room will be marked as read") chatBubbleNotifications.add(notificationId)
notifiable.dismissNotificationUponReadChatRoom = false } else {
chatNotificationsMap[address] = notifiable Log.i("[Notifications Manager] Prevent notification with id [$notificationId] from being dismissed when chat room will be marked as read, used for chat bubble")
chatBubbleNotifications.remove(notificationId)
} }
} }