Skip to content

Commit 1686c81

Browse files
committed
Update parse_live_grid.dart
1 parent 7257b4c commit 1686c81

1 file changed

Lines changed: 41 additions & 97 deletions

File tree

packages/flutter/lib/src/utils/parse_live_grid.dart

Lines changed: 41 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class ParseLiveGridWidget<T extends sdk.ParseObject> extends StatefulWidget {
104104
}
105105

106106
class _ParseLiveGridWidgetState<T extends sdk.ParseObject>
107-
extends State<ParseLiveGridWidget<T>> with ConnectivityHandlerMixin<ParseLiveGridWidget<T>> {
107+
extends State<ParseLiveGridWidget<T>> with ConnectivityHandlerMixin<ParseLiveGridWidget<T>> {
108108
CachedParseLiveList<T>? _liveGrid;
109109
final ValueNotifier<bool> _noDataNotifier = ValueNotifier<bool>(true);
110110
final List<T> _items = <T>[];
@@ -121,7 +121,7 @@ class _ParseLiveGridWidgetState<T extends sdk.ParseObject>
121121
final Connectivity _connectivity = Connectivity();
122122
ConnectivityResult? _connectionStatus;
123123

124-
// --- Implement Mixin Requirements ---
124+
// --- Implement Mixin Requirements ---
125125
@override
126126
Future<void> loadDataFromServer() => _loadData(); // Map to existing method
127127

@@ -153,80 +153,9 @@ class _ParseLiveGridWidgetState<T extends sdk.ParseObject>
153153
_scrollController.addListener(_onScroll);
154154
}
155155

156-
initConnectivityHandler();
157-
158-
// _initConnectivity();
159-
160-
// _connectivitySubscription =
161-
// _connectivity.onConnectivityChanged.listen((List<ConnectivityResult> results) {
162-
// final newResult = results.contains(ConnectivityResult.mobile)
163-
// ? ConnectivityResult.mobile
164-
// : results.contains(ConnectivityResult.wifi)
165-
// ? ConnectivityResult.wifi
166-
// : results.contains(ConnectivityResult.none)
167-
// ? ConnectivityResult.none
168-
// : ConnectivityResult.other;
169-
170-
// _updateConnectionStatus(newResult);
171-
// });
156+
initConnectivityHandler();
172157
}
173158

