Skip to content

Commit

Permalink
Cache thumbnails for improved performance (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
zMoooooritz committed Dec 27, 2023
1 parent 14fd3fa commit 2b64809
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions pkg/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package tui

import (
"fmt"
"image"
"strconv"

"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
Expand Down Expand Up @@ -39,6 +41,7 @@ type Model struct {
reader Reader
imageViewer ImageViewer
spinner spinner.Model
thumbCache map[string]image.Image
width int
height int
}
Expand Down Expand Up @@ -68,6 +71,7 @@ func InitialModel(c config.Configuration) Model {
reader: NewReader(style),
imageViewer: NewImageViewer(style),
spinner: NewDotSpinner(),
thumbCache: make(map[string]image.Image),
width: 0,
height: 0,
}
Expand Down Expand Up @@ -119,11 +123,17 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, m.keymap.left):
m.setFocus(false)
case key.Matches(msg, m.keymap.next):
m.selector.NextList()
m.setFocus(false)
if m.selector.IsFocused() {
m.selector.NextList()
} else {
m.toggelViewer(true)
}
case key.Matches(msg, m.keymap.prev):
m.selector.PrevList()
m.setFocus(false)
if m.selector.IsFocused() {
m.selector.PrevList()
} else {
m.toggelViewer(true)
}
case key.Matches(msg, m.keymap.full):
if m.reader.IsActive() {
m.reader.SetFullScreen(!m.reader.IsFullScreen())
Expand All @@ -144,12 +154,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.reader.GotoBottom()
}
case key.Matches(msg, m.keymap.image):
if m.selector.IsFocused() {
m.reader.SetActive(!m.reader.IsActive())
m.imageViewer.SetActive(!m.imageViewer.IsActive())
m.updateDisplayedArticle()
// m.updateSizes(m.width, m.height)
}
m.toggelViewer(!m.selector.IsFocused())
case key.Matches(msg, m.keymap.open):
article := m.selector.GetSelectedArticle()
m.opener.OpenUrl(config.TypeHTML, article.URL)
Expand Down Expand Up @@ -205,6 +210,16 @@ func (m *Model) setFocus(onViewer bool) {
m.updateDisplayedArticle()
}

func (m *Model) toggelViewer(setFocus bool) {
m.reader.SetActive(!m.reader.IsActive())
m.imageViewer.SetActive(!m.imageViewer.IsActive())
if setFocus {
m.reader.SetFocused(!m.reader.IsFocused())
m.imageViewer.SetFocused(!m.imageViewer.IsFocused())
}
m.updateDisplayedArticle()
}

func (m *Model) updateDisplayedArticle() {
article := m.selector.GetSelectedArticle()
if m.reader.isActive {
Expand All @@ -213,9 +228,15 @@ func (m *Model) updateDisplayedArticle() {
m.reader.SetHeaderContent(article.Topline, article.Date)
}
if m.imageViewer.IsActive() {
image, err := http.LoadImage(article.Image.ImageURLs.RectSmall)
if err != nil {
return
thumbCacheKey := article.Topline + strconv.FormatInt(article.Date.Unix(), 10)
image, ok := m.thumbCache[thumbCacheKey]
if !ok {
var err error
image, err = http.LoadImage(article.Image.ImageURLs.RectSmall)
if err != nil {
return
}
m.thumbCache[thumbCacheKey] = image
}
m.imageViewer.SetImage(image)
m.imageViewer.SetHeaderContent(article.Topline, article.Date)
Expand Down

0 comments on commit 2b64809

Please sign in to comment.