Improve deep link handling on cold start (#2026)

Previously, if the app was not already running when the Safari action extension was used to open a post in the app, the post would open in the in-app Safari instead of using the Ice Cubes UI.
The action extension only worked well if Ice Cubes was already running but backgrounded when it was used.
This was because of the `hasConnection(with:)` check used to ensure that the current server has a federation relationship with the server the post is on.
Early in app launch, the list of federated peers has not come back from the API request yet, so `hasConnection(with:)` was always returning `false`.

To fix, issue a request to fetch the peers as part of the URL handling process, before checking `hasConnection(with:)` to make the final navigation decision.
As an optimization, only do this if `hasConnection(with:)` returns `false` initially -- if it returns `true`, we already know a connection exists so no need to check again.
This commit is contained in:
Nathan Reed 2024-04-02 02:26:58 -04:00 committed by GitHub
parent eb82a67671
commit 8038e8e6af
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 51 additions and 12 deletions

View file

@ -31,7 +31,7 @@ private struct SafariRouter: ViewModifier {
// Open external URL (from icecubesapp://)
let urlString = url.absoluteString.replacingOccurrences(of: AppInfo.scheme, with: "https://")
guard let url = URL(string: urlString), url.host != nil else { return }
_ = routerPath.handle(url: url)
_ = routerPath.handleDeepLink(url: url)
}
.onAppear {
routerPath.urlHandler = { url in

View file

@ -171,20 +171,59 @@ public enum SheetDestination: Identifiable, Hashable {
await navigateToAccountFrom(acct: acct, url: url)
}
return .handled
} else if let client,
client.isAuth,
client.hasConnection(with: url),
let id = Int(url.lastPathComponent)
{
if url.absoluteString.contains(client.server) {
navigate(to: .statusDetail(id: String(id)))
} else {
navigate(to: .remoteStatusDetail(url: url))
}
return .handled
}
return urlHandler?(url) ?? .systemAction
}
public func handleDeepLink(url: URL) -> OpenURLAction.Result {
guard let client,
client.isAuth,
let id = Int(url.lastPathComponent) else {
return urlHandler?(url) ?? .systemAction
}
// First check whether we already know that the client's server federates with the server this post is on
if client.hasConnection(with: url) {
navigateToStatus(url: url, id: id)
return .handled
}
Task {
// Client does not currently report a federation relationship, but that doesn't mean none exists
// Ensure client is aware of all peers its server federates with so it can give a meaningful answer to hasConnection(with:)
do {
let connections: [String] = try await client.get(endpoint: Instances.peers)
client.addConnections(connections)
} catch {
handlerOrDefault(url: url)
return
}
guard client.hasConnection(with: url) else {
handlerOrDefault(url: url)
return
}
navigateToStatus(url: url, id: id)
}
return .handled
}
private func navigateToStatus(url: URL, id: Int) {
guard let client else { return }
if url.absoluteString.contains(client.server) {
navigate(to: .statusDetail(id: String(id)))
} else {
navigate(to: .remoteStatusDetail(url: url))
}
}
private func handlerOrDefault(url: URL) {
if let urlHandler {
_ = urlHandler(url)
} else {
UIApplication.shared.open(url)
}
}
public func navigateToAccountFrom(acct: String, url: URL) async {
guard let client else { return }