Skip to content

Commit

Permalink
Fallback to internal URL until location enforcement is communicated c…
Browse files Browse the repository at this point in the history
…learly
  • Loading branch information
bgoncal committed Dec 16, 2024
1 parent 63ae3a0 commit 7530501
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ class ConnectionSettingsViewController: HAFormViewController, RowControllerType
row.displayValueFor = { [server] _ in
if server.info.connection.internalSSIDs?.isEmpty ?? true,
server.info.connection.internalHardwareAddresses?.isEmpty ?? true,
!server.info.connection.alwaysFallbackToInternalURL {
!server.info.connection.alwaysFallbackToInternalURL,
!ConnectionInfo.shouldFallbackToInternalURL {
return "‼️ \(L10n.Settings.ConnectionSection.InternalBaseUrl.RequiresSetup.title)"
} else {
return server.info.connection.address(for: .internal)?.absoluteString ?? ""
Expand Down
60 changes: 32 additions & 28 deletions Sources/App/Settings/Connection/ConnectionURLViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ final class ConnectionURLViewController: HAFormViewController, TypedRowControlle
$0.tag = RowTag.internalURLWarning.rawValue
if server.info.connection.internalSSIDs?.isEmpty ?? true,
server.info.connection.internalHardwareAddresses?.isEmpty ?? true,
!server.info.connection.alwaysFallbackToInternalURL {
!server.info.connection.alwaysFallbackToInternalURL,
!ConnectionInfo.shouldFallbackToInternalURL {
#if targetEnvironment(macCatalyst)
$0.title = "‼️" + L10n.Settings.ConnectionSection.InternalBaseUrl.SsidBssidRequired.title
#else
Expand Down Expand Up @@ -296,38 +297,41 @@ final class ConnectionURLViewController: HAFormViewController, TypedRowControlle
}
}

form +++ Section(footer: L10n.Settings.ConnectionSection.AlwaysFallbackInternal.footer)
<<< SwitchRow(RowTag.alwaysFallbackToInternalURL.rawValue) {
$0.title = L10n.Settings.ConnectionSection.AlwaysFallbackInternal.title
$0.value = server.info.connection.alwaysFallbackToInternalURL
if !ConnectionInfo.shouldFallbackToInternalURL {
form +++ Section(footer: L10n.Settings.ConnectionSection.AlwaysFallbackInternal.footer)
<<< SwitchRow(RowTag.alwaysFallbackToInternalURL.rawValue) {
$0.title = L10n.Settings.ConnectionSection.AlwaysFallbackInternal.title
$0.value = server.info.connection.alwaysFallbackToInternalURL

$0.cellUpdate { cell, _ in
cell.switchControl.onTintColor = .red
}
$0.cellUpdate { cell, _ in
cell.switchControl.onTintColor = .red
}

$0.onChange { [weak self] row in
if row.value ?? false {
let alert = UIAlertController(
title: L10n.Settings.ConnectionSection.AlwaysFallbackInternal.Confirmation.title,
message: L10n.Settings.ConnectionSection.AlwaysFallbackInternal.Confirmation.message,
preferredStyle: .alert
)
alert.addAction(UIAlertAction(title: L10n.cancelLabel, style: .cancel, handler: { _ in
self?.server.info.connection.alwaysFallbackToInternalURL = false
row.value = false
row.cellUpdate { _, row in
$0.onChange { [weak self] row in
if row.value ?? false {
let alert = UIAlertController(
title: L10n.Settings.ConnectionSection.AlwaysFallbackInternal.Confirmation.title,
message: L10n.Settings.ConnectionSection.AlwaysFallbackInternal.Confirmation.message,
preferredStyle: .alert
)
alert.addAction(UIAlertAction(title: L10n.cancelLabel, style: .cancel, handler: { _ in
self?.server.info.connection.alwaysFallbackToInternalURL = false
row.value = false
}
row.reload()
}))
alert.addAction(UIAlertAction(
title: L10n.Settings.ConnectionSection.AlwaysFallbackInternal.Confirmation.confirmButton,
style: .destructive
))
self?.present(alert, animated: true)
row.cellUpdate { _, row in
row.value = false
}
row.reload()
}))
alert.addAction(UIAlertAction(
title: L10n.Settings.ConnectionSection.AlwaysFallbackInternal.Confirmation
.confirmButton,
style: .destructive
))
self?.present(alert, animated: true)
}
}
}
}
}
}

private func locationPermissionSection() -> Section {
Expand Down
25 changes: 18 additions & 7 deletions Sources/Shared/API/ConnectionInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import Communicator
#endif

public struct ConnectionInfo: Codable, Equatable {
// TODO: Remove when location permission enforcement is in place
/// Developer toggle used while enforcement of location permision is not ready
/// Used as feature toggle
public static var shouldFallbackToInternalURL = true

private var externalURL: URL?
private var internalURL: URL?
private var remoteUIURL: URL?
Expand Down Expand Up @@ -200,13 +205,19 @@ public struct ConnectionInfo: Codable, Equatable {
activeURLType = .internal
url = internalURL
} else {
activeURLType = .none
url = nil
/*
No URL that can be used in this context is available
This can happen when only internal URL is set and
user tries to access the App remotely
*/
// TODO: Remove when location permission enforcement is in place
if ConnectionInfo.shouldFallbackToInternalURL {
activeURLType = .internal
url = internalURL
} else {
activeURLType = .none
url = nil
/*
No URL that can be used in this context is available
This can happen when only internal URL is set and
user tries to access the App remotely
*/
}
}

return url?.sanitized()
Expand Down
4 changes: 4 additions & 0 deletions Tests/Shared/ConnectionInfo.test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import Version
import XCTest

class ConnectionInfoTests: XCTestCase {
override func setUp() async throws {
ConnectionInfo.shouldFallbackToInternalURL = false
}

func testInternalOnlyURL() {
let url = URL(string: "http://example.com:8123")
var info = ConnectionInfo(
Expand Down

0 comments on commit 7530501

Please sign in to comment.