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:
72
lib/services/budget_service.dart
Normal file
72
lib/services/budget_service.dart
Normal file
@@ -0,0 +1,72 @@
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:travel_mate/models/budget_category.dart';
|
||||
|
||||
/// Service to manage per-trip budget envelopes (multi-devise) locally.
|
||||
///
|
||||
/// Stores envelopes in `SharedPreferences` under `budget_<tripId>` so they
|
||||
/// remain available offline. Integration with expense data can later update
|
||||
/// the [spent] field.
|
||||
class BudgetService {
|
||||
/// Loads budget categories for the trip.
|
||||
Future<List<BudgetCategory>> loadBudgets(String tripId) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final raw = prefs.getString(_key(tripId));
|
||||
if (raw == null) return const [];
|
||||
try {
|
||||
return BudgetCategory.decodeList(raw);
|
||||
} catch (_) {
|
||||
await prefs.remove(_key(tripId));
|
||||
return const [];
|
||||
}
|
||||
}
|
||||
|
||||
/// Persists full list.
|
||||
Future<void> saveBudgets(
|
||||
String tripId,
|
||||
List<BudgetCategory> categories,
|
||||
) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_key(tripId), BudgetCategory.encodeList(categories));
|
||||
}
|
||||
|
||||
/// Adds an envelope.
|
||||
Future<List<BudgetCategory>> addBudget(
|
||||
String tripId,
|
||||
BudgetCategory category,
|
||||
) async {
|
||||
final current = await loadBudgets(tripId);
|
||||
final updated = [...current, category];
|
||||
await saveBudgets(tripId, updated);
|
||||
return updated;
|
||||
}
|
||||
|
||||
/// Deletes an envelope.
|
||||
Future<List<BudgetCategory>> deleteBudget(
|
||||
String tripId,
|
||||
String categoryId,
|
||||
) async {
|
||||
final current = await loadBudgets(tripId);
|
||||
final updated = current.where((c) => c.id != categoryId).toList();
|
||||
await saveBudgets(tripId, updated);
|
||||
return updated;
|
||||
}
|
||||
|
||||
/// Updates spent amount for a category (used later by expense sync).
|
||||
Future<List<BudgetCategory>> updateSpent(
|
||||
String tripId,
|
||||
String categoryId,
|
||||
double spent,
|
||||
) async {
|
||||
final current = await loadBudgets(tripId);
|
||||
final updated = current
|
||||
.map((c) {
|
||||
if (c.id != categoryId) return c;
|
||||
return c.copyWith(spent: spent);
|
||||
})
|
||||
.toList(growable: false);
|
||||
await saveBudgets(tripId, updated);
|
||||
return updated;
|
||||
}
|
||||
|
||||
String _key(String tripId) => 'budget_$tripId';
|
||||
}
|
||||
Reference in New Issue
Block a user