Skip to content

Commit 87cbba8

Browse files
authored
ParseLiveList: first implementation of lazyLoading (#407)
Setting lazyLoading=false should disable lazyLoading.
1 parent fd49451 commit 87cbba8

1 file changed

Lines changed: 33 additions & 16 deletions

File tree

lib/src/utils/parse_live_list.dart

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@ import '../../parse_server_sdk.dart';
66

77
// ignore_for_file: invalid_use_of_protected_member
88
class ParseLiveList<T extends ParseObject> {
9-
ParseLiveList._(this._query, this._listeningIncludes);
9+
ParseLiveList._(this._query, this._listeningIncludes, this._lazyLoading) {
10+
_debug = isDebugEnabled();
11+
}
1012

1113
static Future<ParseLiveList<T>> create<T extends ParseObject>(
1214
QueryBuilder<T> _query,
1315
{bool listenOnAllSubItems,
14-
List<String> listeningIncludes}) {
16+
List<String> listeningIncludes,
17+
bool lazyLoading = true}) {
1518
final ParseLiveList<T> parseLiveList = ParseLiveList<T>._(
1619
_query,
1720
listenOnAllSubItems == true
1821
? _toIncludeMap(
1922
_query.limiters['include']?.toString()?.split(',') ??
2023
<String>[])
21-
: _toIncludeMap(listeningIncludes ?? <String>[]));
24+
: _toIncludeMap(listeningIncludes ?? <String>[]),
25+
lazyLoading);
2226

2327
return parseLiveList._init().then((_) {
2428
return parseLiveList;
@@ -28,6 +32,7 @@ class ParseLiveList<T extends ParseObject> {
2832
List<ParseLiveListElement<T>> _list = List<ParseLiveListElement<T>>();
2933
StreamController<ParseLiveListEvent<T>> _eventStreamController;
3034
int _nextID = 0;
35+
bool _debug;
3136

3237
/// is object1 listed after object2?
3338
/// can return null
@@ -89,6 +94,8 @@ class ParseLiveList<T extends ParseObject> {
8994
//The included Items, where LiveList should look for updates.
9095
final Map<String, dynamic> _listeningIncludes;
9196

97+
final bool _lazyLoading;
98+
9299
int get size {
93100
return _list.length;
94101
}
@@ -122,18 +129,21 @@ class ParseLiveList<T extends ParseObject> {
122129

123130
Future<ParseResponse> _runQuery() async {
124131
final QueryBuilder<T> query = QueryBuilder<T>.copy(_query);
125-
if (query.limiters.containsKey('order')) {
126-
query.keysToReturn(
127-
query.limiters['order'].toString().split(',').map((String string) {
128-
if (string.startsWith('-')) {
129-
return string.substring(1);
130-
}
131-
return string;
132-
}).toList());
133-
} else {
134-
query.keysToReturn(List<String>());
132+
if (_debug)
133+
print('ParseLiveList: lazyLoading is ${_lazyLoading ? 'on' : 'off'}');
134+
if (_lazyLoading) {
135+
if (query.limiters.containsKey('order')) {
136+
query.keysToReturn(
137+
query.limiters['order'].toString().split(',').map((String string) {
138+
if (string.startsWith('-')) {
139+
return string.substring(1);
140+
}
141+
return string;
142+
}).toList());
143+
} else {
144+
query.keysToReturn(List<String>());
145+
}
135146
}
136-
137147
return await query.query<T>();
138148
}
139149

@@ -145,7 +155,8 @@ class ParseLiveList<T extends ParseObject> {
145155
_list = parseResponse.results
146156
?.map<ParseLiveListElement<T>>((dynamic element) =>
147157
ParseLiveListElement<T>(element,
148-
updatedSubItems: _listeningIncludes))
158+
updatedSubItems: _listeningIncludes,
159+
loaded: !_lazyLoading))
149160
?.toList() ??
150161
List<ParseLiveListElement<T>>();
151162
}
@@ -748,6 +759,7 @@ class ParseLiveListWidget<T extends ParseObject> extends StatefulWidget {
748759
this.removedItemBuilder,
749760
this.listenOnAllSubItems,
750761
this.listeningIncludes,
762+
this.lazyLoading = true,
751763
}) : super(key: key);
752764

753765
final QueryBuilder<T> query;
@@ -768,12 +780,15 @@ class ParseLiveListWidget<T extends ParseObject> extends StatefulWidget {
768780
final bool listenOnAllSubItems;
769781
final List<String> listeningIncludes;
770782

783+
final bool lazyLoading;
784+
771785
@override
772786
_ParseLiveListWidgetState<T> createState() => _ParseLiveListWidgetState<T>(
773787
query: query,
774788
removedItemBuilder: removedItemBuilder,
775789
listenOnAllSubItems: listenOnAllSubItems,
776790
listeningIncludes: listeningIncludes,
791+
lazyLoading: lazyLoading,
777792
);
778793

779794
static Widget defaultChildBuilder<T extends ParseObject>(
@@ -802,11 +817,13 @@ class _ParseLiveListWidgetState<T extends ParseObject>
802817
{@required this.query,
803818
@required this.removedItemBuilder,
804819
bool listenOnAllSubItems,
805-
List<String> listeningIncludes}) {
820+
List<String> listeningIncludes,
821+
bool lazyLoading = true}) {
806822
ParseLiveList.create(
807823
query,
808824
listenOnAllSubItems: listenOnAllSubItems,
809825
listeningIncludes: listeningIncludes,
826+
lazyLoading: lazyLoading,
810827
).then((ParseLiveList<T> value) {
811828
setState(() {
812829
_liveList = value;

0 commit comments

Comments
 (0)