From ed72a0db66a57e7bdede14d7eb27d8016520ea1f Mon Sep 17 00:00:00 2001 From: surtur Date: Mon, 30 May 2022 18:08:30 +0200 Subject: [PATCH] go: get latest comic on app start --- xkcdreader/app.go | 78 ++++++++++++++++++++++++++++++++++-------- xkcdreader/app_test.go | 50 +++++++++++++++++++++++++-- 2 files changed, 112 insertions(+), 16 deletions(-) diff --git a/xkcdreader/app.go b/xkcdreader/app.go index b8b5cff..57e8dbd 100644 --- a/xkcdreader/app.go +++ b/xkcdreader/app.go @@ -14,14 +14,19 @@ import ( "fyne.io/fyne/v2/canvas" "fyne.io/fyne/v2/container" "fyne.io/fyne/v2/layout" + "fyne.io/fyne/v2/storage" "fyne.io/fyne/v2/theme" "fyne.io/fyne/v2/widget" "git.dotya.ml/wanderer/go-xkcdreader/cmd" + "git.dotya.ml/wanderer/go-xkcdreader/xkcdreader/xkcd" ) const appGreeting = "welcome to go-xkcdreader" -var a fyne.App +var ( + a fyne.App + latestComic = xkcd.GetLatest() +) // RunApp performs sets up and runs the main application. func RunApp() { @@ -96,8 +101,6 @@ func makeTabs() *container.AppTabs { } func makeBrowseUI() *fyne.Container { - imgPath := "xkcdreader/assets/comic.png" - // container for the image and surrounding elements imgC := container.New( layout.NewHBoxLayout(), @@ -109,7 +112,7 @@ func makeBrowseUI() *fyne.Container { // TODO(me): dynamically replace placeholder text with image once // fetched... // widget.NewLabel("img placeholder"), - getImg(imgPath), + getImg(latestComic.Img, false), ), layout.NewSpacer(), widget.NewButtonWithIcon("", theme.NavigateNextIcon(), func() { @@ -139,23 +142,70 @@ func makeBrowseUI() *fyne.Container { return browseUI } -// get img from filesystem, resize it and return as *canvas.Image. -func getImg(imgPath string) *canvas.Image { - _, err := os.Stat(imgPath) - // properly handle error, perhaps panic?...don't panic, I know.. - if err != nil { - log.Println("failed to read file " + imgPath) - return canvas.NewImageFromFile("") +// getComicLatest fetches latest comic, updates latestComic if necessary. +func getComicLatest() { + comic := xkcd.GetLatest() + + if comic == nil { + // TODO(me): properly handle the situation (i.e. bail early) + log.Println("error getting latest comic") } - img := canvas.NewImageFromFile(imgPath) + // update latesComic, if necessary + if latestComic != comic { + latestComic = comic + } +} +// get img from filesystem, resize it and return as *canvas.Image. +func getImg(imgURI string, local bool) *canvas.Image { + if local { + _, err := os.Stat(imgURI) + // properly handle error, perhaps panic?...don't panic, I know.. + if err != nil { + log.Println("failed to read file " + imgURI) + return canvas.NewImageFromFile("") + } + + img := canvas.NewImageFromFile(imgURI) + + img.SetMinSize( + fyne.Size{ + Height: 383, + Width: 273, + }, + ) + + img.ScaleMode = canvas.ImageScaleSmooth + img.FillMode = canvas.ImageFillContain + + return img + } + + preImg, err := storage.ParseURI(imgURI) + if err != nil { + log.Printf("error parsing comic URI: %q", err.Error()) + } + + // get the actual image + img := canvas.NewImageFromURI(preImg) + + if img.Resource == nil { + log.Println("error fetching the image file of the latest comic") + } + + // set basic img properties img.SetMinSize( fyne.Size{ - Height: 383, - Width: 273, + // subject to change, this is what I consider to be sane defaults + // atm... + Height: 650, + Width: 750, }, ) + img.ScaleMode = canvas.ImageScaleSmooth + img.FillMode = canvas.ImageFillContain + return img } diff --git a/xkcdreader/app_test.go b/xkcdreader/app_test.go index 3dd0289..c8c6e4f 100644 --- a/xkcdreader/app_test.go +++ b/xkcdreader/app_test.go @@ -131,7 +131,7 @@ func TestGetImgMinSize(t *testing.T) { w: 273.0, } - got := getImg(imgPath) + got := getImg(imgPath, true) got.SetMinSize( fyne.Size{ Height: 383.0, @@ -155,7 +155,7 @@ func TestGetImg(t *testing.T) { // this is relative to the test file imgPath := "assets/comic.png" - got := getImg(imgPath) + got := getImg(imgPath, true) got.SetMinSize( fyne.Size{ Height: 383.0, @@ -176,3 +176,49 @@ func TestGetImg(t *testing.T) { t.Fatal("Images differ") } } + +func TestGetComicLatest(t *testing.T) { + want := latestComic + + // attempt comic refresh + getComicLatest() + + got := latestComic + + if want.Img != got.Img || + want.Alt != got.Alt || + want.Day != got.Day || + want.Link != got.Link || + want.Month != got.Month || + want.News != got.News || + want.SafeTitle != got.SafeTitle || + want.Title != got.Title || + want.Transcript != got.Transcript || + want.Year != got.Year { + t.Error("latestComic contains stale comic, comic data differ") + } +} + +func TestGetComicLatest_MinSize(t *testing.T) { + want := struct { + height float32 + width float32 + }{ + height: 650.0, + width: 750.0, + } + + getComicLatest() + + got := getImg(latestComic.Img, false) + + if want.height != got.MinSize().Height { + t.Errorf("Latest comic height differs, want: %f, got: %f", + want.height, got.MinSize().Height) + } + + if want.width != got.MinSize().Width { + t.Errorf("Latest comic width differs, want: %f, got: %f", + want.width, got.MinSize().Width) + } +}