diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..163a16c Binary files /dev/null and b/.DS_Store differ diff --git a/nexuschat/lib/contacts.dart b/nexuschat/lib/contacts.dart index 45a6b6e..cb3e472 100644 --- a/nexuschat/lib/contacts.dart +++ b/nexuschat/lib/contacts.dart @@ -120,6 +120,34 @@ class _ContactsState extends State } Future _envoyerDemandeContact(String destinataire) async { + if (destinataire == currentUser) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text("Vous ne pouvez pas vous ajouter vous-même")), + ); + return; + } + + final dejaContact = + _contacts.any((contact) => contact['contacts'] == destinataire); + final demandeExistante = + _demandes.any((demande) => demande['from'] == destinataire); + + if (dejaContact) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text("$destinataire est déjà dans vos contacts")), + ); + return; + } + + if (demandeExistante) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: + Text("Une demande est déjà en attente pour $destinataire")), + ); + return; + } + try { final response = await http.post( Uri.parse("https://nexuschat.derickexm.be/contacts/demande_contact"), @@ -130,6 +158,7 @@ class _ContactsState extends State ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text("Demande envoyée à $destinataire")), ); + await _loadDemandes(); } } catch (e) { print("Erreur : $e"); @@ -173,6 +202,19 @@ class _ContactsState extends State } } + Future _getEtatRelation(String target) async { + final response = await http.get( + Uri.parse( + "https://nexuschat.derickexm.be/contacts/etat_relation?owner=$currentUser&target=$target"), + ); + if (response.statusCode == 200) { + final data = json.decode(response.body); + return data['etat']; + } else { + return "erreur"; + } + } + @override Widget build(BuildContext context) { return Scaffold( @@ -261,15 +303,47 @@ class _ContactsState extends State itemBuilder: (context, index) { final user = _filteredUsers[index]['username']; if (user == currentUser) return SizedBox(); - return Card( - child: ListTile( - title: Text(user), - leading: Icon(Icons.person_outline), - trailing: ElevatedButton( - onPressed: () => _envoyerDemandeContact(user), - child: Text("Envoyer demande"), - ), - ), + + return FutureBuilder( + future: _getEtatRelation(user), + builder: (context, snapshot) { + String? etat = snapshot.data; + + if (!snapshot.hasData) { + return ListTile( + title: Text(user), + subtitle: Text("Chargement..."), + ); + } + + String label = ""; + VoidCallback? action; + + if (etat == "aucune_relation") { + label = "Envoyer demande"; + action = () => _envoyerDemandeContact(user); + } else if (etat == "pending_envoyee" || + etat == "pending_recue") { + label = "Demande en attente"; + action = null; + } else if (etat == "ami") { + return SizedBox(); // cacher les amis + } else { + label = "Erreur"; + action = null; + } + + return Card( + child: ListTile( + title: Text(user), + leading: Icon(Icons.person_outline), + trailing: ElevatedButton( + onPressed: action, + child: Text(label), + ), + ), + ); + }, ); }, ), diff --git a/nexuschat/lib/listechat.dart b/nexuschat/lib/listechat.dart index e88bb9a..d3e5d32 100644 --- a/nexuschat/lib/listechat.dart +++ b/nexuschat/lib/listechat.dart @@ -225,7 +225,10 @@ class Listechatstate extends State { itemBuilder: (context, index) { final conv = conversations[index]; final destinataire = - conv["user2_username"] ?? "Utilisateur inconnu"; + conv["user1_username"] == expediteur + ? conv["user2_username"] + : conv["user1_username"]; + final rawId = conv["id"]?.toString(); final uniqueTag = rawId != null ? "conversation_${expediteur}_$rawId" diff --git a/nexuschat/lib/main.dart b/nexuschat/lib/main.dart index 0616253..750eda5 100644 --- a/nexuschat/lib/main.dart +++ b/nexuschat/lib/main.dart @@ -1,4 +1,4 @@ -import 'dart:io'; // Ajoutez cette importation pour HttpClient et X509Certificate +import 'dart:io'; import 'package:adaptive_theme/adaptive_theme.dart'; import 'package:flutter/material.dart'; import 'package:nexuschat/menu.dart';