76 lines
1.8 KiB
Dart
76 lines
1.8 KiB
Dart
import '../../../../core/router/app_routes.dart';
|
|
|
|
typedef RouteNavigator = void Function(String target, {bool replace});
|
|
|
|
const Set<String> _allowedRoutes = {
|
|
AppRoutes.settingsMain,
|
|
AppRoutes.todoList,
|
|
AppRoutes.todoCreate,
|
|
AppRoutes.calendarDayWeek,
|
|
AppRoutes.calendarMonth,
|
|
AppRoutes.calendarEventCreate,
|
|
AppRoutes.messageInviteList,
|
|
AppRoutes.contactsList,
|
|
AppRoutes.contactsAdd,
|
|
};
|
|
|
|
const List<String> _allowedRoutePrefixes = [
|
|
'/calendar/events/',
|
|
'/todo/',
|
|
'/messages/invites/',
|
|
];
|
|
|
|
class RouteNavigationTool {
|
|
RouteNavigationTool._();
|
|
|
|
static final RouteNavigationTool instance = RouteNavigationTool._();
|
|
|
|
RouteNavigator? _navigator;
|
|
|
|
void bindNavigator(RouteNavigator navigator) {
|
|
_navigator = navigator;
|
|
}
|
|
|
|
void clearNavigator() {
|
|
_navigator = null;
|
|
}
|
|
|
|
Map<String, dynamic> execute(Map<String, dynamic> args) {
|
|
final target = args['target'];
|
|
if (target is! String || target.isEmpty) {
|
|
return {'ok': false, 'error': 'target is required'};
|
|
}
|
|
if (!_isAllowedTarget(target)) {
|
|
return {'ok': false, 'target': target, 'error': 'target is not allowed'};
|
|
}
|
|
final replace = args['replace'] == true;
|
|
final navigator = _navigator;
|
|
if (navigator == null) {
|
|
return {
|
|
'ok': false,
|
|
'target': target,
|
|
'replace': replace,
|
|
'error': 'navigator not bound',
|
|
};
|
|
}
|
|
navigator(target, replace: replace);
|
|
return {'ok': true, 'target': target, 'replace': replace, 'applied': true};
|
|
}
|
|
|
|
bool _isAllowedTarget(String target) {
|
|
if (!target.startsWith('/')) {
|
|
return false;
|
|
}
|
|
final normalized = target.split('?').first;
|
|
if (_allowedRoutes.contains(normalized)) {
|
|
return true;
|
|
}
|
|
for (final prefix in _allowedRoutePrefixes) {
|
|
if (normalized.startsWith(prefix)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|