Skip to content

Commit

Permalink
Navigate to default server when deeplink to the app passing "?server=…
Browse files Browse the repository at this point in the history
…default" (#2392)

## Summary
Navigate to default server when deeplink to the app passing
"?server=default".
Currently when you use deep link to navigate to one lovelace page and
you have multiple HA servers in your iOS App it always asks which server
you want to use.
With this change you are able to default to your current server by using
a query item "?server=default"

## Link to pull request in Documentation repository
<!-- Pull requests that add, change or remove functionality must have a
corresponding pull request in the Companion App Documentation repository
(https://github.com/home-assistant/companion.home-assistant). Please add
the number of this pull request after the "#" -->
Documentation: home-assistant/companion.home-assistant#



https://github.com/home-assistant/iOS/assets/5808343/c29ac74e-8ea4-4d5a-93ec-53ef7c64ced6
  • Loading branch information
bgoncal authored Nov 6, 2023
1 parent de1fc74 commit 0deb391
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ PLATFORMS
arm64-darwin-22
x86_64-darwin-20
x86_64-darwin-21
x86_64-darwin-22

DEPENDENCIES
cocoapods
Expand Down
15 changes: 13 additions & 2 deletions Sources/App/WebView/IncomingURLHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class IncomingURLHandler {
components.scheme = nil
components.host = nil

let queryParameters = components.queryItems
let isFromWidget = components.popWidgetAuthenticity()
let server = components.popWidgetServer(isFromWidget: isFromWidget)

Expand All @@ -61,12 +62,22 @@ class IncomingURLHandler {
presenting is SFSafariViewController {
// Dismiss my.* controller if it's on top - we don't get any other indication
presenting.dismiss(animated: true, completion: { [windowController] in
windowController.openSelectingServer(from: .deeplink, urlString: rawURL, skipConfirm: true)
windowController.openSelectingServer(
from: .deeplink,
urlString: rawURL,
skipConfirm: true,
queryParameters: queryParameters
)
})
} else if let server = server {
windowController.open(from: .deeplink, server: server, urlString: rawURL, skipConfirm: isFromWidget)
} else {
windowController.openSelectingServer(from: .deeplink, urlString: rawURL, skipConfirm: isFromWidget)
windowController.openSelectingServer(
from: .deeplink,
urlString: rawURL,
skipConfirm: isFromWidget,
queryParameters: queryParameters
)
}
default:
Current.Log.warning("Can't route incoming URL: \(url)")
Expand Down
24 changes: 21 additions & 3 deletions Sources/App/WebView/WebViewWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,27 @@ class WebViewWindowController {
}
}

func openSelectingServer(from: OpenSource, urlString openUrlRaw: String, skipConfirm: Bool = false) {
if let first = Current.servers.all.first, Current.servers.all.count == 1 {
open(from: from, server: first, urlString: openUrlRaw, skipConfirm: skipConfirm)
func openSelectingServer(
from: OpenSource,
urlString openUrlRaw: String,
skipConfirm: Bool = false,
queryParameters: [URLQueryItem]? = nil
) {
let serverName = queryParameters?.first(where: { $0.name == "server" })?.value
let servers = Current.servers.all

if let first = servers.first, Current.servers.all.count == 1 || serverName != nil {
if serverName == "default" || serverName == nil {
open(from: from, server: first, urlString: openUrlRaw, skipConfirm: skipConfirm)
} else {
if let selectedServer = servers.first(where: { server in
server.info.name.lowercased() == serverName?.lowercased()
}) {
open(from: from, server: selectedServer, urlString: openUrlRaw, skipConfirm: skipConfirm)
} else {
open(from: from, server: first, urlString: openUrlRaw, skipConfirm: skipConfirm)
}
}
} else if Current.servers.all.count > 1 {
let prompt: String?

Expand Down

0 comments on commit 0deb391

Please sign in to comment.