Skip to content

Commit

Permalink
Request preferred thread network credential alongside all credentials (
Browse files Browse the repository at this point in the history
…#2473)

<!-- 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 -->
Request preferred thread network credential alongside all credentials,
since preferred credentials trigger a privacy permission dialog that may
be necessary to get all credentials later on.

## 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. -->
  • Loading branch information
bgoncal authored Dec 7, 2023
1 parent bcc5574 commit f292874
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions Sources/App/Resources/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -775,3 +775,4 @@ Home Assistant is free and open source home automation software with a focus on
"thread.credentials.border_agent_id_title" = "Border Agent ID";
"thread.credentials.share_credentials_button_title" = "Share credential with Home Assistant";
"thread.credentials.screen_title" = "Thread Credentials";
"thread.credentials.no_credential_available" = "You don't have credentials available on your iCloud Keychain.";
11 changes: 10 additions & 1 deletion Sources/App/ThreadCredentialsSharing/ThreadClientService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,16 @@ final class ThreadClientService: THClientProtocol {

func retrieveAllCredentials() async throws -> [ThreadCredential] {
let placeholder = "Unknown"
return try await client.allCredentials().map { credential in

// Thre preferred credential call is necessary as it triggers a permission dialog
let preferredCredential = try await client.preferredCredentials()

// All credentials retrieve the rest of the credentials after user acceps permission dialog
var allCredentials = try await client.allCredentials()
allCredentials = allCredentials.filter { $0.borderAgentID != preferredCredential.borderAgentID }
allCredentials.insert(preferredCredential)

return allCredentials.map { credential in
ThreadCredential(
networkName: credential.networkName ?? placeholder,
networkKey: credential.networkKey?.hexadecimal ?? placeholder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ struct ThreadCredentialsSharingView: View {
}
.navigationTitle(L10n.Thread.Credentials.screenTitle)
.navigationBarTitleDisplayMode(.inline)
.background(Color(uiColor: .secondarySystemBackground))
.toolbar(content: {
ToolbarItem(placement: .topBarTrailing) {
Button {
Expand Down Expand Up @@ -85,7 +84,7 @@ struct ThreadCredentialsSharingView: View {
@ViewBuilder
private var credentialsList: some View {
if viewModel.credentials.isEmpty {
Text("You don't have credentials available on your iCloud Keychain.")
Text(L10n.Thread.Credentials.noCredentialAvailable)
.multilineTextAlignment(.center)
} else {
List(viewModel.credentials, id: \.borderAgentID) { credential in
Expand All @@ -94,6 +93,7 @@ struct ThreadCredentialsSharingView: View {
.listRowBackground(Color.clear)
}
.listStyle(.plain)
.background(Color(uiColor: .secondarySystemBackground))
}
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/Shared/Resources/Swiftgen/Strings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1769,6 +1769,8 @@ public enum L10n {
public static var networkKeyTitle: String { return L10n.tr("Localizable", "thread.credentials.network_key_title") }
/// Network Name
public static var networkNameTitle: String { return L10n.tr("Localizable", "thread.credentials.network_name_title") }
/// You don't have credentials available on your iCloud Keychain.
public static var noCredentialAvailable: String { return L10n.tr("Localizable", "thread.credentials.no_credential_available") }
/// Thread Credentials
public static var screenTitle: String { return L10n.tr("Localizable", "thread.credentials.screen_title") }
/// Share credential with Home Assistant
Expand Down

0 comments on commit f292874

Please sign in to comment.