Francis Mariano
02/11/2021, 6:01 PMclass DeviceScanPresenter(
private val bluetoothRepository: BluetoothRepositoryKmm
) : BasePresenter<DeviceScanView>() {
private val mainScope = MainScope()
private var scan: Job? = null
.......
fun deviceScanDevice() {
view?.log("presenter - deviceScanDevice444")
view?.showCancelButton()
view?.clearDevices()
view?.log("presenter - deviceScanDevice:beforeOfLaunch")
scan = mainScope.launch(start = CoroutineStart.UNDISPATCHED) {
view?.log("presenter - deviceScanDevice:launch")
try {
view?.log("presenter - deviceScanDevice:try")
withTimeout(20000) {
view?.log("presenter - deviceScanDevice:withTimeout:withTimeout")
view?.log("presenter - deviceScanDevice:withTimeout:bluetoothRepository = $bluetoothRepository")
bluetoothRepository.devices.collect {
view?.log("presenter - deviceScanDevice:withTimeout:collect")
view?.showDevice(it)
}
}
} catch (e: TimeoutCancellationException) {
view?.log("presenter - deviceScanDevice:catch : ${e.message}")
view?.showUpdateButton()
}
}.apply {
invokeOnCompletion {
bluetoothRepository.stopScanLeDevice()
view?.log("presenter - deviceScanDevice444:invokeOnCompletation")
}
}
view?.log("presenter - deviceScanDevice:startScanLeDevice")
bluetoothRepository.startScanLeDevice()
}
.....
}
BluetoothRepositoryKmm
object BluetoothRepositoryKmmImpl : BluetoothRepositoryKmm {
....
private var repositoryScope: CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
....
private val _devices = MutableSharedFlow<Device>()
override val devices = _devices.asSharedFlow()
....
override fun startScanLeDevice() {
println("BluetoothRepositoryKmmImpl - startScanLeDevice333")
scanJob = repositoryScope.launch {
println("BluetoothRepositoryKmmImpl - startScanLeDevice:launch")
Scanner().advertisements.collect {
println("BluetoothRepositoryKmmImpl - startScanLeDevice:collect")
val ret = _devices.tryEmit(it.devicePlatformFrom())
println("BluetoothRepositoryKmmImpl - startScanLeDevice:collect:ret = $ret")
}
}.apply { invokeOnCompletion { println("BluetoothRepositoryKmmImpl - startScanLeDevice:invokeOnCompletation") } }
}
....
}