Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial skeleton for Universal Link handling. #1638

Merged
merged 2 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions ElementX/Sources/Application/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
private let stateMachine: AppCoordinatorStateMachine
private let navigationRootCoordinator: NavigationRootCoordinator
private let userSessionStore: UserSessionStoreProtocol
private let appSettings: AppSettings

/// Common background task to continue long-running tasks in the background.
private var backgroundTask: BackgroundTaskProtocol?
Expand Down Expand Up @@ -71,6 +72,8 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
AppSettings.reset()
}

self.appSettings = appSettings

navigationRootCoordinator = NavigationRootCoordinator()

Self.setupServiceLocator(navigationRootCoordinator: navigationRootCoordinator, appSettings: appSettings)
Expand All @@ -88,21 +91,21 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
userSessionStore = UserSessionStore(backgroundTaskService: backgroundTaskService)

notificationManager = NotificationManager(notificationCenter: UNUserNotificationCenter.current(),
appSettings: ServiceLocator.shared.settings)
appSettings: appSettings)
notificationManager.delegate = self
notificationManager.start()

guard let currentVersion = Version(InfoPlistReader(bundle: .main).bundleShortVersionString) else {
fatalError("The app's version number **must** use semver for migration purposes.")
}

if let previousVersion = ServiceLocator.shared.settings.lastVersionLaunched.flatMap(Version.init) {
if let previousVersion = appSettings.lastVersionLaunched.flatMap(Version.init) {
performMigrationsIfNecessary(from: previousVersion, to: currentVersion)
} else {
// The app has been deleted since the previous run. Reset everything.
wipeUserData(includingSettings: true)
}
ServiceLocator.shared.settings.lastVersionLaunched = currentVersion.description
appSettings.lastVersionLaunched = currentVersion.description

setupStateMachine()

Expand Down Expand Up @@ -132,6 +135,15 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
)
}

func handleUniversalLink(_ url: URL) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should rename this to just handleURL as it should probably handle non-universal links too

// Parse into an AppRoute to redirect these in a type safe way.

// Until we have an OIDC callback AppRoute, handle it manually.
if url.absoluteString.starts(with: appSettings.oidcRedirectURL.absoluteString) {
MXLog.error("OIDC callback through Universal Links not implemented.")
}
}

// MARK: - AuthenticationCoordinatorDelegate

func authenticationCoordinator(_ authenticationCoordinator: AuthenticationCoordinator, didLoginWithSession userSession: UserSessionProtocol) {
Expand Down Expand Up @@ -207,13 +219,13 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
ServiceLocator.shared.register(userIndicatorController: UserIndicatorController(rootCoordinator: navigationRootCoordinator))
ServiceLocator.shared.register(appSettings: appSettings)
ServiceLocator.shared.register(networkMonitor: NetworkMonitor())
ServiceLocator.shared.register(bugReportService: BugReportService(withBaseURL: ServiceLocator.shared.settings.bugReportServiceBaseURL,
sentryURL: ServiceLocator.shared.settings.bugReportSentryURL,
applicationId: ServiceLocator.shared.settings.bugReportApplicationId,
ServiceLocator.shared.register(bugReportService: BugReportService(withBaseURL: appSettings.bugReportServiceBaseURL,
sentryURL: appSettings.bugReportSentryURL,
applicationId: appSettings.bugReportApplicationId,
sdkGitSHA: sdkGitSha(),
maxUploadSize: ServiceLocator.shared.settings.bugReportMaxUploadSize))
maxUploadSize: appSettings.bugReportMaxUploadSize))
ServiceLocator.shared.register(analytics: AnalyticsService(client: PostHogAnalyticsClient(),
appSettings: ServiceLocator.shared.settings,
appSettings: appSettings,
bugReportService: ServiceLocator.shared.bugReportService))
}

Expand All @@ -231,7 +243,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
if oldVersion < Version(1, 1, 7) {
MXLog.info("Migrating to v1.1.0, marking accounts as migrated.")
for userID in userSessionStore.userIDs {
ServiceLocator.shared.settings.migratedAccounts[userID] = true
appSettings.migratedAccounts[userID] = true
}
}
}
Expand Down Expand Up @@ -300,10 +312,10 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,

