| @@ -14,6 +14,15 @@ class EnvironmentParameter { | |||
| this.executeDate, | |||
| this.status}); | |||
| EnvironmentParameter.clone(EnvironmentParameter e) { | |||
| this.id = e.id; | |||
| this.name = e.name; | |||
| this.index = e.index; | |||
| this.activityId = e.activityId; | |||
| this.executeDate = e.executeDate; | |||
| this.status = e.status; | |||
| } | |||
| EnvironmentParameter.fromJson(Map<String, dynamic> json) { | |||
| id = json['id']; | |||
| name = json['name']; | |||
| @@ -2,6 +2,8 @@ import 'dart:async'; | |||
| import 'package:bloc/bloc.dart'; | |||
| import 'package:equatable/equatable.dart'; | |||
| import 'package:farm_tpf/custom_model/Environment.dart'; | |||
| import 'package:farm_tpf/custom_model/EnvironmentParameter.dart'; | |||
| import 'package:farm_tpf/data/api/app_exception.dart'; | |||
| import 'package:farm_tpf/data/repository/repository.dart'; | |||
| import 'package:meta/meta.dart'; | |||
| @@ -13,38 +15,38 @@ class PlotParameterBloc extends Bloc<PlotParameterEvent, PlotParameterState> { | |||
| final Repository repository; | |||
| PlotParameterBloc({@required this.repository}) | |||
| : super(PlotParameterInitial()); | |||
| static int pageSize = 20; | |||
| static int pageSize = 100; | |||
| StreamSubscription _periodicSubscription; | |||
| @override | |||
| Stream<PlotParameterState> mapEventToState( | |||
| PlotParameterEvent event, | |||
| ) async* { | |||
| if (event is DataFetched && | |||
| !(state is PlotParameterSuccess && | |||
| (state as PlotParameterSuccess).hasReachedMax)) { | |||
| if (event is DataFetched) { | |||
| try { | |||
| if (state is PlotParameterInitial) { | |||
| yield PlotParameterLoading(); | |||
| final response = await repository.getEnvironmentParameters( | |||
| cropId: event.cropId, page: 0, size: pageSize); | |||
| List<EnvironmentParameter> updatedList = List<EnvironmentParameter>(); | |||
| response.forEach((element) { | |||
| updatedList.add(EnvironmentParameter.clone(element)); | |||
| }); | |||
| yield PlotParameterSuccess( | |||
| items: response, | |||
| items: updatedList, | |||
| page: 0, | |||
| hasReachedMax: response.length < pageSize ? true : false); | |||
| } | |||
| //TODO: check paging api | |||
| // if (state is PlotParameterSuccess) { | |||
| // final currentState = state as PlotParameterSuccess; | |||
| // int page = currentState.page + 1; | |||
| // final response = await repository.getEnvironmentParameters( | |||
| // cropId: event.cropId, page: page, size: pageSize); | |||
| // yield response.isEmpty | |||
| // ? currentState.copyWith(hasReachedMax: true) | |||
| // : PlotParameterSuccess( | |||
| // items: currentState.items + response, | |||
| // page: currentState.page + 1, | |||
| // hasReachedMax: false); | |||
| // } | |||
| if (state is PlotParameterSuccess) { | |||
| if (_periodicSubscription == null) { | |||
| _periodicSubscription ??= | |||
| Stream.periodic(const Duration(seconds: 5), (x) => x).listen( | |||
| (_) => add(OnRefresh(cropId: event.cropId)), | |||
| onError: (error) => print("Do something with $error")); | |||
| } else { | |||
| _periodicSubscription.resume(); | |||
| } | |||
| } | |||
| } catch (e) { | |||
| var errorString = AppException.handleError(e); | |||
| yield PlotParameterFailure(errorString: errorString); | |||
| @@ -52,9 +54,13 @@ class PlotParameterBloc extends Bloc<PlotParameterEvent, PlotParameterState> { | |||
| } | |||
| if (event is OnRefresh) { | |||
| try { | |||
| yield PlotParameterLoading(); | |||
| print("loading..."); | |||
| final response = await repository.getEnvironmentParameters( | |||
| cropId: event.cropId, page: 0, size: pageSize); | |||
| List<EnvironmentParameter> updatedList = List<EnvironmentParameter>(); | |||
| response.forEach((element) { | |||
| updatedList.add(EnvironmentParameter.clone(element)); | |||
| }); | |||
| yield PlotParameterSuccess( | |||
| items: response, | |||
| page: 0, | |||
| @@ -64,4 +70,11 @@ class PlotParameterBloc extends Bloc<PlotParameterEvent, PlotParameterState> { | |||
| } | |||
| } | |||
| } | |||
| @override | |||
| Future<void> close() async { | |||
| await _periodicSubscription?.cancel(); | |||
| _periodicSubscription = null; | |||
| return super.close(); | |||
| } | |||
| } | |||
| @@ -1,3 +1,5 @@ | |||
| import 'dart:async'; | |||
| import 'package:farm_tpf/custom_model/EnvironmentParameter.dart'; | |||
| import 'package:farm_tpf/data/repository/repository.dart'; | |||
| import 'package:farm_tpf/presentation/custom_widgets/bottom_loader.dart'; | |||
| @@ -17,54 +19,19 @@ class PlotParameterScreen extends StatefulWidget { | |||
| } | |||
| class _PlotParameterScreenState extends State<PlotParameterScreen> { | |||
| @override | |||
| Widget build(BuildContext context) { | |||
| return BlocProvider( | |||
| create: (context) => PlotParameterBloc(repository: Repository()) | |||
| ..add(DataFetched(cropId: widget.cropId)), | |||
| child: HoldInfinityWidget( | |||
| cropId: widget.cropId, | |||
| ), | |||
| ); | |||
| } | |||
| } | |||
| class HoldInfinityWidget extends StatelessWidget { | |||
| final int cropId; | |||
| HoldInfinityWidget({@required this.cropId}); | |||
| final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>(); | |||
| @override | |||
| Widget build(BuildContext context) { | |||
| return Scaffold( | |||
| key: _scaffoldKey, | |||
| body: InfinityView( | |||
| cropId: cropId, | |||
| )); | |||
| } | |||
| } | |||
| class InfinityView extends StatefulWidget { | |||
| final int cropId; | |||
| InfinityView({@required this.cropId}); | |||
| @override | |||
| _InfinityViewState createState() => _InfinityViewState(); | |||
| } | |||
| class _InfinityViewState extends State<InfinityView> { | |||
| var plotParameterBloc = PlotParameterBloc(repository: Repository()); | |||
| final _scrollController = ScrollController(); | |||
| final _scrollThreshold = 250.0; | |||
| PlotParameterBloc _plotParameterBloc; | |||
| @override | |||
| void initState() { | |||
| _scrollController.addListener(() { | |||
| final maxScroll = _scrollController.position.maxScrollExtent; | |||
| final currentScroll = _scrollController.position.pixels; | |||
| if (maxScroll - currentScroll < _scrollThreshold) { | |||
| _plotParameterBloc.add(DataFetched(cropId: widget.cropId)); | |||
| plotParameterBloc.add(DataFetched(cropId: widget.cropId)); | |||
| } | |||
| }); | |||
| _plotParameterBloc = BlocProvider.of<PlotParameterBloc>(context); | |||
| plotParameterBloc.add(DataFetched(cropId: widget.cropId)); | |||
| super.initState(); | |||
| } | |||
| @@ -72,7 +39,9 @@ class _InfinityViewState extends State<InfinityView> { | |||
| Widget build(BuildContext context) { | |||
| return Column( | |||
| children: <Widget>[ | |||
| Expanded(child: BlocBuilder<PlotParameterBloc, PlotParameterState>( | |||
| Expanded( | |||
| child: BlocBuilder<PlotParameterBloc, PlotParameterState>( | |||
| cubit: plotParameterBloc, | |||
| builder: (context, state) { | |||
| if (state is PlotParameterFailure) { | |||
| return Center(child: Text(state.errorString)); | |||
| @@ -95,7 +64,7 @@ class _InfinityViewState extends State<InfinityView> { | |||
| controller: _scrollController, | |||
| ), | |||
| onRefresh: () async { | |||
| _plotParameterBloc.add(OnRefresh(cropId: widget.cropId)); | |||
| plotParameterBloc.add(OnRefresh(cropId: widget.cropId)); | |||
| }); | |||
| } | |||
| return Center( | |||
| @@ -110,6 +79,7 @@ class _InfinityViewState extends State<InfinityView> { | |||
| @override | |||
| void dispose() { | |||
| _scrollController.dispose(); | |||
| plotParameterBloc.close(); | |||
| super.dispose(); | |||
| } | |||
| } | |||