From d8ab907cfb5db7a920144b9c5369a884aed1aa77 Mon Sep 17 00:00:00 2001 From: Vincent Velociter Date: Sat, 7 Dec 2024 11:04:39 +0100 Subject: [PATCH] Resync broadcast state on app resume Fixes #1222 --- lib/src/model/broadcast/broadcast_game_controller.dart | 9 +++++++++ lib/src/model/broadcast/broadcast_round_controller.dart | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/src/model/broadcast/broadcast_game_controller.dart b/lib/src/model/broadcast/broadcast_game_controller.dart index 5a9834613f..2addb7111a 100644 --- a/lib/src/model/broadcast/broadcast_game_controller.dart +++ b/lib/src/model/broadcast/broadcast_game_controller.dart @@ -3,6 +3,7 @@ import 'dart:async'; import 'package:dartchess/dartchess.dart'; import 'package:deep_pick/deep_pick.dart'; import 'package:fast_immutable_collections/fast_immutable_collections.dart'; +import 'package:flutter/widgets.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:lichess_mobile/src/model/analysis/analysis_controller.dart'; import 'package:lichess_mobile/src/model/analysis/analysis_preferences.dart'; @@ -33,6 +34,7 @@ class BroadcastGameController extends _$BroadcastGameController static Uri broadcastSocketUri(BroadcastRoundId broadcastRoundId) => Uri(path: 'study/$broadcastRoundId/socket/v6'); + AppLifecycleListener? _appLifecycleListener; StreamSubscription? _subscription; late SocketClient _socketClient; @@ -55,11 +57,18 @@ class BroadcastGameController extends _$BroadcastGameController final evaluationService = ref.watch(evaluationServiceProvider); + _appLifecycleListener = AppLifecycleListener( + onResume: () { + ref.invalidateSelf(); + }, + ); + ref.onDispose(() { _subscription?.cancel(); _startEngineEvalTimer?.cancel(); _engineEvalDebounce.dispose(); evaluationService.disposeEngine(); + _appLifecycleListener?.dispose(); }); final pgn = await ref.withClient( diff --git a/lib/src/model/broadcast/broadcast_round_controller.dart b/lib/src/model/broadcast/broadcast_round_controller.dart index 3ead272233..7f68bc5eb1 100644 --- a/lib/src/model/broadcast/broadcast_round_controller.dart +++ b/lib/src/model/broadcast/broadcast_round_controller.dart @@ -3,6 +3,7 @@ import 'dart:async'; import 'package:dartchess/dartchess.dart'; import 'package:deep_pick/deep_pick.dart'; import 'package:fast_immutable_collections/fast_immutable_collections.dart'; +import 'package:flutter/widgets.dart'; import 'package:lichess_mobile/src/model/broadcast/broadcast.dart'; import 'package:lichess_mobile/src/model/broadcast/broadcast_repository.dart'; import 'package:lichess_mobile/src/model/common/chess.dart'; @@ -21,6 +22,7 @@ class BroadcastRoundController extends _$BroadcastRoundController { Uri(path: 'study/$broadcastRoundId/socket/v6'); StreamSubscription? _subscription; + AppLifecycleListener? _appLifecycleListener; late SocketClient _socketClient; @@ -34,8 +36,15 @@ class BroadcastRoundController extends _$BroadcastRoundController { _subscription = _socketClient.stream.listen(_handleSocketEvent); + _appLifecycleListener = AppLifecycleListener( + onResume: () { + ref.invalidateSelf(); + }, + ); + ref.onDispose(() { _subscription?.cancel(); + _appLifecycleListener?.dispose(); }); final round = await ref.withClient(