Skip to content

Commit 2571ad3

Browse files
committed
Use the factory in _loadFromCache:
1 parent 2631004 commit 2571ad3

2 files changed

Lines changed: 54 additions & 61 deletions

File tree

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
part of '../../parse_server_sdk.dart';
22

3-
4-
53
extension ParseObjectOffline on ParseObject {
6-
7-
84
/// Load a single object by objectId from local storage.
95
static Future<ParseObject?> loadFromLocalCache(String className, String objectId) async {
106
final CoreStore coreStore = ParseCoreData().getStore();
@@ -13,6 +9,7 @@ extension ParseObjectOffline on ParseObject {
139
for (final s in cached) {
1410
final jsonObj = json.decode(s);
1511
if (jsonObj['objectId'] == objectId) {
12+
print('Loaded object $objectId from local cache for $className');
1613
return ParseObject(className).fromJson(jsonObj);
1714
}
1815
}
@@ -31,9 +28,10 @@ extension ParseObjectOffline on ParseObject {
3128
});
3229
cached.add(json.encode(toJson(full: true)));
3330
await coreStore.setStringList(cacheKey, cached);
31+
print('Saved object ${objectId ?? "(no objectId)"} to local cache for $parseClassName');
3432
}
3533

36-
/// Remove this object from local storage (CoreStore).
34+
/// Remove this object from local storage (CoreStore).
3735
Future<void> removeFromLocalCache() async {
3836
final CoreStore coreStore = ParseCoreData().getStore();
3937
final String cacheKey = 'offline_cache_${parseClassName}';
@@ -43,20 +41,21 @@ extension ParseObjectOffline on ParseObject {
4341
return jsonObj['objectId'] == objectId;
4442
});
4543
await coreStore.setStringList(cacheKey, cached);
44+
print('Removed object ${objectId ?? "(no objectId)"} from local cache for $parseClassName');
4645
}
4746

4847
/// Load all objects of this class from local storage.
4948
static Future<List<ParseObject>> loadAllFromLocalCache(String className) async {
5049
final CoreStore coreStore = ParseCoreData().getStore();
5150
final String cacheKey = 'offline_cache_$className';
52-
final List<String> cached = await _getStringListAsStrings(coreStore, cacheKey);
51+
final List<String> cached = await _getStringListAsStrings(coreStore, cacheKey);
52+
print('Loaded ${cached.length} objects from local cache for $className');
5353
return cached.map<ParseObject>((s) {
5454
final jsonObj = json.decode(s);
5555
return ParseObject(className).fromJson(jsonObj);
5656
}).toList();
5757
}
5858

59-
6059
Future<void> updateInLocalCache(Map<String, dynamic> updates) async {
6160
final CoreStore coreStore = ParseCoreData().getStore();
6261
final String cacheKey = 'offline_cache_${parseClassName}';
@@ -70,54 +69,50 @@ extension ParseObjectOffline on ParseObject {
7069
}
7170
}
7271
await coreStore.setStringList(cacheKey, cached);
72+
print('Updated object ${objectId ?? "(no objectId)"} in local cache for $parseClassName');
7373
}
7474

7575
static Future<void> clearLocalCacheForClass(String className) async {
76-
final CoreStore coreStore = ParseCoreData().getStore();
77-
final String cacheKey = 'offline_cache_$className';
78-
await coreStore.setStringList(cacheKey, []);
79-
}
76+
final CoreStore coreStore = ParseCoreData().getStore();
77+
final String cacheKey = 'offline_cache_$className';
78+
await coreStore.setStringList(cacheKey, []);
79+
print('Cleared local cache for $className');
80+
}
8081

