Skip to content

Commit

Permalink
Improve deep link handling on cold start (#2026)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nathreed authored Apr 2, 2024
1 parent eb82a67 commit 8038e8e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
2 changes: 1 addition & 1 deletion IceCubesApp/App/SafariRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 50 additions & 11 deletions Packages/Env/Sources/Env/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit 8038e8e

Please sign in to comment.