Improve Android mailbox navigation and pagination
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
+65
-7
@@ -16,6 +16,8 @@ const mailFeatureState = {
|
||||
syncMailboxKey: null,
|
||||
displayedMailboxKey: null,
|
||||
refreshPending: false,
|
||||
browseRefreshPending: false,
|
||||
browseRefreshFromStart: false,
|
||||
folderBeforeSearch: null,
|
||||
customFolders: [],
|
||||
folderLoad: null,
|
||||
@@ -692,6 +694,13 @@ async function syncMailbox(showToast, options = {}) {
|
||||
if (refresh) refresh.disabled = true;
|
||||
const loadMoreButton = selectMailUi('#load-more');
|
||||
if (loadMoreButton) loadMoreButton.disabled = true;
|
||||
if (!options.append) {
|
||||
mailFeatureState.browseRefreshPending = false;
|
||||
mailFeatureState.browseRefreshFromStart = false;
|
||||
} else {
|
||||
const status = selectMailUi('#mail-status');
|
||||
if (status) status.textContent = 'Loading more messages…';
|
||||
}
|
||||
const request = {};
|
||||
mailFeatureState.syncRequest = request;
|
||||
mailFeatureState.syncMailboxKey = mailboxKey;
|
||||
@@ -808,7 +817,9 @@ async function syncMailbox(showToast, options = {}) {
|
||||
}
|
||||
updateAccountCard(mailFeatureState.connectedAccount);
|
||||
const status = selectMailUi('#mail-status');
|
||||
if (status) status.textContent = result.offline ? 'Offline — showing saved messages' : 'All caught up with your mail';
|
||||
if (status) status.textContent = mailFeatureState.browseRefreshPending
|
||||
? 'Updates paused while browsing — return to the top or refresh'
|
||||
: result.offline ? 'Offline — showing saved messages' : 'All caught up with your mail';
|
||||
if (showToast) toastMailUi(`Synced ${rendererState.messages.length} messages`);
|
||||
} catch (error) {
|
||||
if (isAndroid && mailFeatureState.syncRequest !== request) return;
|
||||
@@ -816,6 +827,9 @@ async function syncMailbox(showToast, options = {}) {
|
||||
if (showingCachedMessages) {
|
||||
const status = selectMailUi('#mail-status');
|
||||
if (status) status.textContent = 'Could not sync — showing saved messages';
|
||||
} else if (options.append) {
|
||||
const status = selectMailUi('#mail-status');
|
||||
if (status) status.textContent = 'Could not load more messages — use Load more to retry';
|
||||
}
|
||||
toastMailUi(error.message || 'Could not sync the inbox');
|
||||
} finally {
|
||||
@@ -836,12 +850,51 @@ async function syncMailbox(showToast, options = {}) {
|
||||
if (mailFeatureState.syncQueued) {
|
||||
mailFeatureState.syncQueued = false;
|
||||
void syncMailbox(false);
|
||||
} else if (runDeferredMailboxRefresh()) {
|
||||
return;
|
||||
} else if (syncSucceeded) {
|
||||
maybeLoadMoreMessages();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function shouldDeferMailboxRefresh() {
|
||||
if (window.daybreakMail?.platform !== 'android') return false;
|
||||
const list = selectMailUi('#message-list');
|
||||
return Boolean(list && (list.scrollTop > 120 ||
|
||||
(document.body.classList.contains('reader-open') &&
|
||||
(appShell.getReaderListPosition() > 120 ||
|
||||
mailFeatureState.nextOffset > mailFeatureState.pageOffset + 20))));
|
||||
}
|
||||
|
||||
function requestBackgroundMailboxRefresh(options = {}) {
|
||||
if (shouldDeferMailboxRefresh()) {
|
||||
mailFeatureState.browseRefreshPending = true;
|
||||
if (options.offset === 0) mailFeatureState.browseRefreshFromStart = true;
|
||||
const status = selectMailUi('#mail-status');
|
||||
if (status && !mailFeatureState.syncPromise) {
|
||||
status.textContent = 'Updates paused while browsing — return to the top or refresh';
|
||||
}
|
||||
return;
|
||||
}
|
||||
void syncMailbox(false, options);
|
||||
}
|
||||
|
||||
function runDeferredMailboxRefresh() {
|
||||
if (!mailFeatureState.browseRefreshPending || mailFeatureState.syncPromise ||
|
||||
document.visibilityState !== 'visible' || rendererState.activeView !== 'mail' ||
|
||||
shouldDeferMailboxRefresh()) return false;
|
||||
const options = mailFeatureState.browseRefreshFromStart ? { offset: 0 } : {};
|
||||
mailFeatureState.browseRefreshPending = false;
|
||||
mailFeatureState.browseRefreshFromStart = false;
|
||||
void syncMailbox(false, options);
|
||||
return true;
|
||||
}
|
||||
|
||||
function handleMessageListScroll() {
|
||||
if (!runDeferredMailboxRefresh()) maybeLoadMoreMessages();
|
||||
}
|
||||
|
||||
function maybeLoadMoreMessages() {
|
||||
const list = selectMailUi('#message-list');
|
||||
if (!list || !mailFeatureState.connectedAccount || mailFeatureState.syncPromise ||
|
||||
@@ -909,7 +962,7 @@ if (window.daybreakMail) {
|
||||
}
|
||||
if (update.newCount || (update.foldersChanged || []).length) {
|
||||
if (document.visibilityState === 'visible') {
|
||||
syncMailbox(false, update.newCount ? { offset: 0 } : {});
|
||||
requestBackgroundMailboxRefresh(update.newCount ? { offset: 0 } : {});
|
||||
}
|
||||
else mailFeatureState.refreshPending = true;
|
||||
}
|
||||
@@ -917,18 +970,20 @@ if (window.daybreakMail) {
|
||||
document.addEventListener('visibilitychange', () => {
|
||||
if (document.visibilityState === 'visible' && mailFeatureState.refreshPending) {
|
||||
mailFeatureState.refreshPending = false;
|
||||
syncMailbox(false, { offset: 0 });
|
||||
requestBackgroundMailboxRefresh({ offset: 0 });
|
||||
} else if (document.visibilityState === 'visible') {
|
||||
runDeferredMailboxRefresh();
|
||||
}
|
||||
});
|
||||
window.addEventListener('focus', () => {
|
||||
if (mailFeatureState.connectedAccount) syncMailbox(false);
|
||||
if (mailFeatureState.connectedAccount) requestBackgroundMailboxRefresh();
|
||||
});
|
||||
window.addEventListener('online', () => {
|
||||
if (mailFeatureState.connectedAccount) syncMailbox(false, { offset: 0 });
|
||||
if (mailFeatureState.connectedAccount) requestBackgroundMailboxRefresh({ offset: 0 });
|
||||
});
|
||||
setInterval(() => {
|
||||
if (mailFeatureState.connectedAccount && document.visibilityState === 'visible' && rendererState.activeView === 'mail') {
|
||||
syncMailbox(false);
|
||||
requestBackgroundMailboxRefresh();
|
||||
}
|
||||
}, 60 * 1000);
|
||||
selectMailUi('#account-settings').addEventListener('click', openMailAccount);
|
||||
@@ -960,7 +1015,10 @@ if (window.daybreakMail) {
|
||||
selectMailUi('#load-more').onclick = () => {
|
||||
if (!mailFeatureState.syncPromise) void syncMailbox(false, { append: true });
|
||||
};
|
||||
selectMailUi('#message-list').addEventListener('scroll', maybeLoadMoreMessages, { passive: true });
|
||||
selectMailUi('#message-list').addEventListener('scroll', handleMessageListScroll, { passive: true });
|
||||
document.addEventListener('click', event => {
|
||||
if (event.target.closest('#reader-back')) runDeferredMailboxRefresh();
|
||||
});
|
||||
document.addEventListener('click', async event => {
|
||||
const sendButton = event.target.closest('#send');
|
||||
if (!sendButton || !mailFeatureState.connectedAccount) return;
|
||||
|
||||
Reference in New Issue
Block a user