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

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

Merge pull request #5959 from k9mail/NotificationChannelManager_cleanup

Clean up code to update notification channels
parents 35da1015 075324af
Loading
Loading
Loading
Loading
+10 −17
Original line number Diff line number Diff line
@@ -58,7 +58,7 @@ class NotificationChannelManager(
        accounts: List<Account>
    ) {
        for (account in accounts) {
            val groupId = account.uuid
            val groupId = account.notificationChannelGroupId
            val group = NotificationChannelGroup(groupId, account.displayName)

            val channelMessages = getChannelMessages(account)
@@ -75,25 +75,13 @@ class NotificationChannelManager(
        notificationManager: NotificationManager,
        accounts: List<Account>
    ) {
        val existingAccounts = HashMap<String, Account>()
        for (account in accounts) {
            existingAccounts[account.uuid] = account
        }
        val accountUuids = accounts.map { it.uuid }.toSet()

        val groups = notificationManager.notificationChannelGroups
        for (group in groups) {
            val groupId = group.id

            var shouldDelete = false
            if (!existingAccounts.containsKey(groupId)) {
                shouldDelete = true
            } else if (existingAccounts[groupId]?.displayName != group.name.toString()) {
                // There is no way to change group names. Deleting group, so it is re-generated.
                shouldDelete = true
            }

            if (shouldDelete) {
                notificationManager.deleteNotificationChannelGroup(groupId)
            val accountUuid = group.id.toAccountUuid()
            if (accountUuid !in accountUuids) {
                notificationManager.deleteNotificationChannelGroup(group.id)
            }
        }
    }
@@ -248,6 +236,11 @@ class NotificationChannelManager(
        enableVibration(notificationSettings.vibration.isEnabled)
    }

    private val Account.notificationChannelGroupId: String
        get() = uuid

    private fun String.toAccountUuid(): String = this

    private val Account.messagesNotificationChannelSuffix: String
        get() = messagesNotificationChannelVersion.let { version -> if (version == 0) "" else "_$version" }