Loading app/core/src/main/java/com/fsck/k9/notification/NewMailNotificationController.kt +3 −1 Original line number Diff line number Diff line Loading @@ -27,8 +27,10 @@ internal class NewMailNotificationController( fun addNewMailNotification(account: Account, message: LocalMessage, silent: Boolean) { val notificationData = newMailNotificationManager.addNewMailNotification(account, message, silent) if (notificationData != null) { processNewMailNotificationData(notificationData) } } fun removeNewMailNotifications( account: Account, Loading app/core/src/main/java/com/fsck/k9/notification/NewMailNotificationManager.kt +2 −2 Original line number Diff line number Diff line Loading @@ -38,10 +38,10 @@ internal class NewMailNotificationManager( ) } fun addNewMailNotification(account: Account, message: LocalMessage, silent: Boolean): NewMailNotificationData { fun addNewMailNotification(account: Account, message: LocalMessage, silent: Boolean): NewMailNotificationData? { val content = contentCreator.createFromMessage(account, message) val result = notificationRepository.addNotification(account, content, timestamp = now()) val result = notificationRepository.addNotification(account, content, timestamp = now()) ?: return null val singleNotificationData = createSingleNotificationData( account = account, Loading app/core/src/main/java/com/fsck/k9/notification/NotificationDataStore.kt +40 −10 Original line number Diff line number Diff line Loading @@ -2,7 +2,6 @@ package com.fsck.k9.notification import com.fsck.k9.Account import com.fsck.k9.controller.MessageReference import com.fsck.k9.core.BuildConfig internal const val MAX_NUMBER_OF_NEW_MESSAGE_NOTIFICATIONS = 8 Loading Loading @@ -30,15 +29,45 @@ internal class NotificationDataStore { } @Synchronized fun addNotification(account: Account, content: NotificationContent, timestamp: Long): AddNotificationResult { fun addNotification(account: Account, content: NotificationContent, timestamp: Long): AddNotificationResult? { val notificationData = getNotificationData(account) val messageReference = content.messageReference if (BuildConfig.DEBUG && notificationData.contains(messageReference)) { throw AssertionError("Notification for message $messageReference already exists") val activeNotification = notificationData.activeNotifications.firstOrNull { notificationHolder -> notificationHolder.content.messageReference == messageReference } val inactiveNotification = notificationData.inactiveNotifications.firstOrNull { inactiveNotificationHolder -> inactiveNotificationHolder.content.messageReference == messageReference } return if (activeNotification != null) { val newActiveNotification = activeNotification.copy(content = content) val notificationHolder = activeNotification.copy( content = content ) val operations = emptyList<NotificationStoreOperation>() val newActiveNotifications = notificationData.activeNotifications .replace(activeNotification, newActiveNotification) val newNotificationData = notificationData.copy( activeNotifications = newActiveNotifications ) notificationDataMap[account.uuid] = newNotificationData AddNotificationResult.newNotification(newNotificationData, operations, notificationHolder) } else if (inactiveNotification != null) { val newInactiveNotification = inactiveNotification.copy(content = content) val newInactiveNotifications = notificationData.inactiveNotifications .replace(inactiveNotification, newInactiveNotification) return if (notificationData.isMaxNumberOfActiveNotificationsReached) { val newNotificationData = notificationData.copy( inactiveNotifications = newInactiveNotifications ) notificationDataMap[account.uuid] = newNotificationData null } else if (notificationData.isMaxNumberOfActiveNotificationsReached) { val lastNotificationHolder = notificationData.activeNotifications.last() val inactiveNotificationHolder = lastNotificationHolder.toInactiveNotificationHolder() Loading Loading @@ -175,14 +204,15 @@ internal class NotificationDataStore { throw AssertionError("getNewNotificationId() called with no free notification ID") } private fun NotificationData.contains(messageReference: MessageReference): Boolean { return activeNotifications.any { it.content.messageReference == messageReference } || inactiveNotifications.any { it.content.messageReference == messageReference } } private fun NotificationHolder.toInactiveNotificationHolder() = InactiveNotificationHolder(timestamp, content) private fun InactiveNotificationHolder.toNotificationHolder(notificationId: Int): NotificationHolder { return NotificationHolder(notificationId, timestamp, content) } private fun <T> List<T>.replace(old: T, new: T): List<T> { return map { element -> if (element === old) new else element } } } app/core/src/main/java/com/fsck/k9/notification/NotificationRepository.kt +2 −2 Original line number Diff line number Diff line Loading @@ -37,8 +37,8 @@ internal class NotificationRepository( } @Synchronized fun addNotification(account: Account, content: NotificationContent, timestamp: Long): AddNotificationResult { return notificationDataStore.addNotification(account, content, timestamp).also { result -> fun addNotification(account: Account, content: NotificationContent, timestamp: Long): AddNotificationResult? { return notificationDataStore.addNotification(account, content, timestamp)?.also { result -> persistNotificationDataStoreChanges( account = account, operations = result.notificationStoreOperations, Loading app/core/src/test/java/com/fsck/k9/notification/NewMailNotificationManagerTest.kt +4 −0 Original line number Diff line number Diff line Loading @@ -49,6 +49,7 @@ class NewMailNotificationManagerTest { val result = manager.addNewMailNotification(account, message, silent = false) assertNotNull(result) assertThat(result.singleNotificationData.first().content).isEqualTo( NotificationContent( messageReference = createMessageReference("msg-1"), Loading Loading @@ -85,6 +86,7 @@ class NewMailNotificationManagerTest { val result = manager.addNewMailNotification(account, messageTwo, silent = false) assertNotNull(result) assertThat(result.singleNotificationData.first().content).isEqualTo( NotificationContent( messageReference = createMessageReference("msg-2"), Loading Loading @@ -121,6 +123,7 @@ class NewMailNotificationManagerTest { val result = manager.addNewMailNotification(account, message, silent = false) assertNotNull(result) val notificationId = NotificationIds.getSingleMessageNotificationId(account, index = 0) assertThat(result.cancelNotificationIds).isEqualTo(listOf(notificationId)) assertThat(result.singleNotificationData.first().notificationId).isEqualTo(notificationId) Loading Loading @@ -196,6 +199,7 @@ class NewMailNotificationManagerTest { messageUid = "msg-2" ) val dataTwo = manager.addNewMailNotification(account, messageTwo, silent = true) assertNotNull(dataTwo) val notificationIdTwo = dataTwo.singleNotificationData.first().notificationId val messageThree = addMessageToNotificationContentCreator( sender = "Alice", Loading Loading
app/core/src/main/java/com/fsck/k9/notification/NewMailNotificationController.kt +3 −1 Original line number Diff line number Diff line Loading @@ -27,8 +27,10 @@ internal class NewMailNotificationController( fun addNewMailNotification(account: Account, message: LocalMessage, silent: Boolean) { val notificationData = newMailNotificationManager.addNewMailNotification(account, message, silent) if (notificationData != null) { processNewMailNotificationData(notificationData) } } fun removeNewMailNotifications( account: Account, Loading
app/core/src/main/java/com/fsck/k9/notification/NewMailNotificationManager.kt +2 −2 Original line number Diff line number Diff line Loading @@ -38,10 +38,10 @@ internal class NewMailNotificationManager( ) } fun addNewMailNotification(account: Account, message: LocalMessage, silent: Boolean): NewMailNotificationData { fun addNewMailNotification(account: Account, message: LocalMessage, silent: Boolean): NewMailNotificationData? { val content = contentCreator.createFromMessage(account, message) val result = notificationRepository.addNotification(account, content, timestamp = now()) val result = notificationRepository.addNotification(account, content, timestamp = now()) ?: return null val singleNotificationData = createSingleNotificationData( account = account, Loading
app/core/src/main/java/com/fsck/k9/notification/NotificationDataStore.kt +40 −10 Original line number Diff line number Diff line Loading @@ -2,7 +2,6 @@ package com.fsck.k9.notification import com.fsck.k9.Account import com.fsck.k9.controller.MessageReference import com.fsck.k9.core.BuildConfig internal const val MAX_NUMBER_OF_NEW_MESSAGE_NOTIFICATIONS = 8 Loading Loading @@ -30,15 +29,45 @@ internal class NotificationDataStore { } @Synchronized fun addNotification(account: Account, content: NotificationContent, timestamp: Long): AddNotificationResult { fun addNotification(account: Account, content: NotificationContent, timestamp: Long): AddNotificationResult? { val notificationData = getNotificationData(account) val messageReference = content.messageReference if (BuildConfig.DEBUG && notificationData.contains(messageReference)) { throw AssertionError("Notification for message $messageReference already exists") val activeNotification = notificationData.activeNotifications.firstOrNull { notificationHolder -> notificationHolder.content.messageReference == messageReference } val inactiveNotification = notificationData.inactiveNotifications.firstOrNull { inactiveNotificationHolder -> inactiveNotificationHolder.content.messageReference == messageReference } return if (activeNotification != null) { val newActiveNotification = activeNotification.copy(content = content) val notificationHolder = activeNotification.copy( content = content ) val operations = emptyList<NotificationStoreOperation>() val newActiveNotifications = notificationData.activeNotifications .replace(activeNotification, newActiveNotification) val newNotificationData = notificationData.copy( activeNotifications = newActiveNotifications ) notificationDataMap[account.uuid] = newNotificationData AddNotificationResult.newNotification(newNotificationData, operations, notificationHolder) } else if (inactiveNotification != null) { val newInactiveNotification = inactiveNotification.copy(content = content) val newInactiveNotifications = notificationData.inactiveNotifications .replace(inactiveNotification, newInactiveNotification) return if (notificationData.isMaxNumberOfActiveNotificationsReached) { val newNotificationData = notificationData.copy( inactiveNotifications = newInactiveNotifications ) notificationDataMap[account.uuid] = newNotificationData null } else if (notificationData.isMaxNumberOfActiveNotificationsReached) { val lastNotificationHolder = notificationData.activeNotifications.last() val inactiveNotificationHolder = lastNotificationHolder.toInactiveNotificationHolder() Loading Loading @@ -175,14 +204,15 @@ internal class NotificationDataStore { throw AssertionError("getNewNotificationId() called with no free notification ID") } private fun NotificationData.contains(messageReference: MessageReference): Boolean { return activeNotifications.any { it.content.messageReference == messageReference } || inactiveNotifications.any { it.content.messageReference == messageReference } } private fun NotificationHolder.toInactiveNotificationHolder() = InactiveNotificationHolder(timestamp, content) private fun InactiveNotificationHolder.toNotificationHolder(notificationId: Int): NotificationHolder { return NotificationHolder(notificationId, timestamp, content) } private fun <T> List<T>.replace(old: T, new: T): List<T> { return map { element -> if (element === old) new else element } } }
app/core/src/main/java/com/fsck/k9/notification/NotificationRepository.kt +2 −2 Original line number Diff line number Diff line Loading @@ -37,8 +37,8 @@ internal class NotificationRepository( } @Synchronized fun addNotification(account: Account, content: NotificationContent, timestamp: Long): AddNotificationResult { return notificationDataStore.addNotification(account, content, timestamp).also { result -> fun addNotification(account: Account, content: NotificationContent, timestamp: Long): AddNotificationResult? { return notificationDataStore.addNotification(account, content, timestamp)?.also { result -> persistNotificationDataStoreChanges( account = account, operations = result.notificationStoreOperations, Loading
app/core/src/test/java/com/fsck/k9/notification/NewMailNotificationManagerTest.kt +4 −0 Original line number Diff line number Diff line Loading @@ -49,6 +49,7 @@ class NewMailNotificationManagerTest { val result = manager.addNewMailNotification(account, message, silent = false) assertNotNull(result) assertThat(result.singleNotificationData.first().content).isEqualTo( NotificationContent( messageReference = createMessageReference("msg-1"), Loading Loading @@ -85,6 +86,7 @@ class NewMailNotificationManagerTest { val result = manager.addNewMailNotification(account, messageTwo, silent = false) assertNotNull(result) assertThat(result.singleNotificationData.first().content).isEqualTo( NotificationContent( messageReference = createMessageReference("msg-2"), Loading Loading @@ -121,6 +123,7 @@ class NewMailNotificationManagerTest { val result = manager.addNewMailNotification(account, message, silent = false) assertNotNull(result) val notificationId = NotificationIds.getSingleMessageNotificationId(account, index = 0) assertThat(result.cancelNotificationIds).isEqualTo(listOf(notificationId)) assertThat(result.singleNotificationData.first().notificationId).isEqualTo(notificationId) Loading Loading @@ -196,6 +199,7 @@ class NewMailNotificationManagerTest { messageUid = "msg-2" ) val dataTwo = manager.addNewMailNotification(account, messageTwo, silent = true) assertNotNull(dataTwo) val notificationIdTwo = dataTwo.singleNotificationData.first().notificationId val messageThree = addMessageToNotificationContentCreator( sender = "Alice", Loading