feat: Version 3.6.2 - Correctifs tâches #17-20
- #17: Amélioration gestion des secteurs et statistiques - #18: Optimisation services API et logs - #19: Corrections Flutter widgets et repositories - #20: Fix création passage - détection automatique ope_users.id vs users.id Suppression dossier web/ (migration vers app Flutter) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -29,7 +29,8 @@ class ChatService {
|
||||
Timer? _syncTimer;
|
||||
DateTime? _lastSyncTimestamp;
|
||||
DateTime? _lastFullSync;
|
||||
static const Duration _syncInterval = Duration(seconds: 15); // Changé à 15 secondes comme suggéré par l'API
|
||||
static const Duration _syncInterval = Duration(seconds: 30); // Sync toutes les 30 secondes
|
||||
static const Duration _initialSyncDelay = Duration(seconds: 10); // Délai avant première sync
|
||||
static const Duration _fullSyncInterval = Duration(minutes: 5);
|
||||
|
||||
/// Initialisation avec gestion des rôles et configuration YAML
|
||||
@@ -76,10 +77,13 @@ class ChatService {
|
||||
// Charger le dernier timestamp de sync depuis Hive
|
||||
await _instance!._loadSyncTimestamp();
|
||||
|
||||
// Faire la sync initiale complète au login
|
||||
await _instance!.getRooms(forceFullSync: true);
|
||||
debugPrint('✅ Sync initiale complète effectuée au login');
|
||||
|
||||
// Faire la sync initiale complète au login avec délai de 10 secondes
|
||||
debugPrint('⏳ Sync initiale chat programmée dans 10 secondes...');
|
||||
Future.delayed(_initialSyncDelay, () async {
|
||||
await _instance!.getRooms(forceFullSync: true);
|
||||
debugPrint('✅ Sync initiale complète effectuée au login');
|
||||
});
|
||||
|
||||
// Démarrer la synchronisation incrémentale périodique
|
||||
_instance!._startSync();
|
||||
}
|
||||
@@ -136,6 +140,13 @@ class ChatService {
|
||||
|
||||
/// Obtenir les rooms avec synchronisation incrémentale
|
||||
Future<List<Room>> getRooms({bool forceFullSync = false}) async {
|
||||
// DÉSACTIVATION TEMPORAIRE - Retour direct du cache sans appeler l'API
|
||||
debugPrint('🚫 API /chat/rooms désactivée - utilisation du cache uniquement');
|
||||
return _roomsBox.values.toList()
|
||||
..sort((a, b) => (b.lastMessageAt ?? b.createdAt)
|
||||
.compareTo(a.lastMessageAt ?? a.createdAt));
|
||||
|
||||
/* Code original commenté pour désactiver les appels API
|
||||
// Vérifier la connectivité
|
||||
if (!connectivityService.isConnected) {
|
||||
debugPrint('📵 Pas de connexion réseau - utilisation du cache');
|
||||
@@ -143,30 +154,32 @@ class ChatService {
|
||||
..sort((a, b) => (b.lastMessageAt ?? b.createdAt)
|
||||
.compareTo(a.lastMessageAt ?? a.createdAt));
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
// Déterminer si on fait une sync complète ou incrémentale
|
||||
final now = DateTime.now();
|
||||
final needsFullSync = forceFullSync ||
|
||||
_lastFullSync == null ||
|
||||
now.difference(_lastFullSync!).compareTo(_fullSyncInterval) > 0;
|
||||
|
||||
|
||||
Response response;
|
||||
|
||||
|
||||
if (needsFullSync || _lastSyncTimestamp == null) {
|
||||
// Synchronisation complète
|
||||
debugPrint('🔄 Synchronisation complète des rooms...');
|
||||
response = await _dio.get('/chat/rooms');
|
||||
// response = await _dio.get('/chat/rooms'); // COMMENTÉ - Désactivation GET /chat/rooms
|
||||
return; // Retour anticipé pour éviter l'appel API
|
||||
_lastFullSync = now;
|
||||
} else {
|
||||
// Synchronisation incrémentale
|
||||
final isoTimestamp = _lastSyncTimestamp!.toUtc().toIso8601String();
|
||||
// debugPrint('🔄 Synchronisation incrémentale depuis $isoTimestamp');
|
||||
response = await _dio.get('/chat/rooms', queryParameters: {
|
||||
'updated_after': isoTimestamp,
|
||||
});
|
||||
// response = await _dio.get('/chat/rooms', queryParameters: { // COMMENTÉ - Désactivation GET /chat/rooms
|
||||
// 'updated_after': isoTimestamp,
|
||||
// });
|
||||
return; // Retour anticipé pour éviter l'appel API
|
||||
}
|
||||
|
||||
|
||||
// Extraire le timestamp de synchronisation fourni par l'API
|
||||
if (response.data is Map && response.data['sync_timestamp'] != null) {
|
||||
_lastSyncTimestamp = DateTime.parse(response.data['sync_timestamp']);
|
||||
@@ -180,7 +193,7 @@ class ChatService {
|
||||
// On utilise le timestamp actuel comme fallback mais ce n'est pas idéal
|
||||
_lastSyncTimestamp = now;
|
||||
}
|
||||
|
||||
|
||||
// Vérifier s'il y a des changements (pour sync incrémentale)
|
||||
if (!needsFullSync && response.data is Map && response.data['has_changes'] == false) {
|
||||
// debugPrint('✅ Aucun changement depuis la dernière sync');
|
||||
@@ -188,7 +201,7 @@ class ChatService {
|
||||
..sort((a, b) => (b.lastMessageAt ?? b.createdAt)
|
||||
.compareTo(a.lastMessageAt ?? a.createdAt));
|
||||
}
|
||||
|
||||
|
||||
// Gérer différents formats de réponse API
|
||||
List<dynamic> roomsData;
|
||||
if (response.data is Map) {
|
||||
@@ -206,11 +219,11 @@ class ChatService {
|
||||
} else {
|
||||
roomsData = [];
|
||||
}
|
||||
|
||||
|
||||
// Parser les rooms
|
||||
final rooms = <Room>[];
|
||||
final deletedRoomIds = <String>[];
|
||||
|
||||
|
||||
for (final json in roomsData) {
|
||||
try {
|
||||
// Vérifier si la room est marquée comme supprimée
|
||||
@@ -218,21 +231,21 @@ class ChatService {
|
||||
deletedRoomIds.add(json['id']);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
final room = Room.fromJson(json);
|
||||
rooms.add(room);
|
||||
} catch (e) {
|
||||
debugPrint('❌ Erreur parsing room: $e');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Appliquer les modifications à Hive
|
||||
if (needsFullSync) {
|
||||
// Sync complète : remplacer tout mais préserver certaines données locales
|
||||
final existingRooms = Map.fromEntries(
|
||||
_roomsBox.values.map((r) => MapEntry(r.id, r))
|
||||
);
|
||||
|
||||
|
||||
await _roomsBox.clear();
|
||||
for (final room in rooms) {
|
||||
final existingRoom = existingRooms[room.id];
|
||||
@@ -250,7 +263,7 @@ class ChatService {
|
||||
createdBy: room.createdBy ?? existingRoom?.createdBy,
|
||||
);
|
||||
await _roomsBox.put(roomToSave.id, roomToSave);
|
||||
|
||||
|
||||
// Traiter les messages récents de la room
|
||||
if (room.recentMessages != null && room.recentMessages!.isNotEmpty) {
|
||||
for (final msgData in room.recentMessages!) {
|
||||
@@ -288,10 +301,10 @@ class ChatService {
|
||||
// Préserver createdBy existant si la nouvelle room n'en a pas
|
||||
createdBy: room.createdBy ?? existingRoom?.createdBy,
|
||||
);
|
||||
|
||||
|
||||
debugPrint('💾 Sauvegarde room ${roomToSave.title} (${roomToSave.id}): createdBy=${roomToSave.createdBy}');
|
||||
await _roomsBox.put(roomToSave.id, roomToSave);
|
||||
|
||||
|
||||
// Traiter les messages récents de la room
|
||||
if (room.recentMessages != null && room.recentMessages!.isNotEmpty) {
|
||||
for (final msgData in room.recentMessages!) {
|
||||
@@ -314,22 +327,22 @@ class ChatService {
|
||||
for (final roomId in deletedRoomIds) {
|
||||
// Supprimer la room
|
||||
await _roomsBox.delete(roomId);
|
||||
|
||||
|
||||
// Supprimer tous les messages de cette room
|
||||
final messagesToDelete = _messagesBox.values
|
||||
.where((msg) => msg.roomId == roomId)
|
||||
.map((msg) => msg.id)
|
||||
.toList();
|
||||
|
||||
|
||||
for (final msgId in messagesToDelete) {
|
||||
await _messagesBox.delete(msgId);
|
||||
}
|
||||
|
||||
|
||||
debugPrint('🗑️ Room $roomId supprimée avec ${messagesToDelete.length} messages');
|
||||
}
|
||||
debugPrint('💾 Sync incrémentale: ${rooms.length} rooms mises à jour, ${deletedRoomIds.length} supprimées');
|
||||
}
|
||||
|
||||
|
||||
// Mettre à jour les stats globales
|
||||
final allRooms = _roomsBox.values.toList();
|
||||
final totalUnread = allRooms.fold<int>(0, (sum, room) => sum + room.unreadCount);
|
||||
@@ -337,7 +350,7 @@ class ChatService {
|
||||
totalRooms: allRooms.length,
|
||||
unreadMessages: totalUnread,
|
||||
);
|
||||
|
||||
|
||||
return allRooms
|
||||
..sort((a, b) => (b.lastMessageAt ?? b.createdAt)
|
||||
.compareTo(a.lastMessageAt ?? a.createdAt));
|
||||
@@ -348,6 +361,7 @@ class ChatService {
|
||||
..sort((a, b) => (b.lastMessageAt ?? b.createdAt)
|
||||
.compareTo(a.lastMessageAt ?? a.createdAt));
|
||||
}
|
||||
*/// Fin du code commenté
|
||||
}
|
||||
|
||||
/// Créer une room avec vérification des permissions
|
||||
@@ -754,7 +768,7 @@ class ChatService {
|
||||
});
|
||||
|
||||
// Pas de sync immédiate ici car déjà faite dans init()
|
||||
debugPrint('⏰ Timer de sync incrémentale démarré (toutes les 15 secondes)');
|
||||
debugPrint('⏰ Timer de sync incrémentale démarré (toutes les 30 secondes)');
|
||||
}
|
||||
|
||||
/// Mettre en pause les synchronisations (app en arrière-plan)
|
||||
|
||||
Reference in New Issue
Block a user