Restructuration majeure du projet: migration de flutt vers app, ajout de l'API et mise à jour du site web
This commit is contained in:
166
app/lib/chat/example_integration/mqtt_integration_example.dart
Normal file
166
app/lib/chat/example_integration/mqtt_integration_example.dart
Normal file
@@ -0,0 +1,166 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../chat.dart';
|
||||
|
||||
/// Exemple d'intégration du service MQTT dans l'application
|
||||
///
|
||||
/// Montre comment initialiser et utiliser le service de notifications MQTT
|
||||
|
||||
class MqttIntegrationExample extends StatefulWidget {
|
||||
const MqttIntegrationExample({super.key});
|
||||
|
||||
@override
|
||||
State<MqttIntegrationExample> createState() => _MqttIntegrationExampleState();
|
||||
}
|
||||
|
||||
class _MqttIntegrationExampleState extends State<MqttIntegrationExample> {
|
||||
late final MqttNotificationService _notificationService;
|
||||
bool _isInitialized = false;
|
||||
String _status = 'Non initialisé';
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_initializeMqttService();
|
||||
}
|
||||
|
||||
Future<void> _initializeMqttService() async {
|
||||
try {
|
||||
// Initialiser le service avec la configuration
|
||||
_notificationService = MqttNotificationService(
|
||||
mqttHost: MqttConfig.host,
|
||||
mqttPort: MqttConfig.port,
|
||||
mqttUsername: MqttConfig.username,
|
||||
mqttPassword: MqttConfig.password,
|
||||
);
|
||||
|
||||
// Configurer les callbacks
|
||||
_notificationService.onMessageTap = (messageId) {
|
||||
debugPrint('Notification tapée : $messageId');
|
||||
// Naviguer vers la conversation correspondante
|
||||
_navigateToMessage(messageId);
|
||||
};
|
||||
|
||||
_notificationService.onNotificationReceived = (data) {
|
||||
debugPrint('Notification reçue : $data');
|
||||
setState(() {
|
||||
_status = 'Notification reçue : ${data['content']}';
|
||||
});
|
||||
};
|
||||
|
||||
// Initialiser avec l'ID utilisateur (récupéré du UserRepository)
|
||||
final userId = _getCurrentUserId(); // À implémenter selon votre logique
|
||||
await _notificationService.initialize(userId: userId);
|
||||
|
||||
setState(() {
|
||||
_isInitialized = true;
|
||||
_status = 'Service MQTT initialisé';
|
||||
});
|
||||
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_status = 'Erreur : $e';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
String _getCurrentUserId() {
|
||||
// Dans votre application réelle, vous récupéreriez l'ID utilisateur
|
||||
// depuis le UserRepository ou le contexte de l'application
|
||||
return '123'; // Exemple
|
||||
}
|
||||
|
||||
void _navigateToMessage(String messageId) {
|
||||
// Implémenter la navigation vers le message
|
||||
// Par exemple :
|
||||
// Navigator.push(context, MaterialPageRoute(
|
||||
// builder: (_) => ChatScreen(messageId: messageId),
|
||||
// ));
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_notificationService.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Test MQTT Notifications'),
|
||||
),
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(
|
||||
_status,
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
if (_isInitialized) ...[
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
_notificationService.pauseNotifications();
|
||||
setState(() {
|
||||
_status = 'Notifications en pause';
|
||||
});
|
||||
},
|
||||
child: const Text('Pause Notifications'),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
_notificationService.resumeNotifications();
|
||||
setState(() {
|
||||
_status = 'Notifications actives';
|
||||
});
|
||||
},
|
||||
child: const Text('Reprendre Notifications'),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
// Exemple de test en publiant un message
|
||||
await _notificationService.publishMessage(
|
||||
'chat/user/${_getCurrentUserId()}/messages',
|
||||
{
|
||||
'type': 'chat_message',
|
||||
'messageId': 'test_${DateTime.now().millisecondsSinceEpoch}',
|
||||
'content': 'Message de test',
|
||||
'senderId': '999',
|
||||
'senderName': 'Système',
|
||||
},
|
||||
);
|
||||
setState(() {
|
||||
_status = 'Message test envoyé';
|
||||
});
|
||||
},
|
||||
child: const Text('Envoyer Message Test'),
|
||||
),
|
||||
] else ...[
|
||||
const CircularProgressIndicator(),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Exemple d'intégration dans le main.dart de votre application
|
||||
void mainExample() {
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
home: const MqttIntegrationExample(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user