174-
// Future<void> _initConnectivity() async {
175-
// try {
176-
// var connectivityResults = await _connectivity.checkConnectivity();
177-
// final initialResult = connectivityResults.contains(ConnectivityResult.mobile)
178-
// ? ConnectivityResult.mobile
179-
// : connectivityResults.contains(ConnectivityResult.wifi)
180-
// ? ConnectivityResult.wifi
181-
// : connectivityResults.contains(ConnectivityResult.none)
182-
// ? ConnectivityResult.none
183-
// : ConnectivityResult.other;
184-
185-
// await _updateConnectionStatus(initialResult, isInitialCheck: true);
186-
// } catch (e) {
187-
// debugPrint('Error during initial connectivity check: $e');
188-
// await _updateConnectionStatus(ConnectivityResult.none, isInitialCheck: true);
189-
// }
190-
// }
191-
192-
// Future<void> _updateConnectionStatus(ConnectivityResult result, {bool isInitialCheck = false}) async {
193-
// if (result == _connectionStatus) {
194-
// debugPrint('Grid Connectivity status unchanged: $result');
195-
// return;
196-
// }
197-
198-
// debugPrint('Grid Connectivity status changed: From $_connectionStatus to $result');
199-
// final previousStatus = _connectionStatus;
200-
// _connectionStatus = result;
201-
202-
// bool wasOnline = previousStatus != null && previousStatus != ConnectivityResult.none;
203-
// bool isOnline = result == ConnectivityResult.mobile || result == ConnectivityResult.wifi;
204-
205-
// if (isOnline && !wasOnline) {
206-
// _isOffline = false;
207-
// debugPrint('Grid Transitioning Online: $result. Loading data from server...');
208-
// await _loadData();
209-
// } else if (!isOnline && wasOnline) {
210-
// _isOffline = true;
211-
// debugPrint('Grid Transitioning Offline: $result. Disposing liveGrid and loading from cache...');
212-
// _liveGrid?.dispose();
213-
// _liveGrid = null;
214-
// await _loadFromCache();
215-
// } else if (isInitialCheck) {
216-
// if (isOnline) {
217-
// _isOffline = false;
218-
// debugPrint('Grid Initial State Online: $result. Loading data from server...');
219-
// await _loadData();
220-
// } else {
221-
// _isOffline = true;
222-
// debugPrint('Grid Initial State Offline: $result. Loading from cache...');
223-
// await _loadFromCache();
224-
// }
225-
// } else {
226-
// debugPrint('Grid Connectivity changed within same state (Online/Offline): $result');
227-
// }
228-
// }
229-
230159
Future<void> _loadFromCache() async {
231160
if (!isOfflineModeEnabled) {
232161
debugPrint('Offline mode disabled, skipping cache load.');
@@ -364,14 +293,14 @@ class _ParseLiveGridWidgetState<T extends sdk.ParseObject>
364293

365294
Future<void> _loadData() async {
366295
if (isOffline) {
367-
debugPrint('Offline: Skipping server load, relying on cache.');
296+
debugPrint('$connectivityLogPrefix Offline: Skipping server load, relying on cache.');
368297
if (isOfflineModeEnabled) {
369-
await loadDataFromCache();
298+
await loadDataFromCache();
370299
}
371300
return;
372301
}
373302

374-
debugPrint('Grid loading initial data from server...');
303+
debugPrint('$connectivityLogPrefix Loading initial data from server...');
375304
try {
376305
_currentPage = 0;
377306
_loadMoreStatus = LoadMoreStatus.idle;
@@ -405,36 +334,36 @@ class _ParseLiveGridWidgetState<T extends sdk.ParseObject>
405334
_liveGrid?.dispose();
406335
_liveGrid = liveGrid;
407336

337+
final List<T> initialItems = [];
338+
final List<T> itemsToFetchAndCache = [];
339+
408340
if (liveGrid.size > 0) {
409341
for (int i = 0; i < liveGrid.size; i++) {
410342
final item = liveGrid.getPreLoadedAt(i);
411343
if (item != null) {
344+
initialItems.add(item);
412345
if (widget.offlineMode) {
413-
try {
414-
if (widget.lazyLoading) {
415-
await item.fetch();
416-
}
417-
await item.saveToLocalCache();
418-
} catch (e) {
419-
debugPrint('Error saving initial object ${item.objectId} to cache: $e');
420-
}
346+
itemsToFetchAndCache.add(item);
421347
}
422-
_items.add(item);
423348
}
424349
}
425350
}
426351

352+
_items.addAll(initialItems);
427353
_noDataNotifier.value = _items.isEmpty;
428-
429354
if (mounted) {
430355
setState(() {});
431356
}
432357

358+
if (itemsToFetchAndCache.isNotEmpty) {
359+
_fetchAndCacheItemsInBackground(itemsToFetchAndCache);
360+
}
361+
433362
liveGrid.stream.listen((event) {
434363
T? objectToCache;
435364

436365
if (event is sdk.ParseLiveListAddEvent<sdk.ParseObject>) {
437-
final addedItem = event.object as T;
366+
final addedItem = event.object;
438367
if (mounted) {
439368
setState(() {
440369
_items.insert(event.index, addedItem);
@@ -451,10 +380,10 @@ class _ParseLiveGridWidgetState<T extends sdk.ParseObject>
451380
removedItem.removeFromLocalCache();
452381
}
453382
} else {
454-
debugPrint('Grid LiveList Delete Event: Invalid index ${event.index}, list size ${_items.length}');
383+
debugPrint('$connectivityLogPrefix LiveList Delete Event: Invalid index ${event.index}, list size ${_items.length}');
455384
}
456385
} else if (event is sdk.ParseLiveListUpdateEvent<sdk.ParseObject>) {
457-
final updatedItem = event.object as T;
386+
final updatedItem = event.object;
458387
if (event.index >= 0 && event.index < _items.length) {
459388
if (mounted) {
460389
setState(() {
@@ -463,7 +392,7 @@ class _ParseLiveGridWidgetState<T extends sdk.ParseObject>
463392
}
464393
objectToCache = updatedItem;
465394
} else {
466-
debugPrint('Grid LiveList Update Event: Invalid index ${event.index}, list size ${_items.length}');
395+
debugPrint('$connectivityLogPrefix LiveList Update Event: Invalid index ${event.index}, list size ${_items.length}');
467396
}
468397
}
469398

@@ -485,12 +414,31 @@ class _ParseLiveGridWidgetState<T extends sdk.ParseObject>
485414
});
486415
}
487416
} catch (e) {
488-
debugPrint('Error loading grid data: $e');
417+
debugPrint('$connectivityLogPrefix Error loading data: $e');
489418
_noDataNotifier.value = _items.isEmpty;
490419
if (mounted) setState(() {});
491420
}
492421
}
493422

423+
Future<void> _fetchAndCacheItemsInBackground(List<T> items) async {
424+
debugPrint('$connectivityLogPrefix Starting background fetch/cache for ${items.length} items...');
425+
for (final item in items) {
426+
try {
427+
if (!mounted) return;
428+
429+
if (widget.lazyLoading) {
430+
431+
await item.fetch();
432+
433+
}
434+
await item.saveToLocalCache();
435+
} catch (e) {
436+
debugPrint('$connectivityLogPrefix Error background saving object ${item.objectId} to cache: $e');
437+
}
438+
}
439+
debugPrint('$connectivityLogPrefix Finished background fetch/cache.');
440+
}
441+
494442
Future<void> _refreshData() async {
495443
debugPrint('Refreshing Grid data...');
496444
disposeLiveList();
@@ -603,7 +551,7 @@ class _ParseLiveGridWidgetState<T extends sdk.ParseObject>
603551
DataGetter<T>? preLoadedData;
604552

605553
final liveGrid = _liveGrid;
606-
if (!isOffline && liveGrid != null && index < liveGrid.size) {
554+
if (!isOffline && liveGrid != null && index < liveGrid.size) {
607555
itemStream = () => liveGrid.getAt(index);
608556
loadedData = () => liveGrid.getLoadedAt(index);
609557
preLoadedData = () => liveGrid.getPreLoadedAt(index);
@@ -612,8 +560,6 @@ class _ParseLiveGridWidgetState<T extends sdk.ParseObject>
612560
preLoadedData = () => item;
613561
}
614562

615-
616-
617563
return ParseLiveListElementWidget<T>(
618564
key: ValueKey<String>(item.objectId ?? 'unknown-$index'),
619565
stream: itemStream,
@@ -674,6 +620,4 @@ class _ParseLiveGridWidgetState<T extends sdk.ParseObject>
674620
}
675621
super.dispose();
676622
}
677-
678-
679623
}

0 commit comments

Comments
 (0)