tentar aresolver a home
This commit is contained in:
@@ -46,32 +46,48 @@ Future<void> loadGlobalTeam() async {
|
||||
if (userId == null) return;
|
||||
|
||||
try {
|
||||
// 1) Prefer an explicit team selection stored on the user's profile (if any)
|
||||
Map<String, dynamic>? teamData;
|
||||
final profile = await supabase.from('profiles').select('selected_team_id').eq('id', userId).maybeSingle();
|
||||
if (profile != null && profile['selected_team_id'] != null) {
|
||||
final dbTeamId = profile['selected_team_id'].toString();
|
||||
final teamData = await supabase.from('teams').select().eq('id', dbTeamId).maybeSingle();
|
||||
final dbTeam = await supabase.from('teams').select().eq('id', dbTeamId).maybeSingle();
|
||||
if (dbTeam != null) teamData = Map<String, dynamic>.from(dbTeam);
|
||||
}
|
||||
|
||||
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,
|
||||
);
|
||||
globalActiveTeam.value = newTeam;
|
||||
|
||||
// Atualiza a memória do telemóvel para a próxima vez ser rápido
|
||||
await prefs.setString('last_team_id', newTeam.id);
|
||||
await prefs.setString('last_team_name', newTeam.name);
|
||||
if (newTeam.logo != null && newTeam.logo!.isNotEmpty) {
|
||||
await prefs.setString('last_team_logo', newTeam.logo!);
|
||||
}
|
||||
await prefs.setInt('last_team_wins', newTeam.wins);
|
||||
await prefs.setInt('last_team_losses', newTeam.losses);
|
||||
await prefs.setInt('last_team_draws', newTeam.draws);
|
||||
// 2) If the user has no explicit profile selection, fall back to any team
|
||||
// marked as favorite for that user (acts as a default)
|
||||
if (teamData == null) {
|
||||
final favTeam = await supabase
|
||||
.from('teams')
|
||||
.select()
|
||||
.eq('user_id', userId)
|
||||
.eq('is_favorite', true)
|
||||
.maybeSingle();
|
||||
if (favTeam != null) teamData = Map<String, dynamic>.from(favTeam);
|
||||
}
|
||||
|
||||
// If we found a team (favorite or profile selection), set it as active and persist locally
|
||||
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,
|
||||
);
|
||||
globalActiveTeam.value = newTeam;
|
||||
|
||||
// Atualiza a memória do telemóvel para a próxima vez ser rápido
|
||||
await prefs.setString('last_team_id', newTeam.id);
|
||||
await prefs.setString('last_team_name', newTeam.name);
|
||||
if (newTeam.logo != null && newTeam.logo!.isNotEmpty) {
|
||||
await prefs.setString('last_team_logo', newTeam.logo!);
|
||||
}
|
||||
await prefs.setInt('last_team_wins', newTeam.wins);
|
||||
await prefs.setInt('last_team_losses', newTeam.losses);
|
||||
await prefs.setInt('last_team_draws', newTeam.draws);
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint("Erro ao carregar equipa do Supabase: $e");
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user