ndk
This commit is contained in:
25
lib/controllers/game_controller.dart
Normal file
25
lib/controllers/game_controller.dart
Normal file
@@ -0,0 +1,25 @@
|
||||
import 'dart:async';
|
||||
import '../models/game_model.dart';
|
||||
|
||||
class GameController {
|
||||
final List<Game> _games = [];
|
||||
final _gameStreamController = StreamController<List<Game>>.broadcast();
|
||||
|
||||
Stream<List<Game>> get gamesStream => _gameStreamController.stream;
|
||||
|
||||
void addGame(String myTeam, String opponent, String season) {
|
||||
final newGame = Game(
|
||||
id: DateTime.now().toString(),
|
||||
myTeam: myTeam,
|
||||
opponentTeam: opponent,
|
||||
season: season,
|
||||
date: DateTime.now(),
|
||||
);
|
||||
_games.insert(0, newGame); // Adiciona ao topo da lista
|
||||
_gameStreamController.add(List.unmodifiable(_games));
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_gameStreamController.close();
|
||||
}
|
||||
}
|
||||
@@ -5,18 +5,18 @@ import '../models/person_model.dart';
|
||||
class StatsController {
|
||||
final SupabaseClient _supabase = Supabase.instance.client;
|
||||
|
||||
// --- 1. LER MEMBROS (STREAM EM TEMPO REAL) ---
|
||||
// 1. LER
|
||||
Stream<List<Person>> getMembers(String teamId) {
|
||||
return _supabase
|
||||
.from('members') // Nome da tua tabela no Supabase
|
||||
.from('members')
|
||||
.stream(primaryKey: ['id'])
|
||||
.eq('team_id', teamId)
|
||||
.order('name', ascending: true)
|
||||
.map((data) => data.map((json) => Person.fromMap(json)).toList());
|
||||
}
|
||||
|
||||
// --- 2. APAGAR ---
|
||||
Future<void> deletePerson(String teamId, String personId) async {
|
||||
// 2. APAGAR
|
||||
Future<void> deletePerson(String personId) async {
|
||||
try {
|
||||
await _supabase.from('members').delete().eq('id', personId);
|
||||
} catch (e) {
|
||||
@@ -24,112 +24,135 @@ class StatsController {
|
||||
}
|
||||
}
|
||||
|
||||
// --- 3. DIÁLOGOS DE ADICIONAR / EDITAR ---
|
||||
|
||||
// Abrir diálogo para criar novo
|
||||
// 3. DIÁLOGOS
|
||||
void showAddPersonDialog(BuildContext context, String teamId) {
|
||||
_showPersonForm(context, teamId: teamId);
|
||||
_showForm(context, teamId: teamId);
|
||||
}
|
||||
|
||||
// Abrir diálogo para editar existente
|
||||
void showEditPersonDialog(BuildContext context, String teamId, Person person) {
|
||||
_showPersonForm(context, teamId: teamId, person: person);
|
||||
_showForm(context, teamId: teamId, person: person);
|
||||
}
|
||||
|
||||
// Lógica interna do formulário
|
||||
void _showPersonForm(BuildContext context, {required String teamId, Person? person}) {
|
||||
final isEditing = person != null;
|
||||
// --- O POPUP ESTÁ AQUI ---
|
||||
void _showForm(BuildContext context, {required String teamId, Person? person}) {
|
||||
final isEdit = person != null;
|
||||
final nameCtrl = TextEditingController(text: person?.name ?? '');
|
||||
final numCtrl = TextEditingController(text: person?.number ?? '');
|
||||
|
||||
// Controladores de texto
|
||||
final nameController = TextEditingController(text: person?.name ?? '');
|
||||
final numberController = TextEditingController(text: person?.number ?? '');
|
||||
|
||||
// Variável para o Dropdown (Valor inicial)
|
||||
// Define o valor inicial
|
||||
String selectedType = person?.type ?? 'Jogador';
|
||||
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
// StatefulBuilder serve para atualizar o Dropdown DENTRO do diálogo
|
||||
return StatefulBuilder(
|
||||
builder: (context, setState) {
|
||||
return AlertDialog(
|
||||
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 vs 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ó mostramos se for Jogador)
|
||||
if (selectedType == 'Jogador')
|
||||
TextField(
|
||||
controller: numberController,
|
||||
decoration: const InputDecoration(labelText: 'Número da Camisola'),
|
||||
keyboardType: TextInputType.number,
|
||||
),
|
||||
],
|
||||
builder: (ctx) => StatefulBuilder(
|
||||
builder: (ctx, setState) => AlertDialog(
|
||||
title: Text(isEdit ? "Editar" : "Adicionar"),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// NOME
|
||||
TextField(
|
||||
controller: nameCtrl,
|
||||
decoration: const InputDecoration(labelText: "Nome"),
|
||||
textCapitalization: TextCapitalization.sentences,
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('Cancelar'),
|
||||
),
|
||||
ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(backgroundColor: const Color(0xFF00C853)),
|
||||
onPressed: () async {
|
||||
if (nameController.text.trim().isEmpty) return;
|
||||
const SizedBox(height: 10),
|
||||
|
||||
final name = nameController.text.trim();
|
||||
final number = numberController.text.trim();
|
||||
// FUNÇÃO
|
||||
DropdownButtonFormField<String>(
|
||||
value: selectedType,
|
||||
decoration: const InputDecoration(labelText: "Função"),
|
||||
items: ["Jogador", "Treinador"]
|
||||
.map((e) => DropdownMenuItem(value: e, child: Text(e)))
|
||||
.toList(),
|
||||
onChanged: (v) {
|
||||
if (v != null) setState(() => selectedType = v);
|
||||
},
|
||||
),
|
||||
|
||||
if (isEditing) {
|
||||
// ATUALIZAR
|
||||
await _supabase.from('members').update({
|
||||
'name': name,
|
||||
'type': selectedType,
|
||||
'number': number,
|
||||
}).eq('id', person!.id);
|
||||
} else {
|
||||
// CRIAR NOVO
|
||||
await _supabase.from('members').insert({
|
||||
'team_id': teamId,
|
||||
'name': name,
|
||||
'type': selectedType,
|
||||
'number': number,
|
||||
});
|
||||
}
|
||||
|
||||
if (context.mounted) Navigator.pop(context);
|
||||
},
|
||||
child: Text(isEditing ? 'Guardar' : 'Adicionar', style: const TextStyle(color: Colors.white)),
|
||||
// NÚMERO (Só aparece se for Jogador)
|
||||
if (selectedType == "Jogador") ...[
|
||||
const SizedBox(height: 10),
|
||||
TextField(
|
||||
controller: numCtrl,
|
||||
decoration: const InputDecoration(labelText: "Número da Camisola"),
|
||||
keyboardType: TextInputType.text, // Aceita texto para evitar erros
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(ctx),
|
||||
child: const Text("Cancelar")
|
||||
),
|
||||
ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(backgroundColor: const Color(0xFF00C853)),
|
||||
onPressed: () async {
|
||||
print("--- 1. CLICOU EM GUARDAR ---");
|
||||
|
||||
// Validação Simples
|
||||
if (nameCtrl.text.trim().isEmpty) {
|
||||
print("ERRO: Nome vazio");
|
||||
return;
|
||||
}
|
||||
|
||||
// Lógica do Número:
|
||||
// Se for Treinador -> envia NULL
|
||||
// Se for Jogador e estiver vazio -> envia NULL
|
||||
// Se tiver texto -> envia o Texto
|
||||
String? numeroFinal;
|
||||
if (selectedType == "Treinador") {
|
||||
numeroFinal = null;
|
||||
} else {
|
||||
numeroFinal = numCtrl.text.trim().isEmpty ? null : numCtrl.text.trim();
|
||||
}
|
||||
|
||||
print("--- 2. DADOS A ENVIAR ---");
|
||||
print("Nome: ${nameCtrl.text}");
|
||||
print("Tipo: $selectedType");
|
||||
print("Número: $numeroFinal");
|
||||
|
||||
try {
|
||||
if (isEdit) {
|
||||
await _supabase.from('members').update({
|
||||
'name': nameCtrl.text.trim(),
|
||||
'type': selectedType,
|
||||
'number': numeroFinal,
|
||||
}).eq('id', person!.id);
|
||||
} else {
|
||||
await _supabase.from('members').insert({
|
||||
'team_id': teamId, // Verifica se este teamId é válido!
|
||||
'name': nameCtrl.text.trim(),
|
||||
'type': selectedType,
|
||||
'number': numeroFinal,
|
||||
});
|
||||
}
|
||||
|
||||
print("--- 3. SUCESSO! FECHANDO DIÁLOGO ---");
|
||||
if (ctx.mounted) Navigator.pop(ctx);
|
||||
|
||||
} catch (e) {
|
||||
print("--- X. ERRO AO GUARDAR ---");
|
||||
print(e.toString());
|
||||
|
||||
// MOSTRA O ERRO NO TELEMÓVEL
|
||||
if (ctx.mounted) {
|
||||
ScaffoldMessenger.of(ctx).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text("Erro: $e"),
|
||||
backgroundColor: Colors.red,
|
||||
duration: const Duration(seconds: 4),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
child: const Text("Guardar", style: TextStyle(color: Colors.white)),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,80 +1,71 @@
|
||||
import 'dart:async';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
|
||||
class TeamController {
|
||||
// --- BASE DE DADOS LOCAL (Listas Estáticas) ---
|
||||
// Mantemos estático para que os dados persistam entre navegações de ecrãs
|
||||
static final List<Map<String, dynamic>> _teams = [];
|
||||
static final List<Map<String, dynamic>> _members = [];
|
||||
// Instância do cliente Supabase
|
||||
final _supabase = Supabase.instance.client;
|
||||
|
||||
static List<Map<String, dynamic>> get members => _members;
|
||||
|
||||
// StreamController broadcast para permitir múltiplos ouvintes (ex: Home e TeamsPage)
|
||||
final _streamController = StreamController<List<Map<String, dynamic>>>.broadcast();
|
||||
|
||||
// 1. STREAM
|
||||
// Retorna a lista atual mal alguém subscreve
|
||||
// 1. STREAM (Realtime)
|
||||
// Adicionei o .map() no final para garantir que o Dart entende que é uma List<Map>
|
||||
Stream<List<Map<String, dynamic>>> get teamsStream {
|
||||
_notifyListeners();
|
||||
return _streamController.stream;
|
||||
return _supabase
|
||||
.from('teams')
|
||||
.stream(primaryKey: ['id'])
|
||||
.order('name', ascending: true)
|
||||
.map((data) => List<Map<String, dynamic>>.from(data));
|
||||
}
|
||||
|
||||
// 2. CRIAR
|
||||
Future<void> createTeam(String name, String season, String imageUrl) async {
|
||||
await Future.delayed(const Duration(milliseconds: 100)); // Simula latência
|
||||
final newTeam = {
|
||||
'id': DateTime.now().millisecondsSinceEpoch.toString(),
|
||||
'name': name,
|
||||
'season': season,
|
||||
'image_url': imageUrl,
|
||||
'is_favorite': false, // Inicializa sempre como falso
|
||||
};
|
||||
_teams.add(newTeam);
|
||||
_notifyListeners();
|
||||
// Alterei imageUrl para String? (pode ser nulo) para evitar erros se não houver imagem
|
||||
Future<void> createTeam(String name, String season, String? imageUrl) async {
|
||||
try {
|
||||
await _supabase.from('teams').insert({
|
||||
'name': name,
|
||||
'season': season,
|
||||
'image_url': imageUrl,
|
||||
'is_favorite': false,
|
||||
});
|
||||
print("✅ Equipa guardada no Supabase!");
|
||||
} catch (e) {
|
||||
print("❌ Erro ao criar: $e");
|
||||
}
|
||||
}
|
||||
|
||||
// 3. ELIMINAR
|
||||
Future<void> deleteTeam(String id) async {
|
||||
_teams.removeWhere((team) => team['id'] == id);
|
||||
_members.removeWhere((member) => member['team_id'] == id);
|
||||
_notifyListeners();
|
||||
try {
|
||||
await _supabase.from('teams').delete().eq('id', id);
|
||||
} catch (e) {
|
||||
print("❌ Erro ao eliminar: $e");
|
||||
}
|
||||
}
|
||||
|
||||
// 4. FAVORITAR
|
||||
Future<void> toggleFavorite(String teamId) async {
|
||||
final index = _teams.indexWhere((t) => t['id'] == teamId);
|
||||
if (index != -1) {
|
||||
// Inverte o valor booleano (trata null como false)
|
||||
final bool currentStatus = _teams[index]['is_favorite'] ?? false;
|
||||
_teams[index]['is_favorite'] = !currentStatus;
|
||||
_notifyListeners();
|
||||
Future<void> toggleFavorite(String teamId, bool currentStatus) async {
|
||||
try {
|
||||
await _supabase
|
||||
.from('teams')
|
||||
.update({'is_favorite': !currentStatus}) // Inverte o valor
|
||||
.eq('id', teamId);
|
||||
} catch (e) {
|
||||
print("❌ Erro ao favoritar: $e");
|
||||
}
|
||||
}
|
||||
|
||||
// 5. CONTAR JOGADORES
|
||||
// CORRIGIDO: A sintaxe antiga dava erro. O método .count() é o correto agora.
|
||||
Future<int> getPlayerCount(String teamId) async {
|
||||
return _members.where((m) => m['team_id'] == teamId).length;
|
||||
try {
|
||||
final count = await _supabase
|
||||
.from('members')
|
||||
.count() // Retorna diretamente o número inteiro
|
||||
.eq('team_id', teamId);
|
||||
return count;
|
||||
} catch (e) {
|
||||
print("Erro ao contar jogadores: $e");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 6. NOTIFICAR E ORDENAR (Única versão corrigida)
|
||||
void _notifyListeners() {
|
||||
if (_streamController.isClosed) return;
|
||||
|
||||
// Ordenação: 1º Favoritos, 2º Nome (Alfabético)
|
||||
_teams.sort((a, b) {
|
||||
final bool favA = a['is_favorite'] ?? false;
|
||||
final bool favB = b['is_favorite'] ?? false;
|
||||
|
||||
if (favA == favB) {
|
||||
return (a['name'] as String).compareTo(b['name'] as String);
|
||||
}
|
||||
return favB ? 1 : -1; // b (favorito) vem antes de a
|
||||
});
|
||||
|
||||
// Enviamos uma CÓPIA da lista (List.from) para garantir que o StreamBuilder detecte a mudança
|
||||
_streamController.add(List.from(_teams));
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_streamController.close();
|
||||
}
|
||||
// Mantemos o dispose vazio para não quebrar a chamada na TeamsPage
|
||||
void dispose() {}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
// TODO Implement this library.
|
||||
Reference in New Issue
Block a user