private func startAuthentication() {
let authenticationNavigationStackCoordinator = NavigationStackCoordinator()
let authenticationService = AuthenticationServiceProxy(userSessionStore: userSessionStore, appSettings: ServiceLocator.shared.settings)
let authenticationService = AuthenticationServiceProxy(userSessionStore: userSessionStore, appSettings: appSettings)
authenticationCoordinator = AuthenticationCoordinator(authenticationService: authenticationService,
navigationStackCoordinator: authenticationNavigationStackCoordinator,
appSettings: ServiceLocator.shared.settings,
appSettings: appSettings,
analytics: ServiceLocator.shared.analytics,
userIndicatorController: ServiceLocator.shared.userIndicatorController)
authenticationCoordinator?.delegate = self
Expand All @@ -329,7 +341,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
userDisplayName: displayName,
deviceID: userSession.deviceID)

let authenticationService = AuthenticationServiceProxy(userSessionStore: userSessionStore, appSettings: ServiceLocator.shared.settings)
let authenticationService = AuthenticationServiceProxy(userSessionStore: userSessionStore, appSettings: appSettings)
_ = await authenticationService.configure(for: userSession.homeserver)

let parameters = SoftLogoutScreenCoordinatorParameters(authenticationService: authenticationService,
Expand Down Expand Up @@ -361,7 +373,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
navigationSplitCoordinator: navigationSplitCoordinator,
bugReportService: ServiceLocator.shared.bugReportService,
roomTimelineControllerFactory: RoomTimelineControllerFactory(),
appSettings: ServiceLocator.shared.settings,
appSettings: appSettings,
analytics: ServiceLocator.shared.analytics)

userSessionFlowCoordinator.callback = { [weak self] action in
Expand Down Expand Up @@ -668,7 +680,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
// MARK: Background app refresh

private func registerBackgroundAppRefresh() {
let result = BGTaskScheduler.shared.register(forTaskWithIdentifier: ServiceLocator.shared.settings.backgroundAppRefreshTaskIdentifier, using: .main) { [weak self] task in
let result = BGTaskScheduler.shared.register(forTaskWithIdentifier: appSettings.backgroundAppRefreshTaskIdentifier, using: .main) { [weak self] task in
guard let task = task as? BGAppRefreshTask else {
MXLog.error("Invalid background app refresh configuration")
return
Expand All @@ -681,7 +693,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
}

private func scheduleBackgroundAppRefresh() {
let request = BGAppRefreshTaskRequest(identifier: ServiceLocator.shared.settings.backgroundAppRefreshTaskIdentifier)
let request = BGAppRefreshTaskRequest(identifier: appSettings.backgroundAppRefreshTaskIdentifier)

// We have other background tasks that keep the app alive
request.earliestBeginDate = Date(timeIntervalSinceNow: 30)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ import Foundation

protocol AppCoordinatorProtocol: CoordinatorProtocol {
var notificationManager: NotificationManagerProtocol { get }
func handleUniversalLink(_ url: URL)
}
1 change: 1 addition & 0 deletions ElementX/Sources/Application/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct Application: App {
WindowGroup {
appCoordinator.toPresentable()
.statusBarHidden(shouldHideStatusBar)
.onOpenURL { appCoordinator.handleUniversalLink($0) }
.introspect(.window, on: .iOS(.v16)) { window in
// Workaround for SwiftUI not consistently applying the tint colour to Alerts/Confirmation Dialogs.
window.tintColor = .compound.textActionPrimary
Expand Down
4 changes: 4 additions & 0 deletions ElementX/Sources/UITests/UITestsAppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class UITestsAppCoordinator: AppCoordinatorProtocol {
func toPresentable() -> AnyView {
navigationRootCoordinator.toPresentable()
}

func handleUniversalLink(_ url: URL) {
fatalError("Not implemented.")
}
}

@MainActor
Expand Down
4 changes: 4 additions & 0 deletions ElementX/Sources/UnitTests/UnitTestsAppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ class UnitTestsAppCoordinator: AppCoordinatorProtocol {
func toPresentable() -> AnyView {
AnyView(ProgressView("Running Unit Tests"))
}

func handleUniversalLink(_ url: URL) {
fatalError("Not implemented.")
}
}
1 change: 1 addition & 0 deletions changelog.d/pr-1638.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hook up universal links to the App Coordinator (this doesn't actually handle them yet).