39 lines
944 B
Dart
39 lines
944 B
Dart
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
import '../domain/repositories/sos_repository.dart';
|
|
|
|
enum SosPhase { idle, sending, sent, error }
|
|
|
|
class SosState {
|
|
final SosPhase phase;
|
|
final String? message;
|
|
|
|
const SosState({this.phase = SosPhase.idle, this.message});
|
|
}
|
|
|
|
class SosCubit extends Cubit<SosState> {
|
|
final SosRepository _repository;
|
|
|
|
SosCubit(this._repository) : super(const SosState());
|
|
|
|
Future<void> trigger({
|
|
String triggerType = 'MANUAL',
|
|
double? lat,
|
|
double? lng,
|
|
}) async {
|
|
emit(const SosState(phase: SosPhase.sending));
|
|
final result = await _repository.triggerSos(
|
|
triggerType: triggerType,
|
|
lat: lat,
|
|
lng: lng,
|
|
);
|
|
result.fold(
|
|
(failure) => emit(SosState(phase: SosPhase.error, message: failure.message)),
|
|
(_) => emit(const SosState(
|
|
phase: SosPhase.sent,
|
|
message: 'SOS terkirim. Guardian sudah diberi tahu.',
|
|
)),
|
|
);
|
|
}
|
|
}
|