Merge pull request #5632 from k9mail/MessageStore_message_count

Move LocalFolder.getMessageCount() to MessageStore
This commit is contained in:
cketti 2021-09-02 17:47:16 +02:00 committed by GitHub
commit d5fc337a45
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 43 deletions

View file

@ -66,7 +66,6 @@ import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.Part;
import com.fsck.k9.mail.ServerSettings;
import com.fsck.k9.mailstore.FolderDetailsAccessor;
import com.fsck.k9.mailstore.ListenableMessageStore;
import com.fsck.k9.mailstore.LocalFolder;
import com.fsck.k9.mailstore.LocalMessage;
import com.fsck.k9.mailstore.LocalStore;
@ -1487,21 +1486,14 @@ public class MessagingController {
}
private boolean messagesPendingSend(final Account account) {
try {
LocalFolder localFolder = localStoreProvider.getInstance(account).getFolder(account.getOutboxFolderId());
if (!localFolder.exists()) {
return false;
}
localFolder.open();
if (localFolder.getMessageCount() > 0) {
return true;
}
} catch (Exception e) {
Timber.e(e, "Exception while checking for unsent messages");
Long outboxFolderId = account.getOutboxFolderId();
if (outboxFolderId == null) {
Timber.w("Could not get Outbox folder ID from Account");
return false;
}
return false;
MessageStore messageStore = messageStoreManager.getMessageStore(account);
return messageStore.getMessageCount(outboxFolderId) > 0;
}
/**

View file

@ -259,34 +259,6 @@ public class LocalFolder {
});
}
public int getMessageCount() throws MessagingException {
try {
return this.localStore.getDatabase().execute(false, new DbCallback<Integer>() {
@Override
public Integer doDbWork(final SQLiteDatabase db) throws WrappedException {
try {
open();
} catch (MessagingException e) {
throw new WrappedException(e);
}
Cursor cursor = null;
try {
cursor = db.rawQuery(
"SELECT COUNT(id) FROM messages " +
"WHERE empty = 0 AND deleted = 0 and folder_id = ?",
new String[] { Long.toString(databaseId) });
cursor.moveToFirst();
return cursor.getInt(0); //messagecount
} finally {
Utility.closeQuietly(cursor);
}
}
});
} catch (WrappedException e) {
throw (MessagingException) e.getCause();
}
}
public MessageCounts getMessageCounts() throws MessagingException {
return new MessageCounts(getUnreadMessageCount(), getStarredMessageCount());
}

View file

@ -176,6 +176,11 @@ interface MessageStore {
*/
fun getFolderId(folderServerId: String): Long?
/**
* Retrieve the number of messages in a folder.
*/
fun getMessageCount(folderId: Long): Int
/**
* Update a folder's name and type.
*/

View file

@ -134,6 +134,10 @@ class K9MessageStore(
return retrieveFolderOperations.getFolderId(folderServerId)
}
override fun getMessageCount(folderId: Long): Int {
return retrieveFolderOperations.getMessageCount(folderId)
}
override fun getSize(): Long {
return databaseOperations.getSize()
}

View file

@ -130,6 +130,17 @@ internal class RetrieveFolderOperations(private val lockableDatabase: LockableDa
}
}
}
fun getMessageCount(folderId: Long): Int {
return lockableDatabase.execute(false) { db ->
db.rawQuery(
"SELECT COUNT(id) FROM messages WHERE empty = 0 AND deleted = 0 AND folder_id = ?",
arrayOf(folderId.toString())
).use { cursor ->
if (cursor.moveToFirst()) cursor.getInt(0) else 0
}
}
}
}
private class CursorFolderAccessor(val cursor: Cursor) : FolderDetailsAccessor {

View file

@ -301,4 +301,31 @@ class RetrieveFolderOperationsTest : RobolectricTest() {
assertThat(result).isNull()
}
@Test
fun `get message count from empty folder`() {
val folderId = sqliteDatabase.createFolder()
val result = retrieveFolderOperations.getMessageCount(folderId)
assertThat(result).isEqualTo(0)
}
@Test
fun `get message count from non-existent folder`() {
val result = retrieveFolderOperations.getMessageCount(23)
assertThat(result).isEqualTo(0)
}
@Test
fun `get message count from non-empty folder`() {
val folderId = sqliteDatabase.createFolder()
sqliteDatabase.createMessage(folderId = folderId)
sqliteDatabase.createMessage(folderId = folderId)
val result = retrieveFolderOperations.getMessageCount(folderId)
assertThat(result).isEqualTo(2)
}
}