Skip to content

Commit

Permalink
Add setArguments to Octopus
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed Dec 21, 2023
1 parent 14bd005 commit ccbbdae
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 5 deletions.
11 changes: 11 additions & 0 deletions example/lib/src/common/widget/router_state_observer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,17 @@ class _RouterStateObserver$Tree extends StatelessWidget {
padding: const EdgeInsets.all(8),
shrinkWrap: true,
children: <Widget>[
SizedBox(
height: 24,
child: Text(
'Intention: ${state.intention.name}',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: const TextStyle(
fontSize: 12,
),
),
),
for (final arg in state.arguments.entries)
SizedBox(
height: 24,
Expand Down
21 changes: 19 additions & 2 deletions lib/src/controller/information_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:meta/meta.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';

/// The route information provider that propagates
/// the platform route information changes.
Expand Down Expand Up @@ -37,6 +38,9 @@ class OctopusInformationProvider extends RouteInformationProvider
ChangeNotifier.maybeDispatchObjectCreation(this);
}
_refreshListenable?.addListener(notifyListeners);
if (kIsWeb) {
SystemNavigator.selectMultiEntryHistory();
}
}

static RouteInformation _initialRouteInformation(String? initialLocation,
Expand Down Expand Up @@ -120,12 +124,25 @@ class OctopusInformationProvider extends RouteInformationProvider
/* if (kIsWeb && routeInformation.uri == _value.uri) {
config('Uri: ${routeInformation.uri}');
} */
SystemNavigator.selectMultiEntryHistory(); // selectSingleEntryHistory
/* SystemNavigator.selectMultiEntryHistory(); // selectSingleEntryHistory
SystemNavigator.routeInformationUpdated(
uri: routeInformation.uri,
state: routeInformation.state,
replace: replace,
);
); */
if (replace) {
SystemNavigatorUtil.replaceState(
data: routeInformation.state,
url: routeInformation.uri,
/* title: , */
);
} else {
SystemNavigatorUtil.pushState(
data: routeInformation.state,
url: routeInformation.uri,
/* title: , */
);
}
_value = _valueInEngine = routeInformation;
}

Expand Down
34 changes: 32 additions & 2 deletions lib/src/controller/octopus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:collection';
import 'dart:convert';
import 'dart:math' as math;

import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:octopus/src/controller/delegate.dart';
import 'package:octopus/src/controller/guard.dart';
Expand Down Expand Up @@ -137,6 +138,10 @@ abstract base class Octopus {
Map<String, String>? arguments,
});

/// Pop a one of the top routes from the navigation stack.
/// If the stack contains only one route, close the application.
Future<OctopusNode?> pop();

/// Pop a one of the top routes from the navigation stack.
/// If the stack contains only one route, nothing will happen.
Future<OctopusNode?> maybePop();
Expand All @@ -155,6 +160,9 @@ abstract base class Octopus {

/// Get a route by name.
OctopusRoute? getRouteByName(String name);

/// Update state arguments
Future<void> setArguments(void Function(Map<String, String> args) change);
}

/// {@nodoc}
Expand Down Expand Up @@ -270,12 +278,26 @@ base mixin _OctopusMethodsMixin on Octopus {
@override
Future<void> setState(
OctopusState Function(OctopusState$Mutable state) change) =>
config.routerDelegate.setNewRoutePath(change(state.mutate()));
config.routerDelegate.setNewRoutePath(
change(state.mutate()..intention = OctopusStateIntention.auto));

@override
Future<void> navigate(String location) =>
config.routerDelegate.setNewRoutePath(StateUtil.decodeLocation(location));

@override
Future<OctopusNode?> pop() {
OctopusNode? result;
return setState((state) {
if (state.children.length < 2) {
SystemNavigator.pop().ignore();
return state;
}
result = state.removeLast();
return state;
}).then((_) => result);
}

@override
Future<OctopusNode?> maybePop() {
OctopusNode? result;
Expand Down Expand Up @@ -375,6 +397,13 @@ base mixin _OctopusMethodsMixin on Octopus {
bool recursive = true,
}) =>
setState((state) => state..replaceAll(fn, recursive: recursive));

@override
Future<void> setArguments(void Function(Map<String, String> args) change) =>
setState((state) {
change(state.arguments);
return state;
});
}

