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

Commit 9e02b1cb authored by cketti's avatar cketti
Browse files

Use imported auto-expand folder after refreshing the folder list

parent cc637a65
Loading
Loading
Loading
Loading
+18 −2
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ import com.fsck.k9.Account
import com.fsck.k9.Preferences

/**
 * Reset an Account's auto-expand folder when the currently configured folder was removed.
 * Update an Account's auto-expand folder after the folder list has been refreshed.
 */
class AutoExpandFolderBackendFoldersRefreshListener(
    private val preferences: Preferences,
@@ -16,14 +16,30 @@ class AutoExpandFolderBackendFoldersRefreshListener(

    override fun onAfterFolderListRefresh() {
        checkAutoExpandFolder()

        removeImportedAutoExpandFolder()
        saveAccount()
    }

    private fun checkAutoExpandFolder() {
        val folderId = account.importedAutoExpandFolder?.let { folderRepository.getFolderId(it) }
        if (folderId != null) {
            account.autoExpandFolderId = folderId
            return
        }

        account.autoExpandFolderId?.let { autoExpandFolderId ->
            if (!folderRepository.isFolderPresent(autoExpandFolderId)) {
                account.autoExpandFolderId = null
                preferences.saveAccount(account)
            }
        }
    }

    private fun removeImportedAutoExpandFolder() {
        account.importedAutoExpandFolder = null
    }

    private fun saveAccount() {
        preferences.saveAccount(account)
    }
}
+17 −0
Original line number Diff line number Diff line
@@ -63,6 +63,23 @@ class FolderRepository(
        }
    }

    fun getFolderId(folderServerId: String): Long? {
        val database = localStoreProvider.getInstance(account).database
        return database.execute(false) { db ->
            db.query(
                "folders",
                arrayOf("id"),
                "server_id = ?",
                arrayOf(folderServerId),
                null,
                null,
                null
            ).use { cursor ->
                if (cursor.moveToFirst()) cursor.getLong(0) else null
            }
        }
    }

    fun isFolderPresent(folderId: Long): Boolean {
        val database = localStoreProvider.getInstance(account).database
        return database.execute(false) { db ->