Showing
2 changed files
with
47 additions
and
50 deletions
| @@ -11,17 +11,12 @@ class HomeController extends StateController<CasesModel> { | @@ -11,17 +11,12 @@ class HomeController extends StateController<CasesModel> { | ||
| 11 | @override | 11 | @override |
| 12 | void onInit() { | 12 | void onInit() { |
| 13 | super.onInit(); | 13 | super.onInit(); |
| 14 | - | ||
| 15 | //Loading, Success, Error handle with 1 line of code | 14 | //Loading, Success, Error handle with 1 line of code |
| 16 | - listenFuture(() => homeRepository.getCases); | 15 | + futurize(() => homeRepository.getCases); |
| 17 | } | 16 | } |
| 18 | 17 | ||
| 19 | Country getCountryById(String id) { | 18 | Country getCountryById(String id) { |
| 20 | final index = int.tryParse(id); | 19 | final index = int.tryParse(id); |
| 21 | - if (index != null) { | ||
| 22 | - return state.countries[index]; | ||
| 23 | - } | ||
| 24 | - | ||
| 25 | - return state.countries.first; | 20 | + return index != null ? state.countries[index] : state.countries.first; |
| 26 | } | 21 | } |
| 27 | } | 22 | } |
| @@ -7,7 +7,7 @@ import '../../../instance_manager.dart'; | @@ -7,7 +7,7 @@ import '../../../instance_manager.dart'; | ||
| 7 | import '../../get_state_manager.dart'; | 7 | import '../../get_state_manager.dart'; |
| 8 | import '../simple/list_notifier.dart'; | 8 | import '../simple/list_notifier.dart'; |
| 9 | 9 | ||
| 10 | -extension _NullOrEmpty on Object { | 10 | +extension _Empty on Object { |
| 11 | bool _isEmpty() { | 11 | bool _isEmpty() { |
| 12 | final val = this; | 12 | final val = this; |
| 13 | // if (val == null) return true; | 13 | // if (val == null) return true; |
| @@ -25,21 +25,31 @@ extension _NullOrEmpty on Object { | @@ -25,21 +25,31 @@ extension _NullOrEmpty on Object { | ||
| 25 | 25 | ||
| 26 | mixin StateMixin<T> on ListNotifier { | 26 | mixin StateMixin<T> on ListNotifier { |
| 27 | late T _value; | 27 | late T _value; |
| 28 | - GetState? _status; | 28 | + GetState<T>? _status; |
| 29 | 29 | ||
| 30 | - void _fillEmptyStatus() { | 30 | + void _fillInitialStatus() { |
| 31 | _status = (value == null || value!._isEmpty()) | 31 | _status = (value == null || value!._isEmpty()) |
| 32 | - ? GetState.loading() | ||
| 33 | - : GetState.success(_status); | 32 | + ? GetState<T>.loading() |
| 33 | + : GetState<T>.success(_value); | ||
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | - GetState get status { | 36 | + GetState<T> get status { |
| 37 | reportRead(); | 37 | reportRead(); |
| 38 | return _status ??= _status = GetState.loading(); | 38 | return _status ??= _status = GetState.loading(); |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | T get state => value; | 41 | T get state => value; |
| 42 | 42 | ||
| 43 | + set status(GetState<T> newStatus) { | ||
| 44 | + if (newStatus == status) return; | ||
| 45 | + _status = newStatus; | ||
| 46 | + if (newStatus is SuccessState<T>) { | ||
| 47 | + _value = newStatus.data!; | ||
| 48 | + return; | ||
| 49 | + } | ||
| 50 | + refresh(); | ||
| 51 | + } | ||
| 52 | + | ||
| 43 | @protected | 53 | @protected |
| 44 | T get value { | 54 | T get value { |
| 45 | reportRead(); | 55 | reportRead(); |
| @@ -53,33 +63,17 @@ mixin StateMixin<T> on ListNotifier { | @@ -53,33 +63,17 @@ mixin StateMixin<T> on ListNotifier { | ||
| 53 | refresh(); | 63 | refresh(); |
| 54 | } | 64 | } |
| 55 | 65 | ||
| 56 | - @protected | ||
| 57 | - void change(T newState, {GetState? status}) { | ||
| 58 | - var _canUpdate = false; | ||
| 59 | - if (status != null) { | ||
| 60 | - _status = status; | ||
| 61 | - _canUpdate = true; | ||
| 62 | - } | ||
| 63 | - if (newState != _value) { | ||
| 64 | - _value = newState; | ||
| 65 | - _canUpdate = true; | ||
| 66 | - } | ||
| 67 | - if (_canUpdate) { | ||
| 68 | - refresh(); | ||
| 69 | - } | ||
| 70 | - } | ||
| 71 | - | ||
| 72 | - void listenFuture(Future<T> Function() body(), | 66 | + void futurize(Future<T> Function() body(), |
| 73 | {String? errorMessage, bool useEmpty = true}) { | 67 | {String? errorMessage, bool useEmpty = true}) { |
| 74 | final compute = body(); | 68 | final compute = body(); |
| 75 | compute().then((newValue) { | 69 | compute().then((newValue) { |
| 76 | if ((newValue == null || newValue._isEmpty()) && useEmpty) { | 70 | if ((newValue == null || newValue._isEmpty()) && useEmpty) { |
| 77 | - change(newValue, status: GetState.loading()); | 71 | + status = GetState<T>.loading(); |
| 78 | } else { | 72 | } else { |
| 79 | - change(newValue, status: GetState.success(newValue)); | 73 | + status = GetState<T>.success(newValue); |
| 80 | } | 74 | } |
| 81 | }, onError: (err) { | 75 | }, onError: (err) { |
| 82 | - change(state, status: GetState.error(errorMessage ?? err.toString())); | 76 | + status = GetState.error(errorMessage ?? err.toString()); |
| 83 | }); | 77 | }); |
| 84 | } | 78 | } |
| 85 | } | 79 | } |
| @@ -160,7 +154,7 @@ class Value<T> extends ListNotifier | @@ -160,7 +154,7 @@ class Value<T> extends ListNotifier | ||
| 160 | implements ValueListenable<T?> { | 154 | implements ValueListenable<T?> { |
| 161 | Value(T val) { | 155 | Value(T val) { |
| 162 | _value = val; | 156 | _value = val; |
| 163 | - _fillEmptyStatus(); | 157 | + _fillInitialStatus(); |
| 164 | } | 158 | } |
| 165 | 159 | ||
| 166 | @override | 160 | @override |
| @@ -226,36 +220,36 @@ typedef NotifierBuilder<T> = Widget Function(T state); | @@ -226,36 +220,36 @@ typedef NotifierBuilder<T> = Widget Function(T state); | ||
| 226 | 220 | ||
| 227 | abstract class GetState<T> { | 221 | abstract class GetState<T> { |
| 228 | const GetState(); | 222 | const GetState(); |
| 229 | - factory GetState.loading() => GLoading(); | ||
| 230 | - factory GetState.error(String message) => GError(message); | ||
| 231 | - factory GetState.empty() => GEmpty(); | ||
| 232 | - factory GetState.success(T data) => GSuccess(data); | 223 | + factory GetState.loading() => LoadingState(); |
| 224 | + factory GetState.error(String message) => ErrorState(message); | ||
| 225 | + factory GetState.empty() => EmptyState(); | ||
| 226 | + factory GetState.success(T data) => SuccessState(data); | ||
| 233 | } | 227 | } |
| 234 | 228 | ||
| 235 | -class GLoading<T> extends GetState<T> {} | 229 | +class LoadingState<T> extends GetState<T> {} |
| 236 | 230 | ||
| 237 | -class GSuccess<T> extends GetState<T> { | 231 | +class SuccessState<T> extends GetState<T> { |
| 238 | final T data; | 232 | final T data; |
| 239 | 233 | ||
| 240 | - GSuccess(this.data); | 234 | + SuccessState(this.data); |
| 241 | } | 235 | } |
| 242 | 236 | ||
| 243 | -class GError<T, S> extends GetState<T> { | 237 | +class ErrorState<T, S> extends GetState<T> { |
| 244 | final S? error; | 238 | final S? error; |
| 245 | - GError([this.error]); | 239 | + ErrorState([this.error]); |
| 246 | } | 240 | } |
| 247 | 241 | ||
| 248 | -class GEmpty<T> extends GetState<T> {} | 242 | +class EmptyState<T> extends GetState<T> {} |
| 249 | 243 | ||
| 250 | extension StatusDataExt<T> on GetState<T> { | 244 | extension StatusDataExt<T> on GetState<T> { |
| 251 | - bool get isLoading => this is GLoading; | ||
| 252 | - bool get isSuccess => this is GSuccess; | ||
| 253 | - bool get isError => this is GError; | ||
| 254 | - bool get isEmpty => this is GEmpty; | 245 | + bool get isLoading => this is LoadingState; |
| 246 | + bool get isSuccess => this is SuccessState; | ||
| 247 | + bool get isError => this is ErrorState; | ||
| 248 | + bool get isEmpty => this is EmptyState; | ||
| 255 | String get errorMessage { | 249 | String get errorMessage { |
| 256 | - final isError = this is GError; | 250 | + final isError = this is ErrorState; |
| 257 | if (isError) { | 251 | if (isError) { |
| 258 | - final err = this as GError; | 252 | + final err = this as ErrorState; |
| 259 | if (err.error != null && err.error is String) { | 253 | if (err.error != null && err.error is String) { |
| 260 | return err.error as String; | 254 | return err.error as String; |
| 261 | } | 255 | } |
| @@ -263,4 +257,12 @@ extension StatusDataExt<T> on GetState<T> { | @@ -263,4 +257,12 @@ extension StatusDataExt<T> on GetState<T> { | ||
| 263 | 257 | ||
| 264 | return ''; | 258 | return ''; |
| 265 | } | 259 | } |
| 260 | + | ||
| 261 | + T? get data { | ||
| 262 | + if (this is SuccessState<T>) { | ||
| 263 | + final success = this as SuccessState<T>; | ||
| 264 | + return success.data; | ||
| 265 | + } | ||
| 266 | + return null; | ||
| 267 | + } | ||
| 266 | } | 268 | } |
-
Please register or login to post a comment