feat: refactorisation majeure - DataLoadingService + UserRepository simplifié
✅ NOUVEAU SERVICE CRÉÉ: - DataLoadingService: gère tout le chargement des données au login - Sépare les responsabilités: UserRepository se concentre sur l'auth - Simplification massive du code de connexion ✅ USERREPOSITORY REFACTORISÉ: - Suppression de toute la logique de chargement de données (déplacée vers DataLoadingService) - Délégation complète aux services singleton (CurrentUserService, CurrentAmicaleService) - Constructeur ultra-simplifié (plus d'injection ApiService) - Méthodes d'auth optimisées et clarifiées ✅ REPOSITORIES SIMPLIFIÉS: - AmicaleRepository: constructeur sans paramètres, ApiService.instance - ClientRepository: même pattern de simplification - MembreRepository: suppression injection, getters sécurisés - OperationRepository: utilisation ApiService.instance - PassageRepository: simplification massive, nouveau pattern - SectorRepository: optimisation et nouvelle structure ✅ ARCHITECTURE SINGLETONS: - ApiService: pattern singleton thread-safe - CurrentUserService: gestion utilisateur connecté + persistence Hive (Box user) - CurrentAmicaleService: gestion amicale courante + auto-sync - Box Hive 'users' renommée en 'user' avec migration automatique ✅ APP.DART & MAIN.DART: - Suppression injections multiples dans repositories - Intégration des services singleton dans main.dart - Router simplifié avec CurrentUserService État: Architecture singleton opérationnelle, prêt pour tests et widgets
This commit is contained in:
@@ -3,6 +3,9 @@ import 'package:flutter/services.dart';
|
||||
import 'package:flutter/foundation.dart' show kIsWeb;
|
||||
import 'package:flutter_web_plugins/url_strategy.dart';
|
||||
import 'package:geosector_app/core/services/app_info_service.dart';
|
||||
import 'package:geosector_app/core/services/api_service.dart';
|
||||
import 'package:geosector_app/core/services/current_user_service.dart';
|
||||
import 'package:geosector_app/core/services/current_amicale_service.dart';
|
||||
import 'package:geosector_app/app.dart';
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
import 'package:geosector_app/core/data/models/user_model.dart';
|
||||
@@ -51,10 +54,20 @@ void main() async {
|
||||
/// Initialise les services essentiels
|
||||
Future<void> _initializeServices() async {
|
||||
try {
|
||||
// Initialiser ApiService en premier
|
||||
await ApiService.initialize();
|
||||
debugPrint('✅ ApiService singleton initialisé');
|
||||
|
||||
// Les services CurrentUserService et CurrentAmicaleService s'initialisent automatiquement
|
||||
// au premier accès via le pattern singleton lazy
|
||||
debugPrint('✅ CurrentUserService prêt');
|
||||
debugPrint('✅ CurrentAmicaleService prêt');
|
||||
|
||||
await AppInfoService.initialize();
|
||||
debugPrint('Services initialisés avec succès');
|
||||
debugPrint('✅ Tous les services initialisés avec succès');
|
||||
} catch (e) {
|
||||
debugPrint('Erreur lors de l\'initialisation des services: $e');
|
||||
debugPrint('❌ Erreur lors de l\'initialisation des services: $e');
|
||||
rethrow; // Important pour arrêter l'app si les services critiques échouent
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,6 +83,11 @@ Future<bool> _initializeHive() async {
|
||||
// Ouvrir uniquement les boîtes essentielles au démarrage
|
||||
await _openEssentialHiveBoxes();
|
||||
|
||||
// Charger les données depuis Hive au démarrage
|
||||
await CurrentUserService.instance.loadFromHive();
|
||||
await CurrentAmicaleService.instance.loadFromHive();
|
||||
debugPrint('✅ Données utilisateur/amicale chargées depuis Hive');
|
||||
|
||||
debugPrint('Hive initialisé avec succès');
|
||||
return true;
|
||||
} catch (e) {
|
||||
@@ -130,7 +148,7 @@ void _registerHiveAdapters() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Ouvre les boîtes Hive essentielles
|
||||
/// Ouvre les boîtes Hive essentielles avec migration users -> user
|
||||
Future<void> _openEssentialHiveBoxes() async {
|
||||
final boxesToOpen = [
|
||||
{'name': AppKeys.userBoxName, 'type': 'UserModel'},
|
||||
@@ -234,51 +252,4 @@ Future<bool> _doesBoxExist(String boxName) async {
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
final boxesToOpen = [
|
||||
{'name': AppKeys.userBoxName, 'type': 'UserModel'},
|
||||
{'name': AppKeys.amicaleBoxName, 'type': 'AmicaleModel'},
|
||||
{'name': AppKeys.clientsBoxName, 'type': 'ClientModel'},
|
||||
{'name': AppKeys.settingsBoxName, 'type': 'dynamic'},
|
||||
{'name': AppKeys.chatConversationsBoxName, 'type': 'ConversationModel'},
|
||||
{'name': AppKeys.chatMessagesBoxName, 'type': 'MessageModel'},
|
||||
];
|
||||
|
||||
for (final box in boxesToOpen) {
|
||||
try {
|
||||
final boxName = box['name'] as String;
|
||||
final boxType = box['type'] as String;
|
||||
|
||||
// Vérifier si la boîte est déjà ouverte
|
||||
if (Hive.isBoxOpen(boxName)) {
|
||||
debugPrint('Boîte $boxName déjà ouverte');
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (boxType) {
|
||||
case 'UserModel':
|
||||
await Hive.openBox<UserModel>(boxName);
|
||||
break;
|
||||
case 'AmicaleModel':
|
||||
await Hive.openBox<AmicaleModel>(boxName);
|
||||
break;
|
||||
case 'ClientModel':
|
||||
await Hive.openBox<ClientModel>(boxName);
|
||||
break;
|
||||
case 'ConversationModel':
|
||||
await Hive.openBox<ConversationModel>(boxName);
|
||||
break;
|
||||
case 'MessageModel':
|
||||
await Hive.openBox<MessageModel>(boxName);
|
||||
break;
|
||||
default:
|
||||
await Hive.openBox(boxName);
|
||||
}
|
||||
|
||||
debugPrint('Boîte $boxName ouverte avec succès');
|
||||
} catch (e) {
|
||||
debugPrint('Erreur lors de l\'ouverture de la boîte ${box['name']}: $e');
|
||||
// Ne pas lancer d'erreur, continuer avec les autres boîtes
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user