Skip to content

Commit

Permalink
Support the full ui... command option
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Cunningham <[email protected]>
  • Loading branch information
digitaldan committed Jun 27, 2024
1 parent e7c2483 commit 9321334
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
39 changes: 26 additions & 13 deletions openHAB/OpenHABRootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,23 +205,36 @@ class OpenHABRootViewController: UIViewController {
}
}

private func uiCommandAction(_ navigate: String) {
os_log("navigateCommandAction: %{PUBLIC}@", log: .notifications, type: .info, navigate)
if let index = navigate.firstIndex(of: ":") {
let components = navigate.split(separator: ":")
let type = String(components.first ?? "")
let path = components.dropFirst().joined(separator: ":")
switch type {
case "mainui":
private func uiCommandAction(_ command: String) {
os_log("navigateCommandAction: %{PUBLIC}@", log: .notifications, type: .info, command)
let pattern = "^(/basicui/app\\?.*|/.*|.*)$"

do {
let regex = try NSRegularExpression(pattern: pattern, options: [])
let nsString = command as NSString
let results = regex.matches(in: command, options: [], range: NSRange(location: 0, length: nsString.length))

if let match = results.first {
let pathRange = match.range(at: 1)
let path = nsString.substring(with: pathRange)
os_log("navigateCommandAction path: %{PUBLIC}@", log: .notifications, type: .info, path)
if currentView != webViewController {
switchView(target: .webview)
}
webViewController.navigateCommand(path)
case "sitemap":
os_log("handleApnsMessage sitemap", log: .notifications, type: .info)
default:
os_log("navigateCommandAction unknown", log: .notifications, type: .info)
if path.starts(with: "/basicui/app?") {
// TODO: this is a sitemap, we should use the native renderer
// temp hack right now to just use a webview
webViewController.loadWebView(force: true, path: path)
} else if path.starts(with: "/") {
// have the webview load this path itself
webViewController.loadWebView(force: true, path: path)
} else {
// have the mainUI handle the navigation
webViewController.navigateCommand(path)
}
}
} catch {
os_log("Invalid regex: %{PUBLIC}@", log: .notifications, type: .error, error.localizedDescription)
}
}

Expand Down
34 changes: 29 additions & 5 deletions openHAB/OpenHABWebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class OpenHABWebViewController: OpenHABViewController {
OpenHABTracker.shared.restart()
}

func loadWebView(force: Bool = false) {
func loadWebView(force: Bool = false, path: String? = nil) {
os_log("loadWebView %{PUBLIC}@", log: OSLog.remoteAccess, type: .info, openHABTrackedRootUrl)

let authStr = "\(Preferences.username):\(Preferences.password)"
Expand All @@ -106,27 +106,51 @@ class OpenHABWebViewController: OpenHABViewController {
currentTarget = newTarget
let url = URL(string: openHABTrackedRootUrl)

if let modifiedUrl = modifyUrl(orig: url) {
if let modifiedUrl = modifyUrl(orig: url, path: path) {
let request = URLRequest(url: modifiedUrl)
clearExistingPage()
acceptsCommands = false
webView.load(request)
}
}

func modifyUrl(orig: URL?) -> URL? {
func modifyUrl(orig: URL?, path: String? = nil) -> URL? {
// better way to cone/copy ?
guard let urlString = orig?.absoluteString, var url = URL(string: urlString) else { return orig }
if url.host == "myopenhab.org" {
url = URL(string: "https://home.myopenhab.org") ?? url
}

if !Preferences.defaultMainUIPath.isEmpty {
url.appendPathComponent(Preferences.defaultMainUIPath)
if let path {
url = appendPathToURL(baseURL: url, path: path) ?? url
} else if !Preferences.defaultMainUIPath.isEmpty {
url = appendPathToURL(baseURL: url, path: Preferences.defaultMainUIPath) ?? url
}
return url
}

// swift really makes you work to construct simple URLs, uhg.....
func appendPathToURL(baseURL: URL, path: String) -> URL? {
guard var urlComponents = URLComponents(url: baseURL, resolvingAgainstBaseURL: false) else {
return nil
}
// Split the user path into path and query components
if let questionMarkRange = path.range(of: "?") {
// Separate path and query
let pathComponent = String(path[..<questionMarkRange.lowerBound])
let queryComponent = String(path[questionMarkRange.upperBound...])
// Append the path component
urlComponents.path = (urlComponents.path as NSString).appendingPathComponent(pathComponent)
// Append the query component
urlComponents.query = queryComponent
} else {
// No query in the path, just append the path
urlComponents.path = (urlComponents.path as NSString).appendingPathComponent(path)
}
// Return the constructed URL
return urlComponents.url
}

func showActivityIndicator(show: Bool) {
if show {
activityIndicator.startAnimating()
Expand Down

0 comments on commit 9321334

Please sign in to comment.