Jonatas Borges

add ScrollMixin and isLoadingMore to controller status

@@ -152,51 +152,36 @@ class RxStatus { @@ -152,51 +152,36 @@ class RxStatus {
152 final bool isError; 152 final bool isError;
153 final bool isSuccess; 153 final bool isSuccess;
154 final bool isEmpty; 154 final bool isEmpty;
  155 + final bool isLoadingMore;
155 final String errorMessage; 156 final String errorMessage;
156 157
157 RxStatus._({ 158 RxStatus._({
158 - this.isEmpty,  
159 - this.isLoading,  
160 - this.isError,  
161 - this.isSuccess, 159 + this.isEmpty = false,
  160 + this.isLoading = false,
  161 + this.isError = false,
  162 + this.isSuccess = false,
162 this.errorMessage, 163 this.errorMessage,
  164 + this.isLoadingMore = false,
163 }); 165 });
164 166
165 factory RxStatus.loading() { 167 factory RxStatus.loading() {
166 - return RxStatus._(  
167 - isLoading: true,  
168 - isError: false,  
169 - isSuccess: false,  
170 - isEmpty: false,  
171 - ); 168 + return RxStatus._(isLoading: true);
  169 + }
  170 +
  171 + factory RxStatus.loadingMore() {
  172 + return RxStatus._(isSuccess: true, isLoadingMore: true);
172 } 173 }
173 174
174 factory RxStatus.success() { 175 factory RxStatus.success() {
175 - return RxStatus._(  
176 - isLoading: false,  
177 - isError: false,  
178 - isSuccess: true,  
179 - isEmpty: false,  
180 - ); 176 + return RxStatus._(isSuccess: true);
181 } 177 }
182 178
183 factory RxStatus.error([String message]) { 179 factory RxStatus.error([String message]) {
184 - return RxStatus._(  
185 - isLoading: false,  
186 - isError: true,  
187 - isSuccess: false,  
188 - isEmpty: false,  
189 - errorMessage: message,  
190 - ); 180 + return RxStatus._(isError: true, errorMessage: message);
191 } 181 }
192 182
193 factory RxStatus.empty() { 183 factory RxStatus.empty() {
194 - return RxStatus._(  
195 - isLoading: false,  
196 - isError: false,  
197 - isSuccess: false,  
198 - isEmpty: true,  
199 - ); 184 + return RxStatus._(isEmpty: true);
200 } 185 }
201 } 186 }
202 187
1 // ignore: prefer_mixin 1 // ignore: prefer_mixin
2 import 'package:flutter/widgets.dart'; 2 import 'package:flutter/widgets.dart';
  3 +import '../../../instance_manager.dart';
3 4
4 import '../rx_flutter/rx_disposable.dart'; 5 import '../rx_flutter/rx_disposable.dart';
5 import '../rx_flutter/rx_notifier.dart'; 6 import '../rx_flutter/rx_notifier.dart';
@@ -26,6 +27,50 @@ abstract class GetxController extends DisposableInterface with ListNotifier { @@ -26,6 +27,50 @@ abstract class GetxController extends DisposableInterface with ListNotifier {
26 } 27 }
27 } 28 }
28 29
  30 +mixin ScrollMixin on GetLifeCycleBase {
  31 + final ScrollController scroll = ScrollController();
  32 +
  33 + @override
  34 + void onInit() {
  35 + super.onInit();
  36 + scroll.addListener(_listener);
  37 + }
  38 +
  39 + bool _canFetchBottom = true;
  40 +
  41 + bool _canFetchTop = true;
  42 +
  43 + void _listener() {
  44 + if (scroll.position.atEdge) {
  45 + _checkIfCanLoadMore();
  46 + }
  47 + }
  48 +
  49 + Future<void> _checkIfCanLoadMore() async {
  50 + if (scroll.position.pixels == 0) {
  51 + if (!_canFetchTop) return;
  52 + _canFetchTop = false;
  53 + await onTopScroll();
  54 + _canFetchTop = true;
  55 + } else {
  56 + if (!_canFetchBottom) return;
  57 + _canFetchBottom = false;
  58 + await onEndScroll();
  59 + _canFetchBottom = true;
  60 + }
  61 + }
  62 +
  63 + Future<void> onEndScroll();
  64 +
  65 + Future<void> onTopScroll();
  66 +
  67 + @override
  68 + void onClose() {
  69 + scroll.removeListener(_listener);
  70 + super.onClose();
  71 + }
  72 +}
  73 +
29 abstract class RxController extends DisposableInterface {} 74 abstract class RxController extends DisposableInterface {}
30 75
31 abstract class SuperController<T> extends FullLifeCycleController 76 abstract class SuperController<T> extends FullLifeCycleController