iOS/Sources/App/BarcodeScanner/Camera/BarcodeScannerViewModel.swift
Bruno Pantaleão Gonçalves 9ea7951315
Fix barcode scanner camera leak (#3603)
<!-- 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. -->

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-05-19 11:48:59 +02:00

52 lines
1.8 KiB
Swift

import Foundation
import Shared
final class BarcodeScannerViewModel: ObservableObject {
enum AbortReason: String {
case canceled
case alternativeOptions = "alternative_options"
}
private let incomingMessageId: Int
init(incomingMessageId: Int) {
self.incomingMessageId = incomingMessageId
}
func scannedCode(_ code: String, format: String) {
Current.sceneManager.webViewWindowControllerPromise.then(\.webViewControllerPromise)
.done { [weak self] controller in
guard let incomingMessageId = self?.incomingMessageId else { return }
controller.webViewExternalMessageHandler
.sendExternalBus(message: .init(
id: incomingMessageId,
command: WebViewExternalBusOutgoingMessage.barCodeScanResult.rawValue,
payload: [
"rawValue": code,
"format": format,
]
))
}
}
func aborted(_ reason: AbortReason) {
Current.sceneManager.webViewWindowControllerPromise.then(\.webViewControllerPromise)
.done { [weak self] controller in
guard let incomingMessageId = self?.incomingMessageId else { return }
controller.webViewExternalMessageHandler.sendExternalBus(message: .init(
id: incomingMessageId,
command: WebViewExternalBusOutgoingMessage.barCodeScanAborted.rawValue,
payload: [
"reason": reason.rawValue,
]
))
}
}
}
extension BarcodeScannerViewModel: BarcodeScannerDataModelDelegate {
func didDetectBarcode(_ code: String, format: String) {
scannedCode(code, format: format)
}
}