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.
This commit is contained in:
55
lib/services/emergency_service.dart
Normal file
55
lib/services/emergency_service.dart
Normal file
@@ -0,0 +1,55 @@
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:travel_mate/models/emergency_contact.dart';
|
||||
|
||||
/// Stores emergency contacts per trip for offline access.
|
||||
///
|
||||
/// Data is persisted locally in `SharedPreferences` under the key
|
||||
/// `emergency_<tripId>`. Corrupted payloads are cleaned up automatically to
|
||||
/// avoid crashing the UI during critical usage.
|
||||
class EmergencyService {
|
||||
/// Loads contacts for [tripId]. Returns an empty list if none or corrupted.
|
||||
Future<List<EmergencyContact>> loadContacts(String tripId) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final raw = prefs.getString(_key(tripId));
|
||||
if (raw == null) return const [];
|
||||
try {
|
||||
return EmergencyContact.decodeList(raw);
|
||||
} catch (_) {
|
||||
await prefs.remove(_key(tripId));
|
||||
return const [];
|
||||
}
|
||||
}
|
||||
|
||||
/// Saves the complete contact list.
|
||||
Future<void> saveContacts(
|
||||
String tripId,
|
||||
List<EmergencyContact> contacts,
|
||||
) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_key(tripId), EmergencyContact.encodeList(contacts));
|
||||
}
|
||||
|
||||
/// Adds a contact and returns updated list.
|
||||
Future<List<EmergencyContact>> addContact(
|
||||
String tripId,
|
||||
EmergencyContact contact,
|
||||
) async {
|
||||
final current = await loadContacts(tripId);
|
||||
final updated = [...current, contact];
|
||||
await saveContacts(tripId, updated);
|
||||
return updated;
|
||||
}
|
||||
|
||||
/// Deletes a contact.
|
||||
Future<List<EmergencyContact>> deleteContact(
|
||||
String tripId,
|
||||
String contactId,
|
||||
) async {
|
||||
final current = await loadContacts(tripId);
|
||||
final updated = current.where((c) => c.id != contactId).toList();
|
||||
await saveContacts(tripId, updated);
|
||||
return updated;
|
||||
}
|
||||
|
||||
String _key(String tripId) => 'emergency_$tripId';
|
||||
}
|
||||
Reference in New Issue
Block a user