IOS-123: Open profiles and statuses in app (#981)

This commit is contained in:
Nathan Mattes 2023-03-17 10:47:19 +01:00 committed by GitHub
parent f6f90bda9d
commit 245582961b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 19 deletions

View File

@ -4,6 +4,11 @@
<dict>
<key>aps-environment</key>
<string>development</string>
<key>com.apple.developer.associated-domains</key>
<array>
<string>applinks:mastodon.social</string>
<string>applinks:mastodon.online</string>
</array>
<key>com.apple.developer.siri</key>
<true/>
<key>com.apple.security.application-groups</key>

View File

@ -72,6 +72,10 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
handleUrl(context: urlContext)
}
if let userActivity = connectionOptions.userActivities.first {
handleUniversalLink(userActivity: userActivity)
}
#if SNAPSHOT
// speedup animation
// window.layer.speed = 999
@ -104,13 +108,6 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
#endif
}
func sceneDidDisconnect(_ scene: UIScene) {
// Called as the scene is being released by the system.
// This occurs shortly after the scene enters the background, or when its session is discarded.
// Release any resources associated with this scene that can be re-created the next time the scene connects.
// The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
}
func sceneDidBecomeActive(_ scene: UIScene) {
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
@ -135,22 +132,68 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
}
}
func sceneWillResignActive(_ scene: UIScene) {
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
handleUniversalLink(userActivity: userActivity)
}
func sceneWillEnterForeground(_ scene: UIScene) {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
}
private func handleUniversalLink(userActivity: NSUserActivity) {
guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
let incomingURL = userActivity.webpageURL,
let components = NSURLComponents(url: incomingURL, resolvingAgainstBaseURL: true) else {
return
}
func sceneDidEnterBackground(_ scene: UIScene) {
// Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information
// to restore the scene back to its current state.
}
guard let path = components.path, let authContext = coordinator?.authContext else {
return
}
let pathElements = path.split(separator: "/")
let profile: String?
if let profileInPath = pathElements[safe: 0] {
profile = String(profileInPath)
} else {
profile = nil
}
let statusID: String?
if let statusIDInPath = pathElements[safe: 1] {
statusID = String(statusIDInPath)
} else {
statusID = nil
}
switch (profile, statusID) {
case (profile, nil):
let profileViewModel = RemoteProfileViewModel(
context: AppContext.shared,
authContext: authContext,
acct: incomingURL.absoluteString
)
self.coordinator?.present(
scene: .profile(viewModel: profileViewModel),
from: nil,
transition: .show
)
case (profile, statusID):
Task {
guard let statusOnMyInstance = try await AppContext.shared.apiService.search(query: .init(q: incomingURL.absoluteString, resolve: true), authenticationBox: authContext.mastodonAuthenticationBox).value.statuses.first else { return }
let threadViewModel = RemoteThreadViewModel(
context: AppContext.shared,
authContext: authContext,
statusID: statusOnMyInstance.id
)
coordinator?.present(scene: .thread(viewModel: threadViewModel), from: nil, transition: .show)
}
case (_, _):
break
// do nothing
}
}
}
extension SceneDelegate {
@ -298,3 +341,4 @@ extension SceneDelegate {
}
}
}