tentar aresolver a home

This commit is contained in:
2026-06-08 14:54:04 +01:00
parent 7d2f3c4679
commit 947e119dba
5 changed files with 264 additions and 116 deletions

View File

@@ -1,5 +1,6 @@
import 'dart:io';
import 'package:supabase_flutter/supabase_flutter.dart';
import 'package:playmaker/controllers/active_team.dart';
class TeamController {
final _supabase = Supabase.instance.client;
@@ -65,10 +66,34 @@ class TeamController {
// 4. FAVORITAR
Future<void> toggleFavorite(String teamId, bool currentStatus) async {
try {
await _supabase
.from('teams')
.update({'is_favorite': !currentStatus})
.eq('id', teamId);
final userId = _supabase.auth.currentUser?.id;
if (userId == null) return;
// If we're marking this team as favorite, clear other favorites for this user
if (!currentStatus) {
await _supabase.from('teams').update({'is_favorite': false}).eq('user_id', userId);
}
// Toggle the chosen team's favorite flag
await _supabase.from('teams').update({'is_favorite': !currentStatus}).eq('id', teamId);
// If it became favorite, load its data and set global active team
if (!currentStatus) {
final teamData = await _supabase.from('teams').select().eq('id', teamId).maybeSingle();
if (teamData != null) {
final newTeam = ActiveTeam(
id: teamData['id'].toString(),
name: teamData['name'] ?? 'Desconhecido',
logo: teamData['image_url'],
wins: int.tryParse(teamData['wins']?.toString() ?? '0') ?? 0,
losses: int.tryParse(teamData['losses']?.toString() ?? '0') ?? 0,
draws: int.tryParse(teamData['draws']?.toString() ?? '0') ?? 0,
);
// Update global active team so UI reflects the favorite immediately
await saveGlobalTeam(newTeam);
}
}
} catch (e) {
print("❌ Erro ao favoritar: $e");
}