resolveScreen static method
The layout and params screen opens.
Implementation
@visibleForTesting
static (String, Map<String, Object?>)? resolveScreen(PublishModel model, String? screen) {
if (screen == null || screen.trim().isEmpty || screen.trim() == '/') return null;
// Encode what a typed location may hold raw (spaces, `ยท`, a lone `%`),
// keeping its escapes: a link's location is already encoded, and
// encoding it again turned `?toast=Loaded%20%E2%80%9C...` into a toast
// reading `Loaded%20%E2%80%9C...`.
final location = encodeRawLocation(screen.trim());
final CanvasLocationMatch? match;
final Uri? uri;
try {
match = CanvasLocation.match(model, location);
uri = Uri.parse(location);
} on FormatException {
return null;
} on ArgumentError {
return null;
}
final segments = uri.pathSegments.where((s) => s.isNotEmpty).toList();
if (match != null) {
// A catch-all (`*`, an app's not-found screen) takes only what names
// no screen: a one-segment location that is a screen's id or name
// opens that screen, as it would with no catch-all.
final path = CanvasLocation.pathOf(model, match.canvasId);
final catchAll = path != null && CanvasLocation.isCatchAll(CanvasLocation.segmentsOf(path).last);
if (catchAll && segments.length == 1) {
final id = model.layoutIdForScreen(Uri.encodeComponent(segments.single));
if (id != null) return (id, {...uri.queryParameters});
}
return (match.layoutId, match.params);
}
// The app's root with a query (`/?state=empty`): the entry screen,
// opened with those params.
if (segments.isEmpty && uri.queryParameters.isNotEmpty) {
final entry = model.entryLayoutId;
return entry == null ? null : (entry, {...uri.queryParameters});
}
final id = model.layoutIdForScreen(segments.isEmpty ? screen.trim() : Uri.encodeComponent(segments.last));
return id == null ? null : (id, {...uri.queryParameters});
}