This commit is contained in:
exmKrd
2025-04-10 20:05:33 +02:00
parent 7d69c6ea00
commit 91db846958
4 changed files with 88 additions and 11 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

View File

@@ -120,6 +120,34 @@ class _ContactsState extends State<Contacts>
} }
Future<void> _envoyerDemandeContact(String destinataire) async { Future<void> _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 { try {
final response = await http.post( final response = await http.post(
Uri.parse("https://nexuschat.derickexm.be/contacts/demande_contact"), Uri.parse("https://nexuschat.derickexm.be/contacts/demande_contact"),
@@ -130,6 +158,7 @@ class _ContactsState extends State<Contacts>
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Demande envoyée à $destinataire")), SnackBar(content: Text("Demande envoyée à $destinataire")),
); );
await _loadDemandes();
} }
} catch (e) { } catch (e) {
print("Erreur : $e"); print("Erreur : $e");
@@ -173,6 +202,19 @@ class _ContactsState extends State<Contacts>
} }
} }
Future<String> _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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
@@ -261,17 +303,49 @@ class _ContactsState extends State<Contacts>
itemBuilder: (context, index) { itemBuilder: (context, index) {
final user = _filteredUsers[index]['username']; final user = _filteredUsers[index]['username'];
if (user == currentUser) return SizedBox(); if (user == currentUser) return SizedBox();
return FutureBuilder<String>(
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( return Card(
child: ListTile( child: ListTile(
title: Text(user), title: Text(user),
leading: Icon(Icons.person_outline), leading: Icon(Icons.person_outline),
trailing: ElevatedButton( trailing: ElevatedButton(
onPressed: () => _envoyerDemandeContact(user), onPressed: action,
child: Text("Envoyer demande"), child: Text(label),
), ),
), ),
); );
}, },
);
},
), ),
), ),
], ],

View File

@@ -225,7 +225,10 @@ class Listechatstate extends State<Listechat> {
itemBuilder: (context, index) { itemBuilder: (context, index) {
final conv = conversations[index]; final conv = conversations[index];
final destinataire = final destinataire =
conv["user2_username"] ?? "Utilisateur inconnu"; conv["user1_username"] == expediteur
? conv["user2_username"]
: conv["user1_username"];
final rawId = conv["id"]?.toString(); final rawId = conv["id"]?.toString();
final uniqueTag = rawId != null final uniqueTag = rawId != null
? "conversation_${expediteur}_$rawId" ? "conversation_${expediteur}_$rawId"

View File

@@ -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:adaptive_theme/adaptive_theme.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:nexuschat/menu.dart'; import 'package:nexuschat/menu.dart';