fix #37: infinite scroll stops loading if scrolling immediately to bottom

This commit is contained in:
Drew Weymouth
2023-03-06 18:39:23 -08:00
parent 8038a82e04
commit d86d4dde80
+10 -1
View File
@@ -29,6 +29,7 @@ type AlbumGrid struct {
type AlbumGridState struct { type AlbumGridState struct {
albums []*subsonic.AlbumID3 albums []*subsonic.AlbumID3
iter *backend.BatchingIterator iter *backend.BatchingIterator
highestShown int
fetching bool fetching bool
done bool done bool
showYear bool showYear bool
@@ -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,18 +175,22 @@ 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() {
// keep repeating the fetch task as long as the user
// has scrolled near the bottom
for !a.done && a.highestShown >= len(a.albums)-10 {
n := 0 n := 0
for !a.done && n < count { for !a.done && n < count {
albums := a.iter.NextN(albumFetchBatchSize) albums := a.iter.NextN(albumFetchBatchSize)
@@ -195,6 +203,7 @@ func (a *AlbumGrid) fetchMoreAlbums(count int) {
a.Refresh() a.Refresh()
} }
} }
}
a.fetching = false a.fetching = false
}() }()
} }