stats page pagina melhrar

This commit is contained in:
2026-01-20 17:15:29 +00:00
parent 01d5e7adb6
commit 05c4738ef2
23 changed files with 883 additions and 573 deletions

View File

@@ -1,8 +1,9 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
class LoginController with ChangeNotifier {
final FirebaseAuth _auth = FirebaseAuth.instance;
// 1. Substituímos o FirebaseAuth pelo cliente do Supabase
final SupabaseClient _supabase = Supabase.instance.client;
final TextEditingController emailController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
@@ -22,6 +23,7 @@ class LoginController with ChangeNotifier {
notifyListeners();
}
// --- VALIDAÇÕES (Mantêm-se iguais) ---
String? validateEmail(String? value) {
if (value == null || value.isEmpty) return 'Por favor, insira o seu email';
final emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
@@ -37,10 +39,17 @@ class LoginController with ChangeNotifier {
// --- MÉTODO PARA ENTRAR (LOGIN) ---
Future<bool> login() async {
_emailError = validateEmail(emailController.text);
_passwordError = validatePassword(passwordController.text);
// Limpa erros anteriores
_emailError = null;
_passwordError = null;
// Valida localmente primeiro
String? emailValidation = validateEmail(emailController.text);
String? passValidation = validatePassword(passwordController.text);
if (_emailError != null || _passwordError != null) {
if (emailValidation != null || passValidation != null) {
_emailError = emailValidation;
_passwordError = passValidation;
notifyListeners();
return false;
}
@@ -49,16 +58,25 @@ class LoginController with ChangeNotifier {
notifyListeners();
try {
await _auth.signInWithEmailAndPassword(
// 2. Chamada ao Supabase para Login
await _supabase.auth.signInWithPassword(
email: emailController.text.trim(),
password: passwordController.text.trim(),
);
_isLoading = false;
notifyListeners();
return true;
} on FirebaseAuthException catch (e) {
} on AuthException catch (e) {
// 3. Captura erros específicos do Supabase
_isLoading = false;
_handleFirebaseError(e.code);
_handleSupabaseError(e);
notifyListeners();
return false;
} catch (e) {
_isLoading = false;
_emailError = 'Ocorreu um erro inesperado.';
notifyListeners();
return false;
}
@@ -66,10 +84,15 @@ class LoginController with ChangeNotifier {
// --- MÉTODO PARA CRIAR CONTA (SIGN UP) ---
Future<bool> signUp() async {
_emailError = validateEmail(emailController.text);
_passwordError = validatePassword(passwordController.text);
_emailError = null;
_passwordError = null;
if (_emailError != null || _passwordError != null) {
String? emailValidation = validateEmail(emailController.text);
String? passValidation = validatePassword(passwordController.text);
if (emailValidation != null || passValidation != null) {
_emailError = emailValidation;
_passwordError = passValidation;
notifyListeners();
return false;
}
@@ -78,40 +101,46 @@ class LoginController with ChangeNotifier {
notifyListeners();
try {
await _auth.createUserWithEmailAndPassword(
// 4. Chamada ao Supabase para Registo
await _supabase.auth.signUp(
email: emailController.text.trim(),
password: passwordController.text.trim(),
);
_isLoading = false;
notifyListeners();
return true;
} on FirebaseAuthException catch (e) {
} on AuthException catch (e) {
_isLoading = false;
_handleFirebaseError(e.code);
_handleSupabaseError(e);
notifyListeners();
return false;
} catch (e) {
_isLoading = false;
_emailError = 'Ocorreu um erro inesperado.';
notifyListeners();
return false;
}
}
void _handleFirebaseError(String code) {
switch (code) {
case 'email-already-in-use':
_emailError = 'Este e-mail já está a ser utilizado.';
break;
case 'invalid-credential':
_emailError = 'E-mail ou password incorretos.';
break;
case 'user-not-found':
_emailError = 'Utilizador não encontrado.';
break;
case 'wrong-password':
_passwordError = 'Palavra-passe incorreta.';
break;
case 'weak-password':
_passwordError = 'A password é demasiado fraca.';
break;
default:
_emailError = 'Erro: $code';
// --- TRATAMENTO DE ERROS SUPABASE ---
void _handleSupabaseError(AuthException error) {
// O Supabase retorna mensagens em inglês, vamos traduzir as mais comuns.
// O 'message' contém o texto do erro.
final msg = error.message.toLowerCase();
if (msg.contains('invalid login credentials')) {
_emailError = 'E-mail ou password incorretos.';
} else if (msg.contains('user already registered') || msg.contains('already exists')) {
_emailError = 'Este e-mail já está registado.';
} else if (msg.contains('password')) {
_passwordError = 'A password deve ter pelo menos 6 caracteres.';
} else if (msg.contains('email')) {
_emailError = 'Formato de e-mail inválido.';
} else {
// Fallback para mostrar a mensagem original se não conhecermos o erro
_emailError = error.message;
}
}

View File

@@ -1,25 +1,18 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
class RegisterController with ChangeNotifier {
final FirebaseAuth _auth = FirebaseAuth.instance;
class RegisterController extends ChangeNotifier {
// Chave para identificar e validar o formulário
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
final TextEditingController emailController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
final TextEditingController confirmPasswordController = TextEditingController();
bool _isLoading = false;
String? _emailError;
String? _passwordError;
String? _confirmPasswordError; // Novo!
String? get confirmPasswordError => _confirmPasswordError; // Novo!
final nameController = TextEditingController();
final emailController = TextEditingController();
final passwordController = TextEditingController();
final confirmPasswordController = TextEditingController(); // Novo campo
bool get isLoading => _isLoading;
String? get emailError => _emailError;
String? get passwordError => _passwordError;
bool isLoading = false;
// Validações
// --- AS TUAS VALIDAÇÕES ---
String? validateEmail(String? value) {
if (value == null || value.isEmpty) return 'Por favor, insira o seu email';
final emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
@@ -32,6 +25,7 @@ class RegisterController with ChangeNotifier {
if (value.length < 6) return 'A password deve ter pelo menos 6 caracteres';
return null;
}
String? validateConfirmPassword(String? value) {
if (value == null || value.isEmpty) {
return 'Por favor, confirme a sua password';
@@ -41,57 +35,56 @@ class RegisterController with ChangeNotifier {
}
return null;
}
// ---------------------------
// MÉTODO PARA CRIAR CONTA (SIGN UP)
Future<bool> signUp() async {
_emailError = validateEmail(emailController.text);
_passwordError = validatePassword(passwordController.text);
_emailError = validateEmail(emailController.text);
_passwordError = validatePassword(passwordController.text);
_confirmPasswordError = validateConfirmPassword(confirmPasswordController.text); // Valida aqui!
if (_emailError != null || _passwordError != null || _confirmPasswordError != null) {
notifyListeners();
return false;
Future<void> signUp(BuildContext context) async {
// 1. Verifica se o formulário é válido antes de fazer qualquer coisa
if (!formKey.currentState!.validate()) {
return;
}
_isLoading = true;
isLoading = true;
notifyListeners();
try {
await _auth.createUserWithEmailAndPassword(
final AuthResponse res = await Supabase.instance.client.auth.signUp(
email: emailController.text.trim(),
password: passwordController.text.trim(),
data: {'full_name': nameController.text.trim()},
);
_isLoading = false;
notifyListeners();
return true;
} on FirebaseAuthException catch (e) {
_isLoading = false;
_handleFirebaseError(e.code);
notifyListeners();
return false;
}
}
void _handleFirebaseError(String code) {
switch (code) {
case 'email-already-in-use':
_emailError = 'Este e-mail já está a ser utilizado.';
break;
case 'weak-password':
_passwordError = 'A password é demasiado fraca.';
break;
default:
_emailError = 'Erro ao registar: $code';
final user = res.user;
if (user != null && context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Conta criada! Podes fazer login.')),
);
Navigator.pop(context);
}
} on AuthException catch (e) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(e.message), backgroundColor: Colors.red),
);
}
} catch (e) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Erro inesperado'), backgroundColor: Colors.red),
);
}
} finally {
isLoading = false;
notifyListeners();
}
}
@override
void dispose() {
nameController.dispose();
emailController.dispose();
passwordController.dispose();
confirmPasswordController.dispose();
super.dispose();
}
}

View File

@@ -1,146 +1,114 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
import '../models/person_model.dart';
class StatsController {
final FirebaseFirestore _db = FirebaseFirestore.instance;
final SupabaseClient _supabase = Supabase.instance.client;
// --- LÓGICA DE FIREBASE ---
// --- 1. LER DADOS (STREAM) ---
Stream<List<Person>> getMembers(String teamId) {
return _supabase
.from('members')
.stream(primaryKey: ['id'])
.eq('team_id', teamId)
.order('name', ascending: true) // Ordena por nome
.map((data) => data.map((json) => Person.fromMap(json)).toList());
}
// GRAVAR: Cria o personagem numa sub-coleção dentro da equipa
Future<void> addPerson(String teamId, String name, String type, String number) async {
await _db.collection('teams').doc(teamId).collection('members').add({
// --- 2. AÇÕES DE BASE DE DADOS ---
// Adicionar
Future<void> _addPersonToSupabase(String teamId, String name, String type, String number) async {
await _supabase.from('members').insert({
'team_id': teamId,
'name': name,
'type': type,
'number': number,
'createdAt': FieldValue.serverTimestamp(),
});
}
// LER: Vai buscar todos os membros da equipa em tempo real
Stream<List<Person>> getMembers(String teamId) {
return _db
.collection('teams')
.doc(teamId)
.collection('members')
.orderBy('createdAt', descending: false) // Organiza por ordem de criação
.snapshots()
.map((snapshot) => snapshot.docs
.map((doc) => Person.fromFirestore(doc.data(), doc.id))
.toList());
// Editar
Future<void> _updatePersonInSupabase(String personId, String name, String type, String number) async {
await _supabase.from('members').update({
'name': name,
'type': type,
'number': number,
}).eq('id', personId);
}
// --- Adiciona estas funções dentro da classe StatsController ---
// ELIMINAR: Remove o documento da sub-coleção
Future<void> deletePerson(String teamId, String personId) async {
await _db
.collection('teams')
.doc(teamId)
.collection('members')
.doc(personId)
.delete();
}
// Apagar
Future<void> deletePerson(String teamId, String personId) async {
try {
await _supabase.from('members').delete().eq('id', personId);
} catch (e) {
debugPrint("Erro ao apagar: $e");
}
}
// EDITAR (LOGICA): Abre o popup já preenchido com os dados atuais
void showEditPersonDialog(BuildContext context, String teamId, Person person) {
final nameController = TextEditingController(text: person.name);
final numberController = TextEditingController(text: person.number);
String selectedType = person.type;
showDialog(
context: context,
builder: (context) => StatefulBuilder(
builder: (context, setPopupState) => AlertDialog(
title: const Text("Editar Personagem"),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
DropdownButtonFormField<String>(
value: selectedType,
items: ['Jogador', 'Treinador'].map((t) => DropdownMenuItem(value: t, child: Text(t))).toList(),
onChanged: (val) => setPopupState(() => selectedType = val!),
decoration: const InputDecoration(labelText: 'Tipo'),
),
TextField(controller: nameController, decoration: const InputDecoration(labelText: 'Nome')),
if (selectedType == 'Jogador')
TextField(controller: numberController, decoration: const InputDecoration(labelText: 'Número')),
],
),
actions: [
TextButton(onPressed: () => Navigator.pop(context), child: const Text("Cancelar")),
ElevatedButton(
onPressed: () async {
await _db.collection('teams').doc(teamId).collection('members').doc(person.id).update({
'name': nameController.text,
'type': selectedType,
'number': selectedType == 'Jogador' ? numberController.text : '',
});
if (context.mounted) Navigator.pop(context);
},
child: const Text("Guardar Alterações"),
),
],
),
),
);
}
// --- LÓGICA DE INTERFACE (POPUP) ---
// --- 3. DIÁLOGOS (UI) ---
// Mostrar Diálogo de Adicionar
void showAddPersonDialog(BuildContext context, String teamId) {
String selectedType = 'Jogador';
final TextEditingController nameController = TextEditingController();
final TextEditingController numberController = TextEditingController();
_showPersonDialog(context, teamId: teamId);
}
// Mostrar Diálogo de Editar
void showEditPersonDialog(BuildContext context, String teamId, Person person) {
_showPersonDialog(context, teamId: teamId, person: person);
}
// Função Genérica para o Diálogo (Serve para criar e editar)
void _showPersonDialog(BuildContext context, {required String teamId, Person? person}) {
final isEditing = person != null;
final nameController = TextEditingController(text: person?.name ?? '');
final numberController = TextEditingController(text: person?.number ?? '');
// Valor inicial do dropdown ('Jogador' por defeito)
String selectedType = person?.type ?? 'Jogador';
showDialog(
context: context,
builder: (context) {
// Usamos StatefulBuilder para atualizar o Dropdown dentro do Dialog
return StatefulBuilder(
builder: (context, setPopupState) {
builder: (context, setState) {
return AlertDialog(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15)),
title: const Text('Novo Personagem'),
content: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
// Seletor: Jogador ou Treinador
DropdownButtonFormField<String>(
value: selectedType,
decoration: const InputDecoration(labelText: 'Tipo'),
items: ['Jogador', 'Treinador']
.map((t) => DropdownMenuItem(value: t, child: Text(t)))
.toList(),
onChanged: (val) {
setPopupState(() {
selectedType = val!;
});
},
),
const SizedBox(height: 15),
// Campo Nome
TextField(
controller: nameController,
textCapitalization: TextCapitalization.words,
decoration: const InputDecoration(
labelText: 'Nome Completo',
hintText: 'Ex: Stephen Curry',
),
),
// Campo Número (Aparece apenas se for Jogador)
if (selectedType == 'Jogador') ...[
const SizedBox(height: 15),
TextField(
controller: numberController,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
labelText: 'Número da Camisola',
hintText: 'Ex: 30',
),
),
title: Text(isEditing ? 'Editar Membro' : 'Novo Membro'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
// Nome
TextField(
controller: nameController,
decoration: const InputDecoration(labelText: 'Nome'),
textCapitalization: TextCapitalization.sentences,
),
const SizedBox(height: 10),
// Tipo (Jogador/Treinador)
DropdownButtonFormField<String>(
value: selectedType,
decoration: const InputDecoration(labelText: 'Função'),
items: const [
DropdownMenuItem(value: 'Jogador', child: Text('Jogador')),
DropdownMenuItem(value: 'Treinador', child: Text('Treinador')),
],
],
),
onChanged: (value) {
if (value != null) {
setState(() => selectedType = value);
}
},
),
const SizedBox(height: 10),
// Número (Só aparece se for Jogador)
if (selectedType == 'Jogador')
TextField(
controller: numberController,
decoration: const InputDecoration(labelText: 'Número da Camisola'),
keyboardType: TextInputType.number,
),
],
),
actions: [
TextButton(
@@ -148,23 +116,22 @@ void showEditPersonDialog(BuildContext context, String teamId, Person person) {
child: const Text('Cancelar'),
),
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF00C853),
),
style: ElevatedButton.styleFrom(backgroundColor: const Color(0xFF00C853)),
onPressed: () async {
if (nameController.text.isNotEmpty) {
// CHAMA A FUNÇÃO DE GRAVAR DO FIREBASE
await addPerson(
teamId,
nameController.text,
selectedType,
selectedType == 'Jogador' ? numberController.text : '',
);
if (context.mounted) Navigator.pop(context);
if (nameController.text.isEmpty) return;
final name = nameController.text.trim();
final number = numberController.text.trim();
if (isEditing) {
await _updatePersonInSupabase(person!.id, name, selectedType, number);
} else {
await _addPersonToSupabase(teamId, name, selectedType, number);
}
if (context.mounted) Navigator.pop(context);
},
child: const Text('Guardar', style: TextStyle(color: Colors.white)),
child: Text(isEditing ? 'Guardar' : 'Adicionar', style: const TextStyle(color: Colors.white)),
),
],
);

View File

@@ -1,31 +1,37 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:playmaker/service/auth_service.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
class TeamController {
final AuthService _authService = AuthService();
final CollectionReference _teamsRef = FirebaseFirestore.instance.collection('teams');
// Acesso ao cliente do Supabase
final SupabaseClient _supabase = Supabase.instance.client;
// --- STREAM DE EQUIPAS (LER) ---
// Retorna uma Lista de Mapas em tempo real
Stream<List<Map<String, dynamic>>> get teamsStream {
final user = _supabase.auth.currentUser;
if (user == null) {
return const Stream.empty();
}
Stream<QuerySnapshot> get teamsStream {
final uid = _authService.currentUid;
return _teamsRef
.where('userId', isEqualTo: uid)
.orderBy('createdAt', descending: true)
.snapshots();
return _supabase
.from('teams')
.stream(primaryKey: ['id']) // É obrigatório definir a Primary Key para Streams
.eq('user_id', user.id) // Filtra apenas as equipas do utilizador logado
.order('created_at', ascending: false);
}
// --- CRIAR EQUIPA ---
Future<void> createTeam(String name, String season, String imageUrl) async {
final uid = _authService.currentUid;
final user = _supabase.auth.currentUser;
if (uid != null) {
if (user != null) {
try {
await _teamsRef.add({
await _supabase.from('teams').insert({
'name': name,
'season': season,
'imageUrl': imageUrl,
'userId': uid,
'createdAt': FieldValue.serverTimestamp(),
'image_url': imageUrl, // Garante que na tabela a coluna se chama 'image_url' (snake_case)
'user_id': user.id, // Chave estrangeira para ligar ao utilizador
// 'created_at': O Supabase preenche isto sozinho se tiver default: now()
});
} catch (e) {
print("Erro ao criar equipa: $e");
@@ -34,15 +40,32 @@ class TeamController {
print("Erro: Utilizador não autenticado.");
}
}
// --- ELIMINAR EQUIPA ---
Future<void> deleteTeam(String docId) async {
try {
await _teamsRef.doc(docId).delete();
// Se configuraste "ON DELETE CASCADE" no Supabase, isto apaga também os jogadores
await _supabase.from('teams').delete().eq('id', docId);
} catch (e) {
print("Erro ao eliminar: $e");
}
Future<int> getPlayerCount(String teamId) async {
var snapshot = await _teamsRef.doc(teamId).collection('players').get();
return snapshot.docs.length;
}
}
// --- CONTAR JOGADORES ---
// No SQL não entramos dentro da equipa. Vamos à tabela 'members' e filtramos pelo team_id.
// --- CONTAR JOGADORES (CORRIGIDO) ---
Future<int> getPlayerCount(String teamId) async {
try {
// Correção: O Supabase agora retorna o 'int' diretamente, não um objeto response
final int count = await _supabase
.from('members')
.count(CountOption.exact) // Pede o número exato
.eq('team_id', teamId);
return count;
} catch (e) {
print("Erro ao contar jogadores: $e");
return 0;
}
}
}