Refactor timestamp handling and message sending

maj
This commit is contained in:
parotux
2026-01-11 19:13:08 +01:00
committed by GitHub
parent 863dfe6a0b
commit 6eb0bb9510

View File

@@ -283,7 +283,7 @@ class _ChatScreenState extends State<ChatScreen> {
); );
if (response.statusCode == 200) { if (response.statusCode == 200) {
print(" Message supprimé"); print(" Message supprimé");
_fetchMessages(); // refresh _fetchMessages();
} else { } else {
print("Erreur suppression: ${response.body}"); print("Erreur suppression: ${response.body}");
} }
@@ -358,9 +358,7 @@ class _ChatScreenState extends State<ChatScreen> {
final key = cryptoData['key'] ?? ''; final key = cryptoData['key'] ?? '';
final plainText = _controller.text.trim(); final plainText = _controller.text.trim();
// --- MODIFIED LINE FOR TIMESTAMP ---
// Get current local time, then convert to UTC, then add 2 hours (for CEST / UTC+2)
// This ensures the time sent is what CEST time would be for this moment.
final nowCestTime = DateTime.now().toUtc().add(Duration(hours: 2)); final nowCestTime = DateTime.now().toUtc().add(Duration(hours: 2));
final nowCestIsoFormatted = final nowCestIsoFormatted =
DateFormat("yyyy-MM-dd HH:mm:ss").format(nowCestTime); DateFormat("yyyy-MM-dd HH:mm:ss").format(nowCestTime);
@@ -370,14 +368,14 @@ class _ChatScreenState extends State<ChatScreen> {
return; return;
} }
// Affichage local temporaire (sera remplacé par le polling)
setState(() { setState(() {
messages = List<Map<String, dynamic>>.from(messages) messages = List<Map<String, dynamic>>.from(messages)
..add({ ..add({
'sender': expediteur ?? '', 'sender': expediteur ?? '',
'text': plainText, 'text': plainText,
'encrypted': encryptedMessage, 'encrypted': encryptedMessage,
'timestamp': 'En cours...', // Temporaire, will be updated by polling 'timestamp': 'En cours...',
'key': key, 'key': key,
'type': 'text' 'type': 'text'
}); });
@@ -389,7 +387,7 @@ class _ChatScreenState extends State<ChatScreen> {
print("✉️ Envoi du message chiffré : $encryptedMessage"); print("✉️ Envoi du message chiffré : $encryptedMessage");
print("🔑 Clé : $key"); print("🔑 Clé : $key");
print( print(
"⏰ Timestamp envoyé à l'API (CEST): $nowCestIsoFormatted"); // Add this debug print "⏰ Timestamp envoyé à l'API (CEST): $nowCestIsoFormatted"); /
try { try {
final response = await http.post( final response = await http.post(
@@ -403,7 +401,7 @@ class _ChatScreenState extends State<ChatScreen> {
'key': key, 'key': key,
'type': 'text', 'type': 'text',
'timestamp': 'timestamp':
nowCestIsoFormatted, // --- USE THE CEST FORMATTED STRING --- nowCestIsoFormatted,
}), }),
); );
@@ -443,7 +441,7 @@ class _ChatScreenState extends State<ChatScreen> {
} }
} }
// 🎭 Fonction _sendGif simplifiée
Future<void> _sendGif(String gifUrl) async { Future<void> _sendGif(String gifUrl) async {
if (expediteur == null || gifUrl.isEmpty) return; if (expediteur == null || gifUrl.isEmpty) return;
@@ -456,7 +454,7 @@ class _ChatScreenState extends State<ChatScreen> {
final encryptedMessage = cryptoData['encrypted_message'] ?? gifUrl; final encryptedMessage = cryptoData['encrypted_message'] ?? gifUrl;
final key = cryptoData['key'] ?? 'test'; final key = cryptoData['key'] ?? 'test';
// Affichage local temporaire
setState(() { setState(() {
messages = List<Map<String, dynamic>>.from(messages) messages = List<Map<String, dynamic>>.from(messages)
..add({ ..add({
@@ -481,7 +479,7 @@ class _ChatScreenState extends State<ChatScreen> {
'id_conversation': idConversation, 'id_conversation': idConversation,
'key': key, 'key': key,
'type': 'gif', 'type': 'gif',
'timestamp': nowCestIsoFormatted, // --- INCLUDE THIS IN THE BODY --- 'timestamp': nowCestIsoFormatted,
}), }),
); );
@@ -489,7 +487,7 @@ class _ChatScreenState extends State<ChatScreen> {
if (response.statusCode == 200) { if (response.statusCode == 200) {
print('✅ GIF envoyé avec succès.'); print('✅ GIF envoyé avec succès.');
// Refresh pour récupérer le vrai timestamp
await Future.delayed(Duration(milliseconds: 500)); await Future.delayed(Duration(milliseconds: 500));
await _fetchMessages(); await _fetchMessages();
} else { } else {
@@ -549,7 +547,6 @@ class _ChatScreenState extends State<ChatScreen> {
'expediteur': expediteur, 'expediteur': expediteur,
'destinataire': destinataire, 'destinataire': destinataire,
'message': encryptedMessage, 'message': encryptedMessage,
// 'timestamp': nowUtcIso, // removed
'id_conversation': idConversation, 'id_conversation': idConversation,
'key': key, 'key': key,
'type': 'file' 'type': 'file'
@@ -856,7 +853,7 @@ class _ChatScreenState extends State<ChatScreen> {
} }
} }
// ✅ Formatter : Enter = envoi / Shift+Enter = saut de ligne
class _EnterKeyFormatter extends TextInputFormatter { class _EnterKeyFormatter extends TextInputFormatter {
final VoidCallback onEnter; final VoidCallback onEnter;
@@ -872,7 +869,7 @@ class _EnterKeyFormatter extends TextInputFormatter {
!RawKeyboard.instance.keysPressed !RawKeyboard.instance.keysPressed
.contains(LogicalKeyboardKey.shiftRight)) { .contains(LogicalKeyboardKey.shiftRight)) {
onEnter(); onEnter();
return const TextEditingValue(text: ''); // vide le champ après envoi return const TextEditingValue(text: '');
} }
return newValue; return newValue;
} }