passo_mobile_app/lib/bloc/sos/sos_bloc.dart

107 lines
3.4 KiB
Dart
Raw Permalink Normal View History

2023-04-11 06:08:10 +00:00
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
2023-04-13 08:45:19 +00:00
import 'package:flutter/foundation.dart';
2023-04-11 06:08:10 +00:00
import 'package:location/location.dart';
2023-04-13 08:45:19 +00:00
import 'package:unit2/model/sos/session.dart';
2023-04-11 06:08:10 +00:00
import 'package:unit2/sevices/sos/sos_service.dart';
2023-04-13 08:45:19 +00:00
import '../../utils/global.dart';
2023-04-11 06:08:10 +00:00
part 'sos_event.dart';
part 'sos_state.dart';
class SosBloc extends Bloc<SosEvent, SosState> {
SosBloc() : super(SosInitial()) {
LocationData? locationData;
2023-04-13 08:45:19 +00:00
String? mobile1;
String? mobile2;
String? sessionToken;
////get user location
on<LoadUserLocation>((event, emit) async {
emit(const LoadingState(message: "Getting Location"));
mobile1 = await SOS!.get('mobile1');
mobile2 = await SOS!.get('mobile2');
sessionToken = await SOS!.get('session_token');
if (mobile1 != null) {
if (sessionToken != null) {
add(CheckAcknowledgement(sessionToken: sessionToken!));
} else {
try {
LocationData? newLocationData =
await SosService.instance.getUserLocation();
locationData = newLocationData;
2023-04-11 06:08:10 +00:00
2023-04-13 08:45:19 +00:00
emit(RequestSosState(
locationData: locationData!,
mobile1: mobile1!,
mobile2: mobile2));
} catch (e) {
emit(ErrorState(message: e.toString()));
}
}
} else {
try {
2023-04-11 06:08:10 +00:00
LocationData? newLocationData =
2023-04-13 08:45:19 +00:00
await SosService.instance.getUserLocation();
locationData = newLocationData;
emit(UserLocationLoaded(locationData: locationData!));
} catch (e) {
emit(ErrorState(message: e.toString()));
}
}
});
////submit mobile
on<SubmitMobile>((event, emit) async {
emit(const LoadingState(message: ""));
mobile1 = event.mobile1;
mobile2 = event.mobile2;
await SOS!.put('mobile1', event.mobile1);
await SOS!.put('mobile2', event.mobile2);
emit(RequestSosState(
locationData: locationData!,
mobile1: event.mobile1,
mobile2: event.mobile2));
});
//// send SOS
on<SendSOS>((event, emit) async {
SessionData sessionData;
emit(const LoadingState(message: "Sending emergency response request"));
try {
sessionData = await SosService.instance.requestSos(
location: locationData!,
mobile1: mobile1!,
mobile2: mobile2,
msg: event.msg,
requestedDate: event.requestDate);
await SOS!.put('session_token', sessionData.sessionToken);
emit(SOSReceivedState(sessionToken: sessionData.sessionToken!));
} catch (e) {
emit(ErrorState(message: e.toString()));
}
});
//// check acknowledgement
on<CheckAcknowledgement>((event, emit) async {
SessionData sessionData;
try {
SessionData? newSessionData =
await SosService.instance.checkAcknowledgement(event.sessionToken);
if (newSessionData.acknowledgeDate == null) {
debugPrint(newSessionData.sessionToken);
emit(SOSReceivedState(sessionToken: newSessionData.sessionToken!));
} else {
sessionData = newSessionData;
emit(SoSAcknowledgementConfirm(sessionData: sessionData));
}
} catch (e) {
emit(ErrorState(message: e.toString()));
}
});
on<OnDoneRequest>((event, emit) async {
await SOS!.delete('session_token');
add(LoadUserLocation());
2023-04-11 06:08:10 +00:00
});
}
}