Files
TravelMate/lib/services/trip_document_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

50 lines
1.8 KiB
Dart

import 'package:shared_preferences/shared_preferences.dart';
import 'package:travel_mate/models/trip_document.dart';
/// Service that persists per-trip documents metadata locally.
///
/// Documents are stored as JSON in `SharedPreferences` to keep the UI
/// responsive offline. Each trip key is `trip_docs_<tripId>`. The service is
/// tolerant to corrupted payloads and resets gracefully to an empty list.
class TripDocumentService {
/// Loads documents for the given [tripId]. Returns an empty list when none.
Future<List<TripDocument>> loadDocuments(String tripId) async {
final prefs = await SharedPreferences.getInstance();
final raw = prefs.getString(_key(tripId));
if (raw == null) return const [];
try {
return TripDocument.decodeList(raw);
} catch (_) {
await prefs.remove(_key(tripId));
return const [];
}
}
/// Saves the full document list for [tripId].
Future<void> saveDocuments(String tripId, List<TripDocument> docs) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(_key(tripId), TripDocument.encodeList(docs));
}
/// Adds a new document entry and persists the updated list.
Future<List<TripDocument>> addDocument(
String tripId,
TripDocument doc,
) async {
final current = await loadDocuments(tripId);
final updated = [...current, doc];
await saveDocuments(tripId, updated);
return updated;
}
/// Deletes a document by [docId] and persists the change.
Future<List<TripDocument>> deleteDocument(String tripId, String docId) async {
final current = await loadDocuments(tripId);
final updated = current.where((d) => d.id != docId).toList();
await saveDocuments(tripId, updated);
return updated;
}
String _key(String tripId) => 'trip_docs_$tripId';
}