geo-smart-app/lib/bloc/position/position_bloc.dart

46 lines
1.3 KiB
Dart
Raw Normal View History

2020-07-28 18:11:47 +00:00
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:geosmart/bloc/authentication/authentication.dart';
import 'package:geosmart/bloc/position/position_event.dart';
import 'package:geosmart/bloc/position/position_state.dart';
import 'package:geosmart/service/position_service.dart';
import 'package:meta/meta.dart';
class PositionBloc extends Bloc<PositionEvent, PositionState> {
final AuthenticationBloc authenticationBloc;
PositionBloc({@required this.authenticationBloc})
: super(PositionTrackingIdle());
@override
Stream<PositionState> mapEventToState(PositionEvent event) async* {
if (event is PositionStartTracking) {
yield PositionTrackingStarted();
}
if (event is PositionStopTracking) {
try {
await PositionService(
dio: authenticationBloc.dio,
).stopTracking();
yield PositionTrackingIdle();
} catch (e) {
yield PositionTrackingFailed();
}
}
if (event is PositionSend) {
try {
PositionService(
dio: authenticationBloc.dio,
).sendPosition(
event.lat,
event.lng,
);
} catch (_) {
await PositionService(
dio: authenticationBloc.dio,
).stopTracking();
yield PositionTrackingFailed();
}
}
}
}