Skip to content

Commit

Permalink
fix: device montoring bug
Browse files Browse the repository at this point in the history
  • Loading branch information
kaleidot725 committed Jun 18, 2024
1 parent cb08005 commit 2cabe90
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import jp.kaleidot725.adbpad.domain.usecase.appearance.GetAppearanceUseCase
import jp.kaleidot725.adbpad.domain.usecase.appearance.SaveAppearanceUseCase
import jp.kaleidot725.adbpad.domain.usecase.command.ExecuteCommandUseCase
import jp.kaleidot725.adbpad.domain.usecase.command.GetCommandList
import jp.kaleidot725.adbpad.domain.usecase.device.GetDevicesFlowUseCase
import jp.kaleidot725.adbpad.domain.usecase.device.UpdateDevicesUseCase
import jp.kaleidot725.adbpad.domain.usecase.device.GetSelectedDeviceFlowUseCase
import jp.kaleidot725.adbpad.domain.usecase.device.SelectDeviceUseCase
import jp.kaleidot725.adbpad.domain.usecase.event.GetEventFlowUseCase
Expand Down Expand Up @@ -45,9 +45,6 @@ val domainModule =
factory {
GetCommandList(get())
}
factory {
GetDevicesFlowUseCase(get())
}
factory {
GetSelectedDeviceFlowUseCase(get())
}
Expand Down Expand Up @@ -120,4 +117,7 @@ val domainModule =
factory {
RefreshUseCase(get(), get(), get())
}
factory {
UpdateDevicesUseCase(get())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import jp.kaleidot725.adbpad.domain.model.device.Device
import kotlinx.coroutines.flow.Flow

interface DeviceRepository {
suspend fun selectDevice(device: Device): Boolean
fun getSelectedDeviceFlow(): Flow<Device?>

fun getDeviceFlow(): Flow<List<Device>>
suspend fun selectDevice(device: Device?): Boolean

fun getSelectedDeviceFlow(): Flow<Device?>
suspend fun updateDevices() : List<Device>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package jp.kaleidot725.adbpad.domain.usecase.device

import jp.kaleidot725.adbpad.domain.model.device.Device
import jp.kaleidot725.adbpad.domain.repository.DeviceRepository
import kotlinx.coroutines.flow.Flow

class GetDevicesFlowUseCase(
class UpdateDevicesUseCase(
private val deviceRepository: DeviceRepository,
) {
operator fun invoke(): Flow<List<Device>> {
return deviceRepository.getDeviceFlow()
suspend operator fun invoke(): List<Device> {
return deviceRepository.updateDevices()
}
}
Original file line number Diff line number Diff line change
@@ -1,56 +1,40 @@
package jp.kaleidot725.adbpad.repository.impl

import com.malinskiy.adam.AndroidDebugBridgeClientFactory
import com.malinskiy.adam.request.device.AsyncDeviceMonitorRequest
import com.malinskiy.adam.request.device.ListDevicesRequest
import jp.kaleidot725.adbpad.domain.model.device.Device
import jp.kaleidot725.adbpad.domain.model.device.DeviceState
import jp.kaleidot725.adbpad.domain.repository.DeviceRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch

class DeviceRepositoryImpl : DeviceRepository {
private val coroutineScope: CoroutineScope = CoroutineScope(Dispatchers.IO)
private val adbClient = AndroidDebugBridgeClientFactory().build()

private var lastSelectedDevice: Device? = null
private val selectedDevice: MutableSharedFlow<Device?> = MutableSharedFlow(replay = 1)

init {
coroutineScope.launch {
createDevicesFlow().collect { devices ->
if (!devices.contains(lastSelectedDevice)) {
lastSelectedDevice = devices.firstOrNull()
selectedDevice.emit(lastSelectedDevice)
}
}
}
}

override suspend fun selectDevice(device: Device): Boolean {
override suspend fun selectDevice(device: Device?): Boolean {
lastSelectedDevice = device
selectedDevice.emit(device)
return true
}

override fun getDeviceFlow(): Flow<List<Device>> {
return createDevicesFlow()
}

override fun getSelectedDeviceFlow(): Flow<Device?> {
return selectedDevice.asSharedFlow()
}

private fun createDevicesFlow() =
adbClient.execute(
request = AsyncDeviceMonitorRequest(),
scope = coroutineScope,
).receiveAsFlow().map { rowDevices -> rowDevices.convert() }
override suspend fun updateDevices(): List<Device> {
val devices = adbClient.execute(request = ListDevicesRequest()).convert()
if (devices.any { it.serial == lastSelectedDevice?.serial }.not()) {
selectDevice(devices.firstOrNull())
}
return devices
}

private fun List<com.malinskiy.adam.request.device.Device>.convert(): List<Device> {
return map { Device(it.serial, it.state.convert()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ val stateHolderModule =

factory {
MenuStateHolder(
getAndroidDevicesFlowUseCase = get(),
updateDevicesUseCase = get(),
getMenuListUseCase = get(),
getSelectedDeviceFlowUseCase = get(),
selectDeviceUseCase = get(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package jp.kaleidot725.adbpad.ui.screen.menu

import jp.kaleidot725.adbpad.domain.model.Menu
import jp.kaleidot725.adbpad.domain.model.device.Device
import jp.kaleidot725.adbpad.domain.usecase.device.GetDevicesFlowUseCase
import jp.kaleidot725.adbpad.domain.usecase.device.UpdateDevicesUseCase
import jp.kaleidot725.adbpad.domain.usecase.device.GetSelectedDeviceFlowUseCase
import jp.kaleidot725.adbpad.domain.usecase.device.SelectDeviceUseCase
import jp.kaleidot725.adbpad.domain.usecase.menu.GetMenuListUseCase
Expand All @@ -12,16 +12,18 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch

class MenuStateHolder(
private val getAndroidDevicesFlowUseCase: GetDevicesFlowUseCase,
private val updateDevicesUseCase: UpdateDevicesUseCase,
private val getMenuListUseCase: GetMenuListUseCase,
private val getSelectedDeviceFlowUseCase: GetSelectedDeviceFlowUseCase,
private val selectDeviceUseCase: SelectDeviceUseCase,
Expand Down Expand Up @@ -75,19 +77,18 @@ class MenuStateHolder(

private fun collectDevices() {
deviceJob?.cancel()
deviceJob =
coroutineScope.launch {
getAndroidDevicesFlowUseCase().collect {
_devices.value = it
}
deviceJob = coroutineScope.launch {
while (isActive) {
_devices.value = updateDevicesUseCase()
delay(1000)
}
}

selectedDeviceJob?.cancel()
selectedDeviceJob =
coroutineScope.launch {
getSelectedDeviceFlowUseCase().collect {
_selectedDevice.value = it
}
selectedDeviceJob = coroutineScope.launch {
getSelectedDeviceFlowUseCase().collect {
_selectedDevice.value = it
}
}
}
}

0 comments on commit 2cabe90

Please sign in to comment.