Skip to content

Commit e3a0b00

Browse files
committed
refactor: log the output of onPermissionResult
1 parent ec36a09 commit e3a0b00

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

app/src/main/java/org/kabiri/android/usbterminal/data/repository/UsbRepository.kt

+7
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import android.content.IntentFilter
77
import android.hardware.usb.UsbDevice
88
import android.hardware.usb.UsbManager
99
import androidx.core.content.ContextCompat
10+
import kotlinx.coroutines.flow.Flow
1011
import kotlinx.coroutines.flow.MutableStateFlow
1112
import kotlinx.coroutines.flow.StateFlow
1213
import org.kabiri.android.usbterminal.Constants
1314
import javax.inject.Inject
1415

1516
internal interface IUsbRepository {
1617
val usbDevice: StateFlow<UsbDevice?>
18+
val infoMessageFlow: Flow<String>
1719
fun scanForArduinoDevices(): List<UsbDevice>
1820
fun requestUsbPermission(device: UsbDevice)
1921
fun onPermissionResult(device: UsbDevice?, granted: Boolean)
@@ -29,6 +31,10 @@ internal class UsbRepository
2931
private val _usbDevice = MutableStateFlow<UsbDevice?>(null)
3032
override val usbDevice: StateFlow<UsbDevice?> get() = _usbDevice
3133

34+
private val _infoMessageFlow = MutableStateFlow("")
35+
override val infoMessageFlow: Flow<String>
36+
get() = _infoMessageFlow
37+
3238
override fun scanForArduinoDevices(): List<UsbDevice> {
3339
val deviceList = usbManager.deviceList
3440
return deviceList.values.toList()
@@ -52,6 +58,7 @@ internal class UsbRepository
5258
}
5359

5460
override fun onPermissionResult(device: UsbDevice?, granted: Boolean) {
61+
_infoMessageFlow.value = "Permission result: ${device?.vendorId}, granted: $granted\n"
5562
if (granted && device != null) {
5663
_usbDevice.value = device
5764
} else {

app/src/main/java/org/kabiri/android/usbterminal/domain/UsbUseCase.kt

+4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package org.kabiri.android.usbterminal.domain
22

33
import android.hardware.usb.UsbDevice
4+
import kotlinx.coroutines.flow.Flow
45
import kotlinx.coroutines.flow.StateFlow
56
import org.kabiri.android.usbterminal.data.repository.IUsbRepository
67
import javax.inject.Inject
78

89
internal interface IUsbUseCase {
910
val usbDevice: StateFlow<UsbDevice?>
11+
val infoMessageFlow: Flow<String>
1012
fun scanForUsbDevices(): List<UsbDevice>
1113
fun requestPermission(device: UsbDevice)
1214
fun disconnect()
@@ -18,6 +20,8 @@ internal class UsbUseCase
1820
): IUsbUseCase {
1921
override val usbDevice: StateFlow<UsbDevice?> get() = usbRepository.usbDevice
2022

23+
override val infoMessageFlow: Flow<String> = usbRepository.infoMessageFlow
24+
2125
override fun scanForUsbDevices(): List<UsbDevice> = usbRepository.scanForArduinoDevices()
2226

2327
override fun requestPermission(device: UsbDevice) = usbRepository.requestUsbPermission(device)

app/src/main/java/org/kabiri/android/usbterminal/viewmodel/MainActivityViewModel.kt

+26-10
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,6 @@ internal class MainActivityViewModel
3434
private val resourceProvider: IResourceProvider,
3535
): ViewModel() {
3636

37-
init {
38-
// Subscribe to USB device changes.
39-
viewModelScope.launch {
40-
usbUseCase.usbDevice.collect { device ->
41-
device?.let { openDeviceAndPort(it) }
42-
}
43-
}
44-
}
45-
4637
private val _infoMessageFlow = MutableStateFlow("")
4738
val infoMessage: StateFlow<String>
4839
get() = _infoMessageFlow
@@ -57,6 +48,16 @@ internal class MainActivityViewModel
5748

5849
val output2 = SnapshotStateList<OutputText>()
5950

51+
init {
52+
// Subscribe to USB device changes.
53+
viewModelScope.launch {
54+
usbUseCase.usbDevice.collect { device ->
55+
_infoMessageFlow.value = "device discovered: ${device?.vendorId}\n"
56+
device?.let { openDeviceAndPort(it) }
57+
}
58+
}
59+
}
60+
6061
fun connect() {
6162
val usbDeviceList = usbUseCase.scanForUsbDevices()
6263
if (usbDeviceList.isEmpty()) {
@@ -121,6 +122,13 @@ internal class MainActivityViewModel
121122
return@map outputText
122123
}
123124

125+
val usbInfoOutput: Flow<OutputText> = usbUseCase.infoMessageFlow.map {
126+
_outputLive.value = _outputLive.value + it
127+
val outputText = OutputText(it, OutputText.OutputType.TYPE_INFO)
128+
output2.add(outputText)
129+
return@map outputText
130+
}
131+
124132
val arduinoDefaultOutput: Flow<OutputText> = arduinoUseCase.messageFlow.map {
125133
_outputLive.value = _outputLive.value + it
126134
val outputText = OutputText(it, OutputText.OutputType.TYPE_NORMAL)
@@ -157,6 +165,14 @@ internal class MainActivityViewModel
157165
arduinoInfo.text.isNotEmpty() -> arduinoInfo
158166
else -> arduinoDefault
159167
}
160-
}.stateIn(viewModelScope)
168+
}.combine(usbInfoOutput) { outputText, usbInfo ->
169+
// Prioritize USB info output over the rest.
170+
if (usbInfo.text.isNotEmpty()) {
171+
usbInfo
172+
} else {
173+
outputText
174+
}
175+
}
176+
.stateIn(viewModelScope)
161177
}
162178
}

0 commit comments

Comments
 (0)