iOS/Sources/App/WebView/Extensions/ConnectionInfo+WebView.swift
Bruno Pantaleão Gonçalves 0afbd2bb98
Replace orange settings button by webview empty state (#3605)
<!-- Thank you for submitting a Pull Request and helping to improve Home
Assistant. Please complete the following sections to help the processing
and review of your changes. Please do not delete anything from this
template. -->

## Summary
<!-- Provide a brief summary of the changes you have made and most
importantly what they aim to achieve -->

## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->


https://github.com/user-attachments/assets/0f06a22c-3169-4778-afdd-e63b342b8a0f


## 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#

## Any other notes
<!-- If there is any other information of note, like if this Pull
Request is part of a bigger change, please include it here. -->

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-05-19 22:05:21 +02:00

56 lines
1.8 KiB
Swift

import Foundation
import Shared
extension ConnectionInfo {
mutating func webviewURLComponents() -> URLComponents? {
if Current.appConfiguration == .fastlaneSnapshot, prefs.object(forKey: "useDemo") != nil {
return URLComponents(string: "https://companion.home-assistant.io/app/ios/demo")!
}
guard let activeURL = activeURL() else {
Current.Log.error("No activeURL available while webviewURLComponents was called")
return nil
}
guard var components = URLComponents(url: activeURL, resolvingAgainstBaseURL: true) else {
return nil
}
let queryItem = URLQueryItem(name: "external_auth", value: "1")
components.queryItems = [queryItem]
return components
}
mutating func webviewURL() -> URL? {
webviewURLComponents()?.url
}
mutating func webviewURL(from raw: String) -> URL? {
guard let baseURLComponents = webviewURLComponents(), let baseURL = baseURLComponents.url else {
return nil
}
if raw.starts(with: "/") {
if let rawComponents = URLComponents(string: raw) {
var components = baseURLComponents
components.path.append(rawComponents.path)
components.fragment = rawComponents.fragment
if let items = rawComponents.queryItems {
var queryItems = components.queryItems ?? []
queryItems.append(contentsOf: items)
components.queryItems = queryItems
}
return components.url
} else {
return baseURL.appendingPathComponent(raw)
}
} else if let url = URL(string: raw), url.baseIsEqual(to: baseURL) {
return url
} else {
return nil
}
}
}