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:
@@ -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(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user