feat: Release version 3.1.4 - Mode terrain et génération PDF
✨ Nouvelles fonctionnalités: - Ajout du mode terrain pour utilisation mobile hors connexion - Génération automatique de reçus PDF avec template personnalisé - Révision complète du système de cartes avec amélioration des performances 🔧 Améliorations techniques: - Refactoring du module chat avec architecture simplifiée - Optimisation du système de sécurité NIST SP 800-63B - Amélioration de la gestion des secteurs géographiques - Support UTF-8 étendu pour les noms d'utilisateurs 📱 Application mobile: - Nouveau mode terrain dans user_field_mode_page - Interface utilisateur adaptative pour conditions difficiles - Synchronisation offline améliorée 🗺️ Cartographie: - Optimisation des performances MapBox - Meilleure gestion des tuiles hors ligne - Amélioration de l'affichage des secteurs 📄 Documentation: - Ajout guide Android (ANDROID-GUIDE.md) - Documentation sécurité API (API-SECURITY.md) - Guide module chat (CHAT_MODULE.md) 🐛 Corrections: - Résolution des erreurs 400 lors de la création d'utilisateurs - Correction de la validation des noms d'utilisateurs - Fix des problèmes de synchronisation chat 🤖 Generated with Claude Code (https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,81 +1,359 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../widgets/conversations_list.dart';
|
||||
import '../widgets/chat_screen.dart';
|
||||
|
||||
/// Page principale du module chat
|
||||
///
|
||||
/// Cette page sert de point d'entrée pour le module chat
|
||||
/// et gère la navigation entre les conversations
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
import '../models/message.dart';
|
||||
import '../services/chat_service.dart';
|
||||
import '../services/chat_config_loader.dart';
|
||||
|
||||
/// Page simple de chat
|
||||
class ChatPage extends StatefulWidget {
|
||||
const ChatPage({super.key});
|
||||
|
||||
final String roomId;
|
||||
final String roomTitle;
|
||||
|
||||
const ChatPage({
|
||||
super.key,
|
||||
required this.roomId,
|
||||
required this.roomTitle,
|
||||
});
|
||||
|
||||
@override
|
||||
State<ChatPage> createState() => _ChatPageState();
|
||||
}
|
||||
|
||||
class _ChatPageState extends State<ChatPage> {
|
||||
String? _selectedConversationId;
|
||||
|
||||
final _service = ChatService.instance;
|
||||
final _messageController = TextEditingController();
|
||||
final _scrollController = ScrollController();
|
||||
bool _isLoading = true;
|
||||
bool _isLoadingMore = false;
|
||||
bool _hasMore = true;
|
||||
List<Message> _messages = [];
|
||||
String? _oldestMessageId;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isLargeScreen = MediaQuery.of(context).size.width > 900;
|
||||
|
||||
if (isLargeScreen) {
|
||||
// Vue desktop (séparée en deux panneaux)
|
||||
return Scaffold(
|
||||
body: Row(
|
||||
children: [
|
||||
// Liste des conversations à gauche
|
||||
SizedBox(
|
||||
width: 300,
|
||||
child: ConversationsList(
|
||||
onConversationSelected: (conversation) {
|
||||
setState(() {
|
||||
_selectedConversationId =
|
||||
'conversation-id'; // TODO: obtenir l'ID de la conversation
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
const VerticalDivider(width: 1),
|
||||
// Conversation sélectionnée à droite
|
||||
Expanded(
|
||||
child: _selectedConversationId != null
|
||||
? ChatScreen(conversationId: _selectedConversationId!)
|
||||
: const Center(child: Text('Sélectionnez une conversation')),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
} else {
|
||||
// Vue mobile
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Chat'),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.add),
|
||||
onPressed: () {
|
||||
// TODO: Créer une nouvelle conversation
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: ConversationsList(
|
||||
onConversationSelected: (conversation) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const ChatScreen(
|
||||
conversationId:
|
||||
'conversation-id', // TODO: obtenir l'ID de la conversation
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadInitialMessages();
|
||||
_service.markAsRead(widget.roomId);
|
||||
}
|
||||
|
||||
Future<void> _loadInitialMessages() async {
|
||||
setState(() => _isLoading = true);
|
||||
|
||||
final result = await _service.getMessages(widget.roomId);
|
||||
final messages = result['messages'] as List<Message>;
|
||||
|
||||
setState(() {
|
||||
_messages = messages;
|
||||
_hasMore = result['has_more'] as bool;
|
||||
if (messages.isNotEmpty) {
|
||||
_oldestMessageId = messages.first.id;
|
||||
}
|
||||
_isLoading = false;
|
||||
});
|
||||
|
||||
// Attendre un peu avant de scroller pour laisser le temps au ListView de se construire
|
||||
Future.delayed(const Duration(milliseconds: 100), _scrollToBottom);
|
||||
}
|
||||
|
||||
Future<void> _loadMoreMessages() async {
|
||||
if (_isLoadingMore || !_hasMore || _oldestMessageId == null) return;
|
||||
|
||||
setState(() => _isLoadingMore = true);
|
||||
|
||||
final result = await _service.getMessages(widget.roomId, beforeMessageId: _oldestMessageId);
|
||||
final newMessages = result['messages'] as List<Message>;
|
||||
|
||||
setState(() {
|
||||
// Insérer les messages plus anciens au début
|
||||
_messages = [...newMessages, ..._messages];
|
||||
_hasMore = result['has_more'] as bool;
|
||||
|
||||
if (newMessages.isNotEmpty) {
|
||||
_oldestMessageId = newMessages.first.id;
|
||||
}
|
||||
|
||||
_isLoadingMore = false;
|
||||
});
|
||||
}
|
||||
|
||||
void _scrollToBottom() {
|
||||
if (_scrollController.hasClients) {
|
||||
_scrollController.animateTo(
|
||||
_scrollController.position.maxScrollExtent,
|
||||
duration: const Duration(milliseconds: 200),
|
||||
curve: Curves.easeOut,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _sendMessage() async {
|
||||
final text = _messageController.text.trim();
|
||||
if (text.isEmpty) return;
|
||||
|
||||
_messageController.clear();
|
||||
await _service.sendMessage(widget.roomId, text);
|
||||
_scrollToBottom();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// Obtenir le rôle de l'utilisateur pour la colorisation
|
||||
final userRole = _service.getUserRole();
|
||||
|
||||
// Déterminer la couleur du badge selon le rôle
|
||||
Color badgeColor;
|
||||
switch (userRole) {
|
||||
case 1:
|
||||
badgeColor = Colors.green; // Vert pour les membres
|
||||
break;
|
||||
case 2:
|
||||
badgeColor = Colors.blue; // Bleu pour les admins amicale
|
||||
break;
|
||||
case 9:
|
||||
badgeColor = Colors.red; // Rouge pour les super admins
|
||||
break;
|
||||
default:
|
||||
badgeColor = Colors.grey; // Gris par défaut
|
||||
}
|
||||
|
||||
// Obtenir la version du module
|
||||
final moduleVersion = ChatConfigLoader.instance.getModuleVersion();
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: const Color(0xFFFAFAFA),
|
||||
appBar: AppBar(
|
||||
title: Text(widget.roomTitle),
|
||||
backgroundColor: Colors.white,
|
||||
foregroundColor: const Color(0xFF1E293B),
|
||||
elevation: 0,
|
||||
),
|
||||
body: Stack(
|
||||
children: [
|
||||
Column(
|
||||
children: [
|
||||
// Messages
|
||||
Expanded(
|
||||
child: _isLoading
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: ValueListenableBuilder<Box<Message>>(
|
||||
valueListenable: _service.messagesBox.listenable(),
|
||||
builder: (context, box, _) {
|
||||
// Mettre à jour la liste avec les nouveaux messages envoyés
|
||||
final recentMessages = box.values
|
||||
.where((m) => m.roomId == widget.roomId &&
|
||||
!_messages.any((msg) => msg.id == m.id))
|
||||
.toList();
|
||||
|
||||
// Combiner les messages chargés et les nouveaux
|
||||
final allMessages = [..._messages, ...recentMessages]
|
||||
..sort((a, b) => a.sentAt.compareTo(b.sentAt));
|
||||
|
||||
if (allMessages.isEmpty) {
|
||||
return Center(
|
||||
child: Text(
|
||||
'Aucun message',
|
||||
style: TextStyle(
|
||||
color: Colors.grey[600],
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
// Bouton "Charger plus" en haut
|
||||
if (_hasMore)
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
child: _isLoadingMore
|
||||
? const SizedBox(
|
||||
height: 40,
|
||||
child: Center(
|
||||
child: SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
),
|
||||
),
|
||||
)
|
||||
: TextButton.icon(
|
||||
onPressed: _loadMoreMessages,
|
||||
icon: const Icon(Icons.refresh, size: 18),
|
||||
label: const Text('Charger plus de messages'),
|
||||
style: TextButton.styleFrom(
|
||||
foregroundColor: const Color(0xFF2563EB),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// Liste des messages avec pull-to-refresh
|
||||
Expanded(
|
||||
child: RefreshIndicator(
|
||||
onRefresh: _loadInitialMessages,
|
||||
child: ListView.builder(
|
||||
controller: _scrollController,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
itemCount: allMessages.length,
|
||||
itemBuilder: (context, index) {
|
||||
final message = allMessages[index];
|
||||
return _MessageBubble(message: message);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
|
||||
// Input
|
||||
Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
border: Border(
|
||||
top: BorderSide(color: Colors.grey[200]!),
|
||||
),
|
||||
),
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextField(
|
||||
controller: _messageController,
|
||||
decoration: InputDecoration(
|
||||
hintText: 'Message...',
|
||||
hintStyle: TextStyle(color: Colors.grey[400]),
|
||||
border: InputBorder.none,
|
||||
contentPadding: const EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 12,
|
||||
),
|
||||
),
|
||||
maxLines: null,
|
||||
textInputAction: TextInputAction.send,
|
||||
onSubmitted: (_) => _sendMessage(),
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.send),
|
||||
color: const Color(0xFF2563EB),
|
||||
onPressed: _sendMessage,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
// Badge de version en bas à droite
|
||||
Positioned(
|
||||
bottom: 16,
|
||||
right: 16,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: badgeColor.withOpacity(0.9),
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: badgeColor.withOpacity(0.3),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.chat_bubble_outline,
|
||||
size: 14,
|
||||
color: Colors.white.withOpacity(0.9),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
'v$moduleVersion',
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_messageController.dispose();
|
||||
_scrollController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// Widget simple pour une bulle de message
|
||||
class _MessageBubble extends StatelessWidget {
|
||||
final Message message;
|
||||
|
||||
const _MessageBubble({required this.message});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isMe = message.isMe;
|
||||
|
||||
return Align(
|
||||
alignment: isMe ? Alignment.centerRight : Alignment.centerLeft,
|
||||
child: Container(
|
||||
margin: const EdgeInsets.symmetric(vertical: 4),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
constraints: BoxConstraints(
|
||||
maxWidth: MediaQuery.of(context).size.width * 0.75,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: isMe ? const Color(0xFFEFF6FF) : Colors.white,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: !isMe ? Border.all(color: Colors.grey[300]!) : null,
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment:
|
||||
isMe ? CrossAxisAlignment.end : CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (!isMe)
|
||||
Text(
|
||||
message.senderName,
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Color(0xFF2563EB),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
message.content,
|
||||
style: const TextStyle(fontSize: 14),
|
||||
),
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
_formatTime(message.sentAt),
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: Colors.grey[500],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _formatTime(DateTime date) {
|
||||
return '${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}';
|
||||
}
|
||||
}
|
||||
340
app/lib/chat/pages/rooms_page.dart
Normal file
340
app/lib/chat/pages/rooms_page.dart
Normal file
@@ -0,0 +1,340 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
import '../models/room.dart';
|
||||
import '../services/chat_service.dart';
|
||||
import '../services/chat_config_loader.dart';
|
||||
import '../widgets/recipient_selector.dart';
|
||||
import 'chat_page.dart';
|
||||
|
||||
/// Page de liste des conversations avec gestion des permissions
|
||||
class RoomsPage extends StatefulWidget {
|
||||
const RoomsPage({super.key});
|
||||
|
||||
@override
|
||||
State<RoomsPage> createState() => _RoomsPageState();
|
||||
}
|
||||
|
||||
class _RoomsPageState extends State<RoomsPage> {
|
||||
final _service = ChatService.instance;
|
||||
bool _isLoading = true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadRooms();
|
||||
}
|
||||
|
||||
Future<void> _loadRooms() async {
|
||||
setState(() => _isLoading = true);
|
||||
await _service.getRooms();
|
||||
setState(() => _isLoading = false);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final roleLabel = ChatConfigLoader.instance.getRoleName(_service.currentUserRole);
|
||||
final helpText = ChatConfigLoader.instance.getHelpText(_service.currentUserRole);
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: const Color(0xFFFAFAFA),
|
||||
appBar: AppBar(
|
||||
title: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text('Messages'),
|
||||
Text(
|
||||
roleLabel,
|
||||
style: const TextStyle(fontSize: 12, fontWeight: FontWeight.normal),
|
||||
),
|
||||
],
|
||||
),
|
||||
backgroundColor: Colors.white,
|
||||
foregroundColor: const Color(0xFF1E293B),
|
||||
elevation: 0,
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.add),
|
||||
onPressed: _createNewConversation,
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh),
|
||||
onPressed: _loadRooms,
|
||||
),
|
||||
],
|
||||
),
|
||||
body: _isLoading
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: ValueListenableBuilder<Box<Room>>(
|
||||
valueListenable: _service.roomsBox.listenable(),
|
||||
builder: (context, box, _) {
|
||||
final rooms = box.values.toList()
|
||||
..sort((a, b) => (b.lastMessageAt ?? b.createdAt)
|
||||
.compareTo(a.lastMessageAt ?? a.createdAt));
|
||||
|
||||
if (rooms.isEmpty) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.chat_bubble_outline,
|
||||
size: 64,
|
||||
color: Colors.grey[400],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'Aucune conversation',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
color: Colors.grey[600],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
TextButton(
|
||||
onPressed: _createNewConversation,
|
||||
child: const Text('Démarrer une conversation'),
|
||||
),
|
||||
if (helpText.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Text(
|
||||
helpText,
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: Colors.grey[500],
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return RefreshIndicator(
|
||||
onRefresh: _loadRooms,
|
||||
child: ListView.builder(
|
||||
itemCount: rooms.length,
|
||||
itemBuilder: (context, index) {
|
||||
final room = rooms[index];
|
||||
return _RoomTile(room: room);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _createNewConversation() async {
|
||||
final currentRole = _service.currentUserRole;
|
||||
final config = ChatConfigLoader.instance.getPossibleRecipientsConfig(currentRole);
|
||||
|
||||
// Déterminer si on permet la sélection multiple
|
||||
// Pour role 1 (membre), permettre la sélection multiple pour contacter plusieurs membres/admins
|
||||
// Pour role 2 (admin amicale), permettre la sélection multiple pour GEOSECTOR ou Amicale
|
||||
// Pour role 9 (super admin), permettre la sélection multiple selon config
|
||||
final allowMultiple = (currentRole == 1) || (currentRole == 2) ||
|
||||
(currentRole == 9 && config.any((c) => c['allow_selection'] == true));
|
||||
|
||||
// Ouvrir le dialog de sélection
|
||||
final result = await RecipientSelectorDialog.show(
|
||||
context,
|
||||
allowMultiple: allowMultiple,
|
||||
);
|
||||
|
||||
if (result != null) {
|
||||
final recipients = result['recipients'] as List<Map<String, dynamic>>?;
|
||||
final initialMessage = result['initial_message'] as String?;
|
||||
|
||||
if (recipients != null && recipients.isNotEmpty) {
|
||||
try {
|
||||
Room? newRoom;
|
||||
|
||||
if (recipients.length == 1) {
|
||||
// Conversation privée
|
||||
final recipient = recipients.first;
|
||||
newRoom = await _service.createPrivateRoom(
|
||||
recipientId: recipient['id'],
|
||||
recipientName: recipient['name'],
|
||||
recipientRole: recipient['role'],
|
||||
recipientEntite: recipient['entite_id'],
|
||||
initialMessage: initialMessage,
|
||||
);
|
||||
} else {
|
||||
// Conversation de groupe
|
||||
final participantIds = recipients.map((r) => r['id'] as int).toList();
|
||||
|
||||
// Déterminer le titre en fonction du type de groupe
|
||||
String title;
|
||||
if (currentRole == 1) {
|
||||
// Pour un membre
|
||||
final hasAdmins = recipients.any((r) => r['role'] == 2);
|
||||
final hasMembers = recipients.any((r) => r['role'] == 1);
|
||||
|
||||
if (hasAdmins && !hasMembers) {
|
||||
title = 'Administrateurs Amicale';
|
||||
} else if (recipients.length > 3) {
|
||||
title = '${recipients.take(3).map((r) => r['name']).join(', ')} et ${recipients.length - 3} autres';
|
||||
} else {
|
||||
title = recipients.map((r) => r['name']).join(', ');
|
||||
}
|
||||
} else if (currentRole == 2) {
|
||||
// Pour un admin d'amicale
|
||||
final hasSuperAdmins = recipients.any((r) => r['role'] == 9);
|
||||
final hasMembers = recipients.any((r) => r['role'] == 1);
|
||||
|
||||
if (hasSuperAdmins && !hasMembers) {
|
||||
title = 'GEOSECTOR Support';
|
||||
} else if (!hasSuperAdmins && hasMembers && recipients.length > 5) {
|
||||
title = 'Amicale - Tous les membres';
|
||||
} else if (recipients.length > 3) {
|
||||
title = '${recipients.take(3).map((r) => r['name']).join(', ')} et ${recipients.length - 3} autres';
|
||||
} else {
|
||||
title = recipients.map((r) => r['name']).join(', ');
|
||||
}
|
||||
} else {
|
||||
// Pour un super admin
|
||||
if (recipients.length > 3) {
|
||||
title = '${recipients.take(3).map((r) => r['name']).join(', ')} et ${recipients.length - 3} autres';
|
||||
} else {
|
||||
title = recipients.map((r) => r['name']).join(', ');
|
||||
}
|
||||
}
|
||||
|
||||
// Créer la room avec le bon type
|
||||
newRoom = await _service.createRoom(
|
||||
title: title,
|
||||
participantIds: participantIds,
|
||||
type: (currentRole == 1 || currentRole == 2) ? 'group' : 'broadcast',
|
||||
initialMessage: initialMessage,
|
||||
);
|
||||
}
|
||||
|
||||
if (newRoom != null && mounted) {
|
||||
// Naviguer vers la nouvelle conversation
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => ChatPage(
|
||||
roomId: newRoom!.id,
|
||||
roomTitle: newRoom.title,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(e.toString()),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Widget simple pour une tuile de room
|
||||
class _RoomTile extends StatelessWidget {
|
||||
final Room room;
|
||||
|
||||
const _RoomTile({required this.room});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
border: Border(
|
||||
bottom: BorderSide(color: Colors.grey[200]!),
|
||||
),
|
||||
),
|
||||
child: ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
leading: CircleAvatar(
|
||||
backgroundColor: const Color(0xFF2563EB),
|
||||
child: Text(
|
||||
room.title[0].toUpperCase(),
|
||||
style: const TextStyle(color: Colors.white),
|
||||
),
|
||||
),
|
||||
title: Text(
|
||||
room.title,
|
||||
style: const TextStyle(
|
||||
fontWeight: FontWeight.w600,
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
subtitle: room.lastMessage != null
|
||||
? Text(
|
||||
room.lastMessage!,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(color: Colors.grey[600]),
|
||||
)
|
||||
: null,
|
||||
trailing: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
if (room.lastMessageAt != null)
|
||||
Text(
|
||||
_formatTime(room.lastMessageAt!),
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.grey[500],
|
||||
),
|
||||
),
|
||||
if (room.unreadCount > 0)
|
||||
Container(
|
||||
margin: const EdgeInsets.only(top: 4),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF2563EB),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
child: Text(
|
||||
room.unreadCount.toString(),
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 11,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => ChatPage(
|
||||
roomId: room.id,
|
||||
roomTitle: room.title,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _formatTime(DateTime date) {
|
||||
final now = DateTime.now();
|
||||
final diff = now.difference(date);
|
||||
|
||||
if (diff.inDays > 0) {
|
||||
return '${diff.inDays}j';
|
||||
} else if (diff.inHours > 0) {
|
||||
return '${diff.inHours}h';
|
||||
} else if (diff.inMinutes > 0) {
|
||||
return '${diff.inMinutes}m';
|
||||
} else {
|
||||
return 'Maintenant';
|
||||
}
|
||||
}
|
||||
}
|
||||
326
app/lib/chat/pages/rooms_page_embedded.dart
Normal file
326
app/lib/chat/pages/rooms_page_embedded.dart
Normal file
@@ -0,0 +1,326 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
import '../models/room.dart';
|
||||
import '../services/chat_service.dart';
|
||||
import '../services/chat_config_loader.dart';
|
||||
import '../widgets/recipient_selector.dart';
|
||||
import 'chat_page.dart';
|
||||
|
||||
/// Version embarquée de RoomsPage sans AppBar pour intégration
|
||||
class RoomsPageEmbedded extends StatefulWidget {
|
||||
final VoidCallback? onAddPressed;
|
||||
final VoidCallback? onRefreshPressed;
|
||||
|
||||
const RoomsPageEmbedded({
|
||||
super.key,
|
||||
this.onAddPressed,
|
||||
this.onRefreshPressed,
|
||||
});
|
||||
|
||||
@override
|
||||
State<RoomsPageEmbedded> createState() => RoomsPageEmbeddedState();
|
||||
}
|
||||
|
||||
class RoomsPageEmbeddedState extends State<RoomsPageEmbedded> {
|
||||
final _service = ChatService.instance;
|
||||
bool _isLoading = true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadRooms();
|
||||
}
|
||||
|
||||
Future<void> _loadRooms() async {
|
||||
setState(() => _isLoading = true);
|
||||
await _service.getRooms();
|
||||
setState(() => _isLoading = false);
|
||||
widget.onRefreshPressed?.call();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final helpText = ChatConfigLoader.instance.getHelpText(_service.currentUserRole);
|
||||
|
||||
if (_isLoading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
return ValueListenableBuilder<Box<Room>>(
|
||||
valueListenable: _service.roomsBox.listenable(),
|
||||
builder: (context, box, _) {
|
||||
final rooms = box.values.toList()
|
||||
..sort((a, b) => (b.lastMessageAt ?? b.createdAt)
|
||||
.compareTo(a.lastMessageAt ?? a.createdAt));
|
||||
|
||||
if (rooms.isEmpty) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.chat_bubble_outline,
|
||||
size: 64,
|
||||
color: Colors.grey[400],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'Aucune conversation',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
color: Colors.grey[600],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
TextButton(
|
||||
onPressed: widget.onAddPressed ?? createNewConversation,
|
||||
child: const Text('Démarrer une conversation'),
|
||||
),
|
||||
if (helpText.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Text(
|
||||
helpText,
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: Colors.grey[500],
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return RefreshIndicator(
|
||||
onRefresh: _loadRooms,
|
||||
child: ListView.builder(
|
||||
itemCount: rooms.length,
|
||||
itemBuilder: (context, index) {
|
||||
final room = rooms[index];
|
||||
return _RoomTile(room: room);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> createNewConversation() async {
|
||||
final currentRole = _service.currentUserRole;
|
||||
final config = ChatConfigLoader.instance.getPossibleRecipientsConfig(currentRole);
|
||||
|
||||
// Déterminer si on permet la sélection multiple
|
||||
// Pour role 1 (membre), permettre la sélection multiple pour contacter plusieurs membres/admins
|
||||
// Pour role 2 (admin amicale), permettre la sélection multiple pour GEOSECTOR ou Amicale
|
||||
// Pour role 9 (super admin), permettre la sélection multiple selon config
|
||||
final allowMultiple = (currentRole == 1) || (currentRole == 2) ||
|
||||
(currentRole == 9 && config.any((c) => c['allow_selection'] == true));
|
||||
|
||||
// Ouvrir le dialog de sélection
|
||||
final result = await RecipientSelectorDialog.show(
|
||||
context,
|
||||
allowMultiple: allowMultiple,
|
||||
);
|
||||
|
||||
if (result != null) {
|
||||
final recipients = result['recipients'] as List<Map<String, dynamic>>?;
|
||||
final initialMessage = result['initial_message'] as String?;
|
||||
|
||||
if (recipients != null && recipients.isNotEmpty) {
|
||||
try {
|
||||
Room? newRoom;
|
||||
|
||||
if (recipients.length == 1) {
|
||||
// Conversation privée
|
||||
final recipient = recipients.first;
|
||||
newRoom = await _service.createPrivateRoom(
|
||||
recipientId: recipient['id'],
|
||||
recipientName: recipient['name'],
|
||||
recipientRole: recipient['role'],
|
||||
recipientEntite: recipient['entite_id'],
|
||||
initialMessage: initialMessage,
|
||||
);
|
||||
} else {
|
||||
// Conversation de groupe
|
||||
final participantIds = recipients.map((r) => r['id'] as int).toList();
|
||||
|
||||
// Déterminer le titre en fonction du type de groupe
|
||||
String title;
|
||||
if (currentRole == 1) {
|
||||
// Pour un membre
|
||||
final hasAdmins = recipients.any((r) => r['role'] == 2);
|
||||
final hasMembers = recipients.any((r) => r['role'] == 1);
|
||||
|
||||
if (hasAdmins && !hasMembers) {
|
||||
title = 'Administrateurs Amicale';
|
||||
} else if (recipients.length > 3) {
|
||||
title = '${recipients.take(3).map((r) => r['name']).join(', ')} et ${recipients.length - 3} autres';
|
||||
} else {
|
||||
title = recipients.map((r) => r['name']).join(', ');
|
||||
}
|
||||
} else if (currentRole == 2) {
|
||||
// Pour un admin d'amicale
|
||||
final hasSuperAdmins = recipients.any((r) => r['role'] == 9);
|
||||
final hasMembers = recipients.any((r) => r['role'] == 1);
|
||||
|
||||
if (hasSuperAdmins && !hasMembers) {
|
||||
title = 'GEOSECTOR Support';
|
||||
} else if (!hasSuperAdmins && hasMembers && recipients.length > 5) {
|
||||
title = 'Amicale - Tous les membres';
|
||||
} else if (recipients.length > 3) {
|
||||
title = '${recipients.take(3).map((r) => r['name']).join(', ')} et ${recipients.length - 3} autres';
|
||||
} else {
|
||||
title = recipients.map((r) => r['name']).join(', ');
|
||||
}
|
||||
} else {
|
||||
// Pour un super admin
|
||||
if (recipients.length > 3) {
|
||||
title = '${recipients.take(3).map((r) => r['name']).join(', ')} et ${recipients.length - 3} autres';
|
||||
} else {
|
||||
title = recipients.map((r) => r['name']).join(', ');
|
||||
}
|
||||
}
|
||||
|
||||
// Créer la room avec le bon type
|
||||
newRoom = await _service.createRoom(
|
||||
title: title,
|
||||
participantIds: participantIds,
|
||||
type: (currentRole == 1 || currentRole == 2) ? 'group' : 'broadcast',
|
||||
initialMessage: initialMessage,
|
||||
);
|
||||
}
|
||||
|
||||
if (newRoom != null && mounted) {
|
||||
// Naviguer vers la nouvelle conversation
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => ChatPage(
|
||||
roomId: newRoom!.id,
|
||||
roomTitle: newRoom.title,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(e.toString()),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Méthode publique pour rafraîchir
|
||||
void refresh() {
|
||||
_loadRooms();
|
||||
}
|
||||
}
|
||||
|
||||
/// Widget simple pour une tuile de room
|
||||
class _RoomTile extends StatelessWidget {
|
||||
final Room room;
|
||||
|
||||
const _RoomTile({required this.room});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
border: Border(
|
||||
bottom: BorderSide(color: Colors.grey[200]!),
|
||||
),
|
||||
),
|
||||
child: ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
leading: CircleAvatar(
|
||||
backgroundColor: const Color(0xFF2563EB),
|
||||
child: Text(
|
||||
room.title[0].toUpperCase(),
|
||||
style: const TextStyle(color: Colors.white),
|
||||
),
|
||||
),
|
||||
title: Text(
|
||||
room.title,
|
||||
style: const TextStyle(
|
||||
fontWeight: FontWeight.w600,
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
subtitle: room.lastMessage != null
|
||||
? Text(
|
||||
room.lastMessage!,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(color: Colors.grey[600]),
|
||||
)
|
||||
: null,
|
||||
trailing: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
if (room.lastMessageAt != null)
|
||||
Text(
|
||||
_formatTime(room.lastMessageAt!),
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.grey[500],
|
||||
),
|
||||
),
|
||||
if (room.unreadCount > 0)
|
||||
Container(
|
||||
margin: const EdgeInsets.only(top: 4),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF2563EB),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
child: Text(
|
||||
room.unreadCount.toString(),
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 11,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => ChatPage(
|
||||
roomId: room.id,
|
||||
roomTitle: room.title,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _formatTime(DateTime date) {
|
||||
final now = DateTime.now();
|
||||
final diff = now.difference(date);
|
||||
|
||||
if (diff.inDays > 0) {
|
||||
return '${diff.inDays}j';
|
||||
} else if (diff.inHours > 0) {
|
||||
return '${diff.inHours}h';
|
||||
} else if (diff.inMinutes > 0) {
|
||||
return '${diff.inMinutes}m';
|
||||
} else {
|
||||
return 'Maintenant';
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user