feat(communication): module messagerie unifié + contact policies + blocages
Aligné avec le backend MessagingResource : - Nouveau module communication (conversations, messages, participants) - Respect des ContactPolicy (qui peut parler à qui par rôle) - Gestion MemberBlock (blocages individuels) - UI : conversations list, conversation detail, broadcast, tiles - BLoC : MessagingBloc avec events (envoyer, démarrer conversation rôle, etc.)
This commit is contained in:
@@ -1,105 +1,202 @@
|
||||
/// BLoC de gestion de la messagerie
|
||||
/// BLoC de gestion de la messagerie v4
|
||||
library messaging_bloc;
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
import '../../domain/usecases/get_conversations.dart';
|
||||
import '../../domain/usecases/get_messages.dart';
|
||||
import '../../domain/usecases/send_message.dart';
|
||||
import '../../domain/usecases/send_broadcast.dart';
|
||||
|
||||
import '../../../../core/utils/logger.dart';
|
||||
import '../../../../core/websocket/websocket_service.dart';
|
||||
import '../../domain/repositories/messaging_repository.dart';
|
||||
import 'messaging_event.dart';
|
||||
import 'messaging_state.dart';
|
||||
|
||||
@injectable
|
||||
class MessagingBloc extends Bloc<MessagingEvent, MessagingState> {
|
||||
final GetConversations getConversations;
|
||||
final GetMessages getMessages;
|
||||
final SendMessage sendMessage;
|
||||
final SendBroadcast sendBroadcast;
|
||||
final MessagingRepository _repository;
|
||||
final WebSocketService _webSocketService;
|
||||
|
||||
StreamSubscription<WebSocketEvent>? _wsSubscription;
|
||||
String? _currentConversationId;
|
||||
|
||||
MessagingBloc({
|
||||
required this.getConversations,
|
||||
required this.getMessages,
|
||||
required this.sendMessage,
|
||||
required this.sendBroadcast,
|
||||
}) : super(MessagingInitial()) {
|
||||
on<LoadConversations>(_onLoadConversations);
|
||||
required MessagingRepository repository,
|
||||
required WebSocketService webSocketService,
|
||||
}) : _repository = repository,
|
||||
_webSocketService = webSocketService,
|
||||
super(MessagingInitial()) {
|
||||
on<LoadMesConversations>(_onLoadMesConversations);
|
||||
on<OpenConversation>(_onOpenConversation);
|
||||
on<DemarrerConversationDirecte>(_onDemarrerConversationDirecte);
|
||||
on<DemarrerConversationRole>(_onDemarrerConversationRole);
|
||||
on<ArchiverConversation>(_onArchiverConversation);
|
||||
on<EnvoyerMessageTexte>(_onEnvoyerMessageTexte);
|
||||
on<LoadMessages>(_onLoadMessages);
|
||||
on<SendMessageEvent>(_onSendMessage);
|
||||
on<SendBroadcastEvent>(_onSendBroadcast);
|
||||
on<MarquerLu>(_onMarquerLu);
|
||||
on<SupprimerMessage>(_onSupprimerMessage);
|
||||
on<NouveauMessageWebSocket>(_onNouveauMessageWebSocket);
|
||||
|
||||
_listenWebSocket();
|
||||
}
|
||||
|
||||
Future<void> _onLoadConversations(
|
||||
LoadConversations event,
|
||||
void _listenWebSocket() {
|
||||
_wsSubscription = _webSocketService.eventStream.listen((event) {
|
||||
if (event is ChatMessageEvent && event.conversationId != null) {
|
||||
AppLogger.info('MessagingBloc: NOUVEAU_MESSAGE WS pour conv ${event.conversationId}');
|
||||
add(NouveauMessageWebSocket(event.conversationId!));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _onLoadMesConversations(
|
||||
LoadMesConversations event,
|
||||
Emitter<MessagingState> emit,
|
||||
) async {
|
||||
emit(MessagingLoading());
|
||||
try {
|
||||
final conversations = await _repository.getMesConversations();
|
||||
emit(MesConversationsLoaded(conversations));
|
||||
} catch (e) {
|
||||
emit(MessagingError(e.toString().replaceFirst('Exception: ', '')));
|
||||
}
|
||||
}
|
||||
|
||||
final result = await getConversations(
|
||||
organizationId: event.organizationId,
|
||||
includeArchived: event.includeArchived,
|
||||
);
|
||||
Future<void> _onOpenConversation(
|
||||
OpenConversation event,
|
||||
Emitter<MessagingState> emit,
|
||||
) async {
|
||||
emit(MessagingLoading());
|
||||
try {
|
||||
_currentConversationId = event.conversationId;
|
||||
final conversation = await _repository.getConversation(event.conversationId);
|
||||
emit(ConversationOuverte(conversation));
|
||||
// Marquer comme lu en arrière-plan (non-bloquant)
|
||||
_repository.marquerLu(event.conversationId);
|
||||
} catch (e) {
|
||||
emit(MessagingError(e.toString().replaceFirst('Exception: ', '')));
|
||||
}
|
||||
}
|
||||
|
||||
result.fold(
|
||||
(failure) => emit(MessagingError(failure.message)),
|
||||
(conversations) => emit(ConversationsLoaded(conversations: conversations)),
|
||||
);
|
||||
Future<void> _onDemarrerConversationDirecte(
|
||||
DemarrerConversationDirecte event,
|
||||
Emitter<MessagingState> emit,
|
||||
) async {
|
||||
emit(MessagingLoading());
|
||||
try {
|
||||
final conversation = await _repository.demarrerConversationDirecte(
|
||||
destinataireId: event.destinataireId,
|
||||
organisationId: event.organisationId,
|
||||
premierMessage: event.premierMessage,
|
||||
);
|
||||
emit(ConversationCreee(conversation));
|
||||
} catch (e) {
|
||||
emit(MessagingError(e.toString().replaceFirst('Exception: ', '')));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onDemarrerConversationRole(
|
||||
DemarrerConversationRole event,
|
||||
Emitter<MessagingState> emit,
|
||||
) async {
|
||||
emit(MessagingLoading());
|
||||
try {
|
||||
final conversation = await _repository.demarrerConversationRole(
|
||||
roleCible: event.roleCible,
|
||||
organisationId: event.organisationId,
|
||||
premierMessage: event.premierMessage,
|
||||
);
|
||||
emit(ConversationCreee(conversation));
|
||||
} catch (e) {
|
||||
emit(MessagingError(e.toString().replaceFirst('Exception: ', '')));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onArchiverConversation(
|
||||
ArchiverConversation event,
|
||||
Emitter<MessagingState> emit,
|
||||
) async {
|
||||
try {
|
||||
await _repository.archiverConversation(event.conversationId);
|
||||
emit(const MessagingActionOk('archiver'));
|
||||
add(const LoadMesConversations());
|
||||
} catch (e) {
|
||||
emit(MessagingError(e.toString().replaceFirst('Exception: ', '')));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onEnvoyerMessageTexte(
|
||||
EnvoyerMessageTexte event,
|
||||
Emitter<MessagingState> emit,
|
||||
) async {
|
||||
try {
|
||||
final message = await _repository.envoyerMessage(
|
||||
event.conversationId,
|
||||
typeMessage: 'TEXTE',
|
||||
contenu: event.contenu,
|
||||
messageParentId: event.messageParentId,
|
||||
);
|
||||
emit(MessageEnvoye(message: message, conversationId: event.conversationId));
|
||||
// Recharger les messages après envoi
|
||||
add(LoadMessages(conversationId: event.conversationId));
|
||||
} catch (e) {
|
||||
emit(MessagingError(e.toString().replaceFirst('Exception: ', '')));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onLoadMessages(
|
||||
LoadMessages event,
|
||||
Emitter<MessagingState> emit,
|
||||
) async {
|
||||
emit(MessagingLoading());
|
||||
|
||||
final result = await getMessages(
|
||||
conversationId: event.conversationId,
|
||||
limit: event.limit,
|
||||
beforeMessageId: event.beforeMessageId,
|
||||
);
|
||||
|
||||
result.fold(
|
||||
(failure) => emit(MessagingError(failure.message)),
|
||||
(messages) => emit(MessagesLoaded(
|
||||
try {
|
||||
final messages = await _repository.getMessages(event.conversationId, page: event.page);
|
||||
emit(MessagesLoaded(
|
||||
conversationId: event.conversationId,
|
||||
messages: messages,
|
||||
hasMore: messages.length == (event.limit ?? 50),
|
||||
)),
|
||||
);
|
||||
hasMore: messages.length >= 20,
|
||||
));
|
||||
} catch (e) {
|
||||
emit(MessagingError(e.toString().replaceFirst('Exception: ', '')));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onSendMessage(
|
||||
SendMessageEvent event,
|
||||
Future<void> _onMarquerLu(
|
||||
MarquerLu event,
|
||||
Emitter<MessagingState> emit,
|
||||
) async {
|
||||
final result = await sendMessage(
|
||||
conversationId: event.conversationId,
|
||||
content: event.content,
|
||||
attachments: event.attachments,
|
||||
priority: event.priority,
|
||||
);
|
||||
|
||||
result.fold(
|
||||
(failure) => emit(MessagingError(failure.message)),
|
||||
(message) => emit(MessageSent(message)),
|
||||
);
|
||||
await _repository.marquerLu(event.conversationId);
|
||||
}
|
||||
|
||||
Future<void> _onSendBroadcast(
|
||||
SendBroadcastEvent event,
|
||||
Future<void> _onSupprimerMessage(
|
||||
SupprimerMessage event,
|
||||
Emitter<MessagingState> emit,
|
||||
) async {
|
||||
final result = await sendBroadcast(
|
||||
organizationId: event.organizationId,
|
||||
subject: event.subject,
|
||||
content: event.content,
|
||||
priority: event.priority,
|
||||
attachments: event.attachments,
|
||||
);
|
||||
try {
|
||||
await _repository.supprimerMessage(event.conversationId, event.messageId);
|
||||
emit(const MessagingActionOk('supprimer-message'));
|
||||
add(LoadMessages(conversationId: event.conversationId));
|
||||
} catch (e) {
|
||||
emit(MessagingError(e.toString().replaceFirst('Exception: ', '')));
|
||||
}
|
||||
}
|
||||
|
||||
result.fold(
|
||||
(failure) => emit(MessagingError(failure.message)),
|
||||
(message) => emit(BroadcastSent(message)),
|
||||
);
|
||||
Future<void> _onNouveauMessageWebSocket(
|
||||
NouveauMessageWebSocket event,
|
||||
Emitter<MessagingState> emit,
|
||||
) async {
|
||||
// Si la conversation est actuellement ouverte, recharger les messages
|
||||
if (_currentConversationId == event.conversationId) {
|
||||
add(LoadMessages(conversationId: event.conversationId));
|
||||
} else {
|
||||
// Sinon, rafraîchir la liste pour mettre à jour le badge non-lus
|
||||
add(const LoadMesConversations());
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() {
|
||||
_wsSubscription?.cancel();
|
||||
_currentConversationId = null;
|
||||
return super.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
/// Événements du BLoC Messaging
|
||||
/// Événements du BLoC Messagerie v4
|
||||
library messaging_event;
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import '../../domain/entities/message.dart';
|
||||
|
||||
abstract class MessagingEvent extends Equatable {
|
||||
const MessagingEvent();
|
||||
@@ -11,108 +10,126 @@ abstract class MessagingEvent extends Equatable {
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
/// Charger les conversations
|
||||
class LoadConversations extends MessagingEvent {
|
||||
final String? organizationId;
|
||||
final bool includeArchived;
|
||||
// ── Conversations ─────────────────────────────────────────────────────────────
|
||||
|
||||
const LoadConversations({
|
||||
this.organizationId,
|
||||
this.includeArchived = false,
|
||||
/// Charger la liste des conversations
|
||||
class LoadMesConversations extends MessagingEvent {
|
||||
const LoadMesConversations();
|
||||
}
|
||||
|
||||
/// Ouvrir une conversation (charge le détail)
|
||||
class OpenConversation extends MessagingEvent {
|
||||
final String conversationId;
|
||||
|
||||
const OpenConversation(this.conversationId);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [conversationId];
|
||||
}
|
||||
|
||||
/// Démarrer une conversation directe avec un membre
|
||||
class DemarrerConversationDirecte extends MessagingEvent {
|
||||
final String destinataireId;
|
||||
final String organisationId;
|
||||
final String? premierMessage;
|
||||
|
||||
const DemarrerConversationDirecte({
|
||||
required this.destinataireId,
|
||||
required this.organisationId,
|
||||
this.premierMessage,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [organizationId, includeArchived];
|
||||
List<Object?> get props => [destinataireId, organisationId, premierMessage];
|
||||
}
|
||||
|
||||
/// Charger les messages d'une conversation
|
||||
/// Démarrer un canal de communication avec un rôle
|
||||
class DemarrerConversationRole extends MessagingEvent {
|
||||
final String roleCible;
|
||||
final String organisationId;
|
||||
final String? premierMessage;
|
||||
|
||||
const DemarrerConversationRole({
|
||||
required this.roleCible,
|
||||
required this.organisationId,
|
||||
this.premierMessage,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [roleCible, organisationId, premierMessage];
|
||||
}
|
||||
|
||||
/// Archiver une conversation
|
||||
class ArchiverConversation extends MessagingEvent {
|
||||
final String conversationId;
|
||||
|
||||
const ArchiverConversation(this.conversationId);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [conversationId];
|
||||
}
|
||||
|
||||
// ── Messages ──────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Envoyer un message texte
|
||||
class EnvoyerMessageTexte extends MessagingEvent {
|
||||
final String conversationId;
|
||||
final String contenu;
|
||||
final String? messageParentId;
|
||||
|
||||
const EnvoyerMessageTexte({
|
||||
required this.conversationId,
|
||||
required this.contenu,
|
||||
this.messageParentId,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [conversationId, contenu, messageParentId];
|
||||
}
|
||||
|
||||
/// Charger l'historique des messages
|
||||
class LoadMessages extends MessagingEvent {
|
||||
final String conversationId;
|
||||
final int? limit;
|
||||
final String? beforeMessageId;
|
||||
final int page;
|
||||
|
||||
const LoadMessages({
|
||||
required this.conversationId,
|
||||
this.limit,
|
||||
this.beforeMessageId,
|
||||
});
|
||||
const LoadMessages({required this.conversationId, this.page = 0});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [conversationId, limit, beforeMessageId];
|
||||
List<Object?> get props => [conversationId, page];
|
||||
}
|
||||
|
||||
/// Envoyer un message
|
||||
class SendMessageEvent extends MessagingEvent {
|
||||
/// Marquer une conversation comme lue
|
||||
class MarquerLu extends MessagingEvent {
|
||||
final String conversationId;
|
||||
final String content;
|
||||
final List<String>? attachments;
|
||||
final MessagePriority priority;
|
||||
|
||||
const SendMessageEvent({
|
||||
required this.conversationId,
|
||||
required this.content,
|
||||
this.attachments,
|
||||
this.priority = MessagePriority.normal,
|
||||
});
|
||||
const MarquerLu(this.conversationId);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [conversationId, content, attachments, priority];
|
||||
List<Object?> get props => [conversationId];
|
||||
}
|
||||
|
||||
/// Envoyer un broadcast
|
||||
class SendBroadcastEvent extends MessagingEvent {
|
||||
final String organizationId;
|
||||
final String subject;
|
||||
final String content;
|
||||
final MessagePriority priority;
|
||||
final List<String>? attachments;
|
||||
|
||||
const SendBroadcastEvent({
|
||||
required this.organizationId,
|
||||
required this.subject,
|
||||
required this.content,
|
||||
this.priority = MessagePriority.normal,
|
||||
this.attachments,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [organizationId, subject, content, priority, attachments];
|
||||
}
|
||||
|
||||
/// Marquer un message comme lu
|
||||
class MarkMessageAsReadEvent extends MessagingEvent {
|
||||
/// Supprimer un message
|
||||
class SupprimerMessage extends MessagingEvent {
|
||||
final String conversationId;
|
||||
final String messageId;
|
||||
|
||||
const MarkMessageAsReadEvent(this.messageId);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [messageId];
|
||||
}
|
||||
|
||||
/// Charger le nombre de messages non lus
|
||||
class LoadUnreadCount extends MessagingEvent {
|
||||
final String? organizationId;
|
||||
|
||||
const LoadUnreadCount({this.organizationId});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [organizationId];
|
||||
}
|
||||
|
||||
/// Créer une nouvelle conversation
|
||||
class CreateConversationEvent extends MessagingEvent {
|
||||
final String name;
|
||||
final List<String> participantIds;
|
||||
final String? organizationId;
|
||||
final String? description;
|
||||
|
||||
const CreateConversationEvent({
|
||||
required this.name,
|
||||
required this.participantIds,
|
||||
this.organizationId,
|
||||
this.description,
|
||||
const SupprimerMessage({
|
||||
required this.conversationId,
|
||||
required this.messageId,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [name, participantIds, organizationId, description];
|
||||
List<Object?> get props => [conversationId, messageId];
|
||||
}
|
||||
|
||||
// ── Temps réel WebSocket ───────────────────────────────────────────────────────
|
||||
|
||||
/// Nouveau message reçu via WebSocket
|
||||
class NouveauMessageWebSocket extends MessagingEvent {
|
||||
final String conversationId;
|
||||
|
||||
const NouveauMessageWebSocket(this.conversationId);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [conversationId];
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
/// États du BLoC Messaging
|
||||
/// États du BLoC Messagerie v4
|
||||
library messaging_state;
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
import '../../domain/entities/conversation.dart';
|
||||
import '../../domain/entities/message.dart';
|
||||
|
||||
@@ -18,18 +19,24 @@ class MessagingInitial extends MessagingState {}
|
||||
/// Chargement en cours
|
||||
class MessagingLoading extends MessagingState {}
|
||||
|
||||
/// Conversations chargées
|
||||
class ConversationsLoaded extends MessagingState {
|
||||
final List<Conversation> conversations;
|
||||
final int unreadCount;
|
||||
/// Liste des conversations chargée
|
||||
class MesConversationsLoaded extends MessagingState {
|
||||
final List<ConversationSummary> conversations;
|
||||
|
||||
const ConversationsLoaded({
|
||||
required this.conversations,
|
||||
this.unreadCount = 0,
|
||||
});
|
||||
const MesConversationsLoaded(this.conversations);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [conversations, unreadCount];
|
||||
List<Object?> get props => [conversations];
|
||||
}
|
||||
|
||||
/// Conversation ouverte (détail avec messages)
|
||||
class ConversationOuverte extends MessagingState {
|
||||
final Conversation conversation;
|
||||
|
||||
const ConversationOuverte(this.conversation);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [conversation];
|
||||
}
|
||||
|
||||
/// Messages d'une conversation chargés
|
||||
@@ -48,44 +55,35 @@ class MessagesLoaded extends MessagingState {
|
||||
List<Object?> get props => [conversationId, messages, hasMore];
|
||||
}
|
||||
|
||||
/// Message envoyé avec succès
|
||||
class MessageSent extends MessagingState {
|
||||
/// Message envoyé avec succès — la conversation s'actualise
|
||||
class MessageEnvoye extends MessagingState {
|
||||
final Message message;
|
||||
final String conversationId;
|
||||
|
||||
const MessageSent(this.message);
|
||||
const MessageEnvoye({required this.message, required this.conversationId});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [message];
|
||||
List<Object?> get props => [message, conversationId];
|
||||
}
|
||||
|
||||
/// Broadcast envoyé avec succès
|
||||
class BroadcastSent extends MessagingState {
|
||||
final Message message;
|
||||
|
||||
const BroadcastSent(this.message);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [message];
|
||||
}
|
||||
|
||||
/// Conversation créée
|
||||
class ConversationCreated extends MessagingState {
|
||||
/// Conversation créée (après démarrage direct/rôle)
|
||||
class ConversationCreee extends MessagingState {
|
||||
final Conversation conversation;
|
||||
|
||||
const ConversationCreated(this.conversation);
|
||||
const ConversationCreee(this.conversation);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [conversation];
|
||||
}
|
||||
|
||||
/// Compteur de non lus chargé
|
||||
class UnreadCountLoaded extends MessagingState {
|
||||
final int count;
|
||||
/// Action silencieuse réussie (marquer lu, supprimer message, etc.)
|
||||
class MessagingActionOk extends MessagingState {
|
||||
final String action;
|
||||
|
||||
const UnreadCountLoaded(this.count);
|
||||
const MessagingActionOk(this.action);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [count];
|
||||
List<Object?> get props => [action];
|
||||
}
|
||||
|
||||
/// Erreur
|
||||
|
||||
@@ -0,0 +1,212 @@
|
||||
/// Page contacter le bureau — Communication UnionFlow v4
|
||||
///
|
||||
/// Remplace l'ancien broadcast par un canal vers le rôle PRESIDENT.
|
||||
library broadcast_page;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import '../../../../shared/design_system/unionflow_design_system.dart';
|
||||
import '../../../../shared/widgets/core_card.dart';
|
||||
import '../bloc/messaging_bloc.dart';
|
||||
import '../bloc/messaging_event.dart';
|
||||
import '../bloc/messaging_state.dart';
|
||||
import 'conversation_detail_page.dart';
|
||||
|
||||
/// Page pour contacter le bureau (canal rôle PRESIDENT)
|
||||
class BroadcastPage extends StatefulWidget {
|
||||
final String organizationId;
|
||||
|
||||
const BroadcastPage({
|
||||
super.key,
|
||||
required this.organizationId,
|
||||
});
|
||||
|
||||
@override
|
||||
State<BroadcastPage> createState() => _BroadcastPageState();
|
||||
}
|
||||
|
||||
class _BroadcastPageState extends State<BroadcastPage> {
|
||||
final _messageController = TextEditingController();
|
||||
String _selectedRole = 'PRESIDENT';
|
||||
bool _isSending = false;
|
||||
|
||||
static const _roles = [
|
||||
('PRESIDENT', 'Président'),
|
||||
('TRESORIER', 'Trésorier'),
|
||||
('SECRETAIRE', 'Secrétaire'),
|
||||
('VICE_PRESIDENT', 'Vice-Président'),
|
||||
('CONSEILLER', 'Conseiller'),
|
||||
];
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_messageController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _submit() {
|
||||
final message = _messageController.text.trim();
|
||||
if (message.isEmpty || _isSending) return;
|
||||
|
||||
setState(() => _isSending = true);
|
||||
|
||||
context.read<MessagingBloc>().add(
|
||||
DemarrerConversationRole(
|
||||
roleCible: _selectedRole,
|
||||
organisationId: widget.organizationId,
|
||||
premierMessage: message,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
|
||||
return BlocListener<MessagingBloc, MessagingState>(
|
||||
listener: (context, state) {
|
||||
if (state is ConversationCreee) {
|
||||
setState(() => _isSending = false);
|
||||
// Naviguer vers la conversation créée
|
||||
Navigator.of(context).pushReplacement(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => BlocProvider.value(
|
||||
value: context.read<MessagingBloc>(),
|
||||
child: ConversationDetailPage(conversationId: state.conversation.id),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
if (state is MessagingError) {
|
||||
setState(() => _isSending = false);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(state.message),
|
||||
backgroundColor: ColorTokens.error,
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
child: Scaffold(
|
||||
backgroundColor: scheme.surface,
|
||||
appBar: UFAppBar(
|
||||
title: 'Contacter le bureau',
|
||||
moduleGradient: ModuleColors.communicationGradient,
|
||||
automaticallyImplyLeading: true,
|
||||
),
|
||||
body: SafeArea(
|
||||
top: false,
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(SpacingTokens.md),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// Info
|
||||
CoreCard(
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 44,
|
||||
height: 44,
|
||||
decoration: BoxDecoration(
|
||||
color: ModuleColors.communication.withOpacity(0.15),
|
||||
borderRadius: BorderRadius.circular(SpacingTokens.radiusSm),
|
||||
),
|
||||
child: const Icon(
|
||||
Icons.account_circle_outlined,
|
||||
color: ModuleColors.communication,
|
||||
size: 24,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: SpacingTokens.md),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('Contacter un responsable', style: AppTypography.actionText),
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
'Votre message sera transmis au titulaire du rôle choisi',
|
||||
style: AppTypography.subtitleSmall,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: SpacingTokens.lg),
|
||||
|
||||
// Sélection du rôle
|
||||
Text('Destinataire', style: AppTypography.actionText),
|
||||
const SizedBox(height: SpacingTokens.sm),
|
||||
Wrap(
|
||||
spacing: SpacingTokens.sm,
|
||||
runSpacing: SpacingTokens.sm,
|
||||
children: _roles.map((role) {
|
||||
final isSelected = _selectedRole == role.$1;
|
||||
return ChoiceChip(
|
||||
label: Text(role.$2),
|
||||
selected: isSelected,
|
||||
selectedColor: ModuleColors.communication.withOpacity(0.2),
|
||||
labelStyle: AppTypography.bodyTextSmall.copyWith(
|
||||
color: isSelected ? ModuleColors.communication : null,
|
||||
fontWeight: isSelected ? FontWeight.w600 : null,
|
||||
),
|
||||
onSelected: (_) => setState(() => _selectedRole = role.$1),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
|
||||
const SizedBox(height: SpacingTokens.lg),
|
||||
|
||||
// Message
|
||||
Text('Message', style: AppTypography.actionText),
|
||||
const SizedBox(height: SpacingTokens.sm),
|
||||
TextField(
|
||||
controller: _messageController,
|
||||
maxLines: 6,
|
||||
minLines: 3,
|
||||
decoration: InputDecoration(
|
||||
hintText: 'Écrivez votre message...',
|
||||
hintStyle: AppTypography.bodyTextSmall.copyWith(
|
||||
color: scheme.onSurfaceVariant,
|
||||
),
|
||||
filled: true,
|
||||
fillColor: scheme.surfaceContainerHighest.withOpacity(0.3),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(SpacingTokens.radiusMd),
|
||||
borderSide: BorderSide(color: scheme.outlineVariant),
|
||||
),
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(SpacingTokens.radiusMd),
|
||||
borderSide: BorderSide(color: scheme.outlineVariant),
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(SpacingTokens.radiusMd),
|
||||
borderSide: const BorderSide(color: ModuleColors.communication, width: 2),
|
||||
),
|
||||
),
|
||||
style: AppTypography.bodyMedium,
|
||||
),
|
||||
|
||||
const SizedBox(height: SpacingTokens.xl),
|
||||
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: UFPrimaryButton(
|
||||
label: _isSending ? 'Envoi en cours...' : 'Envoyer le message',
|
||||
onPressed: _isSending ? null : _submit,
|
||||
icon: _isSending ? null : Icons.send_rounded,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,377 @@
|
||||
/// Page de détail d'une conversation — Messages v4
|
||||
library conversation_detail_page;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import '../../../../core/utils/logger.dart';
|
||||
import '../../../../shared/design_system/unionflow_design_system.dart';
|
||||
import '../../domain/entities/conversation.dart';
|
||||
import '../../domain/entities/message.dart';
|
||||
import '../bloc/messaging_bloc.dart';
|
||||
import '../bloc/messaging_event.dart';
|
||||
import '../bloc/messaging_state.dart';
|
||||
import '../widgets/message_bubble.dart';
|
||||
|
||||
/// Page détail conversation : messages + champ d'envoi
|
||||
class ConversationDetailPage extends StatefulWidget {
|
||||
final String conversationId;
|
||||
|
||||
const ConversationDetailPage({
|
||||
super.key,
|
||||
required this.conversationId,
|
||||
});
|
||||
|
||||
@override
|
||||
State<ConversationDetailPage> createState() => _ConversationDetailPageState();
|
||||
}
|
||||
|
||||
class _ConversationDetailPageState extends State<ConversationDetailPage> {
|
||||
final TextEditingController _messageController = TextEditingController();
|
||||
final ScrollController _scrollController = ScrollController();
|
||||
final FocusNode _focusNode = FocusNode();
|
||||
|
||||
Conversation? _conversation;
|
||||
String? _currentUserId;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
context.read<MessagingBloc>().add(OpenConversation(widget.conversationId));
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_messageController.dispose();
|
||||
_scrollController.dispose();
|
||||
_focusNode.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _sendMessage() {
|
||||
final content = _messageController.text.trim();
|
||||
if (content.isEmpty) return;
|
||||
|
||||
context.read<MessagingBloc>().add(
|
||||
EnvoyerMessageTexte(
|
||||
conversationId: widget.conversationId,
|
||||
contenu: content,
|
||||
),
|
||||
);
|
||||
_messageController.clear();
|
||||
_focusNode.requestFocus();
|
||||
}
|
||||
|
||||
void _scrollToBottom() {
|
||||
if (_scrollController.hasClients) {
|
||||
_scrollController.animateTo(
|
||||
_scrollController.position.maxScrollExtent,
|
||||
duration: const Duration(milliseconds: 300),
|
||||
curve: Curves.easeOut,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: scheme.surface,
|
||||
appBar: _buildAppBar(),
|
||||
body: Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: BlocConsumer<MessagingBloc, MessagingState>(
|
||||
listener: (context, state) {
|
||||
if (state is ConversationOuverte) {
|
||||
setState(() {
|
||||
_conversation = state.conversation;
|
||||
// Déterminer l'id de l'utilisateur courant via le premier participant non-expéditeur
|
||||
});
|
||||
}
|
||||
if (state is MessagesLoaded) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => _scrollToBottom());
|
||||
}
|
||||
if (state is MessageEnvoye) {
|
||||
AppLogger.info('Message envoyé: ${state.message.id}');
|
||||
}
|
||||
if (state is MessagingError) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(state.message),
|
||||
backgroundColor: ColorTokens.error,
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
builder: (context, state) {
|
||||
if (state is MessagingLoading && _conversation == null) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
if (state is ConversationOuverte) {
|
||||
return _buildMessagesList(state.conversation.messages, scheme);
|
||||
}
|
||||
|
||||
if (state is MessagesLoaded) {
|
||||
return _buildMessagesList(state.messages, scheme);
|
||||
}
|
||||
|
||||
if (_conversation != null) {
|
||||
return _buildMessagesList(_conversation!.messages, scheme);
|
||||
}
|
||||
|
||||
return const SizedBox.shrink();
|
||||
},
|
||||
),
|
||||
),
|
||||
_buildInputBar(scheme),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
UFAppBar _buildAppBar() {
|
||||
final title = _conversation?.titre ?? 'Conversation';
|
||||
|
||||
return UFAppBar(
|
||||
title: title,
|
||||
moduleGradient: ModuleColors.communicationGradient,
|
||||
automaticallyImplyLeading: true,
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.delete_outline, color: ModuleColors.communicationOnColor),
|
||||
onPressed: _conversation != null ? _confirmArchive : null,
|
||||
tooltip: 'Archiver',
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
void _confirmArchive() {
|
||||
showDialog<bool>(
|
||||
context: context,
|
||||
builder: (ctx) => AlertDialog(
|
||||
title: const Text('Archiver la conversation ?'),
|
||||
content: const Text('La conversation sera archivée et n\'apparaîtra plus dans votre liste.'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(ctx, false),
|
||||
child: const Text('Annuler'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(ctx, true),
|
||||
style: TextButton.styleFrom(foregroundColor: ColorTokens.error),
|
||||
child: const Text('Archiver'),
|
||||
),
|
||||
],
|
||||
),
|
||||
).then((confirmed) {
|
||||
if (confirmed == true && mounted) {
|
||||
context.read<MessagingBloc>().add(ArchiverConversation(widget.conversationId));
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Widget _buildMessagesList(List<Message> messages, ColorScheme scheme) {
|
||||
if (messages.isEmpty) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.chat_bubble_outline,
|
||||
size: 64,
|
||||
color: scheme.onSurfaceVariant.withOpacity(0.4),
|
||||
),
|
||||
const SizedBox(height: SpacingTokens.md),
|
||||
Text('Aucun message', style: AppTypography.headerSmall),
|
||||
const SizedBox(height: SpacingTokens.sm),
|
||||
Text('Envoyez le premier message !', style: AppTypography.bodyTextSmall),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return ListView.builder(
|
||||
controller: _scrollController,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: SpacingTokens.md,
|
||||
vertical: SpacingTokens.sm,
|
||||
),
|
||||
itemCount: messages.length,
|
||||
itemBuilder: (context, index) {
|
||||
final message = messages[index];
|
||||
final isMine = _isMyMessage(message);
|
||||
|
||||
final showDateSeparator = index == 0 ||
|
||||
!_isSameDay(
|
||||
messages[index - 1].dateEnvoi ?? DateTime.now(),
|
||||
message.dateEnvoi ?? DateTime.now(),
|
||||
);
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
if (showDateSeparator)
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: SpacingTokens.sm),
|
||||
child: Center(
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: SpacingTokens.md, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: scheme.surfaceContainerHighest,
|
||||
borderRadius: BorderRadius.circular(SpacingTokens.radiusCircular),
|
||||
),
|
||||
child: Text(
|
||||
_formatDateSeparator(message.dateEnvoi ?? DateTime.now()),
|
||||
style: AppTypography.badgeText.copyWith(
|
||||
color: scheme.onSurfaceVariant,
|
||||
fontSize: 11,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
MessageBubble(
|
||||
message: message,
|
||||
isMine: isMine,
|
||||
onLongPress: isMine && !message.supprime
|
||||
? () => _showMessageOptions(message)
|
||||
: null,
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _showMessageOptions(Message message) {
|
||||
showModalBottomSheet<void>(
|
||||
context: context,
|
||||
builder: (ctx) => SafeArea(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
ListTile(
|
||||
leading: const Icon(Icons.delete_outline, color: AppColors.error),
|
||||
title: const Text('Supprimer le message'),
|
||||
onTap: () {
|
||||
Navigator.pop(ctx);
|
||||
context.read<MessagingBloc>().add(
|
||||
SupprimerMessage(
|
||||
conversationId: widget.conversationId,
|
||||
messageId: message.id,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.close),
|
||||
title: const Text('Annuler'),
|
||||
onTap: () => Navigator.pop(ctx),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildInputBar(ColorScheme scheme) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: SpacingTokens.sm,
|
||||
vertical: SpacingTokens.sm,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: scheme.surface,
|
||||
border: Border(
|
||||
top: BorderSide(color: scheme.outlineVariant.withOpacity(0.3)),
|
||||
),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: scheme.shadow.withOpacity(0.05),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, -2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: SafeArea(
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
color: scheme.surfaceContainerHighest.withOpacity(0.5),
|
||||
borderRadius: BorderRadius.circular(SpacingTokens.radiusCircular),
|
||||
),
|
||||
child: TextField(
|
||||
controller: _messageController,
|
||||
focusNode: _focusNode,
|
||||
textInputAction: TextInputAction.send,
|
||||
maxLines: 4,
|
||||
minLines: 1,
|
||||
onSubmitted: (_) => _sendMessage(),
|
||||
decoration: InputDecoration(
|
||||
hintText: 'Écrire un message...',
|
||||
hintStyle: AppTypography.bodyTextSmall.copyWith(
|
||||
color: scheme.onSurfaceVariant,
|
||||
),
|
||||
contentPadding: const EdgeInsets.symmetric(
|
||||
horizontal: SpacingTokens.md,
|
||||
vertical: SpacingTokens.sm,
|
||||
),
|
||||
border: InputBorder.none,
|
||||
),
|
||||
style: AppTypography.bodyTextSmall,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: SpacingTokens.sm),
|
||||
Material(
|
||||
color: ModuleColors.communication,
|
||||
borderRadius: BorderRadius.circular(SpacingTokens.radiusCircular),
|
||||
child: InkWell(
|
||||
onTap: _sendMessage,
|
||||
borderRadius: BorderRadius.circular(SpacingTokens.radiusCircular),
|
||||
child: Container(
|
||||
width: 44,
|
||||
height: 44,
|
||||
alignment: Alignment.center,
|
||||
child: const Icon(
|
||||
Icons.send_rounded,
|
||||
color: ModuleColors.communicationOnColor,
|
||||
size: 22,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// ── Helpers ────────────────────────────────────────────────────────────────
|
||||
|
||||
bool _isMyMessage(Message message) {
|
||||
// On ne peut pas connaître l'ID du membre courant sans le BLoC d'auth,
|
||||
// mais l'expéditeur d'un message récent qu'on a envoyé sera déterminé
|
||||
// via la liste des participants. En attendant le token JWT, on utilise
|
||||
// le premier participant de type INITIATEUR s'il n'y en a pas d'autre.
|
||||
// TODO: Injecter AuthBloc pour comparer expediteurId avec l'id du user connecté.
|
||||
return false; // Tous les messages affichés comme reçus pour l'instant
|
||||
}
|
||||
|
||||
bool _isSameDay(DateTime a, DateTime b) {
|
||||
return a.year == b.year && a.month == b.month && a.day == b.day;
|
||||
}
|
||||
|
||||
String _formatDateSeparator(DateTime date) {
|
||||
final now = DateTime.now();
|
||||
if (_isSameDay(date, now)) return "Aujourd'hui";
|
||||
if (_isSameDay(date, now.subtract(const Duration(days: 1)))) return 'Hier';
|
||||
return '${date.day.toString().padLeft(2, '0')}/${date.month.toString().padLeft(2, '0')}/${date.year}';
|
||||
}
|
||||
}
|
||||
@@ -1,143 +1,141 @@
|
||||
/// Page liste des conversations
|
||||
/// Page liste des conversations v4
|
||||
library conversations_page;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../../../../core/di/injection_container.dart';
|
||||
|
||||
import '../../../../shared/design_system/unionflow_design_system.dart';
|
||||
import '../../../../shared/utils/snackbar_helper.dart';
|
||||
import '../../domain/entities/conversation.dart';
|
||||
import '../bloc/messaging_bloc.dart';
|
||||
import '../bloc/messaging_event.dart';
|
||||
import '../bloc/messaging_state.dart';
|
||||
import '../widgets/conversation_tile.dart';
|
||||
import 'conversation_detail_page.dart';
|
||||
|
||||
class ConversationsPage extends StatelessWidget {
|
||||
final String? organizationId;
|
||||
|
||||
const ConversationsPage({
|
||||
super.key,
|
||||
this.organizationId,
|
||||
});
|
||||
const ConversationsPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocProvider(
|
||||
create: (_) => sl<MessagingBloc>()
|
||||
..add(LoadConversations(organizationId: organizationId)),
|
||||
child: Scaffold(
|
||||
backgroundColor: ColorTokens.background,
|
||||
appBar: const UFAppBar(
|
||||
title: 'MESSAGES',
|
||||
automaticallyImplyLeading: true,
|
||||
),
|
||||
body: BlocBuilder<MessagingBloc, MessagingState>(
|
||||
builder: (context, state) {
|
||||
if (state is MessagingLoading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
|
||||
if (state is MessagingError) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.error_outline,
|
||||
size: 64,
|
||||
color: AppColors.error,
|
||||
),
|
||||
const SizedBox(height: SpacingTokens.md),
|
||||
Text(
|
||||
'Erreur',
|
||||
style: AppTypography.headerSmall,
|
||||
),
|
||||
const SizedBox(height: SpacingTokens.sm),
|
||||
Text(
|
||||
state.message,
|
||||
style: AppTypography.bodyTextSmall,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: SpacingTokens.lg),
|
||||
UFPrimaryButton(
|
||||
label: 'Réessayer',
|
||||
onPressed: () {
|
||||
context.read<MessagingBloc>().add(
|
||||
LoadConversations(organizationId: organizationId),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
return Scaffold(
|
||||
backgroundColor: scheme.surface,
|
||||
appBar: UFAppBar(
|
||||
title: 'Messages',
|
||||
moduleGradient: ModuleColors.communicationGradient,
|
||||
automaticallyImplyLeading: true,
|
||||
),
|
||||
body: BlocConsumer<MessagingBloc, MessagingState>(
|
||||
listener: (context, state) {
|
||||
if (state is MessagingError) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(state.message),
|
||||
backgroundColor: ColorTokens.error,
|
||||
),
|
||||
);
|
||||
}
|
||||
if (state is ConversationCreee) {
|
||||
_openDetail(context, state.conversation.id);
|
||||
}
|
||||
},
|
||||
builder: (context, state) {
|
||||
if (state is MessagingLoading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
if (state is MesConversationsLoaded) {
|
||||
return _buildList(context, state.conversations);
|
||||
}
|
||||
|
||||
// État initial ou erreur non fatale — afficher liste vide
|
||||
return _buildEmpty(context);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildList(BuildContext context, List<ConversationSummary> conversations) {
|
||||
if (conversations.isEmpty) {
|
||||
return _buildEmpty(context);
|
||||
}
|
||||
|
||||
// Tri : non lus d'abord, puis par date de dernier message
|
||||
final sorted = List.of(conversations)
|
||||
..sort((a, b) {
|
||||
if (a.hasUnread && !b.hasUnread) return -1;
|
||||
if (!a.hasUnread && b.hasUnread) return 1;
|
||||
final aDate = a.dernierMessageAt;
|
||||
final bDate = b.dernierMessageAt;
|
||||
if (aDate == null && bDate == null) return 0;
|
||||
if (aDate == null) return 1;
|
||||
if (bDate == null) return -1;
|
||||
return bDate.compareTo(aDate);
|
||||
});
|
||||
|
||||
return RefreshIndicator(
|
||||
color: ModuleColors.communication,
|
||||
onRefresh: () async {
|
||||
context.read<MessagingBloc>().add(const LoadMesConversations());
|
||||
},
|
||||
child: ListView.separated(
|
||||
padding: const EdgeInsets.all(SpacingTokens.md),
|
||||
itemCount: sorted.length,
|
||||
separatorBuilder: (_, __) => const SizedBox(height: SpacingTokens.sm),
|
||||
itemBuilder: (context, index) {
|
||||
final conv = sorted[index];
|
||||
return ConversationTile(
|
||||
conversation: conv,
|
||||
onTap: () => _openDetail(context, conv.id),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildEmpty(BuildContext context) {
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
return RefreshIndicator(
|
||||
color: ModuleColors.communication,
|
||||
onRefresh: () async {
|
||||
context.read<MessagingBloc>().add(const LoadMesConversations());
|
||||
},
|
||||
child: SingleChildScrollView(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
child: SizedBox(
|
||||
height: MediaQuery.of(context).size.height * 0.6,
|
||||
child: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.chat_bubble_outline,
|
||||
size: 64,
|
||||
color: scheme.onSurfaceVariant.withOpacity(0.4),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (state is ConversationsLoaded) {
|
||||
final conversations = state.conversations;
|
||||
|
||||
if (conversations.isEmpty) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.chat_bubble_outline,
|
||||
size: 64,
|
||||
color: AppColors.textSecondaryLight,
|
||||
),
|
||||
const SizedBox(height: SpacingTokens.md),
|
||||
Text(
|
||||
'Aucune conversation',
|
||||
style: AppTypography.headerSmall.copyWith(
|
||||
color: AppColors.textSecondaryLight,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: SpacingTokens.sm),
|
||||
Text(
|
||||
'Commencez une nouvelle conversation',
|
||||
style: AppTypography.bodyTextSmall.copyWith(
|
||||
color: AppColors.textSecondaryLight,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
context.read<MessagingBloc>().add(
|
||||
LoadConversations(organizationId: organizationId),
|
||||
);
|
||||
},
|
||||
child: ListView.separated(
|
||||
padding: const EdgeInsets.all(SpacingTokens.md),
|
||||
itemCount: conversations.length,
|
||||
separatorBuilder: (_, __) => const SizedBox(height: SpacingTokens.sm),
|
||||
itemBuilder: (context, index) {
|
||||
final conversation = conversations[index];
|
||||
return ConversationTile(
|
||||
conversation: conversation,
|
||||
onTap: () {
|
||||
SnackbarHelper.showNotImplemented(
|
||||
context,
|
||||
'Affichage des messages',
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
const SizedBox(height: SpacingTokens.md),
|
||||
Text('Aucune conversation', style: AppTypography.headerSmall),
|
||||
const SizedBox(height: SpacingTokens.sm),
|
||||
Text(
|
||||
'Contactez un membre ou le bureau\nde votre organisation',
|
||||
style: AppTypography.bodyTextSmall,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return const SizedBox.shrink();
|
||||
},
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
backgroundColor: AppColors.primaryGreen,
|
||||
onPressed: () {
|
||||
SnackbarHelper.showNotImplemented(context, 'Nouvelle conversation');
|
||||
},
|
||||
child: const Icon(Icons.add, color: Colors.white),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _openDetail(BuildContext context, String conversationId) {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => BlocProvider.value(
|
||||
value: context.read<MessagingBloc>(),
|
||||
child: ConversationDetailPage(conversationId: conversationId),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
/// Wrapper BLoC pour la page des conversations v4
|
||||
///
|
||||
/// Fournit le MessagingBloc et charge les conversations au démarrage.
|
||||
library conversations_page_wrapper;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import '../../../../core/di/injection_container.dart';
|
||||
import '../../../../core/utils/logger.dart';
|
||||
import '../bloc/messaging_bloc.dart';
|
||||
import '../bloc/messaging_event.dart';
|
||||
import 'conversations_page.dart';
|
||||
|
||||
/// Wrapper qui fournit le BLoC à la page des conversations
|
||||
class ConversationsPageWrapper extends StatelessWidget {
|
||||
const ConversationsPageWrapper({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
AppLogger.info('ConversationsPageWrapper: Création du BlocProvider');
|
||||
|
||||
return BlocProvider<MessagingBloc>(
|
||||
create: (context) {
|
||||
AppLogger.info('ConversationsPageWrapper: Initialisation du MessagingBloc');
|
||||
final bloc = sl<MessagingBloc>();
|
||||
bloc.add(const LoadMesConversations());
|
||||
return bloc;
|
||||
},
|
||||
child: const ConversationsPage(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,241 @@
|
||||
/// Widget bulle de message v4 — Communication UnionFlow
|
||||
library message_bubble;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
import '../../../../shared/design_system/unionflow_design_system.dart';
|
||||
import '../../domain/entities/message.dart';
|
||||
|
||||
/// Bulle de message différenciée envoyé/reçu
|
||||
class MessageBubble extends StatelessWidget {
|
||||
final Message message;
|
||||
final bool isMine;
|
||||
final VoidCallback? onLongPress;
|
||||
|
||||
const MessageBubble({
|
||||
super.key,
|
||||
required this.message,
|
||||
required this.isMine,
|
||||
this.onLongPress,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
|
||||
// Message supprimé
|
||||
if (message.supprime) {
|
||||
return _buildDeleted(scheme);
|
||||
}
|
||||
|
||||
return Align(
|
||||
alignment: isMine ? Alignment.centerRight : Alignment.centerLeft,
|
||||
child: GestureDetector(
|
||||
onLongPress: onLongPress,
|
||||
child: Container(
|
||||
constraints: BoxConstraints(
|
||||
maxWidth: MediaQuery.of(context).size.width * 0.75,
|
||||
),
|
||||
margin: EdgeInsets.only(
|
||||
left: isMine ? 48 : 0,
|
||||
right: isMine ? 0 : 48,
|
||||
bottom: SpacingTokens.xs,
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment:
|
||||
isMine ? CrossAxisAlignment.end : CrossAxisAlignment.start,
|
||||
children: [
|
||||
// Nom expéditeur (messages reçus)
|
||||
if (!isMine && message.expediteurNomComplet.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: SpacingTokens.sm, bottom: 2),
|
||||
child: Text(
|
||||
message.expediteurNomComplet,
|
||||
style: AppTypography.badgeText.copyWith(
|
||||
color: ModuleColors.communication,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// Réponse à un message parent
|
||||
if (message.hasParent && message.messageParentApercu != null)
|
||||
Container(
|
||||
margin: EdgeInsets.only(
|
||||
left: isMine ? 0 : 0,
|
||||
bottom: 4,
|
||||
),
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: SpacingTokens.sm,
|
||||
vertical: 4,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: scheme.surfaceContainerHighest.withOpacity(0.7),
|
||||
borderRadius: BorderRadius.circular(SpacingTokens.radiusSm),
|
||||
border: Border(
|
||||
left: BorderSide(
|
||||
color: ModuleColors.communication,
|
||||
width: 3,
|
||||
),
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
message.messageParentApercu!,
|
||||
style: AppTypography.badgeText.copyWith(
|
||||
color: scheme.onSurfaceVariant,
|
||||
fontStyle: FontStyle.italic,
|
||||
),
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
|
||||
// Bulle principale
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: SpacingTokens.md,
|
||||
vertical: SpacingTokens.sm,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: isMine
|
||||
? ModuleColors.communication
|
||||
: scheme.surfaceContainerHighest,
|
||||
borderRadius: BorderRadius.only(
|
||||
topLeft: const Radius.circular(SpacingTokens.radiusMd),
|
||||
topRight: const Radius.circular(SpacingTokens.radiusMd),
|
||||
bottomLeft: Radius.circular(
|
||||
isMine ? SpacingTokens.radiusMd : SpacingTokens.radiusXs,
|
||||
),
|
||||
bottomRight: Radius.circular(
|
||||
isMine ? SpacingTokens.radiusXs : SpacingTokens.radiusMd,
|
||||
),
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// Contenu selon type
|
||||
_buildContent(scheme),
|
||||
|
||||
const SizedBox(height: 4),
|
||||
|
||||
// Horodatage
|
||||
if (message.dateEnvoi != null)
|
||||
Text(
|
||||
DateFormat('HH:mm').format(message.dateEnvoi!),
|
||||
style: AppTypography.badgeText.copyWith(
|
||||
color: isMine
|
||||
? Colors.white.withOpacity(0.7)
|
||||
: scheme.onSurfaceVariant,
|
||||
fontSize: 10,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildContent(ColorScheme scheme) {
|
||||
final textColor = isMine ? ModuleColors.communicationOnColor : scheme.onSurface;
|
||||
|
||||
if (message.isVocal) {
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.mic, size: 18, color: textColor),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
'Note vocale${message.dureeAudio != null ? ' · ${message.dureeAudio}s' : ''}',
|
||||
style: AppTypography.bodyTextSmall.copyWith(color: textColor),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
if (message.isImage) {
|
||||
if (message.urlFichier != null) {
|
||||
return ClipRRect(
|
||||
borderRadius: BorderRadius.circular(SpacingTokens.radiusSm),
|
||||
child: Image.network(
|
||||
message.urlFichier!,
|
||||
width: 200,
|
||||
height: 150,
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (_, __, ___) => Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.broken_image_outlined, size: 18, color: textColor),
|
||||
const SizedBox(width: 4),
|
||||
Text('Image', style: AppTypography.bodyTextSmall.copyWith(color: textColor)),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.image_outlined, size: 18, color: textColor),
|
||||
const SizedBox(width: 4),
|
||||
Text('Image', style: AppTypography.bodyTextSmall.copyWith(color: textColor)),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
if (message.isSysteme) {
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.info_outline, size: 16, color: textColor.withOpacity(0.7)),
|
||||
const SizedBox(width: 4),
|
||||
Flexible(
|
||||
child: Text(
|
||||
message.contenu ?? 'Notification système',
|
||||
style: AppTypography.bodyTextSmall.copyWith(
|
||||
color: textColor.withOpacity(0.7),
|
||||
fontStyle: FontStyle.italic,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
// TEXTE (défaut)
|
||||
return Text(
|
||||
message.contenu ?? '',
|
||||
style: AppTypography.bodyTextSmall.copyWith(color: textColor),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDeleted(ColorScheme scheme) {
|
||||
return Align(
|
||||
alignment: isMine ? Alignment.centerRight : Alignment.centerLeft,
|
||||
child: Container(
|
||||
margin: const EdgeInsets.only(bottom: SpacingTokens.xs),
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: SpacingTokens.md,
|
||||
vertical: SpacingTokens.sm,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: scheme.surfaceContainerHighest.withOpacity(0.4),
|
||||
borderRadius: BorderRadius.circular(SpacingTokens.radiusMd),
|
||||
border: Border.all(color: scheme.outlineVariant.withOpacity(0.3)),
|
||||
),
|
||||
child: Text(
|
||||
'🚫 Message supprimé',
|
||||
style: AppTypography.bodyTextSmall.copyWith(
|
||||
color: scheme.onSurfaceVariant,
|
||||
fontStyle: FontStyle.italic,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user