Files
TravelMate/lib/services/sos_service.dart
Van Leemput Dayron 9b08b2896c feat: Add services for managing trip-related data
- Implement EmergencyService for handling emergency contacts per trip.
- Create GuestFlagService to manage guest mode flags for trips.
- Introduce NotificationService with local notification capabilities.
- Add OfflineFlagService for managing offline caching flags.
- Develop PackingService for shared packing lists per trip.
- Implement ReminderService for managing reminders/to-dos per trip.
- Create SosService for dispatching SOS events to a backend.
- Add StorageService with album image upload functionality.
- Implement TransportService for managing transport segments per trip.
- Create TripChecklistService for storing and retrieving trip checklists.
- Add TripDocumentService for persisting trip documents metadata.

test: Add unit tests for new services

- Implement tests for AlbumService, BudgetService, EmergencyService, GuestFlagService, PackingService, ReminderService, SosService, TransportService, TripChecklistService, and TripDocumentService.
- Ensure tests cover adding, loading, deleting, and handling corrupted payloads for each service.
2026-03-13 15:02:23 +01:00

55 lines
1.4 KiB
Dart

import 'dart:convert';
import 'package:http/http.dart' as http;
/// Service in charge of dispatching SOS events to a backend endpoint.
///
/// The backend is expected to accept POST JSON payloads like:
/// {
/// "tripId": "...",
/// "lat": 0.0,
/// "lng": 0.0,
/// "message": "...",
/// }
class SosService {
/// Base URL of the backend (e.g. https://api.example.com/sos).
final String baseUrl;
/// Optional API key header.
final String? apiKey;
/// Optional injected HTTP client (useful for testing).
final http.Client _client;
/// Creates a new SOS service.
SosService({required this.baseUrl, this.apiKey, http.Client? client})
: _client = client ?? http.Client();
/// Sends an SOS event. Returns true on HTTP 200.
Future<bool> sendSos({
required String tripId,
required double lat,
required double lng,
String message = 'SOS déclenché',
}) async {
final uri = Uri.parse('$baseUrl/sos');
try {
final response = await _client.post(
uri,
headers: {
'Content-Type': 'application/json',
if (apiKey != null) 'Authorization': 'Bearer $apiKey',
},
body: json.encode({
'tripId': tripId,
'lat': lat,
'lng': lng,
'message': message,
}),
);
return response.statusCode == 200;
} catch (_) {
return false;
}
}
}