Showing
1 changed file
with
37 additions
and
2 deletions
@@ -229,9 +229,41 @@ class Rx<T> extends _RxImpl<T> { | @@ -229,9 +229,41 @@ class Rx<T> extends _RxImpl<T> { | ||
229 | dynamic toJson() => (value as dynamic)?.toJson(); | 229 | dynamic toJson() => (value as dynamic)?.toJson(); |
230 | } | 230 | } |
231 | 231 | ||
232 | +enum RxStatus { loading, error, success } | ||
233 | + | ||
232 | /// It's Experimental class, the Api can be change | 234 | /// It's Experimental class, the Api can be change |
233 | abstract class RxState<T> extends _RxImpl<T> { | 235 | abstract class RxState<T> extends _RxImpl<T> { |
234 | - RxState(T initial) : super(initial); | 236 | + RxState(T initial) : super(initial) { |
237 | + _fillEmptyStatus(); | ||
238 | + } | ||
239 | + | ||
240 | + RxStatus _status; | ||
241 | + | ||
242 | + bool get isNullOrEmpty { | ||
243 | + if (_value == null) return true; | ||
244 | + dynamic val = _value; | ||
245 | + var result = false; | ||
246 | + if (val is Iterable) { | ||
247 | + result = val.isEmpty; | ||
248 | + } else if (val is String) { | ||
249 | + result = val.isEmpty; | ||
250 | + } else if (val is Map) { | ||
251 | + result = val.isEmpty; | ||
252 | + } | ||
253 | + return result; | ||
254 | + } | ||
255 | + | ||
256 | + void _fillEmptyStatus() { | ||
257 | + _status = isNullOrEmpty ? RxStatus.loading : RxStatus.success; | ||
258 | + } | ||
259 | + | ||
260 | + RxStatus get status { | ||
261 | + return _status; | ||
262 | + } | ||
263 | + | ||
264 | + bool get isLoading => _status == RxStatus.loading; | ||
265 | + bool get hasError => _status == RxStatus.error; | ||
266 | + bool get hasData => _status == RxStatus.success; | ||
235 | 267 | ||
236 | @protected | 268 | @protected |
237 | void refresh() { | 269 | void refresh() { |
@@ -259,7 +291,10 @@ abstract class RxState<T> extends _RxImpl<T> { | @@ -259,7 +291,10 @@ abstract class RxState<T> extends _RxImpl<T> { | ||
259 | } | 291 | } |
260 | 292 | ||
261 | @protected | 293 | @protected |
262 | - void change(T newState) { | 294 | + void change(T newState, {RxStatus status}) { |
295 | + if (status != null) { | ||
296 | + _status = status; | ||
297 | + } | ||
263 | if (newState != _value) { | 298 | if (newState != _value) { |
264 | value = newState; | 299 | value = newState; |
265 | } | 300 | } |
-
Please register or login to post a comment