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

@@ -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");