From 91db8469582d4d68c34b965f44fee89d94e57765 Mon Sep 17 00:00:00 2001 From: exmKrd Date: Thu, 10 Apr 2025 20:05:33 +0200 Subject: [PATCH] update --- .DS_Store | Bin 0 -> 10244 bytes nexuschat/lib/contacts.dart | 92 +++++++++++++++++++++++++++++++---- nexuschat/lib/listechat.dart | 5 +- nexuschat/lib/main.dart | 2 +- 4 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..163a16c5ac5b8561ed265fb5fd7509e242b8d7bb GIT binary patch literal 10244 zcmeHMTWl0n7(V~B&>1?=0~A}ZBTEb6lBJYVY|F)LZwd$$x-H$7!ZN!v(t+8TWoLF< zD2JoukEv$dDS7fFOj?=FLPX3=QW=W^iP>46kpy4E`~dxld3b#^X{XXNdIpLB zfdGL3fdGL3fdGNK1p)kKvm};s8I%D60RjO669};PLkKgI5nqmR>AgCz@Qwf|i&5<* ztW#D&8S-Vsmt$O_1ru;rl-w2kBL;AH)Q9|j5nqmR$=wO~hY#>)2LFTt-t0Jj$m>oZ z!evkf2m}bsM1Y;$i%BgxObp^qIls^OeZgc~%5HB@n05kn;(IIVOCK1&+A`Iy{i{7a zIO%Sn!i!RgSVSkoBugBUB!fgHZeqwu`?Fk0q)o$d>*~G&p{RJ)>=Lm=EENw#2koJ# ztGZdIRn6>Yt!~>k6ZyKD*k|ZNaa!JHTdrzYdd71wG#$GP^ct3C54GA^OLIKmC^v|b zD8*^z$VfwTb2!wnp?NeM8fmVtWAz6|M3q?y2#GPeTe$j}-OH zsY5ed+cP2>yKxtNY`7Lp^Y*Z$a&egwReD7FxrmR*uyO`Q5k6KPOkz#Me>OFdHM~~i zD~JcQR1>N56(UhcJ!vO9$sv*8lefAp%pq|FLXf^24D~jI06m~!wDFHlkg;*f~VjNoQ3D$C3pp1h1cLscndDU zWq2FjhimW&deAEBE=j5+@b=Naf7ZS7r1OfyC z1OfyC1Of!^9t4W`QbO$hzvK4*|L>j!2P+8>2oSjA2taXrw7nIF%^!@X&%o~5LzoX? zW{LG10QTTgI&k3!p!>S?+sWz;_Jq^L=C3@>1RN2{twRog#5Sru6y?U F|2OVB?Ysa0 literal 0 HcmV?d00001 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';