-
-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathparse_array.dart
123 lines (100 loc) · 3.23 KB
/
parse_array.dart
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
part of '../../parse_server_sdk.dart';
class _ParseArray implements _Valuable<List>, _ParseSaveStateAwareChild {
_ParseArray({this.setMode = false});
bool setMode;
List _savedArray = [];
List estimatedArray = [];
set savedArray(List array) {
_savedArray = array.toList();
estimatedArray = array.toList();
}
List get savedArray => _savedArray;
_ParseArrayOperation? lastPreformedOperation;
_ParseArray preformArrayOperation(
_ParseArrayOperation arrayOperation,
) {
arrayOperation.mergeWithPrevious(lastPreformedOperation ?? this);
lastPreformedOperation = arrayOperation;
estimatedArray = lastPreformedOperation!.value.toList();
if (setMode) {
lastPreformedOperation = null;
}
return this;
}
Object toJson({bool full = false}) {
if (full) {
return {
'className': 'ParseArray',
'estimatedArray': parseEncode(estimatedArray, full: full),
'savedArray': parseEncode(_savedArray, full: full),
'lastPreformedOperation': lastPreformedOperation?.toJson(full: full)
};
}
return lastPreformedOperation?.toJson(full: full) ??
parseEncode(estimatedArray, full: full);
}
factory _ParseArray.fromFullJson(Map<String, dynamic> json) {
return _ParseArray()
.._savedArray = parseDecode(json['savedArray'])
..estimatedArray = parseDecode(json['estimatedArray'])
..lastPreformedOperation = json['lastPreformedOperation'] == null
? null
: _ParseArrayOperation.fromFullJson(json['lastPreformedOperation']);
}
@override
List getValue() {
return estimatedArray.toList();
}
_ParseArrayOperation? _lastPreformedOperationBeforeSaving;
List? _estimatedArrayBeforeSaving;
@override
@mustCallSuper
void onSaved() {
setMode = false;
if(_estimatedArrayBeforeSaving != null){
_savedArray.clear();
_savedArray.addAll(_estimatedArrayBeforeSaving!);
_estimatedArrayBeforeSaving = null;
}
if (_lastPreformedOperationBeforeSaving == lastPreformedOperation) {
// No operations were performed during the save process
lastPreformedOperation = null;
} else {
// remove the saved objects and keep the new added objects while saving
if (lastPreformedOperation is _ParseRemoveOperation) {
lastPreformedOperation?.valueForApiRequest
.retainWhere((e) => _savedArray.contains(e));
} else {
lastPreformedOperation?.valueForApiRequest
.removeWhere((e) => _savedArray.contains(e));
}
}
_lastPreformedOperationBeforeSaving = null;
}
@override
@mustCallSuper
void onSaving() {
_lastPreformedOperationBeforeSaving = lastPreformedOperation;
_estimatedArrayBeforeSaving = estimatedArray.toList();
}
@override
@mustCallSuper
void onRevertSaving() {
_lastPreformedOperationBeforeSaving = null;
_estimatedArrayBeforeSaving = null;
}
@override
@mustCallSuper
void onErrorSaving() {
_lastPreformedOperationBeforeSaving = null;
_estimatedArrayBeforeSaving = null;
}
@override
@mustCallSuper
void onClearUnsaved() {
estimatedArray = savedArray;
lastPreformedOperation = null;
_lastPreformedOperationBeforeSaving = null;
_estimatedArrayBeforeSaving = null;
}
}