refactor: enhance navigation robustness by introducing popSafe and safePushNamedAndRemoveUntil methods and updating their usage.

This commit is contained in:
Achintha Isuru
2026-02-28 17:23:53 -05:00
parent c26128f1f2
commit 53b612851c
24 changed files with 481 additions and 334 deletions

View File

@@ -103,7 +103,7 @@ extension ClientNavigator on IModularNavigator {
/// Navigates to the full list of invoices awaiting approval.
void toAwaitingApproval({Object? arguments}) {
safeNavigate(ClientPaths.awaitingApproval, arguments: arguments);
safePush(ClientPaths.awaitingApproval, arguments: arguments);
}
/// Navigates to the Invoice Ready page.

View File

@@ -1,3 +1,4 @@
import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'client/route_paths.dart';
import 'staff/route_paths.dart';
@@ -29,8 +30,8 @@ extension NavigationExtensions on IModularNavigator {
return true;
} catch (e) {
// In production, you might want to log this to a monitoring service
// ignore: avoid_print
print('Navigation error to $path: $e');
// ignore: avoid_debugPrint
debugPrint('Navigation error to $path: $e');
navigateToHome();
return false;
}
@@ -54,8 +55,29 @@ extension NavigationExtensions on IModularNavigator {
return await pushNamed<T>(routeName, arguments: arguments);
} catch (e) {
// In production, you might want to log this to a monitoring service
// ignore: avoid_print
print('Push navigation error to $routeName: $e');
// ignore: avoid_debugPrint
debugPrint('Push navigation error to $routeName: $e');
navigateToHome();
return null;
}
}
/// Safely pushes a named route and removes until a predicate is met.
Future<T?> safePushNamedAndRemoveUntil<T extends Object?>(
String routeName,
bool Function(Route<dynamic>) predicate, {
Object? arguments,
}) async {
try {
return await pushNamedAndRemoveUntil<T>(
routeName,
predicate,
arguments: arguments,
);
} catch (e) {
// In production, you might want to log this to a monitoring service
// ignore: avoid_debugPrint
debugPrint('PushNamedAndRemoveUntil error to $routeName: $e');
navigateToHome();
return null;
}
@@ -72,9 +94,9 @@ extension NavigationExtensions on IModularNavigator {
/// Pops the current route if possible, otherwise navigates to home.
///
/// Returns `true` if a route was popped, `false` if it navigated to home.
bool popSafe() {
bool popSafe<T extends Object?>([T? result]) {
if (canPop()) {
pop();
pop(result);
return true;
}
navigateToHome();

View File

@@ -53,7 +53,7 @@ extension StaffNavigator on IModularNavigator {
}
void toStaffHome() {
pushNamedAndRemoveUntil(StaffPaths.home, (_) => false);
safePushNamedAndRemoveUntil(StaffPaths.home, (_) => false);
}
void toBenefits() {
@@ -61,7 +61,7 @@ extension StaffNavigator on IModularNavigator {
}
void toStaffMain() {
pushNamedAndRemoveUntil('${StaffPaths.main}/home/', (_) => false);
safePushNamedAndRemoveUntil('${StaffPaths.main}/home/', (_) => false);
}
void toShifts({
@@ -83,11 +83,11 @@ extension StaffNavigator on IModularNavigator {
}
void toPayments() {
pushNamedAndRemoveUntil(StaffPaths.payments, (_) => false);
safePushNamedAndRemoveUntil(StaffPaths.payments, (_) => false);
}
void toClockIn() {
pushNamedAndRemoveUntil(StaffPaths.clockIn, (_) => false);
safePushNamedAndRemoveUntil(StaffPaths.clockIn, (_) => false);
}
void toProfile() {
@@ -154,6 +154,18 @@ extension StaffNavigator on IModularNavigator {
safePush(StaffPaths.taxForms);
}
void toLanguageSelection() {
safePush(StaffPaths.languageSelection);
}
void toFormI9() {
safeNavigate(StaffPaths.formI9);
}
void toFormW4() {
safeNavigate(StaffPaths.formW4);
}
void toTimeCard() {
safePush(StaffPaths.timeCard);
}