Skip to content

Commit

Permalink
Only show bookmark button if user is logged in
Browse files Browse the repository at this point in the history
  • Loading branch information
julien4215 committed Dec 24, 2024
1 parent 52c9fce commit d7fb43e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 38 deletions.
4 changes: 3 additions & 1 deletion lib/src/view/analysis/analysis_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lichess_mobile/src/model/analysis/analysis_controller.dart';
import 'package:lichess_mobile/src/model/analysis/analysis_preferences.dart';
import 'package:lichess_mobile/src/model/analysis/opening_service.dart';
import 'package:lichess_mobile/src/model/auth/auth_session.dart';
import 'package:lichess_mobile/src/model/common/chess.dart';
import 'package:lichess_mobile/src/model/game/game_share_service.dart';
import 'package:lichess_mobile/src/network/http.dart';
Expand Down Expand Up @@ -74,12 +75,13 @@ class _AnalysisScreenState extends ConsumerState<AnalysisScreen>
final ctrlProvider = analysisControllerProvider(widget.options);
final asyncState = ref.watch(ctrlProvider);
final prefs = ref.watch(analysisPreferencesProvider);
final isLoggedIn = ref.watch(authSessionProvider) != null;

final appBarActions = [
if (prefs.enableComputerAnalysis)
EngineDepth(defaultEval: asyncState.valueOrNull?.currentNode.eval),
AppBarAnalysisTabIndicator(tabs: tabs, controller: _tabController),
if (widget.options.gameId != null) BookmarkButton(id: widget.options.gameId!),
if (widget.options.gameId != null && isLoggedIn) BookmarkButton(id: widget.options.gameId!),
AppBarIconButton(
onPressed: () {
pushPlatformRoute(
Expand Down
40 changes: 35 additions & 5 deletions lib/src/view/game/archived_game_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:intl/intl.dart';
import 'package:lichess_mobile/src/model/analysis/analysis_controller.dart';
import 'package:lichess_mobile/src/model/auth/auth_session.dart';
import 'package:lichess_mobile/src/model/common/id.dart';
import 'package:lichess_mobile/src/model/game/archived_game.dart';
import 'package:lichess_mobile/src/model/game/game.dart';
Expand Down Expand Up @@ -44,8 +45,15 @@ class ArchivedGameScreen extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
final isLoggedIn = ref.watch(authSessionProvider) != null;

if (gameData != null) {
return _Body(gameData: gameData, orientation: orientation, initialCursor: initialCursor);
return _Body(
gameData: gameData,
orientation: orientation,
isLoggedIn: isLoggedIn,
initialCursor: initialCursor,
);
} else {
return _LoadGame(gameId: gameId!, orientation: orientation, initialCursor: initialCursor);
}
Expand All @@ -62,25 +70,40 @@ class _LoadGame extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final game = ref.watch(archivedGameProvider(id: gameId));
final isLoggedIn = ref.watch(authSessionProvider) != null;

return game.when(
data: (game) {
return _Body(gameData: game.data, orientation: orientation, initialCursor: initialCursor);
return _Body(
gameData: game.data,
orientation: orientation,
isLoggedIn: isLoggedIn,
initialCursor: initialCursor,
);
},
loading: () => _Body(gameData: null, orientation: orientation, initialCursor: initialCursor),
loading:
() => _Body(
gameData: null,
orientation: orientation,
isLoggedIn: isLoggedIn,
initialCursor: initialCursor,
),
error: (error, stackTrace) {
debugPrint('SEVERE: [ArchivedGameScreen] could not load game; $error\n$stackTrace');
switch (error) {
case ServerException _ when error.statusCode == 404:
return _Body(
gameData: null,
orientation: orientation,
isLoggedIn: isLoggedIn,
initialCursor: initialCursor,
error: 'Game not found.',
);
default:
return _Body(
gameData: null,
orientation: orientation,
isLoggedIn: isLoggedIn,
initialCursor: initialCursor,
error: error,
);
Expand All @@ -91,10 +114,17 @@ class _LoadGame extends ConsumerWidget {
}

class _Body extends StatelessWidget {
const _Body({required this.gameData, required this.orientation, this.initialCursor, this.error});
const _Body({
required this.gameData,
required this.orientation,
required this.isLoggedIn,
this.initialCursor,
this.error,
});

final LightArchivedGame? gameData;
final Object? error;
final bool isLoggedIn;
final Side orientation;
final int? initialCursor;

Expand All @@ -105,7 +135,7 @@ class _Body extends StatelessWidget {
title: gameData != null ? _GameTitle(gameData: gameData!) : const SizedBox.shrink(),
actions: [
if (gameData == null && error == null) const PlatformAppBarLoadingIndicator(),
if (gameData != null) BookmarkButton(id: gameData!.id),
if (gameData != null && isLoggedIn) BookmarkButton(id: gameData!.id),
const ToggleSoundButton(),
],
),
Expand Down
7 changes: 6 additions & 1 deletion lib/src/view/game/game_common_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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 Down Expand Up @@ -81,6 +82,7 @@ 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(authSessionProvider) != null;

return PlatformAppBar(
leading: shouldPreventGoingBackAsync.maybeWhen<Widget?>(
Expand All @@ -97,7 +99,10 @@ class GameAppBar extends ConsumerWidget {
: const SizedBox.shrink(),
actions: [
const ToggleSoundButton(),
if (id != null) ...[BookmarkButton(id: id!.gameId), _SettingButton(id: id!)],
if (id != null) ...[
if (isLoggedIn) BookmarkButton(id: id!.gameId),
_SettingButton(id: id!),
],
],
);
}
Expand Down
20 changes: 12 additions & 8 deletions lib/src/view/game/game_list_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:intl/intl.dart';
import 'package:lichess_mobile/src/model/analysis/analysis_controller.dart';
import 'package:lichess_mobile/src/model/auth/auth_session.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_share_service.dart';
Expand Down Expand Up @@ -111,6 +112,8 @@ class _ContextMenu extends ConsumerWidget {

final customColors = Theme.of(context).extension<CustomColors>();

final isLoggedIn = ref.watch(authSessionProvider) == null;

return BottomSheetScrollableContainer(
children: [
Padding(
Expand Down Expand Up @@ -234,14 +237,15 @@ class _ContextMenu extends ConsumerWidget {
closeOnPressed: false,
child: Text(context.l10n.mobileShareGameURL),
),
BottomSheetContextMenuAction(
onPressed: () {
ref.withClient((client) => GameRepository(client).bookmark(game.id, v: 1));
},
icon: Icons.star_border_rounded,
closeOnPressed: false,
child: const Text('Bookmark Game'),
),
if (isLoggedIn)
BottomSheetContextMenuAction(
onPressed: () {
ref.withClient((client) => GameRepository(client).bookmark(game.id, v: 1));
},
icon: Icons.star_border_rounded,
closeOnPressed: false,
child: const Text('Bookmark Game'),
),
// Builder is used to retrieve the context immediately surrounding the
// BottomSheetContextMenuAction
// This is necessary to get the correct context for the iPad share dialog
Expand Down
52 changes: 29 additions & 23 deletions lib/src/view/user/game_history_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class _BodyState extends ConsumerState<_Body> {
final gameListState = ref.watch(
userGameHistoryProvider(widget.user?.id, isOnline: widget.isOnline, filter: gameFilterState),
);
final isLoggedIn = ref.watch(authSessionProvider) == null;

return gameListState.when(
data: (state) {
Expand Down Expand Up @@ -173,30 +174,35 @@ class _BodyState extends ConsumerState<_Body> {
);
}

return Slidable(
endActionPane: ActionPane(
motion: const ScrollMotion(),
children: [
SlidableAction(
onPressed: (BuildContext context) {
ref.withClient(
(client) => GameRepository(client).bookmark(list[index].game.id, v: 1),
);
},
icon: Icons.star_outline_rounded,
label: 'Bookmark',
),
],
),
child: ExtendedGameListTile(
item: list[index],
// see: https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/cupertino/list_tile.dart#L30 for horizontal padding value
padding:
Theme.of(context).platform == TargetPlatform.iOS
? const EdgeInsets.symmetric(horizontal: 14.0, vertical: 12.0)
: null,
),
final gameTile = ExtendedGameListTile(
item: list[index],
// see: https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/cupertino/list_tile.dart#L30 for horizontal padding value
padding:
Theme.of(context).platform == TargetPlatform.iOS
? const EdgeInsets.symmetric(horizontal: 14.0, vertical: 12.0)
: null,
);

return isLoggedIn
? Slidable(
endActionPane: ActionPane(
motion: const ScrollMotion(),
children: [
SlidableAction(
onPressed: (BuildContext context) {
ref.withClient(
(client) =>
GameRepository(client).bookmark(list[index].game.id, v: 1),
);
},
icon: Icons.star_outline_rounded,
label: 'Bookmark',
),
],
),
child: gameTile,
)
: gameTile;
},
);
},
Expand Down

0 comments on commit d7fb43e

Please sign in to comment.