diff --git a/app/core/src/main/java/com/fsck/k9/mailstore/AutoExpandFolderBackendFoldersRefreshListener.kt b/app/core/src/main/java/com/fsck/k9/mailstore/AutoExpandFolderBackendFoldersRefreshListener.kt index 45e5b8f7a..e7394c8b9 100644 --- a/app/core/src/main/java/com/fsck/k9/mailstore/AutoExpandFolderBackendFoldersRefreshListener.kt +++ b/app/core/src/main/java/com/fsck/k9/mailstore/AutoExpandFolderBackendFoldersRefreshListener.kt @@ -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) + } } diff --git a/app/core/src/main/java/com/fsck/k9/mailstore/FolderRepository.kt b/app/core/src/main/java/com/fsck/k9/mailstore/FolderRepository.kt index 58704b363..cb355335e 100644 --- a/app/core/src/main/java/com/fsck/k9/mailstore/FolderRepository.kt +++ b/app/core/src/main/java/com/fsck/k9/mailstore/FolderRepository.kt @@ -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 ->