From dd89dee48941b11392d584f647a022c4145a15fe Mon Sep 17 00:00:00 2001 From: cketti Date: Fri, 27 Jan 2023 18:03:05 +0100 Subject: [PATCH] Convert methods in `AccountSearchConditions` to extension functions --- .../main/java/com/fsck/k9/CoreKoinModules.kt | 2 - .../java/com/fsck/k9/controller/KoinModule.kt | 1 - .../k9/controller/MessageCountsProvider.kt | 11 +- .../fsck/k9/search/AccountSearchConditions.kt | 130 ++++++++---------- .../java/com/fsck/k9/search/KoinModule.kt | 7 - 5 files changed, 65 insertions(+), 86 deletions(-) delete mode 100644 app/core/src/main/java/com/fsck/k9/search/KoinModule.kt diff --git a/app/core/src/main/java/com/fsck/k9/CoreKoinModules.kt b/app/core/src/main/java/com/fsck/k9/CoreKoinModules.kt index d79482d55..38991fcf0 100644 --- a/app/core/src/main/java/com/fsck/k9/CoreKoinModules.kt +++ b/app/core/src/main/java/com/fsck/k9/CoreKoinModules.kt @@ -15,14 +15,12 @@ import com.fsck.k9.network.connectivityModule import com.fsck.k9.notification.coreNotificationModule import com.fsck.k9.power.powerModule import com.fsck.k9.preferences.preferencesModule -import com.fsck.k9.search.searchModule val coreModules = listOf( mainModule, openPgpModule, autocryptModule, mailStoreModule, - searchModule, extractorModule, htmlModule, quoteModule, diff --git a/app/core/src/main/java/com/fsck/k9/controller/KoinModule.kt b/app/core/src/main/java/com/fsck/k9/controller/KoinModule.kt index 65ddc4d89..b62e67ebf 100644 --- a/app/core/src/main/java/com/fsck/k9/controller/KoinModule.kt +++ b/app/core/src/main/java/com/fsck/k9/controller/KoinModule.kt @@ -30,7 +30,6 @@ val controllerModule = module { single { DefaultMessageCountsProvider( preferences = get(), - accountSearchConditions = get(), messageStoreManager = get() ) } diff --git a/app/core/src/main/java/com/fsck/k9/controller/MessageCountsProvider.kt b/app/core/src/main/java/com/fsck/k9/controller/MessageCountsProvider.kt index be170258d..1d7e5f27c 100644 --- a/app/core/src/main/java/com/fsck/k9/controller/MessageCountsProvider.kt +++ b/app/core/src/main/java/com/fsck/k9/controller/MessageCountsProvider.kt @@ -3,11 +3,12 @@ package com.fsck.k9.controller import com.fsck.k9.Account import com.fsck.k9.Preferences import com.fsck.k9.mailstore.MessageStoreManager -import com.fsck.k9.search.AccountSearchConditions import com.fsck.k9.search.ConditionsTreeNode import com.fsck.k9.search.LocalSearch import com.fsck.k9.search.SearchAccount +import com.fsck.k9.search.excludeSpecialFolders import com.fsck.k9.search.getAccounts +import com.fsck.k9.search.limitToDisplayableFolders import timber.log.Timber interface MessageCountsProvider { @@ -20,13 +21,13 @@ data class MessageCounts(val unread: Int, val starred: Int) internal class DefaultMessageCountsProvider( private val preferences: Preferences, - private val accountSearchConditions: AccountSearchConditions, private val messageStoreManager: MessageStoreManager ) : MessageCountsProvider { override fun getMessageCounts(account: Account): MessageCounts { - val search = LocalSearch() - accountSearchConditions.excludeSpecialFolders(account, search) - accountSearchConditions.limitToDisplayableFolders(account, search) + val search = LocalSearch().apply { + excludeSpecialFolders(account) + limitToDisplayableFolders(account) + } return getMessageCounts(account, search.conditions) } diff --git a/app/core/src/main/java/com/fsck/k9/search/AccountSearchConditions.kt b/app/core/src/main/java/com/fsck/k9/search/AccountSearchConditions.kt index cb8d20af3..51e80f8d9 100644 --- a/app/core/src/main/java/com/fsck/k9/search/AccountSearchConditions.kt +++ b/app/core/src/main/java/com/fsck/k9/search/AccountSearchConditions.kt @@ -7,81 +7,69 @@ import com.fsck.k9.search.SearchSpecification.Attribute import com.fsck.k9.search.SearchSpecification.SearchCondition import com.fsck.k9.search.SearchSpecification.SearchField -class AccountSearchConditions { - /** - * Modify the supplied [LocalSearch] instance to limit the search to displayable folders. - * - * This method uses the current folder display mode to decide what folders to include/exclude. - * - * @param search - * The `LocalSearch` instance to modify. - * - * @see .getFolderDisplayMode - */ - fun limitToDisplayableFolders(account: Account, search: LocalSearch) { - val displayMode = account.folderDisplayMode +/** + * Modify the supplied [LocalSearch] instance to limit the search to displayable folders. + * + * This method uses the current [folder display mode][Account.folderDisplayMode] to decide what folders to + * include/exclude. + */ +fun LocalSearch.limitToDisplayableFolders(account: Account) { + when (account.folderDisplayMode) { + FolderMode.FIRST_CLASS -> { + // Count messages in the INBOX and non-special first class folders + and(SearchField.DISPLAY_CLASS, FolderClass.FIRST_CLASS.name, Attribute.EQUALS) + } + FolderMode.FIRST_AND_SECOND_CLASS -> { + // Count messages in the INBOX and non-special first and second class folders + and(SearchField.DISPLAY_CLASS, FolderClass.FIRST_CLASS.name, Attribute.EQUALS) - when (displayMode) { - FolderMode.FIRST_CLASS -> { - // Count messages in the INBOX and non-special first class folders - search.and(SearchField.DISPLAY_CLASS, FolderClass.FIRST_CLASS.name, Attribute.EQUALS) - } - FolderMode.FIRST_AND_SECOND_CLASS -> { - // Count messages in the INBOX and non-special first and second class folders - search.and(SearchField.DISPLAY_CLASS, FolderClass.FIRST_CLASS.name, Attribute.EQUALS) - - // TODO: Create a proper interface for creating arbitrary condition trees - val searchCondition = SearchCondition( - SearchField.DISPLAY_CLASS, Attribute.EQUALS, FolderClass.SECOND_CLASS.name - ) - val root = search.conditions - if (root.mRight != null) { - root.mRight.or(searchCondition) - } else { - search.or(searchCondition) - } - } - FolderMode.NOT_SECOND_CLASS -> { - // Count messages in the INBOX and non-special non-second-class folders - search.and(SearchField.DISPLAY_CLASS, FolderClass.SECOND_CLASS.name, Attribute.NOT_EQUALS) - } - FolderMode.ALL, FolderMode.NONE -> { - // Count messages in the INBOX and non-special folders + // TODO: Create a proper interface for creating arbitrary condition trees + val searchCondition = SearchCondition( + SearchField.DISPLAY_CLASS, Attribute.EQUALS, FolderClass.SECOND_CLASS.name + ) + val root = conditions + if (root.mRight != null) { + root.mRight.or(searchCondition) + } else { + or(searchCondition) } } - } - - /** - * Modify the supplied [LocalSearch] instance to exclude special folders. - * - * Currently the following folders are excluded: - * - * * Trash - * * Drafts - * * Spam - * * Outbox - * * Sent - * - * The Inbox will always be included even if one of the special folders is configured to point - * to the Inbox. - * - * @param search - * The `LocalSearch` instance to modify. - */ - fun excludeSpecialFolders(account: Account, search: LocalSearch) { - excludeSpecialFolder(search, account.trashFolderId) - excludeSpecialFolder(search, account.draftsFolderId) - excludeSpecialFolder(search, account.spamFolderId) - excludeSpecialFolder(search, account.outboxFolderId) - excludeSpecialFolder(search, account.sentFolderId) - account.inboxFolderId?.let { inboxFolderId -> - search.or(SearchCondition(SearchField.FOLDER, Attribute.EQUALS, inboxFolderId.toString())) + FolderMode.NOT_SECOND_CLASS -> { + // Count messages in the INBOX and non-special non-second-class folders + and(SearchField.DISPLAY_CLASS, FolderClass.SECOND_CLASS.name, Attribute.NOT_EQUALS) } - } - - private fun excludeSpecialFolder(search: LocalSearch, folderId: Long?) { - if (folderId != null) { - search.and(SearchField.FOLDER, folderId.toString(), Attribute.NOT_EQUALS) + FolderMode.ALL, FolderMode.NONE -> { + // Count messages in the INBOX and non-special folders } } } + +/** + * Modify the supplied [LocalSearch] instance to exclude special folders. + * + * Currently the following folders are excluded: + * - Trash + * - Drafts + * - Spam + * - Outbox + * - Sent + * + * The Inbox will always be included even if one of the special folders is configured to point to the Inbox. + */ +fun LocalSearch.excludeSpecialFolders(account: Account) { + this.excludeSpecialFolder(account.trashFolderId) + this.excludeSpecialFolder(account.draftsFolderId) + this.excludeSpecialFolder(account.spamFolderId) + this.excludeSpecialFolder(account.outboxFolderId) + this.excludeSpecialFolder(account.sentFolderId) + + account.inboxFolderId?.let { inboxFolderId -> + or(SearchCondition(SearchField.FOLDER, Attribute.EQUALS, inboxFolderId.toString())) + } +} + +private fun LocalSearch.excludeSpecialFolder(folderId: Long?) { + if (folderId != null) { + and(SearchField.FOLDER, folderId.toString(), Attribute.NOT_EQUALS) + } +} diff --git a/app/core/src/main/java/com/fsck/k9/search/KoinModule.kt b/app/core/src/main/java/com/fsck/k9/search/KoinModule.kt deleted file mode 100644 index 63dcc100f..000000000 --- a/app/core/src/main/java/com/fsck/k9/search/KoinModule.kt +++ /dev/null @@ -1,7 +0,0 @@ -package com.fsck.k9.search - -import org.koin.dsl.module - -val searchModule = module { - single { AccountSearchConditions() } -}