get_state.dart
8.21 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import 'dart:collection';
import 'package:flutter/widgets.dart';
import '../../../state_manager.dart';
import '../../instance/get_instance.dart';
import '../../navigation/root/smart_management.dart';
import '../rx/rx_core/rx_interface.dart';
import 'simple_builder.dart';
// Changed to VoidCallback.
//typedef Disposer = void Function();
// replacing StateSetter, return if the Widget is mounted for extra validation.
// if it brings overhead the extra call,
typedef GetStateUpdate = void Function();
//typedef GetStateUpdate = void Function(VoidCallback fn);
/// Complies with [GetStateUpdater]
///
/// This mixin's function represents a [GetStateUpdater], and might be used
/// by [GetBuilder()], [SimpleBuilder()] (or similar) to comply
/// with [GetStateUpdate] signature. REPLACING the [StateSetter].
/// Avoids the potential (but extremely unlikely) issue of having
/// the Widget in a dispose() state, and abstracts the
/// API from the ugly fn((){}).
// TODO: check performance HIT for the extra method call.
mixin GetStateUpdaterMixin<T extends StatefulWidget> on State<T> {
// To avoid the creation of an anonym function to be GC later.
// ignore: prefer_function_declarations_over_variables
static final VoidCallback _stateCallback = () {};
/// Experimental method to replace setState((){});
/// Used with GetStateUpdate.
void getUpdate() {
if (mounted) setState(_stateCallback);
}
}
class GetxController extends DisposableInterface {
final _updaters = HashSet<GetStateUpdate>();
// final _updatersIds = HashMap<String, StateSetter>(); //<old>
final _updatersIds = HashMap<String, GetStateUpdate>();
final _updatersGroupIds = HashMap<String, HashSet<GetStateUpdate>>();
/// Rebuilds [GetBuilder] each time you call [update()];
/// Can take a List of [ids], that will only update the matching
/// `GetBuilder( id: )`,
/// [ids] can be reused among `GetBuilders` like group tags.
/// The update will only notify the Widgets, if [condition] is true.
void update([List<String> ids, bool condition = true]) {
if (!condition) {
return;
}
if (ids == null) {
// _updaters?.forEach((rs) => rs(() {})); //<old>
for (final updater in _updaters) {
updater();
}
} else {
// @jonny, remove this commented code if it's not more optimized.
// for (final id in ids) {
// if (_updatersIds[id] != null) _updatersIds[id]();
// if (_updatersGroupIds[id] != null)
// for (final rs in _updatersGroupIds[id]) rs();
// }
for (final id in ids) {
_updatersIds[id]?.call();
// ignore: avoid_function_literals_in_foreach_calls
_updatersGroupIds[id]?.forEach((rs) => rs());
}
}
}
// VoidCallback addListener(StateSetter listener) {//<old>
VoidCallback addListener(GetStateUpdate listener) {
_updaters.add(listener);
return () => _updaters.remove(listener);
}
// VoidCallback addListenerId(String key, StateSetter listener) {//<old>
VoidCallback addListenerId(String key, GetStateUpdate listener) {
// _printCurrentIds();
if (_updatersIds.containsKey(key)) {
_updatersGroupIds[key] ??= HashSet<GetStateUpdate>.identity();
_updatersGroupIds[key].add(listener);
return () {
_updatersGroupIds[key].remove(listener);
};
} else {
_updatersIds[key] = listener;
return () => _updatersIds.remove(key);
}
}
/// To dispose an [id] from future updates(), this ids are registered
/// by [GetBuilder()] or similar, so is a way to unlink the state change with
/// the Widget from the Controller.
void disposeId(String id) {
_updatersIds.remove(id);
_updatersGroupIds.remove(id);
}
/// Remove this after checking the new implementation makes sense.
/// Uncomment this if you wanna control the removal of ids..
/// bool _debugging = false;
/// Future<void> _printCurrentIds() async {
/// if (_debugging) return;
/// _debugging = true;
/// print('about to debug...');
/// await Future.delayed(Duration(milliseconds: 10));
/// int totalGroups = 0;
/// _updatersGroupIds.forEach((key, value) {
/// totalGroups += value.length;
/// });
/// int totalIds = _updatersIds.length;
/// print(
/// 'Total: ${totalIds + totalGroups},'+
/// 'in groups:$totalGroups, solo ids:$totalIds',);
/// _debugging = false;
/// }
}
class GetBuilder<T extends GetxController> extends StatefulWidget {
final Widget Function(T) builder;
final bool global;
final String id;
final String tag;
final bool autoRemove;
final bool assignId;
final void Function(State state) initState, dispose, didChangeDependencies;
final void Function(GetBuilder oldWidget, State state) didUpdateWidget;
final T init;
const GetBuilder({
Key key,
this.init,
this.global = true,
@required this.builder,
this.autoRemove = true,
this.assignId = false,
this.initState,
this.tag,
this.dispose,
this.id,
this.didChangeDependencies,
this.didUpdateWidget,
}) : assert(builder != null),
super(key: key);
@override
_GetBuilderState<T> createState() => _GetBuilderState<T>();
}
class _GetBuilderState<T extends GetxController> extends State<GetBuilder<T>>
with GetStateUpdaterMixin {
T controller;
bool isCreator = false;
VoidCallback remove;
@override
void initState() {
super.initState();
if (widget.initState != null) widget.initState(this);
if (widget.global) {
final isPrepared = GetInstance().isPrepared<T>(tag: widget.tag);
final isRegistered = GetInstance().isRegistered<T>(tag: widget.tag);
if (isPrepared) {
if (GetConfig.smartManagement != SmartManagement.keepFactory) {
isCreator = true;
}
controller = GetInstance().find<T>(tag: widget.tag);
} else if (isRegistered) {
controller = GetInstance().find<T>(tag: widget.tag);
isCreator = false;
} else {
controller = widget.init;
isCreator = true;
GetInstance().put<T>(controller, tag: widget.tag);
}
} else {
controller = widget.init;
isCreator = true;
controller?.onStart();
}
if (widget.global &&
GetConfig.smartManagement == SmartManagement.onlyBuilder) {
controller?.onStart();
}
_subscribeToController();
}
/// Register to listen Controller's events.
/// It gets a reference to the remove() callback, to delete the
/// setState "link" from the Controller.
void _subscribeToController() {
remove?.call();
remove = (widget.id == null)
// ? controller?.addListener(setState) //<old>
// : controller?.addListenerId(widget.id, setState); //<old>
? controller?.addListener(getUpdate)
: controller?.addListenerId(widget.id, getUpdate);
}
/// Sample for [GetStateUpdate] when you don't wanna
/// use [GetStateHelper mixin].
/// bool _getUpdater() {
/// final _mounted = mounted;
/// if (_mounted) setState(() {});
/// return _mounted;
/// }
@override
void dispose() {
super.dispose();
if (widget.dispose != null) widget.dispose(this);
if (isCreator || widget.assignId) {
if (widget.autoRemove && GetInstance().isRegistered<T>(tag: widget.tag)) {
GetInstance().delete<T>(tag: widget.tag);
}
}
remove?.call();
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
if (widget.didChangeDependencies != null) {
widget.didChangeDependencies(this);
}
}
@override
void didUpdateWidget(GetBuilder oldWidget) {
super.didUpdateWidget(oldWidget as GetBuilder<T>);
// to avoid conflicts when modifying a "grouped" id list.
if (oldWidget.id != widget.id) {
_subscribeToController();
}
if (widget.didUpdateWidget != null) widget.didUpdateWidget(oldWidget, this);
}
@override
Widget build(BuildContext context) => widget.builder(controller);
}
/// This is a experimental feature.
/// Meant to be used with SimpleBuilder, it auto-registers the variable
/// like Rx() does with Obx().
class Value<T> extends GetxController {
Value([this._value]);
T _value;
T get value {
TaskManager.instance.notify(_updaters);
return _value;
}
set value(T newValue) {
if (_value == newValue) return;
_value = newValue;
update();
}
}