Browse Source

intercal call api display environment parameter

master
daivph 5 years ago
parent
commit
22308d5758
3 changed files with 51 additions and 59 deletions
  1. +9
    -0
      lib/custom_model/EnvironmentParameter.dart
  2. +32
    -19
      lib/presentation/screens/plot_detail/bloc/plot_parameter_bloc.dart
  3. +10
    -40
      lib/presentation/screens/plot_detail/sc_plot_parameter.dart

+ 9
- 0
lib/custom_model/EnvironmentParameter.dart View File

this.executeDate, this.executeDate,
this.status}); 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) { EnvironmentParameter.fromJson(Map<String, dynamic> json) {
id = json['id']; id = json['id'];
name = json['name']; name = json['name'];

+ 32
- 19
lib/presentation/screens/plot_detail/bloc/plot_parameter_bloc.dart View File



import 'package:bloc/bloc.dart'; import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.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/api/app_exception.dart';
import 'package:farm_tpf/data/repository/repository.dart'; import 'package:farm_tpf/data/repository/repository.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
final Repository repository; final Repository repository;
PlotParameterBloc({@required this.repository}) PlotParameterBloc({@required this.repository})
: super(PlotParameterInitial()); : super(PlotParameterInitial());
static int pageSize = 20;
static int pageSize = 100;
StreamSubscription _periodicSubscription;


@override @override
Stream<PlotParameterState> mapEventToState( Stream<PlotParameterState> mapEventToState(
PlotParameterEvent event, PlotParameterEvent event,
) async* { ) async* {
if (event is DataFetched &&
!(state is PlotParameterSuccess &&
(state as PlotParameterSuccess).hasReachedMax)) {
if (event is DataFetched) {
try { try {
if (state is PlotParameterInitial) { if (state is PlotParameterInitial) {
yield PlotParameterLoading(); yield PlotParameterLoading();
final response = await repository.getEnvironmentParameters( final response = await repository.getEnvironmentParameters(
cropId: event.cropId, page: 0, size: pageSize); cropId: event.cropId, page: 0, size: pageSize);
List<EnvironmentParameter> updatedList = List<EnvironmentParameter>();
response.forEach((element) {
updatedList.add(EnvironmentParameter.clone(element));
});
yield PlotParameterSuccess( yield PlotParameterSuccess(
items: response,
items: updatedList,
page: 0, page: 0,
hasReachedMax: response.length < pageSize ? true : false); 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) { } catch (e) {
var errorString = AppException.handleError(e); var errorString = AppException.handleError(e);
yield PlotParameterFailure(errorString: errorString); yield PlotParameterFailure(errorString: errorString);
} }
if (event is OnRefresh) { if (event is OnRefresh) {
try { try {
yield PlotParameterLoading();
print("loading...");
final response = await repository.getEnvironmentParameters( final response = await repository.getEnvironmentParameters(
cropId: event.cropId, page: 0, size: pageSize); cropId: event.cropId, page: 0, size: pageSize);
List<EnvironmentParameter> updatedList = List<EnvironmentParameter>();
response.forEach((element) {
updatedList.add(EnvironmentParameter.clone(element));
});
yield PlotParameterSuccess( yield PlotParameterSuccess(
items: response, items: response,
page: 0, page: 0,
} }
} }
} }

@override
Future<void> close() async {
await _periodicSubscription?.cancel();
_periodicSubscription = null;
return super.close();
}
} }

+ 10
- 40
lib/presentation/screens/plot_detail/sc_plot_parameter.dart View File

import 'dart:async';

import 'package:farm_tpf/custom_model/EnvironmentParameter.dart'; import 'package:farm_tpf/custom_model/EnvironmentParameter.dart';
import 'package:farm_tpf/data/repository/repository.dart'; import 'package:farm_tpf/data/repository/repository.dart';
import 'package:farm_tpf/presentation/custom_widgets/bottom_loader.dart'; import 'package:farm_tpf/presentation/custom_widgets/bottom_loader.dart';
} }


class _PlotParameterScreenState extends State<PlotParameterScreen> { 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 _scrollController = ScrollController();
final _scrollThreshold = 250.0; final _scrollThreshold = 250.0;
PlotParameterBloc _plotParameterBloc;

@override @override
void initState() { void initState() {
_scrollController.addListener(() { _scrollController.addListener(() {
final maxScroll = _scrollController.position.maxScrollExtent; final maxScroll = _scrollController.position.maxScrollExtent;
final currentScroll = _scrollController.position.pixels; final currentScroll = _scrollController.position.pixels;
if (maxScroll - currentScroll < _scrollThreshold) { 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(); super.initState();
} }


Widget build(BuildContext context) { Widget build(BuildContext context) {
return Column( return Column(
children: <Widget>[ children: <Widget>[
Expanded(child: BlocBuilder<PlotParameterBloc, PlotParameterState>(
Expanded(
child: BlocBuilder<PlotParameterBloc, PlotParameterState>(
cubit: plotParameterBloc,
builder: (context, state) { builder: (context, state) {
if (state is PlotParameterFailure) { if (state is PlotParameterFailure) {
return Center(child: Text(state.errorString)); return Center(child: Text(state.errorString));
controller: _scrollController, controller: _scrollController,
), ),
onRefresh: () async { onRefresh: () async {
_plotParameterBloc.add(OnRefresh(cropId: widget.cropId));
plotParameterBloc.add(OnRefresh(cropId: widget.cropId));
}); });
} }
return Center( return Center(
@override @override
void dispose() { void dispose() {
_scrollController.dispose(); _scrollController.dispose();
plotParameterBloc.close();
super.dispose(); super.dispose();
} }
} }

Loading…
Cancel
Save