some work on the quick search dialog + image load refactoring
This commit is contained in:
@@ -188,6 +188,55 @@ func (i *ImageManager) RefreshCachedArtistImageIfExpired(artistID string, imgURL
|
||||
return err
|
||||
}
|
||||
|
||||
// ThumbnailLoader is a utility type that exposes a single API to load
|
||||
// a cover thumbnail by ID. If the image is immediately available in
|
||||
// the cache, OnLoaded will be called immediately. If it is not,
|
||||
// OnBeforeLoad will be called first, then OnLoaded will be called async
|
||||
// once the image is available.
|
||||
// Any subsequent calls to Load will cancel the previous load if not yet completed.
|
||||
type ThumbnailLoader struct {
|
||||
prevLoadCancel context.CancelFunc
|
||||
im *ImageManager
|
||||
|
||||
OnBeforeLoad func()
|
||||
OnLoaded func(image.Image)
|
||||
}
|
||||
|
||||
func (i *ImageManager) NewThumbnailLoader(onLoaded func(image.Image)) ThumbnailLoader {
|
||||
return ThumbnailLoader{im: i, OnLoaded: onLoaded}
|
||||
}
|
||||
|
||||
func (i *ThumbnailLoader) Load(coverID string) {
|
||||
if i.prevLoadCancel != nil {
|
||||
i.prevLoadCancel()
|
||||
}
|
||||
if coverID == "" {
|
||||
i.callOnLoaded(nil)
|
||||
return
|
||||
}
|
||||
if img, ok := i.im.GetCoverThumbnailFromCache(coverID); ok {
|
||||
i.callOnLoaded(img)
|
||||
return
|
||||
}
|
||||
if i.OnBeforeLoad != nil {
|
||||
i.OnBeforeLoad()
|
||||
}
|
||||
i.prevLoadCancel = i.im.GetCoverThumbnailAsync(coverID, func(img image.Image, err error) {
|
||||
if err != nil {
|
||||
log.Printf("Error loading cover image: %s", err.Error())
|
||||
} else {
|
||||
i.callOnLoaded(img)
|
||||
}
|
||||
i.prevLoadCancel() // Done. Release resources associated with un-cancelled ctx
|
||||
})
|
||||
}
|
||||
|
||||
func (i *ThumbnailLoader) callOnLoaded(im image.Image) {
|
||||
if i.OnLoaded != nil {
|
||||
i.OnLoaded(im)
|
||||
}
|
||||
}
|
||||
|
||||
func (i *ImageManager) ensureCoverCacheDir() string {
|
||||
// if user logged out with pending fetches in progress,
|
||||
// make sure we don't write to nil (00000000-*0) cache directory
|
||||
|
||||
Reference in New Issue
Block a user