Skip to content

Commit

Permalink
Add moves to TV and ensure it doesn't play if not in watch tab
Browse files Browse the repository at this point in the history
  • Loading branch information
veloce committed Sep 25, 2023
1 parent f0f34c9 commit 11cf157
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 11 deletions.
2 changes: 2 additions & 0 deletions lib/src/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down Expand Up @@ -90,6 +91,7 @@ class _AppState extends ConsumerState<App> {
navigatorObservers: [
immersiveModeRouteObserver,
wakelockRouteObserver,
tvRouteObserver,
],
);
}
Expand Down
5 changes: 4 additions & 1 deletion lib/src/model/tv/live_tv_channels.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ import 'featured_player.dart';

part 'live_tv_channels.g.dart';

typedef TvChannels = IMap<TvChannel, TvGameSnapshot>;

@riverpod
class LiveTvChannels extends _$LiveTvChannels {
StreamSubscription<SocketEvent>? _socketSubscription;

@override
Future<IMap<TvChannel, TvGameSnapshot>> build() async {
Future<TvChannels> build() async {
ref.onDispose(() {
_socketSubscription?.cancel();
_socket.close();
});

return _doStartWatching();
Expand Down
3 changes: 3 additions & 0 deletions lib/src/model/tv/tv_ctrl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class TvCtrl extends _$TvCtrl {

return TvCtrlState(
game: fullEvent.game,
stepCursor: fullEvent.game.steps.length - 1,
orientation: orientation,
);
});
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -174,6 +176,7 @@ class TvCtrlState with _$TvCtrlState {

const factory TvCtrlState({
required PlayableGame game,
required int stepCursor,
required Side orientation,
}) = _TvCtrlState;

Expand Down
12 changes: 5 additions & 7 deletions lib/src/view/watch/live_tv_channels_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,17 @@ class _TvChannelsScreenState extends ConsumerState<LiveTvChannelsScreen>
ref.read(liveTvChannelsProvider.notifier).startWatching();
super.didPopNext();
}

@override
void didPop() {
ref.read(authSocketProvider).close();
super.didPop();
}
}

class _Body extends ConsumerWidget {
const _Body();

@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<TvChannels>();
return gamesAsync.when(
data: (games) {
final list = [
Expand All @@ -130,6 +127,7 @@ class _Body extends ConsumerWidget {
channel: game.channel,
initialGame: (game.id, game.orientation),
),
rootNavigator: true,
);
},
orientation: game.orientation.cg,
Expand Down
17 changes: 14 additions & 3 deletions lib/src/view/watch/tv_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<PageRoute<void>> tvRouteObserver =
RouteObserver<PageRoute<void>>();

class TvScreen extends ConsumerStatefulWidget {
const TvScreen({required this.channel, this.initialGame, super.key});

Expand Down Expand Up @@ -87,14 +90,14 @@ class _TvScreenState extends ConsumerState<TvScreen>
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();
}

Expand Down Expand Up @@ -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<TvCtrlState>();

return SafeArea(
child: Center(
Expand Down Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions lib/src/view/watch/watch_tab_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ class _WatchTvWidget extends ConsumerWidget {
pushPlatformRoute(
context,
builder: (context) => const TvScreen(channel: TvChannel.best),
rootNavigator: true,
).then((_) {
ref.invalidate(tvBestSnapshotProvider);
});
Expand Down

0 comments on commit 11cf157

Please sign in to comment.