Skip to content

Commit

Permalink
Merge pull request #626 from lichess-org/home_tab_improvements
Browse files Browse the repository at this point in the history
Home tab improvements
  • Loading branch information
veloce authored Apr 11, 2024
2 parents 90a9077 + a1357da commit 44aab26
Show file tree
Hide file tree
Showing 7 changed files with 586 additions and 798 deletions.
13 changes: 6 additions & 7 deletions lib/src/model/lobby/game_seek.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,16 @@ class GameSeek with _$GameSeek {
/// Construct a correspondence seek from saved [GameSetup].
factory GameSeek.correspondence(GameSetup setup, User? account) {
return GameSeek(
days: setup.correspondenceDaysPerTurn,
rated: account != null && setup.correspondenceRated,
variant: setup.correspondenceVariant,
side: setup.correspondenceRated == true ||
setup.correspondenceSide == PlayableSide.random
days: setup.customDaysPerTurn,
rated: account != null && setup.customRated,
variant: setup.customVariant,
side: setup.customRated == true || setup.customSide == PlayableSide.random
? null
: setup.correspondenceSide == PlayableSide.white
: setup.customSide == PlayableSide.white
? Side.white
: Side.black,
ratingRange:
account != null ? setup.ratingRangeFromCorrespondence(account) : null,
account != null ? setup.ratingRangeFromCustom(account) : null,
);
}

Expand Down
57 changes: 15 additions & 42 deletions lib/src/model/lobby/game_setup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ enum PlayableSide { random, white, black }

enum SeekMode { fast, custom }

enum TimeControl { realTime, correspondence }

@Freezed(fromJson: true, toJson: true)
class GameSetup with _$GameSetup {
const GameSetup._();
Expand All @@ -26,38 +28,32 @@ class GameSetup with _$GameSetup {
// fast game
required TimeIncrement timeIncrement,

// custom time control choice
required TimeControl customTimeControl,

// custom game
required int customTimeSeconds,
required int customIncrementSeconds,
int? customDaysPerTurn,
required Variant customVariant,
required bool customRated,
required PlayableSide customSide,
required (int, int) customRatingDelta,

// correspondence game
int? correspondenceDaysPerTurn,
required Variant correspondenceVariant,
required bool correspondenceRated,
required PlayableSide correspondenceSide,
required (int, int) correspondenceRatingDelta,

// prefered seek mode, set after a seek is made
required SeekMode seekMode,
}) = _GameSetup;

static const defaults = GameSetup(
timeIncrement: TimeIncrement(600, 0),
customTimeControl: TimeControl.realTime,
customTimeSeconds: 180,
customIncrementSeconds: 0,
customVariant: Variant.standard,
customRated: false,
customSide: PlayableSide.random,
customRatingDelta: (-500, 500),
correspondenceDaysPerTurn: 3,
correspondenceVariant: Variant.standard,
correspondenceRated: false,
correspondenceSide: PlayableSide.random,
correspondenceRatingDelta: (-500, 500),
customDaysPerTurn: 3,
seekMode: SeekMode.fast,
);

Expand All @@ -84,17 +80,6 @@ class GameSetup with _$GameSetup {
return (min, max);
}

/// Returns the rating range for the correspondence setup, or null if the user
/// doesn't have a rating for the correspondence setup perf.
(int, int)? ratingRangeFromCorrespondence(User user) {
final perf = user.perfs[Perf.correspondence];
if (perf == null) return null;
if (perf.provisional == true) return null;
final min = math.max(0, perf.rating + correspondenceRatingDelta.$1);
final max = perf.rating + correspondenceRatingDelta.$2;
return (min, max);
}

factory GameSetup.fromJson(Map<String, dynamic> json) {
try {
return _$GameSetupFromJson(json);
Expand All @@ -121,10 +106,14 @@ class GameSetupPreferences extends _$GameSetupPreferences {
: GameSetup.defaults;
}

Future<void> setTimeControl(TimeIncrement timeInc) {
Future<void> setTimeIncrement(TimeIncrement timeInc) {
return _save(state.copyWith(timeIncrement: timeInc));
}

Future<void> setCustomTimeControl(TimeControl control) {
return _save(state.copyWith(customTimeControl: control));
}

Future<void> setCustomTimeSeconds(int seconds) {
return _save(state.copyWith(customTimeSeconds: seconds));
}
Expand Down Expand Up @@ -153,24 +142,8 @@ class GameSetupPreferences extends _$GameSetupPreferences {
return _save(state.copyWith(customRatingDelta: (min, max)));
}

Future<void> setCorrespondenceDaysPerTurn(int? days) {
return _save(state.copyWith(correspondenceDaysPerTurn: days));
}

Future<void> setCorrespondenceVariant(Variant variant) {
return _save(state.copyWith(correspondenceVariant: variant));
}

Future<void> setCorrespondenceRated(bool rated) {
return _save(state.copyWith(correspondenceRated: rated));
}

Future<void> setCorrespondenceSide(PlayableSide side) {
return _save(state.copyWith(correspondenceSide: side));
}

Future<void> setCorrespondenceRatingRange(int min, int max) {
return _save(state.copyWith(correspondenceRatingDelta: (min, max)));
Future<void> setCustomDaysPerTurn(int? days) {
return _save(state.copyWith(customDaysPerTurn: days));
}

Future<void> _save(GameSetup newState) async {
Expand Down
89 changes: 14 additions & 75 deletions lib/src/view/home/home_tab_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ import 'package:lichess_mobile/src/model/correspondence/correspondence_game_stor
import 'package:lichess_mobile/src/model/lobby/game_seek.dart';
import 'package:lichess_mobile/src/model/lobby/game_setup.dart';
import 'package:lichess_mobile/src/navigation.dart';
import 'package:lichess_mobile/src/styles/lichess_icons.dart';
import 'package:lichess_mobile/src/styles/styles.dart';
import 'package:lichess_mobile/src/utils/connectivity.dart';
import 'package:lichess_mobile/src/utils/l10n_context.dart';
import 'package:lichess_mobile/src/utils/layout.dart';
import 'package:lichess_mobile/src/utils/navigation.dart';
import 'package:lichess_mobile/src/view/account/profile_screen.dart';
import 'package:lichess_mobile/src/view/game/lobby_screen.dart';
import 'package:lichess_mobile/src/view/play/create_correspondence_game_screen.dart';
import 'package:lichess_mobile/src/view/play/create_custom_game_screen.dart';
import 'package:lichess_mobile/src/view/play/offline_correspondence_games_screen.dart';
import 'package:lichess_mobile/src/view/play/ongoing_games_screen.dart';
Expand Down Expand Up @@ -220,7 +218,7 @@ class _HomeBody extends ConsumerWidget {
child: Column(
children: [
SizedBox(height: 8.0),
_CreateAGameSection(isExpanded: true),
_CreateAGameSection(),
_OngoingGamesPreview(maxGamesToShow: 4),
],
),
Expand All @@ -244,7 +242,7 @@ class _HomeBody extends ConsumerWidget {
if (Theme.of(context).platform == TargetPlatform.android)
const _SignInWidget(),
const _HelloWidget(),
const _CreateAGameSection(isExpanded: false),
const _CreateAGameSection(),
const _OngoingGamesPreview(maxGamesToShow: 4),
const RecentGames(),
];
Expand Down Expand Up @@ -309,11 +307,12 @@ class _SignInWidget extends ConsumerWidget {
alignment: Alignment.centerLeft,
child: Padding(
padding: Styles.bodySectionPadding,
child: NoPaddingTextButton(
child: FilledButton.tonalIcon(
icon: const Icon(Icons.account_circle),
onPressed: authController.isLoading
? null
: () => ref.read(authControllerProvider.notifier).signIn(),
child: Text(
label: Text(
context.l10n.signIn.toUpperCase(),
),
),
Expand Down Expand Up @@ -379,16 +378,11 @@ class _HelloWidget extends ConsumerWidget {
}
}

class _CreateAGameSection extends ConsumerWidget {
const _CreateAGameSection({this.isExpanded = false});

final bool isExpanded;
class _CreateAGameSection extends StatelessWidget {
const _CreateAGameSection();

@override
Widget build(BuildContext context, WidgetRef ref) {
final expansionTileColor = Styles.expansionTileColor(context);
final session = ref.watch(authSessionProvider);

Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 16.0),
child: Column(
Expand All @@ -399,41 +393,19 @@ class _CreateAGameSection extends ConsumerWidget {
child: Text(context.l10n.createAGame, style: Styles.sectionTitle),
),
const _QuickGameButton(),
if (isExpanded) ...[
const SizedBox(height: 16.0),
const _CustomGameButton(),
if (session != null) const _CorrespondenceGameButton(),
] else
Theme(
data:
Theme.of(context).copyWith(dividerColor: Colors.transparent),
child: ExpansionTile(
title: Text(
context.l10n.more,
),
tilePadding: Styles.horizontalBodyPadding,
iconColor: expansionTileColor,
collapsedIconColor: expansionTileColor,
textColor: expansionTileColor,
collapsedTextColor: expansionTileColor,
controlAffinity: ListTileControlAffinity.leading,
children: [
const _CustomGameButton(),
if (session != null) const _CorrespondenceGameButton(),
],
),
),
const SizedBox(height: 16.0),
const _CustomGameButton(),
],
),
);
}
}

class _CustomGameButton extends StatelessWidget {
class _CustomGameButton extends ConsumerWidget {
const _CustomGameButton();

@override
Widget build(BuildContext context) {
Widget build(BuildContext context, WidgetRef ref) {
return Padding(
padding: Styles.bodySectionBottomPadding,
child: CardButton(
Expand All @@ -449,6 +421,7 @@ class _CustomGameButton extends StatelessWidget {
],
),
onTap: () {
ref.invalidate(accountProvider);
pushPlatformRoute(
context,
title: context.l10n.custom,
Expand All @@ -460,40 +433,6 @@ class _CustomGameButton extends StatelessWidget {
}
}

class _CorrespondenceGameButton extends StatelessWidget {
const _CorrespondenceGameButton();

@override
Widget build(BuildContext context) {
return Padding(
padding: Styles.bodySectionBottomPadding,
child: CardButton(
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
LichessIcons.correspondence,
size: 28,
),
const SizedBox(width: 8.0),
Text(
context.l10n.correspondence,
style: Styles.callout,
),
],
),
onTap: () {
pushPlatformRoute(
context,
title: context.l10n.correspondence,
builder: (_) => const CreateCorrespondenceGameScreen(),
);
},
),
);
}
}

class _QuickGameButton extends ConsumerWidget {
const _QuickGameButton();

Expand Down Expand Up @@ -562,7 +501,7 @@ class _QuickGameButton extends ConsumerWidget {
onSelected: (choice) {
ref
.read(gameSetupPreferencesProvider.notifier)
.setTimeControl(choice);
.setTimeIncrement(choice);
},
);
},
Expand Down
Loading

0 comments on commit 44aab26

Please sign in to comment.