Skip to content

Commit

Permalink
refactor: cleaner error popup handling
Browse files Browse the repository at this point in the history
  • Loading branch information
TypicalAM committed Apr 19, 2024
1 parent f94cb3c commit ee06e3f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
49 changes: 27 additions & 22 deletions internal/ui/browser/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,17 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
log.Printf("Error fetching data in tab %d: %v \n", m.activeTab, msg.Err)
updated, _ := m.tabs[m.activeTab].Update(msg)
m.tabs[m.activeTab] = updated.(tab.Tab)
return m.displayError(fmt.Sprintf("%s: %s", msg.Description, unwrapErrs(msg.Err)))
errMsg := fmt.Sprintf("%s: %s", msg.Description, unwrapErrs(msg.Err))
return m.showPopup(lollypops.NewError(m.style.colors, errMsg))

case overview.ChosenCategoryMsg:
m.popup = nil
m.keymap.SetEnabled(true)

if msg.IsEdit {
if err := m.backend.Rss.UpdateCategory(msg.OldName, msg.Name, msg.Desc); err != nil {
m, cmd := m.displayError(fmt.Sprintf("Error updating category: %s", unwrapErrs(err)))
errMsg := fmt.Sprintf("Error updating category: %s", unwrapErrs(err))
m, cmd := m.showPopup(lollypops.NewError(m.style.colors, errMsg))
return m, tea.Sequence(cmd, m.backend.FetchCategories(""))
}

Expand All @@ -134,7 +136,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

if err := m.backend.Rss.AddCategory(msg.Name, msg.Desc); err != nil {
m, cmd := m.displayError(fmt.Sprintf("Error adding category: %s", unwrapErrs(err)))
errMsg := fmt.Sprintf("Error adding category: %s", unwrapErrs(err))
m, cmd := m.showPopup(lollypops.NewError(m.style.colors, errMsg))
return m, tea.Sequence(cmd, m.backend.FetchCategories(""))
}

Expand All @@ -147,18 +150,22 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

if msg.IsEdit {
if err := m.backend.Rss.UpdateFeed(msg.Parent, msg.OldName, msg.Name, msg.URL); err != nil {
m.displayError(fmt.Sprintf("Error updating feed: %s", unwrapErrs(err)))
} else {
m.msg = fmt.Sprintf("Updated feed %s", msg.Name)
}
} else {
if err := m.backend.Rss.AddFeed(msg.Parent, msg.Name, msg.URL); err != nil {
m.displayError(fmt.Sprintf("Error adding feed: %s", unwrapErrs(err)))
} else {
m.msg = fmt.Sprintf("Added feed %s", msg.Name)
errMsg := fmt.Sprintf("Error updating feed: %s", unwrapErrs(err))
m, cmd := m.showPopup(lollypops.NewError(m.style.colors, errMsg))
return m, tea.Batch(cmd, m.backend.FetchFeeds(msg.Parent))
}

m.msg = fmt.Sprintf("Updated feed %s", msg.Name)
return m, m.backend.FetchFeeds(msg.Parent)
}

if err := m.backend.Rss.AddFeed(msg.Parent, msg.Name, msg.URL); err != nil {
errMsg := fmt.Sprintf("Error adding feed: %s", unwrapErrs(err))
m, cmd := m.showPopup(lollypops.NewError(m.style.colors, errMsg))
return m, tea.Batch(cmd, m.backend.FetchFeeds(msg.Parent))
}

m.msg = fmt.Sprintf("Added feed %s", msg.Name)
return m, m.backend.FetchFeeds(msg.Parent)

case tab.NewTabMsg:
Expand Down Expand Up @@ -408,25 +415,29 @@ func (m Model) deleteItem(msg backend.DeleteItemMsg) (tea.Model, tea.Cmd) {
case overview.Model:
cmd = m.backend.FetchCategories("")
if err := m.backend.Rss.RemoveCategory(msg.ItemName); err != nil {
m.displayError(fmt.Sprintf("Error deleting category %s: %s", msg.ItemName, unwrapErrs(err)))
errMsg := fmt.Sprintf("Error deleting category %s: %s", msg.ItemName, unwrapErrs(err))
return m.showPopup(lollypops.NewError(m.style.colors, errMsg))
}

case category.Model:
cmd = m.backend.FetchFeeds(m.tabs[m.activeTab].Title())
if err := m.backend.Rss.RemoveFeed(m.tabs[m.activeTab].Title(), msg.ItemName); err != nil {
m.displayError(fmt.Sprintf("Error deleting feed %s: %s", msg.ItemName, unwrapErrs(err)))
errMsg := fmt.Sprintf("Error deleting feed %s: %s", msg.ItemName, unwrapErrs(err))
return m.showPopup(lollypops.NewError(m.style.colors, errMsg))
}

case feed.Model:
cmd = m.backend.FetchDownloadedArticles("", false)
if msg.Sender.Title() == rss.DownloadedFeedsName {
index, err := strconv.Atoi(msg.ItemName)
if err != nil {
m.displayError(fmt.Sprintf("Error deleting download %s: %s", msg.ItemName, unwrapErrs(err)))
errMsg := fmt.Sprintf("Error deleting download %s: %s", msg.ItemName, unwrapErrs(err))
return m.showPopup(lollypops.NewError(m.style.colors, errMsg))
}

if err := m.backend.Cache.RemoveFromDownloaded(index); err != nil {
m.displayError(fmt.Sprintf("Error deleting download %s: %s", msg.ItemName, unwrapErrs(err)))
errMsg := fmt.Sprintf("Error deleting download %s: %s", msg.ItemName, unwrapErrs(err))
return m.showPopup(lollypops.NewError(m.style.colors, errMsg))
}
}
}
Expand Down Expand Up @@ -457,12 +468,6 @@ func (m Model) toggleOffline() (tea.Model, tea.Cmd) {
return m, nil
}

// displayError shows a popup which displays the error
func (m Model) displayError(msg string) (Model, tea.Cmd) {
m.msg = msg
return m.showPopup(lollypops.NewError(m.style.colors, msg))
}

// showPopup tells the model to show the popup
func (m Model) showPopup(window popup.Window) (Model, tea.Cmd) {
m.popup = nil
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/tab/overview/popup.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (p Popup) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd

if msg, ok := msg.(tea.KeyMsg); ok {
if p.reserved && msg.String() == "enter" {
if p.reserved {
return p, nil
}

Expand Down

0 comments on commit ee06e3f

Please sign in to comment.