81-
static Future<bool> existsInLocalCache(String className, String objectId) async {
82-
final CoreStore coreStore = ParseCoreData().getStore();
83-
final String cacheKey = 'offline_cache_$className';
84-
final List<String> cached = await _getStringListAsStrings(coreStore, cacheKey);
85-
for (final s in cached) {
86-
final jsonObj = json.decode(s);
87-
if (jsonObj['objectId'] == objectId) {
88-
return true;
82+
static Future<bool> existsInLocalCache(String className, String objectId) async {
83+
final CoreStore coreStore = ParseCoreData().getStore();
84+
final String cacheKey = 'offline_cache_$className';
85+
final List<String> cached = await _getStringListAsStrings(coreStore, cacheKey);
86+
for (final s in cached) {
87+
final jsonObj = json.decode(s);
88+
if (jsonObj['objectId'] == objectId) {
89+
print('Object $objectId exists in local cache for $className');
90+
return true;
91+
}
8992
}
93+
print('Object $objectId does not exist in local cache for $className');
94+
return false;
9095
}
91-
return false;
92-
}
93-
static Future<List<String>> getAllObjectIdsInLocalCache(String className) async {
94-
final CoreStore coreStore = ParseCoreData().getStore();
95-
final String cacheKey = 'offline_cache_$className';
96-
final List<String> cached = await _getStringListAsStrings(coreStore, cacheKey);
97-
return cached.map((s) => json.decode(s)['objectId'] as String).toList();
98-
}
9996

100-
static Future<void> syncLocalCacheWithServer(String className) async {
101-
final objects = await loadAllFromLocalCache(className);
102-
for (final obj in objects) {
103-
await obj.save();
97+
static Future<List<String>> getAllObjectIdsInLocalCache(String className) async {
98+
final CoreStore coreStore = ParseCoreData().getStore();
99+
final String cacheKey = 'offline_cache_$className';
100+
final List<String> cached = await _getStringListAsStrings(coreStore, cacheKey);
101+
print('Fetched all objectIds from local cache for $className');
102+
return cached.map((s) => json.decode(s)['objectId'] as String).toList();
104103
}
105-
}
106104

107-
static Future<List<String>> _getStringListAsStrings(CoreStore coreStore, String cacheKey) async {
105+
static Future<void> syncLocalCacheWithServer(String className) async {
106+
final objects = await loadAllFromLocalCache(className);
107+
for (final obj in objects) {
108+
await obj.save();
109+
}
110+
print('Synced local cache with server for $className');
111+
}
108112

109-
// final rawList = await coreStore.getStringList(cacheKey);
110-
// final List<String> cached = [];
111-
// if (rawList != null) {
112-
// for (final e in rawList) {
113-
// cached.add(e.toString());
114-
// }
115-
// }
113+
static Future<List<String>> _getStringListAsStrings(CoreStore coreStore, String cacheKey) async {
116114
final rawList = await coreStore.getStringList(cacheKey);
117115
if (rawList == null) return [];
118116
return List<String>.from(rawList.map((e) => e.toString()));
119117
}
120-
}
121-
122-
// await object.saveToLocalCache();
123-
// final offlineObjects = await ParseObjectOffline.loadAllFromLocalCache('YourClassName');
118+
}

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

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
part of 'package:parse_server_sdk_flutter/parse_server_sdk_flutter.dart';
22

3-
4-
53
/// The type of function that builds a child widget for a ParseLiveList element.
64
typedef ChildBuilder<T extends sdk.ParseObject> = Widget Function(
75
BuildContext context, sdk.ParseLiveListElementSnapshot<T> snapshot, [int? index]);
@@ -53,6 +51,7 @@ class ParseLiveListWidget<T extends sdk.ParseObject> extends StatefulWidget {
5351
this.loadMoreOffset = 200.0,
5452
this.cacheSize = 50,
5553
this.offlineMode = false,
54+
required this.fromJson,
5655
});
5756

5857
final sdk.QueryBuilder<T> query;
@@ -87,6 +86,8 @@ class ParseLiveListWidget<T extends sdk.ParseObject> extends StatefulWidget {
8786
final int cacheSize;
8887
final bool offlineMode;
8988

89+
final T Function(Map<String, dynamic> json) fromJson;
90+
9091
@override
9192
State<ParseLiveListWidget<T>> createState() => _ParseLiveListWidgetState<T>();
9293

@@ -138,29 +139,26 @@ class _ParseLiveListWidgetState<T extends sdk.ParseObject>
138139
}
139140

140141
Future<void> _checkConnectivityAndLoad() async {
141-
final connectivityResult = await Connectivity().checkConnectivity();
142-
_isOffline = connectivityResult == ConnectivityResult.none;
143-
if (_isOffline) {
144-
if (widget.offlineMode) {
145-
await _loadFromCache();
146-
} else {
147-
// Show a message or empty state, since offlineMode is not enabled
148-
setState(() {
149-
_items.clear();
150-
_noDataNotifier.value = true;
151-
});
142+
final connectivityResult = await Connectivity().checkConnectivity();
143+
_isOffline = connectivityResult == ConnectivityResult.none;
144+
145+
// Always try to load from cache first
146+
await _loadFromCache();
147+
148+
// If cache is empty and we're online, load from server
149+
if (_items.isEmpty && !_isOffline) {
150+
await _loadData();
152151
}
153-
} else {
154-
await _loadData();
155152
}
156-
}
157153

158154
Future<void> _loadFromCache() async {
159155
_items.clear();
160156
final cached = await sdk.ParseObjectOffline.loadAllFromLocalCache(
161157
widget.query.object.parseClassName,
162158
);
163-
_items.addAll(cached.cast<T>());
159+
for (final obj in cached) {
160+
_items.add(widget.fromJson(obj.toJson(full: true)));
161+
}
164162
_noDataNotifier.value = _items.isEmpty;
165163
setState(() {});
166164
}

0 commit comments

Comments
 (0)