Merge pull request #68 from dweymouth/develop

Fix #37: infinite scroll stops loading if scrolling immediately to bottom
This commit is contained in:
Drew Weymouth
2023-03-06 21:22:47 -08:00
committed by GitHub
+25 -16
View File
@@ -27,11 +27,12 @@ type AlbumGrid struct {
} }
type AlbumGridState struct { type AlbumGridState struct {
albums []*subsonic.AlbumID3 albums []*subsonic.AlbumID3
iter *backend.BatchingIterator iter *backend.BatchingIterator
fetching bool highestShown int
done bool fetching bool
showYear bool done bool
showYear bool
imageFetcher ImageFetcher imageFetcher ImageFetcher
OnPlayAlbum func(string) OnPlayAlbum func(string)
@@ -136,6 +137,9 @@ func (ag *AlbumGrid) createGridWrapList() {
} }
func (ag *AlbumGrid) doUpdateAlbumCard(albumIdx int, ac *AlbumCard) { func (ag *AlbumGrid) doUpdateAlbumCard(albumIdx int, ac *AlbumCard) {
if albumIdx > ag.highestShown {
ag.highestShown = albumIdx
}
album := ag.albums[albumIdx] album := ag.albums[albumIdx]
if ac.PrevAlbumID == album.ID { if ac.PrevAlbumID == album.ID {
// nothing to do // nothing to do
@@ -171,28 +175,33 @@ func (ag *AlbumGrid) doUpdateAlbumCard(albumIdx int, ac *AlbumCard) {
ac.ImgLoadCancel = cancel ac.ImgLoadCancel = cancel
} }
// TODO: remove magic number 10 // if user has scrolled near the bottom, fetch more
if !ag.done && !ag.fetching && albumIdx > len(ag.albums)-10 { if !ag.done && !ag.fetching && albumIdx > len(ag.albums)-10 {
ag.fetchMoreAlbums(20) ag.fetchMoreAlbums(20)
} }
} }
// fetches at least count more albums
func (a *AlbumGrid) fetchMoreAlbums(count int) { func (a *AlbumGrid) fetchMoreAlbums(count int) {
if a.iter == nil { if a.iter == nil {
a.done = true a.done = true
} }
a.fetching = true a.fetching = true
go func() { go func() {
n := 0 // keep repeating the fetch task as long as the user
for !a.done && n < count { // has scrolled near the bottom
albums := a.iter.NextN(albumFetchBatchSize) for !a.done && a.highestShown >= len(a.albums)-10 {
a.albums = append(a.albums, albums...) n := 0
if len(albums) < albumFetchBatchSize { for !a.done && n < count {
a.done = true albums := a.iter.NextN(albumFetchBatchSize)
} a.albums = append(a.albums, albums...)
n += len(albums) if len(albums) < albumFetchBatchSize {
if len(albums) > 0 { a.done = true
a.Refresh() }
n += len(albums)
if len(albums) > 0 {
a.Refresh()
}
} }
} }
a.fetching = false a.fetching = false