Code cleanup

This commit is contained in:
cketti 2021-08-17 19:49:00 +02:00
parent 4729ae044a
commit 106c132d8d
9 changed files with 75 additions and 71 deletions

View file

@ -1,8 +1,6 @@
package com.fsck.k9.controller
import android.content.Context
import com.fsck.k9.DefaultMessageCountsProvider
import com.fsck.k9.MessageCountsProvider
import com.fsck.k9.Preferences
import com.fsck.k9.backend.BackendManager
import com.fsck.k9.mailstore.LocalStoreProvider

View file

@ -1,6 +1,8 @@
package com.fsck.k9
package com.fsck.k9.controller
import android.content.Context
import com.fsck.k9.Account
import com.fsck.k9.Preferences
import com.fsck.k9.mail.MessagingException
import com.fsck.k9.mailstore.LocalStoreProvider
import com.fsck.k9.search.AccountSearchConditions
@ -37,7 +39,7 @@ internal class DefaultMessageCountsProvider(
localStore.getMessageCounts(search)
} catch (e: MessagingException) {
Timber.e(e, "Unable to getMessageCounts for account: %s", account)
return MessageCounts(0, 0)
MessageCounts(0, 0)
}
}
@ -48,7 +50,7 @@ internal class DefaultMessageCountsProvider(
var unreadCount = 0
var starredCount = 0
for (account in accounts) {
var accountMessageCount = getMessageCountsWithLocalSearch(account, search)
val accountMessageCount = getMessageCountsWithLocalSearch(account, search)
unreadCount += accountMessageCount.unread
starredCount += accountMessageCount.starred
}
@ -62,7 +64,7 @@ internal class DefaultMessageCountsProvider(
localStore.getMessageCounts(search)
} catch (e: MessagingException) {
Timber.e(e, "Unable to getMessageCounts for account: %s", account)
return MessageCounts(0, 0)
MessageCounts(0, 0)
}
}
}

View file

