Skip to content

Commit

Permalink
feat: refreshing now works as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
TypicalAM committed Aug 5, 2023
1 parent b448557 commit 0ce2ea7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
16 changes: 8 additions & 8 deletions internal/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ func (b Backend) FetchFeeds(catname string) tea.Cmd {
}

// FetchArticles gets the articles from a feed.
func (b Backend) FetchArticles(feedName string) tea.Cmd {
func (b Backend) FetchArticles(feedname string, refresh bool) tea.Cmd {
return func() tea.Msg {
url, err := b.Rss.GetFeedURL(feedName)
url, err := b.Rss.GetFeedURL(feedname)
if err != nil {
return FetchErrorMsg{err, "Error while trying to get the article url"}
}

items, err := b.Cache.GetArticles(url)
items, err := b.Cache.GetArticles(url, refresh)
if err != nil {
return FetchErrorMsg{err, "Error while fetching the article"}
}
Expand All @@ -101,14 +101,14 @@ func (b Backend) FetchArticles(feedName string) tea.Cmd {
}

// FetchAllArticles gets all the articles from all the feeds.
func (b Backend) FetchAllArticles(_ string) tea.Cmd {
func (b Backend) FetchAllArticles(_ string, refresh bool) tea.Cmd {
return func() tea.Msg {
return b.articlesToSuccessMsg(b.Cache.GetArticlesBulk(b.Rss.GetAllURLs()))
return b.articlesToSuccessMsg(b.Cache.GetArticlesBulk(b.Rss.GetAllURLs(), refresh))
}
}

// FetchDownloaded gets the downloaded articles.
func (b Backend) FetchDownloadedArticles(_ string) tea.Cmd {
func (b Backend) FetchDownloadedArticles(_ string, _ bool) tea.Cmd {
return func() tea.Msg {
return b.articlesToSuccessMsg(b.Cache.GetDownloaded())
}
Expand Down Expand Up @@ -189,7 +189,7 @@ func (b Backend) articlesToSuccessMsg(items cache.SortableArticles) FetchArticle
func (b Backend) indexToItem(feedName string, index int) (*gofeed.Item, error) {
switch feedName {
case rss.AllFeedsName:
return &b.Cache.GetArticlesBulk(rss.Default.GetAllURLs())[index], nil
return &b.Cache.GetArticlesBulk(rss.Default.GetAllURLs(), false)[index], nil
case rss.DownloadedFeedsName:
return &b.Cache.GetDownloaded()[index], nil
default:
Expand All @@ -198,7 +198,7 @@ func (b Backend) indexToItem(feedName string, index int) (*gofeed.Item, error) {
return nil, errors.New("getting the article url")
}

items, err := b.Cache.GetArticles(url)
items, err := b.Cache.GetArticles(url, false)
if err != nil {
return nil, errors.New("fetching the article")
}
Expand Down
11 changes: 7 additions & 4 deletions internal/backend/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ func (c *Cache) Save() error {
}

// GetArticles returns an article list using the cache if possible
func (c *Cache) GetArticles(url string) (SortableArticles, error) {
func (c *Cache) GetArticles(url string, ignoreCache bool) (SortableArticles, error) {
log.Println("Getting articles for", url, " from cache: ", !ignoreCache)

// Delete entry if expired
if item, ok := c.Content[url]; ok {
if item, ok := c.Content[url]; ok && !ignoreCache {
if item.Expire.After(time.Now()) {
return item.Articles, nil
}
Expand Down Expand Up @@ -171,11 +173,11 @@ func (c *Cache) GetArticles(url string) (SortableArticles, error) {
}

// GetArticlesBulk returns a sorted list of articles from all the given urls, ignoring any errors
func (c *Cache) GetArticlesBulk(urls []string) SortableArticles {
func (c *Cache) GetArticlesBulk(urls []string, ignoreCache bool) SortableArticles {
var result SortableArticles

for _, url := range urls {
if items, err := c.GetArticles(url); err == nil {
if items, err := c.GetArticles(url, ignoreCache); err == nil {
result = append(result, items...)
}
}
Expand Down Expand Up @@ -207,6 +209,7 @@ func (c *Cache) RemoveFromDownloaded(index int) error {

// fetchArticles fetches articles from the internet and returns them
func fetchArticles(url string) (SortableArticles, error) {
log.Println("Fetching articles from", url)
feed, err := parseFeed(url)
if err != nil {
return nil, err
Expand Down
5 changes: 4 additions & 1 deletion internal/backend/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import (
)

// Fetcher fetches the data, it is used by tabs to query data.
type Fetcher func(string) tea.Cmd
type Fetcher func(feedname string) tea.Cmd

// ArticleFetcher fetches the article data, it is used by tabs to query data.
type ArticleFetcher func(feedname string, refresh bool) tea.Cmd

// FetchSuccessMsg is sent on fetch success.
type FetchSuccessMsg struct{ Items []list.Item }
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/browser/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func (m Model) deleteItem(msg backend.DeleteItemMsg) (tea.Model, tea.Cmd) {
}

case feed.Model:
cmd = m.backend.FetchDownloadedArticles("")
cmd = m.backend.FetchDownloadedArticles("", false)
if msg.Sender.Title() == rss.DownloadedFeedsName {
index, err := strconv.Atoi(msg.ItemName)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/ui/tab/feed/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
// Model contains the state of this tab
type Model struct {
list list.Model
fetcher backend.Fetcher
fetcher backend.ArticleFetcher
colorTr *glamour.TermRenderer
noColorTr *glamour.TermRenderer
colors *theme.Colors
Expand All @@ -44,7 +44,7 @@ type Model struct {
}

// New creates a new feed tab with sensible defaults
func New(colors *theme.Colors, width, height int, title string, fetcher backend.Fetcher) Model {
func New(colors *theme.Colors, width, height int, title string, fetcher backend.ArticleFetcher) Model {
log.Println("Creating new feed tab with title", title)
spin := spinner.New()
spin.Spinner = spinner.Points
Expand Down Expand Up @@ -96,7 +96,7 @@ func (m Model) SetSize(width, height int) tab.Tab {

// Init initializes the tab
func (m Model) Init() tea.Cmd {
return tea.Batch(m.fetcher(m.title), m.spinner.Tick)
return tea.Batch(m.spinner.Tick, m.fetcher(m.title, false))
}

// Update the variables of the tab
Expand Down Expand Up @@ -165,7 +165,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.loaded = false
m.viewportFocused = false

return m, tea.Batch(m.fetcher(m.title), m.spinner.Tick)
return m, tea.Batch(m.spinner.Tick, m.fetcher(m.title, true))

case key.Matches(msg, m.keymap.SaveArticle):
return m, backend.DownloadItem(m.title, m.list.Index())
Expand Down

0 comments on commit 0ce2ea7

Please sign in to comment.