feat: WebSocket temps réel + Finance Workflow + corrections
- Task #6: WebSocket /ws/dashboard + Kafka events (5 topics) * Backend: KafkaEventProducer, KafkaEventConsumer * Mobile: WebSocketService (reconnection, heartbeat, typed events) * DashboardBloc: Auto-refresh depuis WebSocket events - Finance Workflow: approbations + budgets (backend + mobile) * Backend: entities, services, resources, migrations Flyway V6 * Mobile: features finance_workflow complète avec BLoC - Corrections DI: interfaces IRepository partout * IProfileRepository, IOrganizationRepository, IMembreRepository * GetIt configuré avec @injectable - Spec-Kit: constitution + templates mis à jour * .specify/memory/constitution.md enrichie * Templates agent, plan, spec, tasks, checklist - Nettoyage: fichiers temporaires supprimés Signed-off-by: lions dev Team
This commit is contained in:
@@ -1,29 +1,83 @@
|
||||
import 'dart:async';
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import '../../domain/entities/dashboard_entity.dart';
|
||||
import '../../domain/usecases/get_dashboard_data.dart';
|
||||
import '../../../../core/error/failures.dart';
|
||||
import '../../../../core/websocket/websocket_service.dart';
|
||||
import '../../../../core/utils/logger.dart';
|
||||
|
||||
part 'dashboard_event.dart';
|
||||
part 'dashboard_state.dart';
|
||||
|
||||
@injectable
|
||||
class DashboardBloc extends Bloc<DashboardEvent, DashboardState> {
|
||||
final GetDashboardData getDashboardData;
|
||||
final GetDashboardStats getDashboardStats;
|
||||
final GetRecentActivities getRecentActivities;
|
||||
final GetUpcomingEvents getUpcomingEvents;
|
||||
final WebSocketService webSocketService;
|
||||
|
||||
StreamSubscription<WebSocketEvent>? _webSocketEventSubscription;
|
||||
StreamSubscription<bool>? _webSocketConnectionSubscription;
|
||||
|
||||
DashboardBloc({
|
||||
required this.getDashboardData,
|
||||
required this.getDashboardStats,
|
||||
required this.getRecentActivities,
|
||||
required this.getUpcomingEvents,
|
||||
required this.webSocketService,
|
||||
}) : super(DashboardInitial()) {
|
||||
on<LoadDashboardData>(_onLoadDashboardData);
|
||||
on<RefreshDashboardData>(_onRefreshDashboardData);
|
||||
on<LoadDashboardStats>(_onLoadDashboardStats);
|
||||
on<LoadRecentActivities>(_onLoadRecentActivities);
|
||||
on<LoadUpcomingEvents>(_onLoadUpcomingEvents);
|
||||
on<RefreshDashboardFromWebSocket>(_onRefreshDashboardFromWebSocket);
|
||||
on<WebSocketConnectionChanged>(_onWebSocketConnectionChanged);
|
||||
|
||||
// Initialiser WebSocket et écouter les events
|
||||
_initializeWebSocket();
|
||||
}
|
||||
|
||||
/// Initialise la connexion WebSocket et écoute les events
|
||||
void _initializeWebSocket() {
|
||||
// Connexion au WebSocket
|
||||
webSocketService.connect();
|
||||
AppLogger.info('DashboardBloc: WebSocket initialisé');
|
||||
|
||||
// Écouter les events WebSocket
|
||||
_webSocketEventSubscription = webSocketService.eventStream.listen(
|
||||
(event) {
|
||||
AppLogger.info('DashboardBloc: Event WebSocket reçu - ${event.eventType}');
|
||||
|
||||
// Dispatcher uniquement les events pertinents au dashboard
|
||||
if (event is DashboardStatsEvent) {
|
||||
add(RefreshDashboardFromWebSocket(event.data));
|
||||
} else if (event is FinanceApprovalEvent) {
|
||||
// Les approbations affectent les stats, rafraîchir
|
||||
add(RefreshDashboardFromWebSocket(event.data));
|
||||
} else if (event is MemberEvent) {
|
||||
// Les changements de membres affectent les stats
|
||||
add(RefreshDashboardFromWebSocket(event.data));
|
||||
} else if (event is ContributionEvent) {
|
||||
// Les cotisations affectent les stats financières
|
||||
add(RefreshDashboardFromWebSocket(event.data));
|
||||
}
|
||||
},
|
||||
onError: (error) {
|
||||
AppLogger.error('DashboardBloc: Erreur WebSocket', error: error);
|
||||
},
|
||||
);
|
||||
|
||||
// Écouter le statut de connexion WebSocket
|
||||
_webSocketConnectionSubscription = webSocketService.connectionStatusStream.listen(
|
||||
(isConnected) {
|
||||
AppLogger.info('DashboardBloc: WebSocket ${isConnected ? "connecté" : "déconnecté"}');
|
||||
add(WebSocketConnectionChanged(isConnected));
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _onLoadDashboardData(
|
||||
@@ -161,6 +215,61 @@ class DashboardBloc extends Bloc<DashboardEvent, DashboardState> {
|
||||
);
|
||||
}
|
||||
|
||||
/// Rafraîchit le dashboard suite à un event WebSocket
|
||||
Future<void> _onRefreshDashboardFromWebSocket(
|
||||
RefreshDashboardFromWebSocket event,
|
||||
Emitter<DashboardState> emit,
|
||||
) async {
|
||||
AppLogger.info('DashboardBloc: Rafraîchissement depuis WebSocket');
|
||||
|
||||
// Si le dashboard est chargé, on rafraîchit uniquement les stats
|
||||
// pour éviter de recharger toutes les données
|
||||
if (state is DashboardLoaded) {
|
||||
final currentData = (state as DashboardLoaded).dashboardData;
|
||||
|
||||
// Rafraîchir les stats depuis le backend
|
||||
final result = await getDashboardStats(
|
||||
GetDashboardStatsParams(
|
||||
organizationId: currentData.organizationId,
|
||||
userId: currentData.userId,
|
||||
),
|
||||
);
|
||||
|
||||
result.fold(
|
||||
(failure) {
|
||||
AppLogger.error('Erreur rafraîchissement stats WebSocket', error: failure);
|
||||
// Ne pas émettre d'erreur, garder les données actuelles
|
||||
},
|
||||
(stats) {
|
||||
final updatedData = DashboardEntity(
|
||||
stats: stats,
|
||||
recentActivities: currentData.recentActivities,
|
||||
upcomingEvents: currentData.upcomingEvents,
|
||||
userPreferences: currentData.userPreferences,
|
||||
organizationId: currentData.organizationId,
|
||||
userId: currentData.userId,
|
||||
);
|
||||
emit(DashboardLoaded(updatedData));
|
||||
AppLogger.info('DashboardBloc: Stats rafraîchies depuis WebSocket');
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Gère les changements de statut de connexion WebSocket
|
||||
void _onWebSocketConnectionChanged(
|
||||
WebSocketConnectionChanged event,
|
||||
Emitter<DashboardState> emit,
|
||||
) {
|
||||
// Pour l'instant, on log juste le statut
|
||||
// On pourrait ajouter un indicateur visuel dans l'UI plus tard
|
||||
if (event.isConnected) {
|
||||
AppLogger.info('DashboardBloc: WebSocket connecté - Temps réel actif');
|
||||
} else {
|
||||
AppLogger.warning('DashboardBloc: WebSocket déconnecté - Reconnexion en cours...');
|
||||
}
|
||||
}
|
||||
|
||||
String _mapFailureToMessage(Failure failure) {
|
||||
switch (failure.runtimeType) {
|
||||
case ServerFailure:
|
||||
@@ -171,4 +280,18 @@ class DashboardBloc extends Bloc<DashboardEvent, DashboardState> {
|
||||
return 'Une erreur inattendue s\'est produite.';
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() {
|
||||
// Annuler les subscriptions WebSocket
|
||||
_webSocketEventSubscription?.cancel();
|
||||
_webSocketConnectionSubscription?.cancel();
|
||||
|
||||
// Déconnecter le WebSocket
|
||||
webSocketService.disconnect();
|
||||
|
||||
AppLogger.info('DashboardBloc: Fermé et WebSocket déconnecté');
|
||||
|
||||
return super.close();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user