Skip to content

Commit

Permalink
🌟 add loading animation to adb listing screen
Browse files Browse the repository at this point in the history
  • Loading branch information
theapache64 committed Jan 16, 2022
1 parent fcc6c1d commit b0f0c3b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.theapache64.stackzy.data.util.calladapter.flow.Resource
import com.theapache64.stackzy.model.AndroidDeviceWrapper
import com.theapache64.stackzy.ui.common.CustomScaffold
import com.theapache64.stackzy.ui.common.ErrorSnackBar
import com.theapache64.stackzy.ui.common.FullScreenError
import com.theapache64.stackzy.ui.common.Selectable
import com.theapache64.stackzy.ui.common.loading.LoadingAnimation
import com.theapache64.stackzy.util.R

/**
Expand All @@ -27,19 +30,21 @@ fun SelectDeviceScreen(
val devices by deviceListViewModel.connectedDevices.collectAsState()

Content(
devices = devices,
devicesResp = devices,
onDeviceSelected = onDeviceSelected,
onBackClicked = onBackClicked
onBackClicked = onBackClicked,
onRetry = { deviceListViewModel.watchConnectedDevices() }
)
}

@Composable
fun Content(
devices: List<AndroidDeviceWrapper>?,
devicesResp: Resource<List<AndroidDeviceWrapper>>?,
onDeviceSelected: (AndroidDeviceWrapper) -> Unit,
onBackClicked: () -> Unit
onBackClicked: () -> Unit,
onRetry: () -> Unit
) {
if (devices == null) {
if (devicesResp == null) {
// Just background
Box(
modifier = Modifier.fillMaxSize()
Expand All @@ -53,38 +58,45 @@ fun Content(
onBackClicked = onBackClicked
) {

if (devices.isEmpty()) {
FullScreenError(
title = R.string.device_no_device_title,
message = R.string.device_no_device_message,
image = painterResource("drawables/no_device.png")
)
} else {

Spacer(
modifier = Modifier.height(10.dp)
)

LazyColumn {
items(devices) { device ->
Selectable(
data = device,
modifier = Modifier
.width(400.dp),
onSelected = onDeviceSelected
when (devicesResp) {
is Resource.Loading -> {
LoadingAnimation(message = devicesResp.message ?: "", null)
}
is Resource.Error -> {
ErrorSnackBar(syncFailedReason = devicesResp.errorData, onRetryClicked = onRetry)
}
is Resource.Success -> {
val devices = devicesResp.data
if (devices.isEmpty()) {
FullScreenError(
title = R.string.device_no_device_title,
message = R.string.device_no_device_message,
image = painterResource("drawables/no_device.png")
)
} else {

Spacer(
modifier = Modifier.height(10.dp)
)

LazyColumn {
items(devices) { device ->
Selectable(
data = device,
modifier = Modifier
.width(400.dp),
onSelected = onDeviceSelected
)

Spacer(
modifier = Modifier.height(10.dp)
)
}
}
}
}

}

}


}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.theapache64.stackzy.ui.feature.devicelist

import com.theapache64.stackzy.data.repo.AdbRepo
import com.theapache64.stackzy.data.util.calladapter.flow.Resource
import com.theapache64.stackzy.model.AndroidDeviceWrapper
import com.toxicbakery.logging.Arbor
import kotlinx.coroutines.CoroutineScope
Expand All @@ -16,8 +17,8 @@ class DeviceListViewModel @Inject constructor(
) {

private lateinit var viewModelScope: CoroutineScope
private val _connectedDevices = MutableStateFlow<List<AndroidDeviceWrapper>?>(null)
val connectedDevices: StateFlow<List<AndroidDeviceWrapper>?> = _connectedDevices
private val _connectedDevices = MutableStateFlow<Resource<List<AndroidDeviceWrapper>>?>(null)
val connectedDevices: StateFlow<Resource<List<AndroidDeviceWrapper>>?> = _connectedDevices


fun init(scope: CoroutineScope) {
Expand All @@ -30,13 +31,15 @@ class DeviceListViewModel @Inject constructor(
*/
fun watchConnectedDevices() {
viewModelScope.launch {
_connectedDevices.value = Resource.Loading("πŸ” Scanning for devices...")
adbRepo.watchConnectedDevice()
.catch {
Arbor.d("Error: ${it.message}")
_connectedDevices.value = Resource.Error(it.message ?: "Something went wrong")
}
.collect {
Arbor.d("Devices : $it")
_connectedDevices.value = it.map { device -> AndroidDeviceWrapper(device) }
_connectedDevices.value = Resource.Success(it.map { device -> AndroidDeviceWrapper(device) })
}
}
}
Expand Down

0 comments on commit b0f0c3b

Please sign in to comment.