Showing
5 changed files
with
35 additions
and
11 deletions
| @@ -3,7 +3,7 @@ import 'package:get/get.dart'; | @@ -3,7 +3,7 @@ import 'package:get/get.dart'; | ||
| 3 | import '../../domain/adapters/repository_adapter.dart'; | 3 | import '../../domain/adapters/repository_adapter.dart'; | 
| 4 | import '../../domain/entity/cases_model.dart'; | 4 | import '../../domain/entity/cases_model.dart'; | 
| 5 | 5 | ||
| 6 | -class HomeController extends GetxController with StatusMixin<CasesModel> { | 6 | +class HomeController extends GetxController with StateMixin<CasesModel> { | 
| 7 | HomeController({this.homeRepository}); | 7 | HomeController({this.homeRepository}); | 
| 8 | 8 | ||
| 9 | /// inject repo abstraction dependency | 9 | /// inject repo abstraction dependency | 
| @@ -29,9 +29,9 @@ class CountryView extends GetView<HomeController> { | @@ -29,9 +29,9 @@ class CountryView extends GetView<HomeController> { | ||
| 29 | ), | 29 | ), | 
| 30 | body: Center( | 30 | body: Center( | 
| 31 | child: ListView.builder( | 31 | child: ListView.builder( | 
| 32 | - itemCount: controller.value.countries.length, | 32 | + itemCount: controller.state.countries.length, | 
| 33 | itemBuilder: (context, index) { | 33 | itemBuilder: (context, index) { | 
| 34 | - final country = controller.value.countries[index]; | 34 | + final country = controller.state.countries[index]; | 
| 35 | return ListTile( | 35 | return ListTile( | 
| 36 | onTap: () { | 36 | onTap: () { | 
| 37 | Get.toNamed('/details', arguments: country); | 37 | Get.toNamed('/details', arguments: country); | 
| @@ -27,7 +27,7 @@ class HomeView extends GetView<HomeController> { | @@ -27,7 +27,7 @@ class HomeView extends GetView<HomeController> { | ||
| 27 | centerTitle: true, | 27 | centerTitle: true, | 
| 28 | ), | 28 | ), | 
| 29 | body: Center( | 29 | body: Center( | 
| 30 | - child: controller( | 30 | + child: controller.obx( | 
| 31 | (state) { | 31 | (state) { | 
| 32 | return Column( | 32 | return Column( | 
| 33 | mainAxisAlignment: MainAxisAlignment.center, | 33 | mainAxisAlignment: MainAxisAlignment.center, | 
| @@ -65,12 +65,12 @@ void main() { | @@ -65,12 +65,12 @@ void main() { | ||
| 65 | await Future.delayed(Duration(milliseconds: 100)); | 65 | await Future.delayed(Duration(milliseconds: 100)); | 
| 66 | 66 | ||
| 67 | if (controller.status.isError) { | 67 | if (controller.status.isError) { | 
| 68 | - expect(controller.value, null); | 68 | + expect(controller.state, null); | 
| 69 | } | 69 | } | 
| 70 | 70 | ||
| 71 | if (controller.status.isSuccess) { | 71 | if (controller.status.isSuccess) { | 
| 72 | - expect(controller.value.global.totalDeaths, 100); | ||
| 73 | - expect(controller.value.global.totalConfirmed, 200); | 72 | + expect(controller.state.global.totalDeaths, 100); | 
| 73 | + expect(controller.state.global.totalConfirmed, 200); | ||
| 74 | } | 74 | } | 
| 75 | }); | 75 | }); | 
| 76 | 76 | 
| @@ -5,7 +5,7 @@ import '../../../instance_manager.dart'; | @@ -5,7 +5,7 @@ import '../../../instance_manager.dart'; | ||
| 5 | import '../../get_state_manager.dart'; | 5 | import '../../get_state_manager.dart'; | 
| 6 | import '../simple/list_notifier.dart'; | 6 | import '../simple/list_notifier.dart'; | 
| 7 | 7 | ||
| 8 | -mixin StatusMixin<T> on ListNotifier { | 8 | +mixin StateMixin<T> on ListNotifier { | 
| 9 | T _value; | 9 | T _value; | 
| 10 | RxStatus _status; | 10 | RxStatus _status; | 
| 11 | 11 | ||
| @@ -31,11 +31,15 @@ mixin StatusMixin<T> on ListNotifier { | @@ -31,11 +31,15 @@ mixin StatusMixin<T> on ListNotifier { | ||
| 31 | return _status ??= _status = RxStatus.loading(); | 31 | return _status ??= _status = RxStatus.loading(); | 
| 32 | } | 32 | } | 
| 33 | 33 | ||
| 34 | + T get state => value; | ||
| 35 | + | ||
| 36 | + @protected | ||
| 34 | T get value { | 37 | T get value { | 
| 35 | notifyChildrens(); | 38 | notifyChildrens(); | 
| 36 | return _value; | 39 | return _value; | 
| 37 | } | 40 | } | 
| 38 | 41 | ||
| 42 | + @protected | ||
| 39 | set value(T newValue) { | 43 | set value(T newValue) { | 
| 40 | if (_value == newValue) return; | 44 | if (_value == newValue) return; | 
| 41 | _value = newValue; | 45 | _value = newValue; | 
| @@ -60,13 +64,33 @@ mixin StatusMixin<T> on ListNotifier { | @@ -60,13 +64,33 @@ mixin StatusMixin<T> on ListNotifier { | ||
| 60 | } | 64 | } | 
| 61 | 65 | ||
| 62 | class Value<T> extends ListNotifier | 66 | class Value<T> extends ListNotifier | 
| 63 | - with StatusMixin<T> | 67 | + with StateMixin<T> | 
| 64 | implements ValueListenable<T> { | 68 | implements ValueListenable<T> { | 
| 65 | Value(T val) { | 69 | Value(T val) { | 
| 66 | _value = val; | 70 | _value = val; | 
| 67 | _fillEmptyStatus(); | 71 | _fillEmptyStatus(); | 
| 68 | } | 72 | } | 
| 69 | 73 | ||
| 74 | + @override | ||
| 75 | + T get value { | ||
| 76 | + notifyChildrens(); | ||
| 77 | + return _value; | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + @override | ||
| 81 | + set value(T newValue) { | ||
| 82 | + if (_value == newValue) return; | ||
| 83 | + _value = newValue; | ||
| 84 | + refresh(); | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + T call([T v]) { | ||
| 88 | + if (v != null) { | ||
| 89 | + value = v; | ||
| 90 | + } | ||
| 91 | + return value; | ||
| 92 | + } | ||
| 93 | + | ||
| 70 | void update(void fn(T value)) { | 94 | void update(void fn(T value)) { | 
| 71 | fn(value); | 95 | fn(value); | 
| 72 | refresh(); | 96 | refresh(); | 
| @@ -97,8 +121,8 @@ abstract class GetNotifier<T> extends Value<T> with GetLifeCycleBase { | @@ -97,8 +121,8 @@ abstract class GetNotifier<T> extends Value<T> with GetLifeCycleBase { | ||
| 97 | } | 121 | } | 
| 98 | } | 122 | } | 
| 99 | 123 | ||
| 100 | -extension StateExt<T> on StatusMixin<T> { | ||
| 101 | - Widget call(NotifierBuilder<T> widget, {Widget onError, Widget onLoading}) { | 124 | +extension StateExt<T> on StateMixin<T> { | 
| 125 | + Widget obx(NotifierBuilder<T> widget, {Widget onError, Widget onLoading}) { | ||
| 102 | assert(widget != null); | 126 | assert(widget != null); | 
| 103 | return SimpleBuilder(builder: (_) { | 127 | return SimpleBuilder(builder: (_) { | 
| 104 | if (status.isLoading) { | 128 | if (status.isLoading) { | 
- 
Please register or login to post a comment