Skip to content

Commit

Permalink
gui: don't cache library between runs
Browse files Browse the repository at this point in the history
Signed-off-by: Jesse Stuart <email@jessestuart.ca>
  • Loading branch information
jvatic committed Jan 29, 2021
1 parent 2df2cb4 commit b204f66
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 97 deletions.
21 changes: 0 additions & 21 deletions gui/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,6 @@ func (c *Controller) Run(w fyne.Window) {

// main logic

// {
// // TODO: remove this block
// books, err := LoadLibrary()
// if err == nil {
// sort.Sort(audible.ByTitle(books))
// stateCh := NewLibState(&audible.Client{}, []byte{}, books)
// if err := Library(w, c.render, stateCh); err != nil {
// ShowFatalErrorDialog(c.render, err)
// return
// }
// } else {
// log.Warn(err)
// }
// }

client, err := signin.Run(c.render)
if err != nil {
ShowFatalErrorDialog(c.render, err)
Expand All @@ -79,12 +64,6 @@ func (c *Controller) Run(w fyne.Window) {
return
}
sort.Sort(audible.ByTitle(books))
{
// TODO: remove this block
if err := library.SaveLibrary(books); err != nil {
log.Warn(err)
}
}

stateCh := library.NewState(client, activationBytes, books)
if err := library.Run(w, c.render, stateCh); err != nil {
Expand Down
76 changes: 0 additions & 76 deletions gui/library/library.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package library

import (
"encoding/json"
"fmt"
"image"
"image/color"
"image/jpeg"
"net/http"
"os"
"path/filepath"
Expand All @@ -25,7 +22,6 @@ import (
"github.com/jvatic/audible-downloader/audible"
"github.com/jvatic/audible-downloader/gui/components"
"github.com/jvatic/audible-downloader/internal/common"
"github.com/jvatic/audible-downloader/internal/config"
"github.com/jvatic/audible-downloader/internal/downloader"
"github.com/jvatic/audible-downloader/internal/errgroup"
"github.com/jvatic/audible-downloader/internal/ffmpeg"
Expand Down Expand Up @@ -862,75 +858,3 @@ func Run(w fyne.Window, renderQueue chan func(w fyne.Window), actionQueue chan<-
<-done
return nil
}

func SaveLibrary(books []*audible.Book) error {
path := filepath.Join(config.Dir(), "books.json")
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return err
}
defer file.Close()
if err := json.NewEncoder(file).Encode(books); err != nil {
return fmt.Errorf("error encoding library to json: %s", err)
}
var wg sync.WaitGroup
for i, b := range books {
wg.Add(1)
go func(i int, img image.Image) {
defer wg.Done()
if err := SaveLibraryThumb(i, img); err != nil {
log.Errorf("error saving thumb %02d: %s", i, err)
}
}(i, b.ThumbImage)
}
wg.Wait()
return nil
}

func SaveLibraryThumb(i int, img image.Image) error {
path := filepath.Join(config.Dir(), fmt.Sprintf("%02d.jpeg", i))
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return err
}
defer file.Close()
return jpeg.Encode(file, img, &jpeg.Options{Quality: 100})
}

func LoadLibrary() ([]*audible.Book, error) {
path := filepath.Join(config.Dir(), "books.json")
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var books []*audible.Book
if err := json.NewDecoder(file).Decode(&books); err != nil {
return nil, fmt.Errorf("error decoding library from json: %s", err)
}
var wg sync.WaitGroup
for i, b := range books {
wg.Add(1)
go func(i int, b *audible.Book) {
defer wg.Done()
img, err := LoadLibraryThumb(i)
if err != nil {
log.Errorf("error decoding library thumb %02d: %s", i, err)
return
}
b.ThumbImage = img
}(i, b)
}
wg.Wait()
return books, nil
}

func LoadLibraryThumb(i int) (image.Image, error) {
path := filepath.Join(config.Dir(), fmt.Sprintf("%02d.jpeg", i))
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
return jpeg.Decode(file)
}

0 comments on commit b204f66

Please sign in to comment.