From 11cf157299cf90562d74c0b8ab1cf15dee50d98c Mon Sep 17 00:00:00 2001 From: Vincent Velociter Date: Mon, 25 Sep 2023 11:59:54 +0200 Subject: [PATCH] Add moves to TV and ensure it doesn't play if not in watch tab --- lib/src/app.dart | 2 ++ lib/src/model/tv/live_tv_channels.dart | 5 ++++- lib/src/model/tv/tv_ctrl.dart | 3 +++ lib/src/view/watch/live_tv_channels_screen.dart | 12 +++++------- lib/src/view/watch/tv_screen.dart | 17 ++++++++++++++--- lib/src/view/watch/watch_tab_screen.dart | 1 + 6 files changed, 29 insertions(+), 11 deletions(-) diff --git a/lib/src/app.dart b/lib/src/app.dart index 516e27b54b..07cbab6cf2 100644 --- a/lib/src/app.dart +++ b/lib/src/app.dart @@ -15,6 +15,7 @@ import 'package:lichess_mobile/src/model/settings/board_preferences.dart'; import 'package:lichess_mobile/src/utils/immersive_mode.dart'; import 'package:lichess_mobile/src/utils/wakelock.dart'; import 'package:lichess_mobile/src/utils/layout.dart'; +import 'package:lichess_mobile/src/view/watch/tv_screen.dart'; class App extends ConsumerStatefulWidget { const App({super.key}); @@ -90,6 +91,7 @@ class _AppState extends ConsumerState { navigatorObservers: [ immersiveModeRouteObserver, wakelockRouteObserver, + tvRouteObserver, ], ); } diff --git a/lib/src/model/tv/live_tv_channels.dart b/lib/src/model/tv/live_tv_channels.dart index 9314cd49ec..fbbd779623 100644 --- a/lib/src/model/tv/live_tv_channels.dart +++ b/lib/src/model/tv/live_tv_channels.dart @@ -16,14 +16,17 @@ import 'featured_player.dart'; part 'live_tv_channels.g.dart'; +typedef TvChannels = IMap; + @riverpod class LiveTvChannels extends _$LiveTvChannels { StreamSubscription? _socketSubscription; @override - Future> build() async { + Future build() async { ref.onDispose(() { _socketSubscription?.cancel(); + _socket.close(); }); return _doStartWatching(); diff --git a/lib/src/model/tv/tv_ctrl.dart b/lib/src/model/tv/tv_ctrl.dart index 3936f57fc1..7b61993bff 100644 --- a/lib/src/model/tv/tv_ctrl.dart +++ b/lib/src/model/tv/tv_ctrl.dart @@ -88,6 +88,7 @@ class TvCtrl extends _$TvCtrl { return TvCtrlState( game: fullEvent.game, + stepCursor: fullEvent.game.steps.length - 1, orientation: orientation, ); }); @@ -140,6 +141,7 @@ class TvCtrl extends _$TvCtrl { game: curState.game.copyWith( steps: curState.game.steps.add(newStep), ), + stepCursor: curState.stepCursor + 1, ); if (newState.game.clock != null && data.clock != null) { @@ -174,6 +176,7 @@ class TvCtrlState with _$TvCtrlState { const factory TvCtrlState({ required PlayableGame game, + required int stepCursor, required Side orientation, }) = _TvCtrlState; diff --git a/lib/src/view/watch/live_tv_channels_screen.dart b/lib/src/view/watch/live_tv_channels_screen.dart index cd36d0e668..06eb84c824 100644 --- a/lib/src/view/watch/live_tv_channels_screen.dart +++ b/lib/src/view/watch/live_tv_channels_screen.dart @@ -98,12 +98,6 @@ class _TvChannelsScreenState extends ConsumerState ref.read(liveTvChannelsProvider.notifier).startWatching(); super.didPopNext(); } - - @override - void didPop() { - ref.read(authSocketProvider).close(); - super.didPop(); - } } class _Body extends ConsumerWidget { @@ -111,7 +105,10 @@ class _Body extends ConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { - final gamesAsync = ref.watch(liveTvChannelsProvider); + final currentBottomTab = ref.watch(currentBottomTabProvider); + final gamesAsync = currentBottomTab == BottomTab.watch + ? ref.watch(liveTvChannelsProvider) + : const AsyncLoading(); return gamesAsync.when( data: (games) { final list = [ @@ -130,6 +127,7 @@ class _Body extends ConsumerWidget { channel: game.channel, initialGame: (game.id, game.orientation), ), + rootNavigator: true, ); }, orientation: game.orientation.cg, diff --git a/lib/src/view/watch/tv_screen.dart b/lib/src/view/watch/tv_screen.dart index a105c7a619..1c88f84403 100644 --- a/lib/src/view/watch/tv_screen.dart +++ b/lib/src/view/watch/tv_screen.dart @@ -17,6 +17,9 @@ import 'package:lichess_mobile/src/model/tv/tv_channel.dart'; import 'package:lichess_mobile/src/model/tv/tv_ctrl.dart'; import 'package:lichess_mobile/src/view/settings/toggle_sound_button.dart'; +final RouteObserver> tvRouteObserver = + RouteObserver>(); + class TvScreen extends ConsumerStatefulWidget { const TvScreen({required this.channel, this.initialGame, super.key}); @@ -87,14 +90,14 @@ class _TvScreenState extends ConsumerState super.didChangeDependencies(); final route = ModalRoute.of(context); if (route != null && route is PageRoute) { - watchTabRouteObserver.subscribe(this, route); + tvRouteObserver.subscribe(this, route); } } @override void dispose() { WidgetsBinding.instance.removeObserver(this); - watchTabRouteObserver.unsubscribe(this); + tvRouteObserver.unsubscribe(this); super.dispose(); } @@ -130,7 +133,10 @@ class _Body extends ConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { - final asyncGame = ref.watch(tvCtrlProvider(channel, initialGame)); + final currentBottomTab = ref.watch(currentBottomTabProvider); + final asyncGame = currentBottomTab == BottomTab.watch + ? ref.watch(tvCtrlProvider(channel, initialGame)) + : const AsyncLoading(); return SafeArea( child: Center( @@ -176,6 +182,11 @@ class _Body extends ConsumerWidget { bottomTable: gameState.orientation == Side.white ? whitePlayerWidget : blackPlayerWidget, + moves: game.steps + .skip(1) + .map((e) => e.sanMove!.san) + .toList(growable: false), + currentMoveIndex: gameState.stepCursor, ); }, loading: () => const BoardTable( diff --git a/lib/src/view/watch/watch_tab_screen.dart b/lib/src/view/watch/watch_tab_screen.dart index 040c9bd530..40f8b48138 100644 --- a/lib/src/view/watch/watch_tab_screen.dart +++ b/lib/src/view/watch/watch_tab_screen.dart @@ -184,6 +184,7 @@ class _WatchTvWidget extends ConsumerWidget { pushPlatformRoute( context, builder: (context) => const TvScreen(channel: TvChannel.best), + rootNavigator: true, ).then((_) { ref.invalidate(tvBestSnapshotProvider); });