fix race condition with async image fetches
This commit is contained in:
+8
-1
@@ -1,6 +1,7 @@
|
|||||||
package ui
|
package ui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"image"
|
"image"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
@@ -39,9 +40,15 @@ type AlbumCard struct {
|
|||||||
albumID string
|
albumID string
|
||||||
title *widget.Label
|
title *widget.Label
|
||||||
artist *widget.Label
|
artist *widget.Label
|
||||||
Cover *TappableImage
|
|
||||||
container *fyne.Container
|
container *fyne.Container
|
||||||
|
|
||||||
|
// updated by AlbumGrid
|
||||||
|
Cover *TappableImage
|
||||||
|
|
||||||
|
// these fields are used by AlbumGrid to track async update tasks
|
||||||
|
PrevAlbumID string
|
||||||
|
ImgLoadCancel context.CancelFunc
|
||||||
|
|
||||||
OnTapped func()
|
OnTapped func()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+23
-4
@@ -1,6 +1,7 @@
|
|||||||
package ui
|
package ui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"gomuse/backend"
|
"gomuse/backend"
|
||||||
|
|
||||||
"fyne.io/fyne/v2"
|
"fyne.io/fyne/v2"
|
||||||
@@ -41,15 +42,33 @@ func NewAlbumGrid(iter backend.AlbumIterator, pm *backend.PlaybackManager, im *b
|
|||||||
// update func
|
// update func
|
||||||
func(itemID int, obj fyne.CanvasObject) {
|
func(itemID int, obj fyne.CanvasObject) {
|
||||||
ac := obj.(*AlbumCard)
|
ac := obj.(*AlbumCard)
|
||||||
ac.Update(ag.albums[itemID])
|
album := ag.albums[itemID]
|
||||||
|
if ac.PrevAlbumID == album.ID {
|
||||||
|
// nothing to do
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ac.Update(album)
|
||||||
|
ac.PrevAlbumID = album.ID
|
||||||
// TODO: set image to a placeholder before spinning off async fetch
|
// TODO: set image to a placeholder before spinning off async fetch
|
||||||
go func() {
|
// cancel any previous image fetch
|
||||||
i, err := im.GetAlbumThumbnail(ag.albums[itemID].ID)
|
if ac.ImgLoadCancel != nil {
|
||||||
|
ac.ImgLoadCancel()
|
||||||
|
ac.ImgLoadCancel = nil
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
go func(ctx context.Context) {
|
||||||
|
i, err := im.GetAlbumThumbnail(album.ID)
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
default:
|
||||||
if err == nil {
|
if err == nil {
|
||||||
ac.Cover.SetImage(i)
|
ac.Cover.SetImage(i)
|
||||||
ac.Refresh()
|
ac.Refresh()
|
||||||
}
|
}
|
||||||
}()
|
}
|
||||||
|
}(ctx)
|
||||||
|
ac.ImgLoadCancel = cancel
|
||||||
|
|
||||||
// TODO: remove magic number 10
|
// TODO: remove magic number 10
|
||||||
if !ag.done && !ag.fetching && itemID > len(ag.albums)-10 {
|
if !ag.done && !ag.fetching && itemID > len(ag.albums)-10 {
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ func NewNowPlayingCard() *NowPlayingCard {
|
|||||||
n.cover.SetMinSize(fyne.NewSize(100, 100))
|
n.cover.SetMinSize(fyne.NewSize(100, 100))
|
||||||
n.cover.FillMode = canvas.ImageFillContain
|
n.cover.FillMode = canvas.ImageFillContain
|
||||||
|
|
||||||
n.c = container.NewBorder(nil, nil, n.cover, nil, container.New(&layout.VboxCustomPadding{-10}, n.trackName, n.artistName, n.albumName))
|
n.c = container.NewBorder(nil, nil, n.cover, nil, container.New(&layout.VboxCustomPadding{ExtraPad: -10}, n.trackName, n.artistName, n.albumName))
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user