Line data Source code
1 : import 'package:flutter/foundation.dart'; 2 : import 'package:flutter/material.dart'; 3 : import 'package:flutter/widgets.dart'; 4 : import 'package:theseus_navigator/src/navigation_controller.dart'; 5 : 6 : import 'destination.dart'; 7 : import 'exceptions.dart'; 8 : import 'navigation_scheme.dart'; 9 : import 'utils/utils.dart'; 10 : 11 : /// Implementation of [RouteInformationParser]. 12 : /// 13 : /// Uses [navigationScheme] to get access to route's information. 14 : /// 15 : /// See also: 16 : /// - [NavigationScheme] 17 : /// - [NavigationController] 18 : /// - [Destination] 19 : /// 20 : class TheseusRouteInformationParser 21 : extends RouteInformationParser<Destination> { 22 : /// Creates a route information parser. 23 : /// 24 5 : TheseusRouteInformationParser({ 25 : required this.navigationScheme, 26 : }); 27 : 28 : /// A navigation scheme that contains destinations to parse. 29 : /// 30 : final NavigationScheme navigationScheme; 31 : 32 : @override 33 4 : Future<Destination> parseRouteInformation( 34 : RouteInformation routeInformation) async { 35 4 : final uri = routeInformation.location ?? ''; 36 12 : Log.d(runtimeType, 'parseRouteInformation(): $uri'); 37 8 : final matchedDestination = navigationScheme.findDestination(uri); 38 : if (matchedDestination == null) { 39 4 : if (navigationScheme.errorDestination != null) { 40 6 : return SynchronousFuture(navigationScheme.errorDestination!); 41 : } else { 42 2 : throw UnknownUriException(uri); 43 : } 44 : } 45 : try { 46 8 : final result = await matchedDestination.parse(uri); 47 : return result; 48 : } catch (error) { 49 4 : if (navigationScheme.errorDestination != null) { 50 6 : return SynchronousFuture(navigationScheme.errorDestination!); 51 : } else { 52 2 : throw UnknownUriException(uri); 53 : } 54 : } 55 : } 56 : 57 3 : @override 58 : // ignore: avoid_renaming_method_parameters 59 : RouteInformation? restoreRouteInformation(Destination destination) { 60 6 : if (!destination.settings.updateHistory) { 61 4 : Log.d(runtimeType, 62 4 : 'restoreRouteInformation(): Would not restore route information for ${destination.uri}'); 63 : return null; 64 : } 65 12 : Log.d(runtimeType, 'restoreRouteInformation(): ${destination.uri}'); 66 6 : return RouteInformation(location: destination.uri); 67 : } 68 : }