go: get latest comic on app start
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2022-05-30 18:08:30 +02:00
parent 34936a5f72
commit ed72a0db66
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
2 changed files with 112 additions and 16 deletions

View File

@ -14,14 +14,19 @@
"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
}

View File

@ -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)
}
}