feat(inbox): add force-refresh (#98)

Drew Smirnoff created

Change summary

main.go         | 12 +++++++++---
tui/inbox.go    |  5 +++++
tui/messages.go |  8 ++++++--
3 files changed, 20 insertions(+), 5 deletions(-)

Detailed changes

main.go 🔗

@@ -63,10 +63,10 @@ type mainModel struct {
 	config        *config.Config
 	emails        []fetcher.Email
 	emailsByAcct  map[string][]fetcher.Email
-	sentEmails   []fetcher.Email
-	sentByAcct   map[string][]fetcher.Email
+	sentEmails    []fetcher.Email
+	sentByAcct    map[string][]fetcher.Email
 	inbox         *tui.Inbox
-	sentInbox    *tui.Inbox
+	sentInbox     *tui.Inbox
 	width         int
 	height        int
 	err           error
@@ -263,6 +263,12 @@ func (m *mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 			refreshEmails(m.config, tui.MailboxInbox),
 		)
 
+	case tui.RequestRefreshMsg:
+		return m, tea.Batch(
+			func() tea.Msg { return tui.RefreshingEmailsMsg{Mailbox: msg.Mailbox} },
+			refreshEmails(m.config, msg.Mailbox),
+		)
+
 	case tui.EmailsRefreshedMsg:
 		if msg.Mailbox == tui.MailboxSent {
 			m.sentByAcct = msg.EmailsByAccount

tui/inbox.go 🔗

@@ -196,6 +196,7 @@ func (m *Inbox) updateList() {
 		bindings := []key.Binding{
 			key.NewBinding(key.WithKeys("d"), key.WithHelp("d", "delete")),
 			key.NewBinding(key.WithKeys("a"), key.WithHelp("a", "archive")),
+			key.NewBinding(key.WithKeys("r"), key.WithHelp("r", "refresh")),
 		}
 		if len(m.tabs) > 1 {
 			bindings = append(bindings,
@@ -301,6 +302,10 @@ func (m *Inbox) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 					return ArchiveEmailMsg{UID: selectedItem.uid, AccountID: selectedItem.accountID, Mailbox: m.mailbox}
 				}
 			}
+		case "r":
+			return m, func() tea.Msg {
+				return RequestRefreshMsg{Mailbox: m.mailbox}
+			}
 		case "enter":
 			selectedItem, ok := m.list.SelectedItem().(item)
 			if ok {

tui/messages.go 🔗

@@ -63,7 +63,6 @@ type GoToInboxMsg struct{}
 
 type GoToSentInboxMsg struct{}
 
-
 type GoToSendMsg struct {
 	To      string
 	Subject string
@@ -254,7 +253,7 @@ type CachedEmailsLoadedMsg struct {
 }
 
 // RefreshingEmailsMsg signals that a background refresh is in progress.
-type RefreshingEmailsMsg struct{
+type RefreshingEmailsMsg struct {
 	Mailbox MailboxKind
 }
 
@@ -263,3 +262,8 @@ type EmailsRefreshedMsg struct {
 	EmailsByAccount map[string][]fetcher.Email
 	Mailbox         MailboxKind
 }
+
+// RequestRefreshMsg signals a request to refresh emails from the server.
+type RequestRefreshMsg struct {
+	Mailbox MailboxKind
+}