From c65adba2fa3f7e2675cc164170d5883411d2120a Mon Sep 17 00:00:00 2001 From: Plague Fox Date: Tue, 28 Nov 2023 04:48:30 +0400 Subject: [PATCH] Update OctopusInformationProvider --- lib/src/controller/information_provider.dart | 65 +++++++++++++++++--- lib/src/state/state_util.dart | 2 +- 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/lib/src/controller/information_provider.dart b/lib/src/controller/information_provider.dart index baf0541..dea8871 100644 --- a/lib/src/controller/information_provider.dart +++ b/lib/src/controller/information_provider.dart @@ -2,6 +2,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; import 'package:meta/meta.dart'; +import 'package:octopus/src/state/jenkins_hash.dart'; /// The route information provider that propagates /// the platform route information changes. @@ -25,6 +26,7 @@ class OctopusInformationProvider extends RouteInformationProvider Map? initialState, Listenable? refreshListenable, }) : _value = _initialRouteInformation(initialLocation, initialState), + _valueInEngine = _kEmptyRouteInformation, _refreshListenable = refreshListenable { _refreshListenable?.addListener(notifyListeners); } @@ -52,6 +54,8 @@ class OctopusInformationProvider extends RouteInformationProvider final Listenable? _refreshListenable; static WidgetsBinding get _binding => WidgetsBinding.instance; + static final RouteInformation _kEmptyRouteInformation = + RouteInformation(uri: Uri()); @override void routerReportsNewRouteInformation( @@ -63,10 +67,28 @@ class OctopusInformationProvider extends RouteInformationProvider } */ // Avoid adding a new history entry if the route is the same as before. - final replace = type == RouteInformationReportingType.neglect || + /* final replace = type == RouteInformationReportingType.neglect || (type == RouteInformationReportingType.none && _valueInEngine.uri == routeInformation.uri && - _valueInEngine.state == routeInformation.state); + _valueInEngine.state == routeInformation.state); */ + + final bool replace; + switch (type) { + case RouteInformationReportingType.none: + if (_valueInEngine.uri == routeInformation.uri) { + if (identical(_valueInEngine.state, routeInformation.state)) { + return; + } + final hashA = jenkinsHash(_valueInEngine.state); + final hashB = jenkinsHash(routeInformation.state); + if (hashA == hashB) return; + } + replace = _valueInEngine == _kEmptyRouteInformation; + case RouteInformationReportingType.neglect: + replace = true; + case RouteInformationReportingType.navigate: + replace = false; + } /* if (!replace && routeInformation is OctopusRouteInformation) { replace = routeInformation.replace; @@ -78,8 +100,7 @@ class OctopusInformationProvider extends RouteInformationProvider state: routeInformation.state, replace: replace, ); - _value = routeInformation; - _valueInEngine = routeInformation; + _value = _valueInEngine = routeInformation; } @override @@ -91,16 +112,12 @@ class OctopusInformationProvider extends RouteInformationProvider if (shouldNotify) notifyListeners(); } - RouteInformation _valueInEngine = RouteInformation( - uri: Uri.tryParse( - WidgetsBinding.instance.platformDispatcher.defaultRouteName, - ), - ); + RouteInformation _valueInEngine; void _platformReportsNewRouteInformation(RouteInformation routeInformation) { if (_value == routeInformation) return; _value = routeInformation; - _valueInEngine = routeInformation; + _valueInEngine = _kEmptyRouteInformation; notifyListeners(); } @@ -132,4 +149,32 @@ class OctopusInformationProvider extends RouteInformationProvider _platformReportsNewRouteInformation(routeInformation); return SynchronousFuture(true); } + + @override + @Deprecated('Use didPushRouteInformation instead') + Future didPushRoute(String route) { + assert( + hasListeners, + 'A OctopusInformationProvider must have ' + 'at least one listener before it can be used.'); + _platformReportsNewRouteInformation( + RouteInformation(uri: Uri.tryParse(route))); + return SynchronousFuture(true); + } } + + +/* Useful methods for the package + + SystemNavigator.pop(); + SystemNavigator.setFrameworkHandlesBack(true); + SystemNavigator.selectMultiEntryHistory(); + SystemNavigator.selectSingleEntryHistory(); + SystemNavigator.routeInformationUpdated( + uri: Uri.parse('/'), + state: const {}, + replace: false, + ); + Router.neglect(context, () {}); + Router.navigate(context, () {}); +*/ \ No newline at end of file diff --git a/lib/src/state/state_util.dart b/lib/src/state/state_util.dart index 5a98e68..1200fd7 100644 --- a/lib/src/state/state_util.dart +++ b/lib/src/state/state_util.dart @@ -72,10 +72,10 @@ abstract final class StateUtil { for (final node in state.children) { encodeNode(node, 0); } - return Uri( pathSegments: segments, queryParameters: state.arguments.isEmpty ? null : state.arguments, + //fragment: , ); }