Skip to content

Commit

Permalink
implement partial infinite scroll to prevent app from freezing when l…
Browse files Browse the repository at this point in the history
…aoding multi wallet txs
  • Loading branch information
Sirmorrison committed Nov 26, 2023
1 parent f23cac8 commit 8bf5dfe
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 38 deletions.
48 changes: 27 additions & 21 deletions ui/page/components/items_scroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,26 @@ func (s *Scroll[T]) FetchScrollData(isReverse bool, window app.WindowNavigator)
}
s.mu.Unlock()

s.fetchScrollData(isReverse, window)
// set isReset to true here since all the callers of FetchScrollData()
// needs the data to be refreashed and data reset.
// TODO: set isreverse to default false all the callers of this
// FetchScrollData() is not initiating a reverse scroll action.
s.fetchScrollData(isReverse, true, window)
}

// fetchScrollData fetchs the scroll data and manages data returned depending on
// on the up or downwards scrollbar movement. Unless the data fetched is less than
// the page, all the old data is replaced by the new fetched data making it
// easier and smoother to scroll on the UI. At the end of the function call
// a window reload is triggered.
func (s *Scroll[T]) fetchScrollData(isReverse bool, window app.WindowNavigator) {
func (s *Scroll[T]) fetchScrollData(isReverse, isReset bool, window app.WindowNavigator) {
s.mu.Lock()

if isReset {
// resets the values for use on the next iteration.
s.isReset()
}

if s.isLoadingItems || s.loadedAllItems || s.queryFunc == nil {
return
}
Expand All @@ -90,7 +99,7 @@ func (s *Scroll[T]) fetchScrollData(isReverse bool, window app.WindowNavigator)
} else {
s.list.Position.Offset = 1
s.list.Position.OffsetLast = s.scrollView*-1 + 1
if s.data != nil {
if s.data != nil && !isReset {
s.offset += s.pageSize
}
}
Expand All @@ -102,12 +111,13 @@ func (s *Scroll[T]) fetchScrollData(isReverse bool, window app.WindowNavigator)

s.mu.Unlock()

items, itemsLen, isReset, err := s.queryFunc(offset, tempSize*2)
// Check if enough list items exists to fill the next page. If they do only query
// enough items to fit the current page otherwise return all the queried items.
if itemsLen > int(tempSize) && itemsLen%int(tempSize) == 0 {
items, itemsLen, isReset, err = s.queryFunc(offset, tempSize)
}
// keep fetching data until the last item.
// TODO. for inifinte scroll will implement fetching data in advance while
// having enough data on the page for reverse scroll. No need for making
// 2 queries to determine if there is enough tx. This approach will also
// prevent the app from freezing when the last item on the list is reached.
// TODO: deprecate isReset in a follow up PR as it cuts across multliple files.
items, itemsLen, _, err := s.queryFunc(offset, tempSize)

s.mu.Lock()

Expand All @@ -120,20 +130,16 @@ func (s *Scroll[T]) fetchScrollData(isReverse bool, window app.WindowNavigator)
return
}

if itemsLen > int(tempSize) {
// Since this is the last page set of items, prevent further scroll down queries.
if itemsLen < int(tempSize) {
// we know whe are at the last item when the item returned is less than the
// the page size.
s.loadedAllItems = true
}

s.data = items
s.itemsCount = itemsLen
s.isLoadingItems = false
s.mu.Unlock()

if isReset {
// resets the values for use on the next iteration.
s.resetList()
}
}

// FetchedData returns the latest queried data.
Expand Down Expand Up @@ -163,9 +169,9 @@ func (s *Scroll[T]) List() *cryptomaterial.ListStyle {
return s.listStyle
}

func (s *Scroll[T]) resetList() {
s.mu.Lock()
defer s.mu.Unlock()
func (s *Scroll[T]) isReset() {
// s.mu.Lock()
// defer s.mu.Unlock()

s.offset = 0
s.loadedAllItems = false
Expand Down Expand Up @@ -231,7 +237,7 @@ func (s *Scroll[T]) OnScrollChangeListener(window app.WindowNavigator) {

s.mu.Unlock()

go s.fetchScrollData(false, window)
go s.fetchScrollData(false, false, window)
}

if isScrollingUp {
Expand All @@ -241,7 +247,7 @@ func (s *Scroll[T]) OnScrollChangeListener(window app.WindowNavigator) {

s.mu.Unlock()

go s.fetchScrollData(true, window)
go s.fetchScrollData(true, false, window)
}

if !isScrollingUp && !isScrollingDown {
Expand Down
12 changes: 0 additions & 12 deletions ui/page/transaction/transaction_details_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,7 @@ func (pg *TxDetailsPage) OnNavigatedTo() {
}

pg.getTXSourceAccountAndDirection()
<<<<<<< HEAD
pg.txnWidgets = pg.initTxnWidgets()
=======
pg.txnWidgets = initTxnWidgets(pg.Load, pg.wallet, pg.transaction)
>>>>>>> 03851b24 (pr rebase)
}

func (pg *TxDetailsPage) getMoreItem() []moreItem {
Expand Down Expand Up @@ -296,11 +292,7 @@ func (pg *TxDetailsPage) Layout(gtx C) D {
}
pg.transaction = pg.txBackStack
pg.getTXSourceAccountAndDirection()
<<<<<<< HEAD
pg.txnWidgets = pg.initTxnWidgets()
=======
pg.txnWidgets = initTxnWidgets(pg.Load, pg.wallet, pg.transaction)
>>>>>>> 03851b24 (pr rebase)
pg.txBackStack = nil
pg.ParentWindow().Reload()
},
Expand Down Expand Up @@ -949,11 +941,7 @@ func (pg *TxDetailsPage) HandleUserInteractions() {
pg.txBackStack = pg.transaction
pg.transaction = pg.ticketSpent
pg.getTXSourceAccountAndDirection()
<<<<<<< HEAD
pg.txnWidgets = pg.initTxnWidgets()
=======
pg.txnWidgets = initTxnWidgets(pg.Load, pg.wallet, pg.transaction)
>>>>>>> 03851b24 (pr rebase)
pg.ParentWindow().Reload()
}
}
Expand Down
11 changes: 6 additions & 5 deletions ui/page/transaction/transactions_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func NewTransactionsPage(l *load.Load, isHomepageLayout bool) *TransactionsPage
pg.transactionList.IsShadowEnabled = true

pg.materialLoader = material.Loader(pg.Theme.Base)
go pg.scroll.FetchScrollData(false, pg.ParentWindow())

return pg
}
Expand All @@ -108,7 +109,6 @@ func (pg *TransactionsPage) OnNavigatedTo() {
pg.refreshAvailableTxType()

pg.listenForTxNotifications() // tx ntfn listener is stopped in OnNavigatedFrom().
go pg.scroll.FetchScrollData(false, pg.ParentWindow())
}

// initWalletSelector initializes the wallet selector dropdown to enable
Expand Down Expand Up @@ -390,11 +390,12 @@ func (pg *TransactionsPage) desktopLayoutContent(gtx C) D {
return pg.txListLayout(gtx) // nothing else to display on this page at this time
}

pageElements := []layout.StackChild{
layout.Expanded(pg.txListLayout),
layout.Expanded(func(gtx C) D {
pageElements := []layout.StackChild{layout.Expanded(pg.txListLayout)}

if pg.walletDropDown != nil {
pageElements = append(pageElements, layout.Expanded(func(gtx C) D {
return pg.walletDropDown.Layout(gtx, 0, false)
}),
}))
}

// display tx dropdown if selected wallet is ready and showLoader is false
Expand Down

0 comments on commit 8bf5dfe

Please sign in to comment.