@@ -147,10 +147,20 @@ class RenderList {
147
147
148
148
if (groupBy == GroupAssetsBy .none) {
149
149
final int total = assets? .length ?? query! .countSync ();
150
+
151
+ final dateLoader = query != null
152
+ ? DateBatchLoader (
153
+ query: query,
154
+ totalCount: total,
155
+ batchSize: 1000 * sectionSize,
156
+ )
157
+ : null ;
158
+
150
159
for (int i = 0 ; i < total; i += sectionSize) {
151
160
final date = assets != null
152
161
? assets[i].fileCreatedAt
153
- : await query! .offset (i).fileCreatedAtProperty ().findFirst ();
162
+ : await dateLoader? .getDate (i);
163
+
154
164
final int count = i + sectionSize > total ? total - i : sectionSize;
155
165
if (date == null ) break ;
156
166
elements.add (
@@ -320,3 +330,46 @@ class RenderList {
320
330
_bufOffset = 0 ;
321
331
}
322
332
}
333
+
334
+ class DateBatchLoader {
335
+ final QueryBuilder <Asset , Asset , QAfterSortBy > query;
336
+ final int totalCount;
337
+ final int batchSize;
338
+
339
+ List <DateTime > _buffer = [];
340
+ int _bufferStart = 0 ;
341
+
342
+ DateBatchLoader ({
343
+ required this .query,
344
+ required this .totalCount,
345
+ required this .batchSize,
346
+ });
347
+
348
+ Future <DateTime ?> getDate (int index) async {
349
+ if (! _isIndexInBuffer (index)) {
350
+ await _loadBatch (index);
351
+ }
352
+
353
+ if (_isIndexInBuffer (index)) {
354
+ return _buffer[index - _bufferStart];
355
+ }
356
+
357
+ return null ;
358
+ }
359
+
360
+ Future <void > _loadBatch (int targetIndex) async {
361
+ final batchStart = (targetIndex ~ / batchSize) * batchSize;
362
+
363
+ _buffer = await query!
364
+ .offset (batchStart)
365
+ .limit (batchSize)
366
+ .fileCreatedAtProperty ()
367
+ .findAll ();
368
+
369
+ _bufferStart = batchStart;
370
+ }
371
+
372
+ bool _isIndexInBuffer (int index) {
373
+ return index >= _bufferStart && index < _bufferStart + _buffer.length;
374
+ }
375
+ }
0 commit comments