@ -34,8 +34,6 @@ import com.fsck.k9.Account.DeletePolicy;
import com.fsck.k9.Account.Expunge;
import com.fsck.k9.DI;
import com.fsck.k9.K9;
import com.fsck.k9.MessageCounts;
import com.fsck.k9.MessageCountsProvider;
import com.fsck.k9.Preferences;
import com.fsck.k9.backend.BackendManager;
import com.fsck.k9.backend.api.Backend;
@ -1687,14 +1685,6 @@ public class MessagingController {
return unreadMessageCountProvider.getUnreadMessageCount(searchAccount);
}
public MessageCounts getMessageCounts(Account account) {
return messageCountsProvider.getMessageCounts(account);
}
public MessageCounts getMessageCounts(SearchAccount searchAccount) {
return messageCountsProvider.getMessageCounts(searchAccount);
}
public int getFolderUnreadMessageCount(Account account, Long folderId) throws MessagingException {
LocalStore localStore = localStoreProvider.getInstance(account);
LocalFolder localFolder = localStore.getFolder(folderId);

View file

@ -10,7 +10,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.fsck.k9.Account;
import com.fsck.k9.K9;
import com.fsck.k9.MessageCounts;
import com.fsck.k9.controller.MessageCounts;
import com.fsck.k9.controller.MessageReference;
import com.fsck.k9.helper.FileHelper;
import com.fsck.k9.helper.Utility;
@ -330,20 +330,20 @@ public class LocalFolder {
return this.localStore.getDatabase().execute(false, new DbCallback<Integer>() {
@Override
public Integer doDbWork(final SQLiteDatabase db) throws WrappedException {
int unreadMessageCount = 0;
int starredMessageCount = 0;
Cursor cursor = db.query("messages", new String[] { "COUNT(id)" },
"folder_id = ? AND empty = 0 AND deleted = 0 AND flagged=1",
"folder_id = ? AND empty = 0 AND deleted = 0 AND flagged = 1",
new String[] { Long.toString(databaseId) }, null, null, null);
try {
if (cursor.moveToFirst()) {
unreadMessageCount = cursor.getInt(0);
starredMessageCount = cursor.getInt(0);
}
} finally {
cursor.close();
}
return unreadMessageCount;
return starredMessageCount;
}
});
} catch (WrappedException e) {

View file

@ -31,7 +31,7 @@ import com.fsck.k9.Account;
import com.fsck.k9.Clock;
import com.fsck.k9.DI;
import com.fsck.k9.K9;
import com.fsck.k9.MessageCounts;
import com.fsck.k9.controller.MessageCounts;
import com.fsck.k9.Preferences;
import com.fsck.k9.controller.MessagingControllerCommands.PendingCommand;
import com.fsck.k9.controller.PendingCommandSerializer;

View file

@ -11,8 +11,6 @@ import android.content.Context;
import com.fsck.k9.Account;
import com.fsck.k9.K9;
import com.fsck.k9.K9RobolectricTest;
import com.fsck.k9.MessageCounts;
import com.fsck.k9.MessageCountsProvider;
import com.fsck.k9.Preferences;
import com.fsck.k9.backend.BackendManager;
import com.fsck.k9.backend.api.Backend;

View file

@ -14,7 +14,6 @@ import androidx.drawerlayout.widget.DrawerLayout
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.fsck.k9.Account
import com.fsck.k9.K9
import com.fsck.k9.MessageCounts
import com.fsck.k9.activity.MessageList
import com.fsck.k9.controller.MessagingController
import com.fsck.k9.controller.SimpleMessagingListener
@ -56,6 +55,11 @@ import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import org.koin.core.parameter.parametersOf
private const val UNREAD_SYMBOL = "\u2B24"
private const val STARRED_SYMBOL = "\u2605"
private const val THIN_SPACE = "\u2009"
private const val EN_SPACE = "\u2000"
class K9Drawer(private val parent: MessageList, savedInstanceState: Bundle?) : KoinComponent {
private val foldersViewModel: FoldersViewModel by parent.viewModel()
private val accountsViewModel: AccountsViewModel by parent.viewModel()
@ -163,24 +167,46 @@ class K9Drawer(private val parent: MessageList, savedInstanceState: Bundle?) : K
}
}
/**
* Format the unread and starred counts for display in the fragment Badge box
*/
private fun formatBadgeText(messageCounts: MessageCounts): String {
var badgeText = ""
if (K9.isShowStarredCount) {
if (messageCounts.unread > 0) {
badgeText = "\u2B24\u2009" + messageCounts.unread.toString()
}
if (messageCounts.starred > 0) {
badgeText = badgeText + "\u2000\u2605\u2009" + messageCounts.starred.toString()
}
private fun buildBadgeText(displayAccount: DisplayAccount): String? {
return buildBadgeText(displayAccount.unreadMessageCount, displayAccount.starredMessageCount)
}
private fun buildBadgeText(displayFolder: DisplayFolder): String? {
return buildBadgeText(displayFolder.unreadMessageCount, displayFolder.starredMessageCount)
}
private fun buildBadgeText(unreadCount: Int, starredCount: Int): String? {
return if (K9.isShowStarredCount) {
buildBadgeTextWithStarredCount(unreadCount, starredCount)
} else {
if (messageCounts.unread > 0) {
badgeText = messageCounts.unread.toString()
buildBadgeTextWithUnreadCount(unreadCount)
}
}
private fun buildBadgeTextWithStarredCount(unreadCount: Int, starredCount: Int): String? {
if (unreadCount == 0 && starredCount == 0) return null
return buildString {
val hasUnreadCount = unreadCount > 0
if (hasUnreadCount) {
append(UNREAD_SYMBOL)
append(THIN_SPACE)
append(unreadCount)
}
if (starredCount > 0) {
if (hasUnreadCount) {
append(EN_SPACE)
}
append(STARRED_SYMBOL)
append(THIN_SPACE)
append(starredCount)
}
}
return badgeText.trim()
}
private fun buildBadgeTextWithUnreadCount(unreadCount: Int): String? {
return if (unreadCount > 0) unreadCount.toString() else null
}
private fun setAccounts(displayAccounts: List<DisplayAccount>) {
@ -204,13 +230,12 @@ class K9Drawer(private val parent: MessageList, savedInstanceState: Bundle?) : K
descriptionTextColor = selectedTextColor
selectedColorInt = drawerColors.selectedColor
icon = ImageHolder(createAccountImageUri(account))
formatBadgeText(displayAccount.messageCounts).takeIf { it.isNotEmpty() }
?.let { bText ->
badgeText = bText
badgeStyle = BadgeStyle().apply {
textColorStateList = selectedTextColor
}
buildBadgeText(displayAccount)?.let { text ->
badgeText = text
badgeStyle = BadgeStyle().apply {
textColorStateList = selectedTextColor
}
}
}
if (account.uuid == openedAccountUuid) {
@ -327,16 +352,6 @@ class K9Drawer(private val parent: MessageList, savedInstanceState: Bundle?) : K
selectedColorInt = selectedBackgroundColor
textColor = selectedTextColor
isSelected = unifiedInboxSelected
// TODO: get the real message counts from the back end
var messageCounts = MessageCounts(0, 0)
formatBadgeText(messageCounts).takeIf { it.isNotEmpty() }
?.let { bText ->
badgeText = bText
badgeStyle = BadgeStyle().apply {
textColorStateList = selectedTextColor
}
}
}
sliderView.addItems(unifiedInboxItem)
@ -360,16 +375,10 @@ class K9Drawer(private val parent: MessageList, savedInstanceState: Bundle?) : K
identifier = drawerId
tag = folder
nameText = getFolderDisplayName(folder)
formatBadgeText(
MessageCounts(
displayFolder.unreadMessageCount,
displayFolder.starredMessageCount
)
).takeIf { it.isNotEmpty() }
?.let { bText ->
badgeText = bText
badgeStyle = folderBadgeStyle
}
buildBadgeText(displayFolder)?.let { text ->
badgeText = text
badgeStyle = folderBadgeStyle
}
selectedColorInt = selectedBackgroundColor
textColor = selectedTextColor
}

View file

@ -9,9 +9,9 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import com.fsck.k9.Account
import com.fsck.k9.AccountsChangeListener
import com.fsck.k9.MessageCounts
import com.fsck.k9.MessageCountsProvider
import com.fsck.k9.Preferences
import com.fsck.k9.controller.MessageCounts
import com.fsck.k9.controller.MessageCountsProvider
import com.fsck.k9.provider.EmailProvider
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
@ -53,7 +53,11 @@ class AccountsViewModel(
combine(messageCountsFlows) { messageCountsList ->
messageCountsList.mapIndexed { index, messageCounts ->
DisplayAccount(account = accounts[index], messageCounts)
DisplayAccount(
account = accounts[index],
unreadMessageCount = messageCounts.unread,
starredMessageCount = messageCounts.starred
)
}
}
}

View file

@ -1,6 +1,9 @@
package com.fsck.k9.ui.account
import com.fsck.k9.Account
import com.fsck.k9.MessageCounts
data class DisplayAccount(val account: Account, val messageCounts: MessageCounts)
data class DisplayAccount(
val account: Account,
val unreadMessageCount: Int,
val starredMessageCount: Int
)