Skip to content

Commit a4ccb3e

Browse files
committed
refactor(widgets): enhance ad interaction and navigation flow
- Modify handleHeadlineTap to await ad completion before navigation - Add context.mounted check before navigating to prevent errors - Update method signature to return Future<void> - Improve code readability and maintainability
1 parent 92eba47 commit a4ccb3e

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

lib/shared/widgets/feed_core/headline_tap_handler.dart

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,25 @@ import 'package:go_router/go_router.dart';
1212
abstract final class HeadlineTapHandler {
1313
/// Handles a tap on a [Headline] item.
1414
///
15-
/// This method performs two key actions:
16-
/// 1. Notifies the [InterstitialAdManager] of a potential ad transition.
17-
/// 2. Navigates to the [Routes.articleDetailsName] page for the given headline.
15+
/// This method performs two key actions sequentially:
16+
/// 1. Notifies the [InterstitialAdManager] of a potential ad transition and
17+
/// awaits its completion (e.g., the user closing the ad).
18+
/// 2. Navigates to the [Routes.articleDetailsName] page for the given headline,
19+
/// but only after the ad has been handled and if the context is still mounted.
1820
///
1921
/// - [context]: The current [BuildContext] to access BLoCs and for navigation.
2022
/// - [headline]: The [Headline] item that was tapped.
21-
static void handleHeadlineTap(BuildContext context, Headline headline) {
22-
context.read<InterstitialAdManager>().onPotentialAdTrigger();
23+
static Future<void> handleHeadlineTap(
24+
BuildContext context,
25+
Headline headline,
26+
) async {
27+
// Await for the ad to be shown and dismissed.
28+
await context.read<InterstitialAdManager>().onPotentialAdTrigger();
29+
30+
// Check if the widget is still in the tree before navigating.
31+
if (!context.mounted) return;
32+
33+
// Proceed with navigation after the ad is closed.
2334
context.goNamed(
2435
Routes.articleDetailsName,
2536
pathParameters: {'id': headline.id},

0 commit comments

Comments
 (0)