"@HiltViewModel class BluetoothViewModel @Inject constructor( private val…
- User
"@HiltViewModel class BluetoothViewModel @Inject constructor( private val bluetoothController: BluetoothController ): ViewModel() {
private val _state = MutableStateFlow(BluetoothUiState()) val state = combine( bluetoothController.scannedDevices, bluetoothController.pairedDevices, _state ) { scannedDevices, pairedDevices, state -> state.copy( scannedDevices = scannedDevices, pairedDevices = pairedDevices ) }.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), _state.value)
private var deviceConnectionJob: Job? = null
init { bluetoothController.isConnected.onEach { isConnected -> _state.update { it.copy(isConnected = isConnected) } }.launchIn(viewModelScope)
codebluetoothController.errors.onEach { error -> _state.update { it.copy( errorMessage = error ) } }.launchIn(viewModelScope)}
fun connectToDevice(device: BluetoothDeviceDomain) { _state.update { it.copy(isConnecting = true) } deviceConnectionJob = bluetoothController .connectToDevice(device) .listen() }
fun disconnectFromDevice() { deviceConnectionJob?.cancel() bluetoothController.closeConnection() _state.update { it.copy( isConnecting = false, isConnected = false ) } }
fun waitForIncomingConnections() { _state.update { it.copy(isConnecting = true) } deviceConnectionJob = bluetoothController .startBluetoothServer() .listen() }
fun startScan() { bluetoothController.startDiscovery() }
fun stopScan() { bluetoothController.stopDiscovery() }
private fun Flow.listen(): Job { return onEach { result -> when(result) { ConnectionResult.ConnectionEstablished -> { _state.update { it.copy( isConnected = true, isConnecting = false, errorMessage = null ) } } is ConnectionResult.Error -> { _state.update { it.copy( isConnected = false, isConnecting = false, errorMessage = result.message ) } } } } .catch { throwable -> bluetoothController.closeConnection() _state.update { it.copy( isConnected = false, isConnecting = false, ) } } .launchIn(viewModelScope) }
override fun onCleared() { super.onCleared() bluetoothController.release() } }" in this kotlin android code, elaborate on the "deviceConnectionJob?.cancel()"
- Assistant
- User
how the return looks like from "listen" extension function ?
- Assistant
- User
may you show me a concrete return of potential returns from listen function
- Assistant