Skip to content

Commit

Permalink
Rename controller for more clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
veloce committed Jan 12, 2025
1 parent 60ae857 commit be7ddf0
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ import 'package:lichess_mobile/src/view/engine/engine_gauge.dart';
import 'package:lichess_mobile/src/widgets/pgn.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'broadcast_game_controller.freezed.dart';
part 'broadcast_game_controller.g.dart';
part 'broadcast_analysis_controller.freezed.dart';
part 'broadcast_analysis_controller.g.dart';

@riverpod
class BroadcastGameController extends _$BroadcastGameController implements PgnTreeNotifier {
class BroadcastAnalysisController extends _$BroadcastAnalysisController implements PgnTreeNotifier {
static Uri broadcastSocketUri(BroadcastRoundId broadcastRoundId) =>
Uri(path: 'study/$broadcastRoundId/socket/v6');

Expand All @@ -48,10 +48,10 @@ class BroadcastGameController extends _$BroadcastGameController implements PgnTr
Object? _key = Object();

@override
Future<BroadcastGameState> build(BroadcastRoundId roundId, BroadcastGameId gameId) async {
Future<BroadcastAnalysisState> build(BroadcastRoundId roundId, BroadcastGameId gameId) async {
_socketClient = ref
.watch(socketPoolProvider)
.open(BroadcastGameController.broadcastSocketUri(roundId));
.open(BroadcastAnalysisController.broadcastSocketUri(roundId));

_subscription = _socketClient.stream.listen(_handleSocketEvent);

Expand Down Expand Up @@ -104,7 +104,7 @@ class BroadcastGameController extends _$BroadcastGameController implements PgnTr
// don't use ref.watch here: we don't want to invalidate state when the
// analysis preferences change
final prefs = ref.read(analysisPreferencesProvider);
final broadcastState = BroadcastGameState(
final broadcastState = BroadcastAnalysisState(
id: gameId,
currentPath: currentPath,
broadcastPath: currentPath,
Expand Down Expand Up @@ -560,10 +560,10 @@ class BroadcastGameController extends _$BroadcastGameController implements PgnTr
}

@freezed
class BroadcastGameState with _$BroadcastGameState {
const BroadcastGameState._();
class BroadcastAnalysisState with _$BroadcastAnalysisState {
const BroadcastAnalysisState._();

const factory BroadcastGameState({
const factory BroadcastAnalysisState({
/// Broadcast game ID
required StringId id,

Expand Down
18 changes: 9 additions & 9 deletions lib/src/view/broadcast/broadcast_game_bottom_bar.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lichess_mobile/src/model/broadcast/broadcast_game_controller.dart';
import 'package:lichess_mobile/src/model/broadcast/broadcast_analysis_controller.dart';
import 'package:lichess_mobile/src/model/broadcast/broadcast_repository.dart';
import 'package:lichess_mobile/src/model/common/id.dart';
import 'package:lichess_mobile/src/model/game/game_share_service.dart';
Expand Down Expand Up @@ -29,8 +29,8 @@ class BroadcastGameBottomBar extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
final ctrlProvider = broadcastGameControllerProvider(roundId, gameId);
final broadcastGameState = ref.watch(ctrlProvider).requireValue;
final ctrlProvider = broadcastAnalysisControllerProvider(roundId, gameId);
final broadcastAnalysisState = ref.watch(ctrlProvider).requireValue;

return BottomBar(
children: [
Expand Down Expand Up @@ -107,22 +107,22 @@ class BroadcastGameBottomBar extends ConsumerWidget {
icon: CupertinoIcons.arrow_2_squarepath,
),
RepeatButton(
onLongPress: broadcastGameState.canGoBack ? () => _moveBackward(ref) : null,
onLongPress: broadcastAnalysisState.canGoBack ? () => _moveBackward(ref) : null,
child: BottomBarButton(
key: const ValueKey('goto-previous'),
onTap: broadcastGameState.canGoBack ? () => _moveBackward(ref) : null,
onTap: broadcastAnalysisState.canGoBack ? () => _moveBackward(ref) : null,
label: 'Previous',
icon: CupertinoIcons.chevron_back,
showTooltip: false,
),
),
RepeatButton(
onLongPress: broadcastGameState.canGoNext ? () => _moveForward(ref) : null,
onLongPress: broadcastAnalysisState.canGoNext ? () => _moveForward(ref) : null,
child: BottomBarButton(
key: const ValueKey('goto-next'),
icon: CupertinoIcons.chevron_forward,
label: context.l10n.next,
onTap: broadcastGameState.canGoNext ? () => _moveForward(ref) : null,
onTap: broadcastAnalysisState.canGoNext ? () => _moveForward(ref) : null,
showTooltip: false,
),
),
Expand All @@ -131,7 +131,7 @@ class BroadcastGameBottomBar extends ConsumerWidget {
}

void _moveForward(WidgetRef ref) =>
ref.read(broadcastGameControllerProvider(roundId, gameId).notifier).userNext();
ref.read(broadcastAnalysisControllerProvider(roundId, gameId).notifier).userNext();
void _moveBackward(WidgetRef ref) =>
ref.read(broadcastGameControllerProvider(roundId, gameId).notifier).userPrevious();
ref.read(broadcastAnalysisControllerProvider(roundId, gameId).notifier).userPrevious();
}
67 changes: 44 additions & 23 deletions lib/src/view/broadcast/broadcast_game_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lichess_mobile/src/constants.dart';
import 'package:lichess_mobile/src/model/analysis/analysis_preferences.dart';
import 'package:lichess_mobile/src/model/broadcast/broadcast.dart';
import 'package:lichess_mobile/src/model/broadcast/broadcast_game_controller.dart';
import 'package:lichess_mobile/src/model/broadcast/broadcast_analysis_controller.dart';
import 'package:lichess_mobile/src/model/common/chess.dart';
import 'package:lichess_mobile/src/model/common/eval.dart';
import 'package:lichess_mobile/src/model/common/id.dart';
Expand All @@ -20,7 +20,6 @@ import 'package:lichess_mobile/src/view/analysis/analysis_layout.dart';
import 'package:lichess_mobile/src/view/broadcast/broadcast_game_bottom_bar.dart';
import 'package:lichess_mobile/src/view/broadcast/broadcast_game_screen_providers.dart';
import 'package:lichess_mobile/src/view/broadcast/broadcast_game_settings.dart';
import 'package:lichess_mobile/src/view/broadcast/broadcast_game_tree_view.dart';
import 'package:lichess_mobile/src/view/broadcast/broadcast_player_results_screen.dart';
import 'package:lichess_mobile/src/view/broadcast/broadcast_player_widget.dart';
import 'package:lichess_mobile/src/view/engine/engine_gauge.dart';
Expand Down Expand Up @@ -134,17 +133,15 @@ class _Body extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
final broadcastState = ref.watch(broadcastGameControllerProvider(roundId, gameId));

switch (broadcastState) {
case AsyncValue(value: final broadcastState?, hasValue: true):
switch (ref.watch(broadcastAnalysisControllerProvider(roundId, gameId))) {
case AsyncValue(value: final state?, hasValue: true):
final analysisPrefs = ref.watch(analysisPreferencesProvider);
final showEvaluationGauge = analysisPrefs.showEvaluationGauge;
final numEvalLines = analysisPrefs.numEvalLines;

final engineGaugeParams = broadcastState.engineGaugeParams;
final isLocalEvaluationEnabled = broadcastState.isLocalEvaluationEnabled;
final currentNode = broadcastState.currentNode;
final engineGaugeParams = state.engineGaugeParams;
final isLocalEvaluationEnabled = state.isLocalEvaluationEnabled;
final currentNode = state.currentNode;

return AnalysisLayout(
tabController: tabController,
Expand Down Expand Up @@ -188,7 +185,7 @@ class _Body extends ConsumerWidget {
isGameOver: currentNode.position.isGameOver,
onTapMove:
ref
.read(broadcastGameControllerProvider(roundId, gameId).notifier)
.read(broadcastAnalysisControllerProvider(roundId, gameId).notifier)
.onUserMove,
)
: null,
Expand All @@ -198,7 +195,7 @@ class _Body extends ConsumerWidget {
tournamentSlug: tournamentSlug,
roundSlug: roundSlug,
),
children: [_OpeningExplorerTab(roundId, gameId), BroadcastGameTreeView(roundId, gameId)],
children: [_OpeningExplorerTab(roundId, gameId), _BroadcastGameTreeView(roundId, gameId)],
);
case AsyncValue(:final error?):
return Center(child: Text('Cannot load broadcast game: $error'));
Expand All @@ -208,6 +205,32 @@ class _Body extends ConsumerWidget {
}
}

class _BroadcastGameTreeView extends ConsumerWidget {
const _BroadcastGameTreeView(this.roundId, this.gameId);

final BroadcastRoundId roundId;
final BroadcastGameId gameId;

@override
Widget build(BuildContext context, WidgetRef ref) {
final ctrlProvider = broadcastAnalysisControllerProvider(roundId, gameId);
final state = ref.watch(ctrlProvider).requireValue;

final analysisPrefs = ref.watch(analysisPreferencesProvider);

return SingleChildScrollView(
child: DebouncedPgnTreeView(
root: state.root,
currentPath: state.currentPath,
broadcastLivePath: state.broadcastLivePath,
pgnRootComments: state.pgnRootComments,
shouldShowAnnotations: analysisPrefs.showAnnotations,
notifier: ref.read(ctrlProvider.notifier),
),
);
}
}

class _OpeningExplorerTab extends ConsumerWidget {
const _OpeningExplorerTab(this.roundId, this.gameId);

Expand All @@ -216,7 +239,7 @@ class _OpeningExplorerTab extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
final ctrlProvider = broadcastGameControllerProvider(roundId, gameId);
final ctrlProvider = broadcastAnalysisControllerProvider(roundId, gameId);
final state = ref.watch(ctrlProvider).requireValue;

return OpeningExplorerView(
Expand All @@ -243,7 +266,7 @@ class _BroadcastBoardState extends ConsumerState<_BroadcastBoard> {

@override
Widget build(BuildContext context) {
final ctrlProvider = broadcastGameControllerProvider(widget.roundId, widget.gameId);
final ctrlProvider = broadcastAnalysisControllerProvider(widget.roundId, widget.gameId);
final broadcastAnalysisState = ref.watch(ctrlProvider).requireValue;
final boardPrefs = ref.watch(boardPreferencesProvider);
final analysisPrefs = ref.watch(analysisPreferencesProvider);
Expand Down Expand Up @@ -344,26 +367,24 @@ class _PlayerWidget extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
final game = ref.watch(broadcastRoundGameProvider(roundId, gameId));

switch (game) {
switch (ref.watch(broadcastRoundGameProvider(roundId, gameId))) {
case AsyncValue(value: final game?, hasValue: true):
final broadcastGameState =
ref.watch(broadcastGameControllerProvider(roundId, gameId)).requireValue;
final broadcastAnalysisState =
ref.watch(broadcastAnalysisControllerProvider(roundId, gameId)).requireValue;

final isCursorOnLiveMove =
broadcastGameState.currentPath == broadcastGameState.broadcastLivePath;
final sideToMove = broadcastGameState.position.turn;
broadcastAnalysisState.currentPath == broadcastAnalysisState.broadcastLivePath;
final sideToMove = broadcastAnalysisState.position.turn;
final side = switch (widgetPosition) {
_PlayerWidgetPosition.bottom => broadcastGameState.pov,
_PlayerWidgetPosition.top => broadcastGameState.pov.opposite,
_PlayerWidgetPosition.bottom => broadcastAnalysisState.pov,
_PlayerWidgetPosition.top => broadcastAnalysisState.pov.opposite,
};

final player = game.players[side]!;
final liveClock = isCursorOnLiveMove ? player.clock : null;
final gameStatus = game.status;

final pastClocks = broadcastGameState.clocks;
final pastClocks = broadcastAnalysisState.clocks;
final pastClock = (sideToMove == side) ? pastClocks?.parentClock : pastClocks?.clock;

return GestureDetector(
Expand Down
18 changes: 6 additions & 12 deletions lib/src/view/broadcast/broadcast_game_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lichess_mobile/src/model/analysis/analysis_preferences.dart';
import 'package:lichess_mobile/src/model/broadcast/broadcast_game_controller.dart';
import 'package:lichess_mobile/src/model/broadcast/broadcast_analysis_controller.dart';
import 'package:lichess_mobile/src/model/common/id.dart';
import 'package:lichess_mobile/src/model/settings/general_preferences.dart';
import 'package:lichess_mobile/src/utils/l10n_context.dart';
Expand All @@ -21,7 +21,7 @@ class BroadcastGameSettings extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
final broacdcastGameAnalysisController = broadcastGameControllerProvider(roundId, gameId);
final controller = broadcastAnalysisControllerProvider(roundId, gameId);

final analysisPrefs = ref.watch(analysisPreferencesProvider);
final isSoundEnabled = ref.watch(
Expand All @@ -33,17 +33,11 @@ class BroadcastGameSettings extends ConsumerWidget {
body: ListView(
children: [
StockfishSettingsWidget(
onToggleLocalEvaluation:
() => ref.read(broacdcastGameAnalysisController.notifier).toggleLocalEvaluation(),
onToggleLocalEvaluation: () => ref.read(controller.notifier).toggleLocalEvaluation(),
onSetEngineSearchTime:
(value) =>
ref.read(broacdcastGameAnalysisController.notifier).setEngineSearchTime(value),
onSetNumEvalLines:
(value) =>
ref.read(broacdcastGameAnalysisController.notifier).setNumEvalLines(value),
onSetEngineCores:
(value) =>
ref.read(broacdcastGameAnalysisController.notifier).setEngineCores(value),
(value) => ref.read(controller.notifier).setEngineSearchTime(value),
onSetNumEvalLines: (value) => ref.read(controller.notifier).setNumEvalLines(value),
onSetEngineCores: (value) => ref.read(controller.notifier).setEngineCores(value),
),
ListSection(
children: [
Expand Down
34 changes: 0 additions & 34 deletions lib/src/view/broadcast/broadcast_game_tree_view.dart

This file was deleted.

0 comments on commit be7ddf0

Please sign in to comment.