Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 2607b24b authored by cketti's avatar cketti Committed by GitHub
Browse files

Merge pull request #5946 from k9mail/update_new_mail_notifications

Add support for updating existing notifications
parents b5d1ac7b d784151e
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -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,
+2 −2
Original line number Diff line number Diff line
@@ -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,
+40 −10
Original line number Diff line number Diff line
@@ -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

@@ -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()

@@ -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
        }
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -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,
+4 −0
Original line number Diff line number Diff line
@@ -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"),
@@ -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"),
@@ -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)
@@ -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