Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions lib/src/model/game/game_history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:lichess_mobile/src/model/account/account_repository.dart';
import 'package:lichess_mobile/src/model/auth/auth_session.dart';
import 'package:lichess_mobile/src/model/common/http.dart';
import 'package:lichess_mobile/src/model/common/id.dart';
import 'package:lichess_mobile/src/model/common/perf.dart';
import 'package:lichess_mobile/src/model/game/archived_game.dart';
import 'package:lichess_mobile/src/model/game/game_repository.dart';
import 'package:lichess_mobile/src/model/game/game_storage.dart';
Expand Down Expand Up @@ -63,9 +64,10 @@ Future<IList<LightArchivedGameWithPov>> myRecentGames(
Future<IList<LightArchivedGameWithPov>> userRecentGames(
UserRecentGamesRef ref, {
required UserId userId,
Perf? perf,
}) {
return ref.withClientCacheFor(
(client) => GameRepository(client).getUserGames(userId),
(client) => GameRepository(client).getUserGames(userId, perfType: perf),
// cache is important because the associated widget is in a [ListView] and
// the provider may be instanciated multiple times in a short period of time
// (e.g. when scrolling)
Expand Down Expand Up @@ -114,6 +116,7 @@ class UserGameHistory extends _$UserGameHistory {
/// server. If this is false, the provider will fetch the games from the
/// local storage.
required bool isOnline,
Perf? perf,
}) async {
ref.cacheFor(const Duration(minutes: 5));
ref.onDispose(() {
Expand All @@ -123,7 +126,12 @@ class UserGameHistory extends _$UserGameHistory {
final session = ref.watch(authSessionProvider);

final recentGames = userId != null
? ref.read(userRecentGamesProvider(userId: userId).future)
? ref.read(
userRecentGamesProvider(
userId: userId,
perf: perf,
).future,
)
: ref.read(myRecentGamesProvider.future);

_list.addAll(await recentGames);
Expand All @@ -134,6 +142,7 @@ class UserGameHistory extends _$UserGameHistory {
hasMore: true,
hasError: false,
online: isOnline,
perfType: perf,
session: session,
);
}
Expand All @@ -151,6 +160,7 @@ class UserGameHistory extends _$UserGameHistory {
userId!,
max: _nbPerPage,
until: _list.last.game.createdAt,
perfType: currentVal.perfType,
),
)
: currentVal.online && currentVal.session != null
Expand All @@ -159,6 +169,7 @@ class UserGameHistory extends _$UserGameHistory {
currentVal.session!.user.id,
max: _nbPerPage,
until: _list.last.game.createdAt,
perfType: currentVal.perfType,
),
)
: ref
Expand Down Expand Up @@ -208,6 +219,7 @@ class UserGameHistoryState with _$UserGameHistoryState {
const factory UserGameHistoryState({
required IList<LightArchivedGameWithPov> gameList,
required bool isLoading,
Perf? perfType,
required bool hasMore,
required bool hasError,
required bool online,
Expand Down
7 changes: 7 additions & 0 deletions lib/src/model/game/game_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:http/http.dart' as http;
import 'package:lichess_mobile/src/model/common/http.dart';
import 'package:lichess_mobile/src/model/common/id.dart';
import 'package:lichess_mobile/src/model/common/perf.dart';
import 'package:lichess_mobile/src/model/game/archived_game.dart';
import 'package:lichess_mobile/src/model/game/playable_game.dart';

Expand Down Expand Up @@ -39,7 +40,12 @@ class GameRepository {
UserId userId, {
int max = 20,
DateTime? until,
Perf? perfType,
}) {
assert(
![Perf.fromPosition, Perf.puzzle, Perf.storm, Perf.streak]
.contains(perfType),
);
return client
.readNdJsonList(
Uri(
Expand All @@ -48,6 +54,7 @@ class GameRepository {
'max': max.toString(),
if (until != null)
'until': until.millisecondsSinceEpoch.toString(),
if (perfType != null) 'perfType': perfType.name,
'moves': 'false',
'lastFen': 'true',
'accuracy': 'true',
Expand Down
28 changes: 22 additions & 6 deletions lib/src/view/user/game_history_screen.dart
Original file line number Diff line number Diff line change
@@ -1,6 +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/common/perf.dart';
import 'package:lichess_mobile/src/model/game/game_history.dart';
import 'package:lichess_mobile/src/model/user/user.dart';
import 'package:lichess_mobile/src/utils/l10n_context.dart';
Expand All @@ -12,10 +13,14 @@ class GameHistoryScreen extends ConsumerWidget {
const GameHistoryScreen({
required this.user,
required this.isOnline,
this.perf,
this.games,
super.key,
});
final LightUser? user;
final bool isOnline;
final Perf? perf;
final int? games;

@override
Widget build(BuildContext context, WidgetRef ref) {
Expand All @@ -33,12 +38,12 @@ class GameHistoryScreen extends ConsumerWidget {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: nbGamesAsync.when(
data: (nbGames) => Text(context.l10n.nbGames(nbGames)),
data: (nbGames) => Text(context.l10n.nbGames(games ?? nbGames)),
loading: () => const CupertinoActivityIndicator(),
error: (e, s) => const Text('All Games'),
),
),
child: _Body(user: user, isOnline: isOnline),
child: _Body(user: user, isOnline: isOnline, perf: perf),
);
}

Expand All @@ -49,21 +54,26 @@ class GameHistoryScreen extends ConsumerWidget {
return Scaffold(
appBar: AppBar(
title: nbGamesAsync.when(
data: (nbGames) => Text(context.l10n.nbGames(nbGames)),
data: (nbGames) => Text(context.l10n.nbGames(games ?? nbGames)),
loading: () => const ButtonLoadingIndicator(),
error: (e, s) => const Text('All Games'),
),
),
body: _Body(user: user, isOnline: isOnline),
body: _Body(user: user, isOnline: isOnline, perf: perf),
);
}
}

class _Body extends ConsumerStatefulWidget {
const _Body({required this.user, required this.isOnline});
const _Body({
required this.user,
required this.isOnline,
required this.perf,
});

final LightUser? user;
final bool isOnline;
final Perf? perf;

@override
ConsumerState<_Body> createState() => _BodyState();
Expand Down Expand Up @@ -92,6 +102,7 @@ class _BodyState extends ConsumerState<_Body> {
userGameHistoryProvider(
widget.user?.id,
isOnline: widget.isOnline,
perf: widget.perf,
),
);

Expand All @@ -108,6 +119,7 @@ class _BodyState extends ConsumerState<_Body> {
userGameHistoryProvider(
widget.user?.id,
isOnline: widget.isOnline,
perf: widget.perf,
).notifier,
)
.getNext();
Expand All @@ -118,7 +130,11 @@ class _BodyState extends ConsumerState<_Body> {
@override
Widget build(BuildContext context) {
final gameListState = ref.watch(
userGameHistoryProvider(widget.user?.id, isOnline: widget.isOnline),
userGameHistoryProvider(
widget.user?.id,
isOnline: widget.isOnline,
perf: widget.perf,
),
);

return gameListState.when(
Expand Down
35 changes: 27 additions & 8 deletions lib/src/view/user/perf_stats_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import 'package:lichess_mobile/src/utils/l10n_context.dart';
import 'package:lichess_mobile/src/utils/navigation.dart';
import 'package:lichess_mobile/src/utils/string.dart';
import 'package:lichess_mobile/src/view/game/archived_game_screen.dart';
import 'package:lichess_mobile/src/view/user/game_history_screen.dart';
import 'package:lichess_mobile/src/widgets/adaptive_action_sheet.dart';
import 'package:lichess_mobile/src/widgets/buttons.dart';
import 'package:lichess_mobile/src/widgets/feedback.dart';
Expand Down Expand Up @@ -267,16 +268,34 @@ class _Body extends ConsumerWidget {
]),
statGroupSpace,
Row(
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'${context.l10n.perfStatTotalGames} '.localizeNumbers(),
style: Styles.sectionTitle,
Row(
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: [
Text(
'${context.l10n.perfStatTotalGames} '
.localizeNumbers(),
style: Styles.sectionTitle,
),
Text(
data.totalGames.toString().localizeNumbers(),
style: _mainValueStyle,
),
],
),
Text(
data.totalGames.toString().localizeNumbers(),
style: _mainValueStyle,
TextButton(
onPressed: () => pushPlatformRoute(
context,
builder: (context) => GameHistoryScreen(
user: user.lightUser,
isOnline: true,
perf: perf,
games: data.totalGames,
),
),
child: Text(context.l10n.perfStatViewTheGames),
),
],
),
Expand Down