Skip to content

Commit 7f0455d

Browse files
authored
fix(cat-voices): When user locks app should be redirected to Discovery Page (#2255)
* feat(cat-voices): refactor session listener and app content handling for improved state management and navigation (#2240) * feat(cat-voices): streamline app content structure and session listener for better readability and performance improvements
1 parent e21d737 commit 7f0455d

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

catalyst_voices/apps/voices/lib/app/view/app_content.dart

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,13 @@ final class _AppContent extends StatelessWidget {
7171
brightness: Brightness.dark,
7272
),
7373
debugShowCheckedModeBanner: false,
74-
builder: (context, child) {
75-
return Scaffold(
76-
primary: false,
77-
backgroundColor: Colors.transparent,
78-
body: AppActiveStateListener(
79-
child: GlobalPrecacheImages(
80-
child: GlobalSessionListener(
81-
child: AppMobileAccessRestriction(
82-
child: AppSplashScreenManager(
83-
child: child ?? const SizedBox.shrink(),
84-
),
74+
builder: (_, child) {
75+
return AppActiveStateListener(
76+
child: GlobalPrecacheImages(
77+
child: GlobalSessionListener(
78+
child: AppMobileAccessRestriction(
79+
child: AppSplashScreenManager(
80+
child: child ?? const SizedBox.shrink(),
8581
),
8682
),
8783
),

catalyst_voices/apps/voices/lib/app/view/app_session_listener.dart

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
1+
import 'package:catalyst_voices/routes/routes.dart';
12
import 'package:catalyst_voices/widgets/snackbar/voices_snackbar.dart';
23
import 'package:catalyst_voices/widgets/snackbar/voices_snackbar_type.dart';
34
import 'package:catalyst_voices_assets/catalyst_voices_assets.dart';
45
import 'package:catalyst_voices_blocs/catalyst_voices_blocs.dart';
56
import 'package:catalyst_voices_localization/catalyst_voices_localization.dart';
67
import 'package:flutter/material.dart';
78
import 'package:flutter_bloc/flutter_bloc.dart';
9+
import 'package:go_router/go_router.dart';
810

911
/// Listens globally to a session and can show different
1012
/// snackBars when a session changes.
11-
class GlobalSessionListener extends StatelessWidget {
13+
class GlobalSessionListener extends StatefulWidget {
1214
final Widget child;
1315

1416
const GlobalSessionListener({
1517
super.key,
1618
required this.child,
1719
});
1820

21+
@override
22+
State<GlobalSessionListener> createState() => _GlobalSessionListenerState();
23+
}
24+
25+
class _GlobalSessionListenerState extends State<GlobalSessionListener> {
26+
String? _lastLocation;
27+
1928
@override
2029
Widget build(BuildContext context) {
2130
return BlocListener<SessionCubit, SessionState>(
2231
listenWhen: _listenToSessionChangesWhen,
2332
listener: _onSessionChanged,
24-
child: child,
33+
child: widget.child,
2534
);
2635
}
2736

@@ -34,6 +43,22 @@ class GlobalSessionListener extends StatelessWidget {
3443
return keychainUnlocked || keychainLocked;
3544
}
3645

46+
void _onLockedKeychain(BuildContext context) {
47+
VoicesSnackBar(
48+
type: VoicesSnackBarType.error,
49+
behavior: SnackBarBehavior.floating,
50+
icon: VoicesAssets.icons.lockClosed.buildIcon(),
51+
title: context.l10n.lockSnackbarTitle,
52+
message: context.l10n.lockSnackbarMessage,
53+
).show(context);
54+
55+
final routerContext = AppRouter.rootNavigatorKey.currentContext;
56+
if (routerContext != null) {
57+
_lastLocation = GoRouter.of(routerContext).state.uri.toString();
58+
routerContext.go(const DiscoveryRoute().location);
59+
}
60+
}
61+
3762
void _onSessionChanged(BuildContext context, SessionState state) {
3863
if (state.isActive) {
3964
_onUnlockedKeychain(context);
@@ -50,15 +75,10 @@ class GlobalSessionListener extends StatelessWidget {
5075
title: context.l10n.unlockSnackbarTitle,
5176
message: context.l10n.unlockSnackbarMessage,
5277
).show(context);
53-
}
5478

55-
void _onLockedKeychain(BuildContext context) {
56-
VoicesSnackBar(
57-
type: VoicesSnackBarType.error,
58-
behavior: SnackBarBehavior.floating,
59-
icon: VoicesAssets.icons.lockClosed.buildIcon(),
60-
title: context.l10n.lockSnackbarTitle,
61-
message: context.l10n.lockSnackbarMessage,
62-
).show(context);
79+
final routerContext = AppRouter.rootNavigatorKey.currentContext;
80+
if (_lastLocation != null && routerContext != null) {
81+
routerContext.go(_lastLocation!);
82+
}
6383
}
6484
}

catalyst_voices/apps/voices/lib/routes/app_router.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'package:go_router/go_router.dart';
88
import 'package:sentry_flutter/sentry_flutter.dart';
99

1010
abstract final class AppRouter {
11-
static final _rootNavigatorKey = GlobalKey<NavigatorState>(
11+
static final rootNavigatorKey = GlobalKey<NavigatorState>(
1212
debugLabel: 'rootNavigatorKey',
1313
);
1414

@@ -18,7 +18,7 @@ abstract final class AppRouter {
1818
Listenable? refreshListenable,
1919
}) {
2020
return GoRouter(
21-
navigatorKey: _rootNavigatorKey,
21+
navigatorKey: rootNavigatorKey,
2222
initialLocation: initialLocation ?? Routes.initialLocation,
2323
redirect: (context, state) async => _guard(context, state, guards),
2424
observers: [

0 commit comments

Comments
 (0)