Skip to content

Commit a74f40f

Browse files
Copilotbgoncal
andcommitted
Create URLOpenerService and refactor all UIApplication.shared.open calls
Co-authored-by: bgoncal <5808343+bgoncal@users.noreply.github.com>
1 parent 1dd5598 commit a74f40f

File tree

19 files changed

+113
-24
lines changed

19 files changed

+113
-24
lines changed

Sources/App/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
271271
title: L10n.Updater.UpdateAvailable.open(update.name),
272272
style: .default,
273273
handler: { _ in
274-
UIApplication.shared.open(update.htmlUrl, options: [:], completionHandler: nil)
274+
Current.urlOpener.open(update.htmlUrl, options: [:], completionHandler: nil)
275275
}
276276
))
277277
alert.addAction(UIAlertAction(title: L10n.okLabel, style: .cancel, handler: nil))

Sources/App/Onboarding/Steps/Permissions/OnboardingPermissionsNavigationViewModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ final class OnboardingPermissionsNavigationViewModel: NSObject, ObservableObject
209209
case .denied, .restricted:
210210
// Open iOS settings for user to manually enable location
211211
if let settingsUrl = URL(string: UIApplication.openSettingsURLString) {
212-
UIApplication.shared.open(settingsUrl)
212+
Current.urlOpener.open(settingsUrl, options: [:], completionHandler: nil)
213213
}
214214
case .authorizedWhenInUse, .authorizedAlways:
215215
// Permission already granted, apply the context-specific needs

