Skip to content

Commit

Permalink
Add bookmark functionality to archived game screen and game screen
Browse files Browse the repository at this point in the history
  • Loading branch information
julien4215 committed Jan 23, 2025
1 parent d1aa50e commit f142db1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/src/model/game/bookmark_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'bookmark_provider.g.dart';

@riverpod
@Riverpod(keepAlive: true)
class BookmarkNotifier extends _$BookmarkNotifier {
@override
bool? build(GameId id) => null;
Expand Down
2 changes: 2 additions & 0 deletions lib/src/model/game/game_history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ class UserGameHistory extends _$UserGameHistory {
max: _nbPerPage,
until: _list.last.game.createdAt,
filter: currentVal.filter,
withBookmarked: true,
),
)
: currentVal.online && currentVal.session != null
Expand All @@ -175,6 +176,7 @@ class UserGameHistory extends _$UserGameHistory {
max: _nbPerPage,
until: _list.last.game.createdAt,
filter: currentVal.filter,
withBookmarked: true,
),
)
: (await ref.watch(gameStorageProvider.future))
Expand Down
22 changes: 14 additions & 8 deletions lib/src/view/game/game_common_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:intl/intl.dart';
import 'package:lichess_mobile/src/model/auth/auth_session.dart';
import 'package:lichess_mobile/src/model/challenge/challenge.dart';
import 'package:lichess_mobile/src/model/common/id.dart';
import 'package:lichess_mobile/src/model/common/time_increment.dart';
Expand All @@ -28,17 +27,17 @@ import 'package:lichess_mobile/src/widgets/platform_scaffold.dart';
import 'package:lichess_mobile/src/widgets/shimmer.dart';

class BookmarkButton extends ConsumerWidget {
const BookmarkButton({required this.id, this.bookmarked = false, this.onPressed});
const BookmarkButton({required this.id, required this.bookmarked});

final GameId id;
final bool bookmarked;
final Function? onPressed;

@override
Widget build(BuildContext context, WidgetRef ref) {
final bookmarkCurrentState = ref.watch(bookmarkNotifierProvider(id)) ?? bookmarked;

return AppBarIconButton(
onPressed: () async {
final bookmarkCurrentState = ref.watch(bookmarkNotifierProvider(id)) ?? bookmarked;
final newBookmarkValue = !bookmarkCurrentState;

try {
Expand All @@ -53,7 +52,7 @@ class BookmarkButton extends ConsumerWidget {
}
},
semanticsLabel: context.l10n.bookmarkThisGame,
icon: Icon(bookmarked ? Icons.star : Icons.star_outline_rounded),
icon: Icon(bookmarkCurrentState ? Icons.star : Icons.star_outline_rounded),
);
}
}
Expand Down Expand Up @@ -83,11 +82,19 @@ class _SettingButton extends ConsumerWidget {
final _gameTitledateFormat = DateFormat.yMMMd();

class GameAppBar extends ConsumerWidget {
const GameAppBar({this.id, this.seek, this.challenge, this.lastMoveAt, super.key});
const GameAppBar({
this.id,
this.seek,
this.challenge,
this.lastMoveAt,
this.bookmarked,
super.key,
});

final GameSeek? seek;
final ChallengeRequest? challenge;
final GameFullId? id;
final bool? bookmarked;

/// The date of the last move played in the game. If null, the game is in progress.
final DateTime? lastMoveAt;
Expand All @@ -101,7 +108,6 @@ class GameAppBar extends ConsumerWidget {
Widget build(BuildContext context, WidgetRef ref) {
final shouldPreventGoingBackAsync =
id != null ? ref.watch(shouldPreventGoingBackProvider(id!)) : const AsyncValue.data(true);
final isLoggedIn = ref.watch(isLoggedInProvider);

return PlatformAppBar(
leading: shouldPreventGoingBackAsync.maybeWhen<Widget?>(
Expand All @@ -119,7 +125,7 @@ class GameAppBar extends ConsumerWidget {
actions: [
const ToggleSoundButton(),
if (id != null) ...[
if (isLoggedIn) BookmarkButton(id: id!.gameId),
if (bookmarked != null) BookmarkButton(id: id!.gameId, bookmarked: bookmarked ?? false),
_SettingButton(id: id!),
],
],
Expand Down
1 change: 1 addition & 0 deletions lib/src/view/game/game_list_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ class ExtendedGameListTile extends StatelessWidget {
loadingLastMove: game.lastMove,
loadingOrientation: youAre,
lastMoveAt: game.lastMoveAt,
bookmarked: game.bookmarked,
)
: ArchivedGameScreen(gameData: game, orientation: youAre),
);
Expand Down
9 changes: 8 additions & 1 deletion lib/src/view/game/game_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class GameScreen extends ConsumerStatefulWidget {
this.loadingLastMove,
this.loadingOrientation,
this.lastMoveAt,
this.bookmarked,
super.key,
}) : assert(
initialGameId != null || seek != null || challenge != null,
Expand All @@ -56,6 +57,8 @@ class GameScreen extends ConsumerStatefulWidget {
/// The date of the last move played in the game. If null, the game is in progress.
final DateTime? lastMoveAt;

final bool? bookmarked;

_GameSource get source {
if (initialGameId != null) {
return _GameSource.game;
Expand Down Expand Up @@ -152,7 +155,11 @@ class _GameScreenState extends ConsumerState<GameScreen> with RouteAware {
: const LoadGameError('Could not create the game.');
return PlatformScaffold(
resizeToAvoidBottomInset: false,
appBar: GameAppBar(id: gameId, lastMoveAt: widget.lastMoveAt),
appBar: GameAppBar(
id: gameId,
lastMoveAt: widget.lastMoveAt,
bookmarked: widget.bookmarked,
),
body: body,
);
},
Expand Down

0 comments on commit f142db1

Please sign in to comment.