231 lines
4.7 KiB
Go
231 lines
4.7 KiB
Go
// 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
|
|
}
|