Merge pull request #5632 from k9mail/MessageStore_message_count
Move LocalFolder.getMessageCount() to MessageStore
This commit is contained in:
commit
d5fc337a45
6 changed files with 54 additions and 43 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
*/
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue