mirror of
https://github.com/home-assistant/iOS.git
synced 2026-02-04 11:42:39 -06:00
<!-- 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. --> https://github.com/user-attachments/assets/e79c6e4d-13ff-405a-9463-02e597ce4996
81 lines
2.7 KiB
Swift
81 lines
2.7 KiB
Swift
import Foundation
|
|
import HAKit
|
|
import Shared
|
|
|
|
final class ThreadTransferCredentialToHAViewModel: ThreadCredentialsSharingViewModelProtocol {
|
|
@Published var credentials: [ThreadCredential] = []
|
|
@Published var showAlert = false
|
|
@Published var alertType: ThreadCredentialsAlertType?
|
|
@Published var showOperationSuccess = false
|
|
|
|
private let threadClient: ThreadClientProtocol
|
|
private var credentialsToImport: [String] = []
|
|
private let server: Server
|
|
|
|
init(server: Server, threadClient: ThreadClientProtocol) {
|
|
self.threadClient = threadClient
|
|
self.server = server
|
|
}
|
|
|
|
@MainActor
|
|
func mainOperation() async {
|
|
do {
|
|
credentials = try await threadClient.retrieveAllCredentials()
|
|
|
|
if credentials.isEmpty {
|
|
showAlert(type: .empty(
|
|
title: L10n.Thread.Credentials.ShareCredentials.noCredentialsTitle,
|
|
message: L10n.Thread.Credentials.ShareCredentials.noCredentialsMessage
|
|
))
|
|
} else {
|
|
credentialsToImport = credentials.map(\.activeOperationalDataSet)
|
|
processImport()
|
|
}
|
|
} catch {
|
|
Current.Log.error("Failed to retrieve all thread credentials, error: \(error.localizedDescription)")
|
|
showAlert(type: .error(title: L10n.errorLabel, message: error.localizedDescription))
|
|
}
|
|
}
|
|
|
|
@MainActor
|
|
private func processImport() {
|
|
guard let first = credentialsToImport.first else {
|
|
showOperationSuccess = true
|
|
return
|
|
}
|
|
|
|
shareCredentialWithHomeAssistant(credential: first) { [weak self] success in
|
|
if success {
|
|
self?.credentialsToImport.removeFirst()
|
|
self?.processImport()
|
|
}
|
|
}
|
|
}
|
|
|
|
@MainActor
|
|
private func shareCredentialWithHomeAssistant(credential: String, completion: @escaping (Bool) -> Void) {
|
|
let request = HARequest(type: .webSocket("thread/add_dataset_tlv"), data: [
|
|
"tlv": credential,
|
|
"source": "iOS-app",
|
|
])
|
|
Current.api(for: server)?.connection.send(request).promise.pipe { [weak self] result in
|
|
guard let self else { return }
|
|
switch result {
|
|
case .fulfilled:
|
|
completion(true)
|
|
case let .rejected(error):
|
|
showAlert(type: .error(
|
|
title: L10n.errorLabel,
|
|
message: error.localizedDescription
|
|
))
|
|
completion(false)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func showAlert(type: ThreadCredentialsAlertType) {
|
|
alertType = type
|
|
showAlert = true
|
|
}
|
|
}
|