Sources/App/Scenes/WebViewSceneDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ final class WebViewSceneDelegate: NSObject, UIWindowSceneDelegate {
4646
let shortcutItem = connectionOptions.shortcutItem {
4747
self.windowScene(scene, performActionFor: shortcutItem, completionHandler: { _ in })
4848
} else if let url = Current.servers.all.first?.info.connection.activeURL() {
49-
UIApplication.shared.open(url)
49+
Current.urlOpener.open(url, options: [:], completionHandler: nil)
5050
// Close window to avoid empty window left behind
5151
if let scene = window.windowScene {
5252
UIApplication.shared.requestSceneSessionDestruction(scene.session, options: nil, errorHandler: nil)

Sources/App/Settings/Connection/ConnectionSettingsViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ class ConnectionSettingsViewController: HAFormViewController, RowControllerType
372372
@objc private func activateServerTapped() {
373373
if Current.isCatalyst, Current.settingsStore.macNativeFeaturesOnly {
374374
if let url = server.info.connection.activeURL() {
375-
UIApplication.shared.open(url)
375+
Current.urlOpener.open(url, options: [:], completionHandler: nil)
376376
}
377377
} else {
378378
Current.sceneManager.webViewWindowControllerPromise.done {

Sources/App/Settings/DebugView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct DebugView: View {
3838
logsURL = url
3939
if Current.isCatalyst {
4040
if let url = Current.Log.archiveURL() {
41-
UIApplication.shared.open(url, options: [:], completionHandler: nil)
41+
Current.urlOpener.open(url, options: [:], completionHandler: nil)
4242
}
4343
} else {
4444
loadingLogs = true

Sources/App/Settings/Notifications/NotificationSoundsViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class NotificationSoundsViewController: HAFormViewController, UIDocumentPickerDe
100100
$0.title = L10n.SettingsDetails.Notifications.Sounds.importMacOpenFolder
101101
$0.onCellSelection { _, _ in
102102
do {
103-
try UIApplication.shared.open(self.librarySoundsURL(), options: [:], completionHandler: nil)
103+
try Current.urlOpener.open(self.librarySoundsURL(), options: [:], completionHandler: nil)
104104
} catch {
105105
Current.Log.error("couldn't open folder: \(error)")
106106
}

Sources/App/Settings/SettingsDetailViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class SettingsDetailViewController: HAFormViewController, TypedRowControllerType
195195
$0.accessoryIcon = .openInNewIcon
196196
$0.onCellSelection { _, row in
197197
guard let url = URL(string: "https://companion.home-assistant.io/docs/core/actions/") else { return }
198-
UIApplication.shared.open(url)
198+
Current.urlOpener.open(url, options: [:], completionHandler: nil)
199199
row.deselect(animated: true)
200200
}
201201
}

Sources/App/Utilities/Extensions/UIApplication+OpenSettings.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ extension UIApplication {
4242

4343
func openSettings(destination: OpenSettingsDestination, completionHandler: ((Bool) -> Void)? = nil) {
4444
if let url = destination.url {
45-
UIApplication.shared.open(url, options: [:], completionHandler: completionHandler)
45+
Current.urlOpener.open(url, options: [:], completionHandler: completionHandler)
4646
} else {
4747
completionHandler?(false)
4848
}

Sources/App/Utilities/OpenInChromeController.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ open class OpenInChromeController {
5454
open func isChromeInstalled() -> Bool {
5555
if let simpleURL = URL(string: googleChromeHTTPScheme),
5656
let callbackURL = URL(string: googleChromeCallbackScheme) {
57-
return UIApplication.shared.canOpenURL(simpleURL) || UIApplication.shared.canOpenURL(callbackURL)
57+
return Current.urlOpener.canOpenURL(simpleURL) || Current.urlOpener.canOpenURL(callbackURL)
5858
}
5959
return false
6060
}
@@ -63,7 +63,7 @@ open class OpenInChromeController {
6363
open func openInChrome(_ url: URL, callbackURL: URL?, createNewTab: Bool = false) {
6464
if let chromeSimpleURL = URL(string: googleChromeHTTPScheme),
6565
let chromeCallbackURL = URL(string: googleChromeCallbackScheme) {
66-
if UIApplication.shared.canOpenURL(chromeCallbackURL) {
66+
if Current.urlOpener.canOpenURL(chromeCallbackURL) {
6767
var appName = Bundle.main.infoDictionary?["CFBundleDisplayName"] as? String
6868
// CFBundleDisplayName is an optional key, so we will use CFBundleName if it does not exist
6969
if appName == nil {
@@ -87,10 +87,10 @@ open class OpenInChromeController {
8787
chromeURLString += "&create-new-tab"
8888
}
8989
if let chromeURL = URL(string: chromeURLString) {
90-
UIApplication.shared.open(chromeURL, options: [:], completionHandler: nil)
90+
Current.urlOpener.open(chromeURL, options: [:], completionHandler: nil)
9191
}
9292
}
93-
} else if UIApplication.shared.canOpenURL(chromeSimpleURL) {
93+
} else if Current.urlOpener.canOpenURL(chromeSimpleURL) {
9494
let scheme = url.scheme?.lowercased()
9595
var chromeScheme: String?
9696
if scheme == "http" {
@@ -102,7 +102,7 @@ open class OpenInChromeController {
102102
let absoluteURLString = url.absoluteString
103103
let lowerBound = absoluteURLString.range(of: ":")!.lowerBound
104104
let chromeURLString = chromeScheme + absoluteURLString.suffix(from: lowerBound)
105-
UIApplication.shared.open(URL(string: chromeURLString)!, options: [:], completionHandler: nil)
105+
Current.urlOpener.open(URL(string: chromeURLString)!, options: [:], completionHandler: nil)
106106
}
107107
}
108108
}

Sources/App/Utilities/Utils.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func resetStores() {
2121

2222
func openURLInBrowser(_ urlToOpen: URL, _ sender: UIViewController?) {
2323
guard ["http", "https"].contains(urlToOpen.scheme?.lowercased()) else {
24-
UIApplication.shared.open(urlToOpen, options: [:], completionHandler: nil)
24+
Current.urlOpener.open(urlToOpen, options: [:], completionHandler: nil)
2525
return
2626
}
2727

@@ -42,7 +42,7 @@ func openURLInBrowser(_ urlToOpen: URL, _ sender: UIViewController?) {
4242
let sfv = SFSafariViewController(url: urlToOpen)
4343
sender!.present(sfv, animated: true)
4444
default:
45-
UIApplication.shared.open(urlToOpen, options: [:], completionHandler: nil)
45+
Current.urlOpener.open(urlToOpen, options: [:], completionHandler: nil)
4646
}
4747
}
4848

0 commit comments

Comments
 (0)