Refactor signup fields and update home page titles; add trip service and model

This commit is contained in:
Van Leemput Dayron
2025-10-04 17:42:21 +02:00
parent 6aaf2406b3
commit 02754f3506
6 changed files with 830 additions and 150 deletions

View File

@@ -0,0 +1,108 @@
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import '../models/trip.dart';
class TripService {
static const String _tripsKey = 'trips_data';
// Charger tous les voyages
Future<List<Trip>> loadTrips() async {
try {
final prefs = await SharedPreferences.getInstance();
final tripsJson = prefs.getString(_tripsKey);
if (tripsJson == null || tripsJson.isEmpty) return [];
final List<dynamic> jsonList = json.decode(tripsJson);
return jsonList.map((json) => Trip.fromMap(json)).toList();
} catch (e) {
print('Erreur lors du chargement des voyages: $e');
return [];
}
}
// Sauvegarder tous les voyages
Future<void> saveTrips(List<Trip> trips) async {
try {
final prefs = await SharedPreferences.getInstance();
final jsonList = trips.map((trip) => trip.toMap()).toList();
await prefs.setString(_tripsKey, json.encode(jsonList));
} catch (e) {
print('Erreur lors de la sauvegarde des voyages: $e');
throw Exception('Erreur de sauvegarde');
}
}
// Ajouter un nouveau voyage
Future<bool> addTrip(Trip trip) async {
try {
final trips = await loadTrips();
// Générer un ID unique
final newTrip = trip.copyWith(
id: DateTime.now().millisecondsSinceEpoch.toString(),
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
);
trips.add(newTrip);
await saveTrips(trips);
return true;
} catch (e) {
print('Erreur lors de l\'ajout du voyage: $e');
return false;
}
}
// Obtenir les voyages d'un utilisateur
Future<List<Trip>> getTripsByUser(String userId) async {
try {
final trips = await loadTrips();
return trips
.where(
(trip) =>
trip.createdBy == userId || trip.participants.contains(userId),
)
.toList();
} catch (e) {
print('Erreur lors de la récupération des voyages: $e');
return [];
}
}
// Mettre à jour un voyage
Future<bool> updateTrip(Trip updatedTrip) async {
try {
final trips = await loadTrips();
final index = trips.indexWhere((trip) => trip.id == updatedTrip.id);
if (index != -1) {
trips[index] = updatedTrip.copyWith(updatedAt: DateTime.now());
await saveTrips(trips);
return true;
}
return false;
} catch (e) {
print('Erreur lors de la mise à jour du voyage: $e');
return false;
}
}
// Supprimer un voyage
Future<bool> deleteTrip(String tripId) async {
try {
final trips = await loadTrips();
final initialLength = trips.length;
trips.removeWhere((trip) => trip.id == tripId);
if (trips.length < initialLength) {
await saveTrips(trips);
return true;
}
return false;
} catch (e) {
print('Erreur lors de la suppression du voyage: $e');
return false;
}
}
}