fazer a tela de jogo ta tudo no PlacarPage
This commit is contained in:
@@ -1,25 +1,40 @@
|
||||
import 'dart:async';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
import '../models/game_model.dart';
|
||||
|
||||
class GameController {
|
||||
final List<Game> _games = [];
|
||||
final _gameStreamController = StreamController<List<Game>>.broadcast();
|
||||
final _supabase = Supabase.instance.client;
|
||||
|
||||
Stream<List<Game>> get gamesStream => _gameStreamController.stream;
|
||||
// 1. LER JOGOS (Stream em Tempo Real)
|
||||
Stream<List<Game>> get gamesStream {
|
||||
return _supabase
|
||||
.from('games')
|
||||
.stream(primaryKey: ['id'])
|
||||
.order('game_date', ascending: false) // Mais recentes primeiro
|
||||
.map((data) => data.map((json) => Game.fromMap(json)).toList());
|
||||
}
|
||||
|
||||
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));
|
||||
// 2. CRIAR JOGO
|
||||
// Retorna o ID do jogo criado para podermos navegar para o placar
|
||||
Future<String?> createGame(String myTeam, String opponent, String season) async {
|
||||
try {
|
||||
final response = await _supabase.from('games').insert({
|
||||
'my_team': myTeam,
|
||||
'opponent_team': opponent,
|
||||
'season': season,
|
||||
'my_score': 0,
|
||||
'opponent_score': 0,
|
||||
'status': 'Decorrer', // Começa como "Decorrer"
|
||||
'game_date': DateTime.now().toIso8601String(),
|
||||
}).select().single(); // .select().single() retorna o objeto criado
|
||||
|
||||
return response['id']; // Retorna o UUID gerado pelo Supabase
|
||||
} catch (e) {
|
||||
print("Erro ao criar jogo: $e");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_gameStreamController.close();
|
||||
// Não é necessário fechar streams do Supabase manualmente aqui
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user