base mixin _OctopusTransactionMixin on Octopus, _OctopusMethodsMixin {
Expand All @@ -391,7 +420,8 @@ base mixin _OctopusTransactionMixin on Octopus, _OctopusMethodsMixin {
if (_txnCompleter == null || _txnCompleter!.isCompleted) {
completer = _txnCompleter = Completer<void>.sync();
scheduleMicrotask(() {
var mutableState = state.mutate();
var mutableState = state.mutate()
..intention = OctopusStateIntention.auto;
final list = _txnQueue.toList(growable: false)
..sort((a, b) => b.$2.compareTo(a.$2));
_txnQueue.clear();
Expand Down
40 changes: 40 additions & 0 deletions lib/src/util/js/system_navigator_util_js.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//import 'dart:html' as html; // ignore: avoid_web_libraries_in_flutter

import 'package:flutter/services.dart';
import 'package:meta/meta.dart';

/// {@nodoc}
@internal
void $pushState(Object? data, String? title, Uri? url) {
// SystemNavigator.selectMultiEntryHistory(); // selectSingleEntryHistory
SystemNavigator.routeInformationUpdated(
uri: url,
state: data,
replace: false,
);
/* html.window.history.pushState(
data,
title ?? html.document.title,
'#$url',
); */
}

/// {@nodoc}
@internal
void $replaceState(
Object? data,
String? title,
Uri? url,
) {
// SystemNavigator.selectMultiEntryHistory(); // selectSingleEntryHistory
SystemNavigator.routeInformationUpdated(
uri: url,
state: data,
replace: true,
);
/* html.window.history.replaceState(
data,
title ?? html.document.title,
'#$url',
); */
}
18 changes: 18 additions & 0 deletions lib/src/util/system_navigator_util.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// ignore_for_file: avoid_classes_with_only_static_members

import 'package:meta/meta.dart';
import 'package:octopus/src/util/js/system_navigator_util_js.dart'
// ignore: uri_does_not_exist
if (dart.library.io) 'package:octopus/src/util/js/system_navigator_util_vm.dart';

/// {@nodoc}
@internal
abstract final class SystemNavigatorUtil {
/// {@nodoc}
static void pushState({Object? data, String? title, Uri? url}) =>
$pushState(data, title, url);

/// {@nodoc}
static void replaceState({Object? data, String? title, Uri? url}) =>
$replaceState(data, title, url);
}
24 changes: 24 additions & 0 deletions lib/src/util/vm/system_navigator_util_vm.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:flutter/services.dart';
import 'package:meta/meta.dart';

/// {@nodoc}
@internal
void $pushState(Object? data, String? title, Uri? url) {
// SystemNavigator.selectMultiEntryHistory(); // selectSingleEntryHistory
SystemNavigator.routeInformationUpdated(
uri: url,
state: data,
replace: false,
);
}

/// {@nodoc}
@internal
void $replaceState(Object? data, String? title, Uri? url) {
// SystemNavigator.selectMultiEntryHistory(); // selectSingleEntryHistory
SystemNavigator.routeInformationUpdated(
uri: url,
state: data,
replace: true,
);
}
2 changes: 1 addition & 1 deletion lib/src/widget/navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ class _OctopusNestedNavigatorBuilderState
);
if (parent == null) return state;
parent.children.add(
OctopusNode.immutable(
OctopusNode.mutable(
bucket,
children: <OctopusNode>[widget.defaultRoute.node()],
),
Expand Down

0 comments on commit ccbbdae

Please sign in to comment.