Use imported auto-expand folder after refreshing the folder list

This commit is contained in:
cketti 2020-05-01 22:25:26 +02:00
parent cc637a6575
commit 9e02b1cba5
2 changed files with 35 additions and 2 deletions

View file

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

View file

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