|
1 | 1 | package me.vanpetegem.accentor.ui.devices |
2 | 2 |
|
| 3 | +import androidx.annotation.DrawableRes |
| 4 | +import androidx.annotation.StringRes |
3 | 5 | import androidx.compose.foundation.Image |
4 | | -import androidx.compose.foundation.layout.Column |
5 | | -import androidx.compose.foundation.layout.aspectRatio |
6 | | -import androidx.compose.foundation.layout.fillMaxWidth |
7 | | -import androidx.compose.foundation.layout.padding |
| 6 | +import androidx.compose.foundation.clickable |
| 7 | +import androidx.compose.foundation.layout.* |
| 8 | +import androidx.compose.foundation.rememberScrollState |
| 9 | +import androidx.compose.foundation.verticalScroll |
8 | 10 | import androidx.compose.material.Card |
9 | 11 | import androidx.compose.material.MaterialTheme |
10 | 12 | import androidx.compose.material.Text |
11 | 13 | import androidx.compose.runtime.Composable |
12 | 14 | import androidx.compose.runtime.getValue |
13 | 15 | import androidx.compose.runtime.livedata.observeAsState |
| 16 | +import androidx.compose.ui.Alignment |
14 | 17 | import androidx.compose.ui.Modifier |
15 | | -import androidx.compose.ui.layout.ContentScale |
16 | 18 | import androidx.compose.ui.res.painterResource |
17 | 19 | import androidx.compose.ui.res.stringResource |
18 | 20 | import androidx.compose.ui.unit.dp |
19 | 21 | import androidx.hilt.navigation.compose.hiltViewModel |
20 | | -import coil.compose.rememberImagePainter |
21 | 22 | import me.vanpetegem.accentor.R |
22 | 23 | import me.vanpetegem.accentor.devices.Device |
23 | | -import me.vanpetegem.accentor.ui.util.FastScrollableGrid |
24 | 24 |
|
25 | 25 | @Composable |
26 | 26 | fun Devices(devicesViewModel: DevicesViewModel = hiltViewModel()) { |
27 | 27 | val devices: List<Device>? by devicesViewModel.devices().observeAsState() |
28 | | - FastScrollableGrid(devices ?: emptyList(), { it.firstCharacter }) { DeviceCard(it) } |
| 28 | + DeviceList(devices ?: emptyList()) |
29 | 29 | } |
30 | 30 |
|
31 | 31 | @Composable |
32 | | -fun DeviceCard(device: Device) { |
| 32 | +fun DeviceList(devices: List<Device>) { |
| 33 | + Column(Modifier.fillMaxWidth().verticalScroll(rememberScrollState())) { |
| 34 | + DeviceCard( |
| 35 | + name = stringResource(R.string.local_device), |
| 36 | + icon = R.drawable.ic_smartphone_sound, |
| 37 | + iconDescription = R.string.local_device_description |
| 38 | + ) |
| 39 | + Spacer(Modifier.size(8.dp)) |
| 40 | + Text( |
| 41 | + stringResource(R.string.devices_available), |
| 42 | + modifier = Modifier.padding(8.dp), |
| 43 | + style = MaterialTheme.typography.h5 |
| 44 | + ) |
| 45 | + devices.forEach { device -> |
| 46 | + DeviceCard( |
| 47 | + name = device.friendlyName, |
| 48 | + icon = R.drawable.ic_menu_devices |
| 49 | + ) |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +@Composable |
| 55 | +fun DeviceCard( |
| 56 | + name: String, |
| 57 | + @StringRes |
| 58 | + iconDescription: Int = R.string.device_image, |
| 59 | + @DrawableRes |
| 60 | + icon: Int = R.drawable.ic_menu_devices, |
| 61 | + onClick: () -> Unit = {}) { |
33 | 62 | Card( |
34 | | - modifier = Modifier.padding(8.dp), |
| 63 | + modifier = Modifier.padding(8.dp).fillMaxWidth().clickable(onClick = onClick) |
35 | 64 | ) { |
36 | | - Column { |
| 65 | + Row( |
| 66 | + modifier = Modifier.padding(8.dp), |
| 67 | + verticalAlignment = Alignment.CenterVertically, |
| 68 | + horizontalArrangement = Arrangement.Start |
| 69 | + ) { |
37 | 70 | Image( |
38 | | - painter = if (device.imageURL != null) { |
39 | | - rememberImagePainter(device.imageURL) { |
40 | | - placeholder(R.drawable.ic_artist) |
41 | | - } |
42 | | - } else { |
43 | | - painterResource(R.drawable.ic_artist) |
44 | | - }, |
45 | | - contentDescription = stringResource(R.string.device_image), |
46 | | - modifier = Modifier.fillMaxWidth().aspectRatio(1f), |
47 | | - contentScale = ContentScale.Crop, |
48 | | - ) |
49 | | - Text( |
50 | | - device.friendlyName, |
51 | | - maxLines = 1, |
52 | | - modifier = Modifier.padding(4.dp), |
53 | | - style = MaterialTheme.typography.subtitle1, |
54 | | - ) |
55 | | - Text( |
56 | | - device.type |
| 71 | + painter = painterResource(icon), |
| 72 | + contentDescription = stringResource(iconDescription), |
| 73 | + modifier = Modifier.requiredSize(48.dp) |
57 | 74 | ) |
| 75 | + Column() { |
| 76 | + Text( |
| 77 | + name, |
| 78 | + maxLines = 1, |
| 79 | + modifier = Modifier.padding(16.dp), |
| 80 | + style = MaterialTheme.typography.subtitle1 |
| 81 | + ) |
| 82 | + } |
58 | 83 | } |
59 | 84 | } |
60 | 85 | } |
0 commit comments