-
Notifications
You must be signed in to change notification settings - Fork 23
/
items_scroll.go
306 lines (267 loc) · 8.07 KB
/
items_scroll.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package components
import (
"math"
"sync"
"gioui.org/layout"
"gioui.org/widget"
"github.com/crypto-power/cryptopower/app"
"github.com/crypto-power/cryptopower/ui/cryptomaterial"
"github.com/crypto-power/cryptopower/ui/load"
"github.com/crypto-power/cryptopower/ui/modal"
)
// ScrollFunc is a query function that accepts offset and pagesize parameters and
// returns data interface, count of the items in the data interface, isReset and an error.
// isReset is used to reset the offset value.
type ScrollFunc[T any] func(offset, pageSize int32) (data []T, count int, isReset bool, err error)
type scrollDerection int
const (
down scrollDerection = 1
up scrollDerection = 2
)
type dataList[T any] struct {
items []T
idxStart int
idxEnd int
}
type Scroll[T any] struct {
load *load.Load
list *widget.List
listStyle *cryptomaterial.ListStyle
prevListOffset int
prevScrollView int
pageSize int32
listSize int32
offset int32
itemsCount int
queryFunc ScrollFunc[T]
data *dataList[T]
cacheData []T
// scrollView defines the scroll view length in pixels.
scrollView int
direction scrollDerection
isLoadingItems bool
loadedAllItems bool
isHaveKeySearch bool
mu sync.RWMutex
}
// NewScroll returns a new scroll items component.
func NewScroll[T any](load *load.Load, pageSize int32, queryFunc ScrollFunc[T]) *Scroll[T] {
return &Scroll[T]{
list: &widget.List{
List: layout.List{
Axis: layout.Vertical,
},
},
load: load,
pageSize: pageSize,
queryFunc: queryFunc,
listSize: pageSize * 2,
itemsCount: -1,
prevListOffset: -1,
direction: down,
}
}
func (s *Scroll[T]) SetIsHaveKeySearch(isHaveKeySearch bool) {
s.isHaveKeySearch = isHaveKeySearch
}
// FetchScrollData is a mutex protected fetchScrollData function. At the end of
// the function call a window reload is triggered. Returns that latest records.
func (s *Scroll[T]) FetchScrollData(isScrollUp bool, window app.WindowNavigator, isResetList bool) {
s.mu.Lock()
// s.data is not nil when moving from details page to list page.
if s.data != nil {
s.isLoadingItems = false
}
if isResetList {
s.loadedAllItems = false
s.isLoadingItems = false
s.offset = 0
s.itemsCount = 0
s.data = nil
s.cacheData = nil
s.list.Position.Offset = 0
}
s.mu.Unlock()
if s.data == nil || len(s.data.items) == 0 {
s.fetchScrollData(isScrollUp, 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]) IsLoadingData() bool {
s.mu.RLock()
defer s.mu.RUnlock()
return s.isLoadingItems
}
func (s *Scroll[T]) fetchScrollData(isScrollUp bool, window app.WindowNavigator) {
if s.isLoadingItems || s.queryFunc == nil {
return
}
s.mu.Lock()
tempSize := s.pageSize
// Scroll up and down without load more
if s.data != nil {
temStartIdx := s.data.idxStart
if isScrollUp {
if s.data.idxStart <= 0 {
s.mu.Unlock()
return
}
itemsUp := s.cacheData[s.data.idxStart-int(s.pageSize) : s.data.idxStart-1]
itemdata := s.data.items[:int(s.pageSize)-1]
s.data.items = append(itemsUp, itemdata...)
if s.data.idxStart < int(s.pageSize) {
s.data.idxStart = 0
} else {
s.data.idxStart = s.data.idxStart - int(s.pageSize)
}
s.data.idxEnd = s.data.idxEnd - int(s.pageSize)
s.itemsCount = len(s.data.items)
if temStartIdx > 0 {
s.list.Position.Offset = s.list.Position.Length / len(s.data.items) * (int(s.pageSize) - 4)
}
s.mu.Unlock()
return
}
if s.data.idxEnd < len(s.cacheData)-1 {
itemsDown := s.cacheData[s.data.idxEnd+1 : s.data.idxEnd+int(s.pageSize)]
s.data.items = s.data.items[int(s.pageSize):]
s.data.items = append(s.data.items, itemsDown...)
s.data.idxStart = s.data.idxStart + int(s.pageSize)
s.data.idxEnd = s.data.idxEnd + int(s.pageSize)
s.itemsCount = len(s.data.items)
if temStartIdx > 0 {
s.list.Position.Offset = s.list.Position.Length / len(s.data.items) * (int(s.pageSize) - 4)
}
s.mu.Unlock()
return
}
}
// handle when there is a need to load more items.
if s.data == nil {
s.data = &dataList[T]{
idxEnd: -1,
}
tempSize = s.pageSize * 2
}
s.mu.Unlock()
if s.loadedAllItems && !isScrollUp {
s.isLoadingItems = false
return
}
items, _, _, err := s.queryFunc(s.offset, tempSize)
if len(items) <= 0 {
s.isLoadingItems = false
if s.itemsCount == -1 {
s.itemsCount = 0
}
return
}
s.mu.Lock()
s.isLoadingItems = false
if err != nil {
errModal := modal.NewErrorModal(s.load, err.Error(), modal.DefaultClickFunc())
window.ShowModal(errModal)
s.mu.Unlock()
return
}
if s.cacheData == nil {
s.cacheData = make([]T, 0)
}
s.cacheData = append(s.cacheData, items...)
itemCount := len(items)
if len(s.data.items) > itemCount {
s.data.items = s.data.items[s.pageSize-1:]
s.data.idxStart += itemCount
}
s.data.items = append(s.data.items, items...)
s.data.idxEnd += itemCount
s.offset += int32(itemCount)
s.itemsCount = len(s.data.items)
if s.data.idxStart > 0 {
s.list.Position.Offset = s.list.Position.Length / len(s.data.items) * (int(s.pageSize) - 4)
}
if itemCount < int(s.pageSize) {
s.loadedAllItems = true
}
s.mu.Unlock()
}
// FetchedData returns the latest queried data.
func (s *Scroll[T]) FetchedData() []T {
defer s.mu.RUnlock()
s.mu.RLock()
return s.data.items
}
// ItemsCount returns the count of the last fetched items.
func (s *Scroll[T]) ItemsCount() int {
defer s.mu.RUnlock()
s.mu.RLock()
return s.itemsCount
}
// List returns the list theme already in existence or newly created.
// Multiple list instances shouldn't exist for a given scroll component.
func (s *Scroll[T]) List() *cryptomaterial.ListStyle {
defer s.mu.RUnlock()
s.mu.RLock()
if s.listStyle == nil {
// If no existing instance exists, create it.
style := s.load.Theme.List(s.list)
s.listStyle = &style
}
return s.listStyle
}
// OnScrollChangeListener listens for the scroll bar movement and update the items
// list view accordingly. FetchScrollData needs to be invoked first before calling
// this function.
func (s *Scroll[T]) OnScrollChangeListener(window app.WindowNavigator) {
s.mu.Lock()
// Ignore if the list component on the UI hasn't been drawn.
if s.listStyle == nil {
s.mu.Unlock()
return
}
scrollPos := s.list.Position
// Ignore if the query is in theprocess of fetching the list items.
if s.itemsCount == -1 && s.isLoadingItems {
s.mu.Unlock()
return
}
s.scrollView = int(math.Abs(float64(scrollPos.Offset)) + math.Abs(float64(scrollPos.OffsetLast)))
// Ignore the first time OnScrollChangeListener is called. When loading the
// page a prexisting list of items to display already exist therefore no need
// to load it again.
if s.prevListOffset == -1 {
s.prevListOffset = scrollPos.Offset
s.prevScrollView = s.scrollView
s.mu.Unlock()
return
}
// Ignore duplicate events triggered without moving the scrollbar by checking
// if the current list offset matches the previously set offset.
if scrollPos.Offset == s.prevListOffset {
s.mu.Unlock()
return
}
// isScrollingDown checks when to fetch the Next page items because the scrollbar
// has reached at the end of the current list loaded.
isScrollingDown := scrollPos.Offset == s.scrollView && scrollPos.OffsetLast == 0 && s.prevListOffset < scrollPos.Offset && s.prevScrollView == s.scrollView
// isScrollingUp checks when to fetch the Previous page items because the scrollbar
// has reached at the beginning of the current list loaded.
isScrollingUp := scrollPos.Offset == 0 && scrollPos.OffsetLast < 0 && s.offset > 0 && s.prevListOffset > scrollPos.Offset
s.prevListOffset = scrollPos.Offset
s.prevScrollView = s.scrollView
if isScrollingDown {
s.mu.Unlock()
go s.fetchScrollData(false, window)
}
if isScrollingUp {
s.mu.Unlock()
go s.fetchScrollData(true, window)
}
if !isScrollingUp && !isScrollingDown {
s.mu.Unlock()
}
}