Skip to content

Commit

Permalink
Update OctopusInformationProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed Nov 28, 2023
1 parent b18abb7 commit c65adba
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
65 changes: 55 additions & 10 deletions lib/src/controller/information_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -25,6 +26,7 @@ class OctopusInformationProvider extends RouteInformationProvider
Map<String, Object?>? initialState,
Listenable? refreshListenable,
}) : _value = _initialRouteInformation(initialLocation, initialState),
_valueInEngine = _kEmptyRouteInformation,
_refreshListenable = refreshListenable {
_refreshListenable?.addListener(notifyListeners);
}
Expand Down Expand Up @@ -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(
Expand All @@ -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;
Expand All @@ -78,8 +100,7 @@ class OctopusInformationProvider extends RouteInformationProvider
state: routeInformation.state,
replace: replace,
);
_value = routeInformation;
_valueInEngine = routeInformation;
_value = _valueInEngine = routeInformation;
}

@override
Expand All @@ -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();
}

Expand Down Expand Up @@ -132,4 +149,32 @@ class OctopusInformationProvider extends RouteInformationProvider
_platformReportsNewRouteInformation(routeInformation);
return SynchronousFuture<bool>(true);
}

@override
@Deprecated('Use didPushRouteInformation instead')
Future<bool> 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<bool>(true);
}
}


/* Useful methods for the package
SystemNavigator.pop();
SystemNavigator.setFrameworkHandlesBack(true);
SystemNavigator.selectMultiEntryHistory();
SystemNavigator.selectSingleEntryHistory();
SystemNavigator.routeInformationUpdated(
uri: Uri.parse('/'),
state: const <String, Object?>{},
replace: false,
);
Router.neglect(context, () {});
Router.navigate(context, () {});
*/
2 changes: 1 addition & 1 deletion lib/src/state/state_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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: ,
);
}

Expand Down

0 comments on commit c65adba

Please sign in to comment.