Skip to content

Commit

Permalink
[fix] incorrect handling of uris (e.g. deep links)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bungeefan committed Mar 9, 2024
1 parent dcda31e commit 986a633
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 15 deletions.
11 changes: 3 additions & 8 deletions package/lib/src/beam_location.dart
Original file line number Diff line number Diff line change
Expand Up @@ -520,14 +520,9 @@ class RoutesBeamLocation extends BeamLocation<BeamState> {

final matched = <Pattern, String>{};
var overrideNotFound = false;
if (routeInformation.uri.toString() != '/' &&
(routeInformation.uri.toString().endsWith('/'))) {
final location = routeInformation.uri.toString();
routeInformation = routeInformation.copyWith(
location: location.substring(0, location.length - 1),
);
}
final uri = routeInformation.uri;
final uri = routeInformation.uri.replace(
path: Utils.trimmed(routeInformation.uri.path),
);

for (final route in routes) {
if (route is String) {
Expand Down
5 changes: 3 additions & 2 deletions package/lib/src/beamer_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class BeamerDelegate extends RouterDelegate<RouteInformation>
/// This is not null only if multiple [Beamer]s are used;
/// `*App.router` and at least one more [Beamer] in the Widget tree.
BeamerDelegate? get parent => _parent;

set parent(BeamerDelegate? parent) {
if (parent == null && _parent != null) {
_parent!.removeListener(_updateFromParent);
Expand Down Expand Up @@ -813,7 +814,7 @@ class BeamerDelegate extends RouterDelegate<RouteInformation>

@override
SynchronousFuture<void> setNewRoutePath(RouteInformation configuration) {
if (configuration.uri.toString() == '/' && initialPath != '/') {
if (configuration.uri.path == '/' && initialPath != '/') {
configuration = configuration.copyWith(location: initialPath);
}
update(configuration: configuration);
Expand Down Expand Up @@ -907,7 +908,7 @@ class BeamerDelegate extends RouterDelegate<RouteInformation>
);
}

if (clearBeamingHistoryOn.contains(configuration.uri.toString())) {
if (clearBeamingHistoryOn.contains(configuration.uri.path)) {
_clearBeamingHistory();
}
}
Expand Down
16 changes: 11 additions & 5 deletions package/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,18 @@ abstract class Utils {
RouteInformation current,
RouteInformation incoming,
) {
final incomingLocation = incoming.uri.toString();
if (!incomingLocation.startsWith('/')) {
if (!incoming.uri.hasAbsolutePath && !incoming.uri.hasEmptyPath) {
String currentPath = current.uri.path.endsWith('/')
? current.uri.path
: '${current.uri.path}/';
return current.copyWith(
location: current.uri.toString().endsWith('/')
? '${current.uri}$incomingLocation'
: '${current.uri}/$incomingLocation',
location: current.uri
.replace(
path: currentPath + incoming.uri.path,
query: incoming.uri.hasQuery ? incoming.uri.query : null,
fragment: incoming.uri.hasFragment ? incoming.uri.fragment : null,
)
.toString(),
state: incoming.state,
);
}
Expand Down
51 changes: 51 additions & 0 deletions package/test/utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,39 @@ void main() {
.toString(),
'/incoming',
);
expect(
Utils.maybeAppend(current, RouteInformation(uri: Uri.parse('')))
.uri
.toString(),
'',
);
expect(
Utils.maybeAppend(current, RouteInformation(uri: Uri.parse('/')))
.uri
.toString(),
'/',
);
expect(
Utils.maybeAppend(current,
RouteInformation(uri: Uri.parse('example://app/incoming')))
.uri
.toString(),
'example://app/incoming',
);
expect(
Utils.maybeAppend(current,
RouteInformation(uri: Uri.parse('example://app/incoming')))
.uri
.toString(),
'example://app/incoming',
);
expect(
Utils.maybeAppend(
current, RouteInformation(uri: Uri.parse('//app/incoming')))
.uri
.toString(),
'//app/incoming',
);
});

test('Appending with new routeState', () {
Expand All @@ -167,6 +200,24 @@ void main() {
.state,
42,
);
expect(
Utils.maybeAppend(
current,
RouteInformation(
uri: Uri.parse('example://app/incoming'),
state: 42,
)).state,
42,
);
expect(
Utils.maybeAppend(
current,
RouteInformation(
uri: Uri.parse('//app/incoming'),
state: 42,
)).state,
42,
);
});
});

Expand Down

0 comments on commit 986a633

Please sign in to comment.