Improve Android mailbox navigation and pagination
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
+19
-1
@@ -4,11 +4,26 @@ const state = window.epMailState;
|
||||
const $=s=>document.querySelector(s), $$=s=>[...document.querySelectorAll(s)];
|
||||
function esc(x){return String(x).replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]))}
|
||||
const { safeRemoteImageUrl, sanitizeMailHtml } = window.epMailFeatures.contentSecurity;
|
||||
let readerListPosition = null;
|
||||
function rememberReaderListPosition() {
|
||||
if (window.daybreakMail?.platform !== 'android' || document.body.classList.contains('reader-open')) return;
|
||||
const list = $('#message-list');
|
||||
readerListPosition = { folder: state.activeFolder, scrollTop: list.scrollTop };
|
||||
}
|
||||
function returnToMessageList() {
|
||||
state.activeMessage = null;
|
||||
document.body.classList.remove('reader-open');
|
||||
$('#reader-pane').innerHTML = emptyReader();
|
||||
renderList();
|
||||
if (readerListPosition?.folder === state.activeFolder) $('#message-list').scrollTop = readerListPosition.scrollTop;
|
||||
readerListPosition = null;
|
||||
}
|
||||
function renderList(){let rows=state.messages.filter(m=>{if(state.activeFolder==='Starred'&&!m.star)return false;if(state.activeFolder==='Inbox'&&m.archived)return false;if(state.activeFolder==='Archive'&&!m.archived)return false;if(state.activeFolder==='Trash'&&!m.deleted)return false;if(state.activeFolder==='Sent'&&!m.sent)return false;if(state.activeFolder==='Drafts')return false;if(state.activeFolder==='Snoozed')return false;if(m.deleted&&state.activeFolder!=='Trash')return false;return !state.query||`${m.sender} ${m.email} ${m.subject} ${m.snippet}`.toLowerCase().includes(state.query.toLowerCase())});$('#message-list').innerHTML=rows.length?rows.map(m=>`<div class="message ${m.unread?'unread':''} ${m.id===state.activeMessage?'selected':''}" data-id="${m.id}"><input class="row-check" type="checkbox" aria-label="Select message" ${state.selectedIds.has(m.id)?'checked':''}><div class="contact-avatar" style="background:${m.color}">${m.initials}</div><div class="message-body"><div class="sender">${esc(m.sender)}</div><div class="subject">${esc(m.subject)}</div><div class="snippet">${esc(m.snippet)}</div></div><div class="message-meta"><span class="time">${esc(m.time)}</span><button class="star ${m.star?'on':''}" aria-label="${m.star?'Unstar':'Star'}">${m.star?'★':'☆'}</button></div></div>`).join(''):`<div class="reader-empty"><div><div class="empty-icon">⌕</div><h3>No messages here</h3><p>Try another folder or search.</p></div></div>`;$$('.message').forEach(row=>{row.addEventListener('click',e=>{if(e.target.closest('.row-check')){const id=+row.dataset.id;state.selectedIds.has(id)?state.selectedIds.delete(id):state.selectedIds.add(id);return}if(e.target.closest('.star')){const m=state.messages.find(x=>x.id===+row.dataset.id);m.star=!m.star;renderList();return}openMessage(+row.dataset.id)});});if(!rows.length){document.body.classList.remove('reader-open');$('#reader-pane').innerHTML=emptyReader()}}
|
||||
function openMessage(id){
|
||||
state.activeMessage=id;
|
||||
const m=state.messages.find(x=>x.id===id);
|
||||
if(!m){state.activeMessage=null;document.body.classList.remove('reader-open');return;}
|
||||
rememberReaderListPosition();
|
||||
document.body.classList.add('reader-open');
|
||||
m.unread=false;
|
||||
renderList();
|
||||
@@ -18,7 +33,7 @@ function openMessage(id){
|
||||
const imageNotice=safeHtml.remoteImageCount?`<section class="remote-image-notice" role="status"><p>This message contains ${safeHtml.remoteImageCount} remote image${safeHtml.remoteImageCount===1?'':'s'}. Loading them can reveal your network address and that you opened this message to the image host.</p><button type="button" class="reply-button" id="load-remote-images">Load images for this message</button></section>`:'';
|
||||
const attachments=(m.attachments?.length?m.attachments:m.attachment?[{filename:m.attachment,size:m.size}]:[]).map(file=>`<div class="attachment"><div class="file-icon">${file.filename.toLowerCase().endsWith('.pdf')?'▤':'▧'}</div><div class="attachment-info"><strong title="${esc(file.filename)}">${esc(file.filename)}</strong><small>${file.size?(typeof file.size==='string'?esc(file.size):`${(file.size/1048576).toFixed(1)} MB`):'Attachment'}</small></div><button class="download" data-filename="${esc(file.filename)}" title="Download ${esc(file.filename)}">↓</button></div>`).join('');
|
||||
$('#reader-pane').innerHTML=`<div class="reader-header"><div class="reader-topline"><button class="reader-back" id="reader-back" type="button" aria-label="Back to messages">Back</button><div class="reader-tags">${m.tags.map((t,i)=>`<span class="tag ${i?'green':''}">${esc(t)}</span>`).join('')}</div><div class="reader-tools"><button class="icon-button" id="reader-archive" title="Archive">▱</button><button class="icon-button" id="reader-delete" title="Delete">⌫</button><button class="icon-button" title="More">···</button></div></div><h2 class="reader-subject">${esc(m.subject)}</h2><div class="sender-row"><div class="contact-avatar" style="background:${m.color}">${m.initials}</div><div class="sender-details"><strong>${esc(m.sender)}</strong><span>${esc(m.email)} · to me</span></div><span class="message-date">${esc(m.date)}</span><button class="icon-button" title="Star" id="reader-star">${m.star?'★':'☆'}</button></div></div>${imageNotice}<div class="reader-body">${body}${attachments}</div><div class="reply-bar"><button class="reply-button primary" id="reply">↶ Reply</button><button class="reply-button" id="forward">↗ Forward</button></div>`;
|
||||
$('#reader-back').onclick=()=>{state.activeMessage=null;document.body.classList.remove('reader-open');$('#reader-pane').innerHTML=emptyReader();renderList()};
|
||||
$('#reader-back').onclick=returnToMessageList;
|
||||
$('#load-remote-images')?.addEventListener('click',()=>{
|
||||
$('#reader-pane').querySelectorAll('.reader-body img[data-remote-src]').forEach(image=>{
|
||||
const url=safeRemoteImageUrl(image.getAttribute('data-remote-src'));
|
||||
@@ -51,6 +66,9 @@ window.epMailShell = Object.freeze({
|
||||
get renderList() { return renderList; },
|
||||
get openMessage() { return openMessage; },
|
||||
get showCompose() { return showCompose; },
|
||||
getReaderListPosition() {
|
||||
return readerListPosition?.folder === state.activeFolder ? readerListPosition.scrollTop : 0;
|
||||
},
|
||||
emptyReader,
|
||||
wrapRenderList(decorator) { renderList = decorator(renderList); },
|
||||
wrapOpenMessage(decorator) { openMessage = decorator(openMessage); },
|
||||
|
||||
+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;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const CACHE='ep-mail-v20';
|
||||
const CACHE='ep-mail-v22';
|
||||
const FILES=['./','./index.html','./styles.css','./branding.css','./renderer-state.js','./mail-content-security.js','./renderer-mail-api.js','./app.js','./platform-bridge-loader.js','./platform-bridge.js','./mail-ui.js','./productivity.js','./snooze-ui.js','./account-ui.js','./dav-ui.js','./shortcuts.js','./attachments.js','./pgp-ui.js','./pgp-mail-ui.js','./manifest.webmanifest','./icon.svg','./logo-ep-daylight.svg','./logo-ep-wasteland.svg'];
|
||||
self.addEventListener('install',event=>event.waitUntil(caches.open(CACHE).then(cache=>cache.addAll(FILES)).then(()=>self.skipWaiting())));
|
||||
self.addEventListener('activate',event=>event.waitUntil(caches.keys().then(keys=>Promise.all(keys.filter(k=>k!==CACHE).map(k=>caches.delete(k)))).then(()=>self.clients.claim())));
|
||||
|
||||
Reference in New Issue
Block a user