Add feature to scroll down in imap folder to load more mails, and speedup of imap folder change

This commit is contained in:
2026-11-09 20:42:31 +02:00
parent b14209a5ae
commit d7b6e31518
9 changed files with 395 additions and 41 deletions
@@ -339,7 +339,7 @@ internal object AndroidAccountVault {
}
@Synchronized
fun cacheMessages(id: String, folder: String, messages: JSONArray) {
fun cacheMessages(id: String, folder: String, messages: JSONArray, replace: Boolean = false) {
val store = read()
val accounts = store.optJSONArray("accounts") ?: return
for (index in 0 until accounts.length()) {
@@ -348,16 +348,18 @@ internal object AndroidAccountVault {
val cache = account.optJSONObject("mailCache") ?: JSONObject()
val previous = cache.optJSONArray(folder) ?: JSONArray()
val merged = LinkedHashMap<String, JSONObject>()
for (itemIndex in 0 until previous.length()) {
val item = previous.optJSONObject(itemIndex) ?: continue
merged[item.optString("uid", item.optString("id"))] = item
if (!replace) {
for (itemIndex in 0 until previous.length()) {
val item = previous.optJSONObject(itemIndex) ?: continue
merged[item.optString("uid", item.optString("id"))] = item
}
}
for (itemIndex in 0 until messages.length()) {
val item = messages.optJSONObject(itemIndex) ?: continue
merged[item.optString("uid", item.optString("id"))] = item
}
cache.put(folder, JSONArray().apply {
merged.values.toList().takeLast(30).forEach { put(it) }
merged.values.sortedByDescending { it.optLong("timestamp") }.take(30).forEach { put(it) }
})
account.put("mailCache", cache)
write(store)
@@ -182,7 +182,7 @@ internal object AndroidMailService {
result.put(value)
cached.put(value)
}
AndroidAccountVault.cacheMessages(account.optString("id"), requestedFolder, cached)
AndroidAccountVault.cacheMessages(account.optString("id"), requestedFolder, cached, replace = offset <= 0)
JSONObject().put("messages", result).put("total", total)
.put("start", first).put("end", end).put("folder", requestedFolder)
} finally {
@@ -26,6 +26,8 @@ class EpMailPlugin : Plugin() {
private var syncReceiver: BroadcastReceiver? = null
private var wipeDialogShowing = false
private val mailboxExecutor = Executors.newFixedThreadPool(3)
private val folderListExecutor = Executors.newFixedThreadPool(2)
private val mailActionExecutor = Executors.newSingleThreadExecutor()
private val outboxExecutor = Executors.newSingleThreadExecutor()
override fun load() {
@@ -62,6 +64,8 @@ class EpMailPlugin : Plugin() {
override fun handleOnDestroy() {
mailboxExecutor.shutdown()
folderListExecutor.shutdown()
mailActionExecutor.shutdown()
outboxExecutor.shutdown()
syncReceiver?.let {
try { context.unregisterReceiver(it) } catch (_: IllegalArgumentException) {}
@@ -484,9 +488,15 @@ class EpMailPlugin : Plugin() {
try {
val account = AndroidAccountVault.account()
?: throw IllegalStateException("Add a mail account before listing folders.")
call.resolve(AndroidMailService.listFolders(account).toJSObject())
folderListExecutor.execute {
try {
call.resolve(AndroidMailService.listFolders(account).toJSObject())
} catch (error: Exception) {
call.reject(error.message ?: "Could not list mail folders.")
}
}
} catch (error: Exception) {
call.resolve(JSObject().put("folders", org.json.JSONArray()).put("offline", true))
call.reject(error.message ?: "Could not list mail folders.")
}
}
@@ -560,16 +570,26 @@ class EpMailPlugin : Plugin() {
try {
val account = AndroidAccountVault.account()
?: throw IllegalStateException("Add a mail account before changing messages.")
call.resolve(AndroidMailService.updateMessage(account, call.data).toJSObject())
val input = JSONObject(call.data.toString())
mailActionExecutor.execute {
try {
call.resolve(AndroidMailService.updateMessage(account, input).toJSObject())
} catch (error: Exception) {
if (isConnectivityFailure(error)) {
try {
AndroidAccountVault.enqueueMailOperation(account.optString("id"), JSONObject()
.put("id", UUID.randomUUID().toString())
.put("type", "action")
.put("data", input))
call.resolve(JSObject().put("ok", true).put("queued", true))
} catch (queueError: Exception) {
call.reject(queueError.message ?: "Could not queue the message action.")
}
} else call.reject(error.message ?: "Could not update the message.")
}
}
} catch (error: Exception) {
val account = AndroidAccountVault.account()
if (account != null && isConnectivityFailure(error)) {
AndroidAccountVault.enqueueMailOperation(account.optString("id"), JSONObject()
.put("id", UUID.randomUUID().toString())
.put("type", "action")
.put("data", JSONObject(call.data.toString())))
call.resolve(JSObject().put("ok", true).put("queued", true))
} else call.reject(error.message ?: "Could not update the message.")
call.reject(error.message ?: "Could not update the message.")
}
}