Skip to content

Commit

Permalink
Update platform initialization and history handling
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed Dec 27, 2023
1 parent f75866a commit c930c2c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import 'dart:html' as html;

import 'package:flutter_web_plugins/url_strategy.dart';

//import 'package:flutter_web_plugins/flutter_web_plugins.dart';

Future<void> $platformInitialization() async {
Expand All @@ -11,6 +13,11 @@ Future<void> $platformInitialization() async {
Future<void>.delayed(
const Duration(seconds: 1),
() {
// Before running your app:
//setUrlStrategy(null);
setUrlStrategy(NoHistoryUrlStrategy());
//const HashUrlStrategy();

html.document.getElementById('splash')?.remove();
html.document.getElementById('splash-branding')?.remove();
html.document.body?.style.background = 'transparent';
Expand All @@ -21,3 +28,9 @@ Future<void> $platformInitialization() async {
},
);
}

class NoHistoryUrlStrategy extends PathUrlStrategy {
@override
void pushState(Object? state, String title, String url) =>
replaceState(state, title, url);
}
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ packages:
source: sdk
version: "0.0.0"
flutter_web_plugins:
dependency: transitive
dependency: "direct main"
description: flutter
source: sdk
version: "0.0.0"
Expand Down
3 changes: 3 additions & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ dependencies:
flutter:
sdk: flutter

flutter_web_plugins:
sdk: flutter

# Localization
flutter_localizations:
sdk: flutter
Expand Down
53 changes: 38 additions & 15 deletions lib/src/controller/information_provider_js.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:meta/meta.dart';
import 'package:octopus/src/controller/information_provider.dart';
import 'package:octopus/src/state/state.dart';
import 'package:octopus/src/state/state_codec.dart';
import 'package:octopus/src/util/jenkins_hash.dart';
import 'package:octopus/src/util/system_navigator_util.dart';

/// {@nodoc}
Expand All @@ -29,7 +28,8 @@ final class OctopusInformationProvider$JS extends OctopusInformationProvider {
required RouteInformation value,
super.refreshListenable,
}) : _value = value,
_valueInEngine = valueInEngine;
_valueInEngine = valueInEngine,
_history = <RouteInformation>[value];

@override
void routerReportsNewRouteInformation(
Expand All @@ -48,6 +48,14 @@ final class OctopusInformationProvider$JS extends OctopusInformationProvider {
if (routeInformation.intention == OctopusStateIntention.neglect) return;
}

if (_valueInEngine.uri == routeInformation.uri) {
return; // Remove dubplicates in history.
} else if (routeInformation.uri.path.isEmpty) {
return;
} else if (!routeInformation.uri.path.startsWith('/')) {
return;
}

// Avoid adding a new history entry if the route is the same as before.
/* final replace = type == RouteInformationReportingType.neglect ||
(type == RouteInformationReportingType.none &&
Expand All @@ -72,13 +80,13 @@ final class OctopusInformationProvider$JS extends OctopusInformationProvider {
case RouteInformationReportingType.none:
if (_valueInEngine.uri == routeInformation.uri) {
replace = true;
if (identical(_valueInEngine.state, routeInformation.state)) {
/* if (identical(_valueInEngine.state, routeInformation.state)) {
return;
} else {
final hashA = jenkinsHash(_valueInEngine.state);
final hashB = jenkinsHash(routeInformation.state);
if (hashA == hashB) return;
}
} */
}
case RouteInformationReportingType.neglect:
replace = true;
Expand All @@ -100,29 +108,44 @@ final class OctopusInformationProvider$JS extends OctopusInformationProvider {
if (replace) {
SystemNavigatorUtil.replaceState(
data: routeInformation.state, url: routeInformation.uri);
_history.last = routeInformation;
} else {
SystemNavigatorUtil.pushState(
data: routeInformation.state, url: routeInformation.uri);
_history.add(routeInformation);
}
_value = _valueInEngine = routeInformation;
value = _valueInEngine = routeInformation;
}

@override
RouteInformation get value => _value;
RouteInformation _value;
set value(RouteInformation other) {
final shouldNotify = _value.uri != other.uri || _value.state != other.state;
final shouldNotify = _value.uri != other.uri;
_value = other;
if (shouldNotify) notifyListeners();
}

/// History stack of the states.
/// {@nodoc}
final List<RouteInformation> _history;

RouteInformation _valueInEngine;

void pushRoute(RouteInformation routeInformation) {
if (_value == routeInformation) return;
//if (routeInformation.uri.path.startsWith('/')) return;
_value = _valueInEngine = routeInformation;
notifyListeners();
bool pushRoute(RouteInformation routeInformation) {
if (_value == routeInformation) return false;
if (!routeInformation.uri.path.startsWith('/')) {
// sims the back button pressed
if (_history.length < 2) return false;
_history.removeLast();
routerReportsNewRouteInformation(
_history.removeLast(),
type: RouteInformationReportingType.navigate,
);
return true;
}
value = _valueInEngine = routeInformation;
return true;
}

@override
Expand All @@ -131,8 +154,7 @@ final class OctopusInformationProvider$JS extends OctopusInformationProvider {
hasListeners,
'A OctopusInformationProvider must have '
'at least one listener before it can be used.');
pushRoute(routeInformation);
return SynchronousFuture<bool>(true);
return SynchronousFuture<bool>(pushRoute(routeInformation));
}

@override
Expand All @@ -142,8 +164,9 @@ final class OctopusInformationProvider$JS extends OctopusInformationProvider {
hasListeners,
'A OctopusInformationProvider must have '
'at least one listener before it can be used.');
pushRoute(RouteInformation(uri: Uri.tryParse(route)));
return SynchronousFuture<bool>(true);
return SynchronousFuture<bool>(
pushRoute(RouteInformation(uri: Uri.tryParse(route))),
);
}

@override
Expand Down

0 comments on commit c930c2c

Please sign in to comment.