Compare commits

..

1 Commits

Author SHA1 Message Date
0fc49afc6b
go: add current comic logic
All checks were successful
continuous-integration/drone/push Build is passing
2022-07-14 22:17:26 +02:00
3 changed files with 328 additions and 27 deletions

@ -4,7 +4,6 @@
package xkcdreader package xkcdreader
import ( import (
"fmt"
"log" "log"
"os" "os"
@ -12,6 +11,7 @@ import (
"fyne.io/fyne/v2/app" "fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas" "fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container" "fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/layout" "fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/storage" "fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/theme" "fyne.io/fyne/v2/theme"
@ -25,6 +25,8 @@ const appGreeting = "welcome to go-xkcdreader"
var ( var (
a fyne.App a fyne.App
latestComic = xkcd.GetLatest() latestComic = xkcd.GetLatest()
currentComic = latestComic
cc xkcdcomic
) )
// RunApp performs sets up and runs the main application. // RunApp performs sets up and runs the main application.
@ -100,60 +102,98 @@ func makeTabs() *container.AppTabs {
} }
func makeBrowseUI() *fyne.Container { func makeBrowseUI() *fyne.Container {
err := setCurrentComicBinding()
if err != nil {
log.Println(err)
}
cc.pic = getImg(currentComic.Img, false)
// container for the image and surrounding elements // container for the image and surrounding elements
imgC := container.New( imgC := container.New(
layout.NewHBoxLayout(), layout.NewHBoxLayout(),
widget.NewButtonWithIcon("", theme.NavigateBackIcon(), func() { widget.NewButtonWithIcon("", theme.NavigateBackIcon(), func() {
log.Println("Previous comic") log.Println("Previous comic")
showPrevious()
cc.refreshComicPic(getImg(currentComic.Img, false))
}), }),
layout.NewSpacer(), layout.NewSpacer(),
container.NewCenter( container.NewCenter(
// TODO(me): dynamically replace placeholder text with image once // TODO(me): dynamically replace placeholder text with image once
// fetched... // fetched...
// widget.NewLabel("img placeholder"), // widget.NewLabel("img placeholder"),
getImg(latestComic.Img, false), cc.pic,
), ),
layout.NewSpacer(), layout.NewSpacer(),
widget.NewButtonWithIcon("", theme.NavigateNextIcon(), func() { widget.NewButtonWithIcon("", theme.NavigateNextIcon(), func() {
log.Println("Next comic") log.Println("Next comic")
showNext()
}), }),
) )
year := widget.NewLabelWithData(comicCurrent.year)
month := widget.NewLabelWithData(comicCurrent.month)
day := widget.NewLabelWithData(comicCurrent.day)
title := widget.NewLabelWithData(comicCurrent.title)
num := widget.NewLabelWithData(
binding.IntToString(comicCurrent.num),
)
altText := widget.NewLabelWithData(comicCurrent.alt)
title.TextStyle.Bold = true
num.TextStyle.Monospace = true
altText.TextStyle.Italic = true
altText.Wrapping = fyne.TextTruncate
altText.Alignment = fyne.TextAlignCenter
browseUI := container.New( browseUI := container.New(
layout.NewVBoxLayout(), layout.NewVBoxLayout(),
layout.NewSpacer(), // layout.NewSpacer(),
container.NewCenter( container.NewCenter(
container.NewVBox( container.NewVBox(
&widget.Label{ ///&widget.Label{
Text: "published on " + /// Text: "published on " +
fmt.Sprint( /// fmt.Sprint(
latestComic.Year, "-", /// currentComic.Year, "-",
latestComic.Month, "-", /// currentComic.Month, "-",
latestComic.Day, /// currentComic.Day,
), /// ),
TextStyle: fyne.TextStyle{Italic: true}, /// TextStyle: fyne.TextStyle{Italic: true},
}, ///},
container.NewHBox( container.NewHBox(
&widget.Label{ year, month, day,
Text: latestComic.Title, ),
TextStyle: fyne.TextStyle{Bold: true}, container.NewHBox(
}, ///&widget.Label{
&widget.Label{ /// Text: currentComic.Title,
Text: "(#" + fmt.Sprint(latestComic.Num) + ")", /// TextStyle: fyne.TextStyle{Bold: true},
TextStyle: fyne.TextStyle{Monospace: true}, ///},
}, ///&widget.Label{
/// Text: "(#" + fmt.Sprint(currentComic.Num) + ")",
/// TextStyle: fyne.TextStyle{Monospace: true},
///},
title,
num,
), ),
), ),
), ),
imgC, imgC,
layout.NewSpacer(), // layout.NewSpacer(),
container.NewCenter( container.NewCenter(
// comic "alt text" // comic "alt text"
&widget.Label{ ///&widget.Label{
Text: latestComic.Alt, /// Text: currentComic.Alt,
TextStyle: fyne.TextStyle{Italic: true}, /// TextStyle: fyne.TextStyle{Italic: true},
}, ///},
// container.NewCenter(
// container.NewAdaptiveGrid(
// 1,
// layout.NewSpacer(),
altText,
layout.NewSpacer(),
// ),
), ),
layout.NewSpacer(),
) )
return browseUI return browseUI
@ -207,6 +247,8 @@ func getImg(imgURI string, local bool) *canvas.Image {
// get the actual image // get the actual image
img := canvas.NewImageFromURI(preImg) img := canvas.NewImageFromURI(preImg)
log.Println("getImg: img name: ", img.Resource.Name())
if img.Resource == nil { if img.Resource == nil {
log.Println("error fetching the image file of the latest comic") log.Println("error fetching the image file of the latest comic")
} }

230
xkcdreader/comic.go Normal file

@ -0,0 +1,230 @@
// Copyright 2022 wanderer <a_mirre at utb dot cz>
// SPDX-License-Identifier: GPL-3.0-or-later
package xkcdreader
import (
"log"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/data/binding"
x "git.dotya.ml/wanderer/go-xkcdreader/xkcdreader/xkcd"
)
type xkcdcomic struct {
pic *canvas.Image
}
var comicCurrent = struct {
num binding.Int `default:"0"`
alt binding.String
day binding.String
img binding.String
link binding.String
month binding.String
news binding.String
safeTitle binding.String
title binding.String
transcript binding.String
year binding.String
}{
num: binding.NewInt(),
alt: binding.NewString(),
day: binding.NewString(),
img: binding.NewString(),
link: binding.NewString(),
month: binding.NewString(),
news: binding.NewString(),
safeTitle: binding.NewString(),
title: binding.NewString(),
transcript: binding.NewString(),
year: binding.NewString(),
}
// showPrevious loads and shows a previous comic.
// nolint: dupl
func showPrevious() {
previous := x.GetPrevious(currentComic.Num)
if previous != nil {
log.Printf("current num pre: %d", currentComic.Num)
if err := comicCurrent.num.Set(previous.Num); err != nil {
log.Println(err)
}
if err := comicCurrent.alt.Set(previous.Alt); err != nil {
log.Println(err)
}
if err := comicCurrent.day.Set(previous.Day); err != nil {
log.Println(err)
}
if err := comicCurrent.img.Set(previous.Img); err != nil {
log.Println(err)
}
if err := comicCurrent.link.Set(previous.Link); err != nil {
log.Println(err)
}
if err := comicCurrent.month.Set(previous.Month); err != nil {
log.Println(err)
}
if err := comicCurrent.news.Set(previous.News); err != nil {
log.Println(err)
}
if err := comicCurrent.safeTitle.Set(previous.SafeTitle); err != nil {
log.Println(err)
}
if err := comicCurrent.title.Set(previous.Title); err != nil {
log.Println(err)
}
if err := comicCurrent.transcript.Set(previous.Transcript); err != nil {
log.Println(err)
}
if err := comicCurrent.year.Set(previous.Year); err != nil {
log.Println(err)
}
cc.refreshComicPic(getImg(previous.Img, false))
currentComic = previous
log.Printf("current num post: %d", currentComic.Num)
} else {
log.Println("no previous comic")
}
}
// showNext loads and shows a next comic.
// nolint: dupl
func showNext() {
next := x.GetNext(currentComic.Num)
if next != nil {
log.Printf("current num pre: %d", currentComic.Num)
if err := comicCurrent.num.Set(next.Num); err != nil {
log.Println(err)
}
if err := comicCurrent.alt.Set(next.Alt); err != nil {
log.Println(err)
}
if err := comicCurrent.day.Set(next.Day); err != nil {
log.Println(err)
}
if err := comicCurrent.img.Set(next.Img); err != nil {
log.Println(err)
}
if err := comicCurrent.link.Set(next.Link); err != nil {
log.Println(err)
}
if err := comicCurrent.month.Set(next.Month); err != nil {
log.Println(err)
}
if err := comicCurrent.news.Set(next.News); err != nil {
log.Println(err)
}
if err := comicCurrent.safeTitle.Set(next.SafeTitle); err != nil {
log.Println(err)
}
if err := comicCurrent.title.Set(next.Title); err != nil {
log.Println(err)
}
if err := comicCurrent.transcript.Set(next.Transcript); err != nil {
log.Println(err)
}
if err := comicCurrent.year.Set(next.Year); err != nil {
log.Println(err)
}
cc.refreshComicPic(getImg(next.Img, false))
currentComic = next
log.Printf("current num post: %d", currentComic.Num)
} else {
log.Println("no next comic")
}
}
func (xc *xkcdcomic) refreshComicPic(pic *canvas.Image) {
xc.pic.Resource = pic.Resource
xc.pic.Refresh()
}
func setCurrentComicBinding() error {
err := comicCurrent.num.Set(currentComic.Num)
if err != nil {
return err
}
err = comicCurrent.alt.Set(currentComic.Alt)
if err != nil {
return err
}
err = comicCurrent.day.Set(currentComic.Day)
if err != nil {
return err
}
err = comicCurrent.img.Set(currentComic.Img)
if err != nil {
return err
}
err = comicCurrent.link.Set(currentComic.Link)
if err != nil {
return err
}
err = comicCurrent.month.Set(currentComic.Month)
if err != nil {
return err
}
err = comicCurrent.news.Set(currentComic.News)
if err != nil {
return err
}
err = comicCurrent.safeTitle.Set(currentComic.SafeTitle)
if err != nil {
return err
}
err = comicCurrent.title.Set(currentComic.Title)
if err != nil {
return err
}
err = comicCurrent.transcript.Set(currentComic.Transcript)
if err != nil {
return err
}
err = comicCurrent.year.Set(currentComic.Year)
if err != nil {
return err
}
return nil
}

@ -35,3 +35,32 @@ func GetComic(num int) *xkcd.Comic {
return comic return comic
} }
// GetPrevious returns a previous comic relative to the one provided.
func GetPrevious(current int) *xkcd.Comic {
if current == 1 {
log.Printf("error: there is no previous comic available, requested: '%d'",
current-1)
return GetComic(1)
}
previous := GetComic(current - 1)
if previous == nil {
return GetComic(current)
}
return previous
}
// GetNext returns a next comic relative to the one currently provided.
func GetNext(current int) *xkcd.Comic {
next := GetComic(current + 1)
if next == nil {
log.Printf("error: there is no next comic available, requested: '%d'",
current+1)
}
return next
}