Files
iOS/Sources/Shared/Common/Extensions/URL+Extensions.swift
Bruno Pantaleão Gonçalves eaec51171e Improve open page intent, avoid reload webview when possible (#3435)
<!-- 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. -->

## 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. -->
2025-02-17 11:26:34 +01:00

62 lines
2.0 KiB
Swift

import Foundation
public extension URL {
/// Return true if receiver's host and scheme is equal to `otherURL`
func baseIsEqual(to otherURL: URL) -> Bool {
host?.lowercased() == otherURL.host?.lowercased()
&& portWithFallback == otherURL.portWithFallback
&& scheme?.lowercased() == otherURL.scheme?.lowercased()
&& user == otherURL.user
&& password == otherURL.password
}
/// Return true if receiver's URL is equal to `otherURL` ignoring query params
func isEqualIgnoringQueryParams(to otherURL: URL) -> Bool {
baseIsEqual(to: otherURL) &&
(path == otherURL.path || path == "\(otherURL.path)/0")
// Workaround for Home Assistant behavior where /0 is added to the end
}
// port will be removed if 80 or 443 by WKWebView, so we provide defaults for comparison
internal var portWithFallback: Int? {
if let port {
return port
}
switch scheme?.lowercased() {
case "http": return 80
case "https": return 443
default: return nil
}
}
func sanitized() -> URL {
guard path.hasSuffix("/"),
var components = URLComponents(url: self, resolvingAgainstBaseURL: false) else {
return self
}
while components.path.hasSuffix("/") {
components.path.removeLast()
}
return components.url ?? self
}
internal func adapting(url: URL) -> URL {
guard
let components = URLComponents(url: self, resolvingAgainstBaseURL: false),
var futureComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
return url
}
futureComponents.host = components.host
futureComponents.port = components.port
futureComponents.scheme = components.scheme
futureComponents.user = components.user
futureComponents.password = components.password
return futureComponents.url ?? url
}
}