Posible mejora en el multidispositivo
This commit is contained in:
@@ -5,7 +5,10 @@ import 'package:farolero/modelos/inicio_partida_multijugador.dart';
|
||||
import 'package:farolero/modelos/jugador.dart';
|
||||
import 'package:farolero/pantallas/pantalla_notas_online.dart';
|
||||
import 'package:farolero/pantallas/pantalla_revision_palabra.dart';
|
||||
import 'package:farolero/pantallas/pantalla_votacion_cliente.dart';
|
||||
import 'package:farolero/servicios/servicio_nearby.dart';
|
||||
import 'package:farolero/tema/tema_app.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
/// Pantalla que ve el jugador durante la fase de debate (multidispositivo).
|
||||
/// El cliente recibe el cambio de fase via Nearby y se navega aquí.
|
||||
@@ -37,10 +40,36 @@ class _PantallaDebateClienteState extends State<PantallaDebateCliente> {
|
||||
Timer? _timer;
|
||||
int _segundosRestantes = 0;
|
||||
bool _votacionSolicitada = false;
|
||||
OnMensajeCallback? _listener;
|
||||
ServicioNearby? _nearby;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_listener = (endpointId, mensaje) {
|
||||
if (!mounted || mensaje.tipo != TipoMensaje.fase) return;
|
||||
final fase = mensaje.datos['fase'] as String?;
|
||||
if (fase == 'votacion') {
|
||||
Navigator.of(context).pushReplacement(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => PantallaVotacionCliente(
|
||||
jugadores: widget.jugadores,
|
||||
jugadoresControlados: widget.jugadoresControlados,
|
||||
partidaId: widget.partidaId,
|
||||
pistaCategoria: widget.pistaCategoria,
|
||||
onVotos: _enviarVotos,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
};
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
final listener = _listener;
|
||||
if (listener != null && mounted) {
|
||||
_nearby = context.read<ServicioNearby>();
|
||||
_nearby!.onMensaje(listener);
|
||||
}
|
||||
});
|
||||
if (widget.tiempoDebateSegundos != null) {
|
||||
_segundosRestantes = widget.tiempoDebateSegundos!;
|
||||
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
||||
@@ -56,9 +85,31 @@ class _PantallaDebateClienteState extends State<PantallaDebateCliente> {
|
||||
@override
|
||||
void dispose() {
|
||||
_timer?.cancel();
|
||||
final listener = _listener;
|
||||
if (listener != null) {
|
||||
_nearby?.removeMensajeListener(listener);
|
||||
}
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _enviarVotos(Map<String, String> votos) {
|
||||
final nearby = context.read<ServicioNearby>();
|
||||
if (nearby.hostEndpointId == null) return;
|
||||
for (final entry in votos.entries) {
|
||||
nearby.enviarMensaje(
|
||||
nearby.hostEndpointId!,
|
||||
MensajeP2P(
|
||||
tipo: TipoMensaje.voto,
|
||||
datos: {
|
||||
'votanteId': entry.key,
|
||||
'votadoId': entry.value,
|
||||
'votoporId': entry.value,
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
String _formatearTiempo(int segundos) {
|
||||
final min = segundos ~/ 60;
|
||||
final seg = segundos % 60;
|
||||
|
||||
Reference in New Issue
Block a user