go: get latest comic on app start
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
34936a5f72
commit
ed72a0db66
@ -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,16 +142,32 @@ func makeBrowseUI() *fyne.Container {
|
||||
return browseUI
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
|
||||
// update latesComic, if necessary
|
||||
if latestComic != comic {
|
||||
latestComic = comic
|
||||
}
|
||||
}
|
||||
|
||||
// get img from filesystem, resize it and return as *canvas.Image.
|
||||
func getImg(imgPath string) *canvas.Image {
|
||||
_, err := os.Stat(imgPath)
|
||||
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 " + imgPath)
|
||||
log.Println("failed to read file " + imgURI)
|
||||
return canvas.NewImageFromFile("")
|
||||
}
|
||||
|
||||
img := canvas.NewImageFromFile(imgPath)
|
||||
img := canvas.NewImageFromFile(imgURI)
|
||||
|
||||
img.SetMinSize(
|
||||
fyne.Size{
|
||||
@ -157,5 +176,36 @@ func getImg(imgPath string) *canvas.Image {
|
||||
},
|
||||
)
|
||||
|
||||
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{
|
||||
// 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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user