Skip to content

Commit f063c3a

Browse files
committed
feat: support redirect by NavigatorPushBeginHandle & support popResult by NavigatorPushEndHandle
1 parent 5da639f commit f063c3a

File tree

7 files changed

+111
-22
lines changed

7 files changed

+111
-22
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 4.17.0
2+
3+
- feat: support redirect by NavigatorPushBeginHandle
4+
- feat: support popResult by NavigatorPushEndHandle
5+
16
## 4.16.3
27

38
- fix: try to fix NavigatorPageView RangeError

lib/src/navigator/navigator_types.dart

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
// ignore_for_file: avoid_positional_boolean_parameters
2323

24+
import 'dart:async';
25+
2426
import 'package:flutter/widgets.dart';
2527

2628
import 'navigator_route.dart';
@@ -65,10 +67,23 @@ typedef NavigatorRoutePushHandle = Future<NavigatorRoutePushHandleType>
6567
bool animated,
6668
});
6769

68-
/// Signature of push begin/return handler with url.
69-
typedef NavigatorPushHandle = Future<void> Function<TParams>(
70+
/// Signature of push begin handler with url.
71+
///
72+
/// return a new url for redirect.
73+
///
74+
typedef NavigatorPushBeginHandle = Future<String?> Function<TParams>(
75+
String url, {
76+
TParams? params,
77+
String? fromURL,
78+
String? innerURL,
79+
});
80+
81+
/// Signature of push end handler.
82+
///
83+
typedef NavigatorPushEndHandle = Future<void> Function<TParams, TPopParams>(
7084
String url, {
7185
TParams? params,
7286
String? fromURL,
7387
String? innerURL,
88+
TPopParams? popResult,
7489
});

lib/src/navigator/thrio_navigator.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ import 'thrio_navigator_implement.dart';
3232
abstract class ThrioNavigator {
3333
/// Register a handle called before push.
3434
///
35-
static VoidCallback registerPushBeginHandle(NavigatorPushHandle handle) =>
35+
static VoidCallback registerPushBeginHandle(
36+
NavigatorPushBeginHandle handle) =>
3637
ThrioNavigatorImplement.shared().registerPushBeginHandle(handle);
3738

3839
/// Register a handle called before the push return.
3940
///
40-
static VoidCallback registerPushReturnHandle(NavigatorPushHandle handle) =>
41-
ThrioNavigatorImplement.shared().registerPushReturnHandle(handle);
41+
static VoidCallback registerPushEndHandle(NavigatorPushEndHandle handle) =>
42+
ThrioNavigatorImplement.shared().registerPushEndHandle(handle);
4243

4344
/// Push the page onto the navigation stack.
4445
///

lib/src/navigator/thrio_navigator_implement.dart

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import '../extension/thrio_uri_string.dart';
3232
import '../module/module_anchor.dart';
3333
import '../module/module_types.dart';
3434
import '../module/thrio_module.dart';
35-
import '../registry/registry_set.dart';
35+
import '../registry/registry_order_set.dart';
3636
import 'navigator_dialog_route.dart';
3737
import 'navigator_material_app.dart';
3838
import 'navigator_observer_manager.dart';
@@ -111,9 +111,9 @@ class ThrioNavigatorImplement {
111111

112112
NavigatorWidgetState? get navigatorState => _stateKey.currentState;
113113

114-
final _pushBeginHandlers = RegistrySet<NavigatorPushHandle>();
114+
final _pushBeginHandlers = RegistryOrderSet<NavigatorPushBeginHandle>();
115115

116-
final _pushReturnHandlers = RegistrySet<NavigatorPushHandle>();
116+
final _pushEndHandlers = RegistryOrderSet<NavigatorPushEndHandle>();
117117

118118
final poppedResults = <String, NavigatorParamsCallback>{};
119119

@@ -146,11 +146,11 @@ class ThrioNavigatorImplement {
146146
_channel.invokeMethod<bool>('ready');
147147
}
148148

149-
VoidCallback registerPushBeginHandle(NavigatorPushHandle handle) =>
149+
VoidCallback registerPushBeginHandle(NavigatorPushBeginHandle handle) =>
150150
_pushBeginHandlers.registry(handle);
151151

152-
VoidCallback registerPushReturnHandle(NavigatorPushHandle handle) =>
153-
_pushReturnHandlers.registry(handle);
152+
VoidCallback registerPushEndHandle(NavigatorPushEndHandle handle) =>
153+
_pushEndHandlers.registry(handle);
154154

155155
Future<TPopParams?> push<TParams, TPopParams>({
156156
required String url,
@@ -160,7 +160,7 @@ class ThrioNavigatorImplement {
160160
String? fromURL,
161161
String? innerURL,
162162
}) async {
163-
await _onPushBeginHandle(
163+
final newURL = await _onPushBeginHandle(
164164
url: url,
165165
params: params,
166166
fromURL: fromURL,
@@ -170,7 +170,7 @@ class ThrioNavigatorImplement {
170170
final completer = Completer<TPopParams?>();
171171

172172
final handled = await _pushToHandler(
173-
url: url,
173+
url: newURL,
174174
params: params,
175175
animated: animated,
176176
completer: completer,
@@ -182,7 +182,7 @@ class ThrioNavigatorImplement {
182182
Future<void> pushFuture() {
183183
final resultCompleter = Completer();
184184
_pushToNative<TParams, TPopParams>(
185-
url: url,
185+
url: newURL,
186186
params: params,
187187
animated: animated,
188188
completer: completer,
@@ -201,25 +201,40 @@ class ThrioNavigatorImplement {
201201
return completer.future;
202202
}
203203

204-
Future<void> _onPushBeginHandle<TParams>({
204+
Future<String> _onPushBeginHandle<TParams>({
205205
required String url,
206206
TParams? params,
207207
String? fromURL,
208208
String? innerURL,
209209
}) async {
210-
for (final handle in _pushBeginHandlers) {
211-
await handle(url, params: params, fromURL: fromURL, innerURL: innerURL);
210+
final reversed = _pushBeginHandlers.reversed;
211+
var newURL = url;
212+
for (final handle in reversed) {
213+
final ret = await handle(url,
214+
params: params, fromURL: fromURL, innerURL: innerURL);
215+
if (ret != null) {
216+
newURL = ret;
217+
}
212218
}
219+
return newURL;
213220
}
214221

215-
Future<void> _onPushReturnHandle<TParams>({
222+
Future<void> _onPushReturnHandle<TParams, TPopParams>({
216223
required String url,
217224
TParams? params,
218225
String? fromURL,
219226
String? innerURL,
227+
TPopParams? popResult,
220228
}) async {
221-
for (final handle in _pushReturnHandlers) {
222-
await handle(url, params: params, fromURL: fromURL, innerURL: innerURL);
229+
final reversed = _pushEndHandlers.reversed;
230+
for (final handle in reversed) {
231+
await handle(
232+
url,
233+
params: params,
234+
fromURL: fromURL,
235+
innerURL: innerURL,
236+
popResult: popResult,
237+
);
223238
}
224239
}
225240

@@ -719,6 +734,7 @@ class ThrioNavigatorImplement {
719734
params: params,
720735
fromURL: fromURL,
721736
innerURL: innerURL,
737+
popResult: value,
722738
);
723739
}));
724740

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// The MIT License (MIT)
2+
//
3+
// Copyright (c) 2024 foxsofter
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a
6+
// copy of this software and associated documentation files (the "Software"),
7+
// to deal in the Software without restriction, including without limitation
8+
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
9+
// and/or sell copies of the Software, and to permit persons to whom the
10+
// Software is furnished to do so, subject to the following conditions:
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17+
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20+
// IN THE SOFTWARE.
21+
22+
import 'dart:collection';
23+
import 'package:flutter/foundation.dart';
24+
25+
class RegistryOrderSet<T> with IterableMixin<T> {
26+
final List<T> _sets = [];
27+
28+
VoidCallback registry(T value) {
29+
assert(value != null, 'value must not be null.');
30+
_sets
31+
..remove(value)
32+
..add(value);
33+
return () {
34+
_sets.remove(value);
35+
};
36+
}
37+
38+
VoidCallback registryAll(Set<T> values) {
39+
assert(values.isNotEmpty, 'values must not be null or empty');
40+
values.forEach(_sets.remove);
41+
_sets.addAll(values);
42+
return () {
43+
values.forEach(_sets.remove);
44+
};
45+
}
46+
47+
void clear() => _sets.clear();
48+
49+
@override
50+
Iterator<T> get iterator => _sets.iterator;
51+
52+
Iterable<T> get reversed => _sets.reversed;
53+
}

lib/src/registry/registry_set.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import 'dart:collection';
2323
import 'package:flutter/foundation.dart';
2424

25-
// ignore: prefer_mixin
2625
class RegistrySet<T> with IterableMixin<T> {
2726
final Set<T> _sets = {};
2827

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_thrio
22
description: Thrio makes it easy and fast to add flutter to existing mobile applications, and provide a simple and consistent navigator APIs.
3-
version: 4.16.3
3+
version: 4.17.0
44
homepage: https://github.com/flutter-thrio/flutter_thrio
55

66
environment:

0 commit comments

Comments
 (0)