Skip to content

Commit

Permalink
Disable buttons if capability missing
Browse files Browse the repository at this point in the history
  • Loading branch information
heroslender committed Jan 15, 2025
1 parent 7c18a24 commit 0c442eb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ android {
applicationId = "com.github.heroslender.lgtvcontroller"
minSdk = 30
targetSdk = 34
versionCode = 2
versionName = "0.2.0"
versionCode = 3
versionName = "0.2.1"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ import androidx.compose.ui.unit.em
fun CTextButton(
text: String,
modifier: Modifier = Modifier,
enabled: Boolean = true,
shape: Shape = ShapeDefaults.ExtraSmall,
fontSize: TextUnit = 3.7.em,
onClick: () -> Unit = {},
) {
CButton(shape = shape, modifier = modifier, onClick = onClick) {
CButton(enabled = enabled, shape = shape, modifier = modifier, onClick = onClick) {
Text(text, fontSize = fontSize)
}
}
Expand All @@ -41,12 +42,13 @@ fun CIconButton(
iconId: Int,
contentDescription: String,
modifier: Modifier = Modifier,
enabled: Boolean = true,
shape: Shape = ShapeDefaults.ExtraSmall,
useDefaultTint: Boolean = false,
onClick: () -> Unit = {},
) {
val color = if (useDefaultTint) Color.Unspecified else null
CButton(shape = shape, modifier = modifier, onClick = onClick) {
CButton(enabled = enabled, shape = shape, modifier = modifier, onClick = onClick) {
if (color == null) {
Icon(painterResource(iconId), contentDescription, modifier = Modifier.size(32.dp))
} else {
Expand All @@ -63,6 +65,7 @@ fun CIconButton(
@Composable
fun CButton(
modifier: Modifier = Modifier,
enabled: Boolean = true,
shape: Shape = ShapeDefaults.ExtraSmall,
contentPadding: PaddingValues = ButtonDefaults.ContentPadding,
onClick: () -> Unit,
Expand All @@ -71,6 +74,7 @@ fun CButton(

Button(
onClick = onClick,
enabled = enabled,
shape = shape,
contentPadding = contentPadding,
colors = ButtonColors(
Expand All @@ -86,13 +90,14 @@ fun CButton(

@Composable
fun RowScope.VerticalControls(
topButton: @Composable () -> Unit,
centerText: String,
enabled: Boolean = true,
topButton: @Composable () -> Unit,
bottomButton: @Composable () -> Unit,
) {
Surface(
shape = ShapeDefaults.ExtraSmall,
color = MaterialTheme.colorScheme.secondary,
color = if (enabled) MaterialTheme.colorScheme.secondary else MaterialTheme.colorScheme.surfaceVariant,
modifier = Modifier
.fillMaxHeight()
.weight(1F)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.em
import androidx.hilt.navigation.compose.hiltViewModel
import com.connectsdk.service.capability.ExternalInputControl
import com.connectsdk.service.capability.KeyControl
import com.connectsdk.service.capability.Launcher
import com.connectsdk.service.capability.PowerControl
import com.connectsdk.service.capability.TVControl
import com.connectsdk.service.capability.VolumeControl
import com.github.heroslender.lgtvcontroller.R
import com.github.heroslender.lgtvcontroller.device.Device
import com.github.heroslender.lgtvcontroller.device.DeviceStatus
Expand All @@ -40,7 +46,7 @@ import kotlinx.coroutines.runBlocking
@Preview(showBackground = true)
@Composable
fun ControlPreview(
isConnected: Boolean = false,
isConnected: Boolean = true,
isFavorite: Boolean = false,
) {
val device = if (isConnected) PreviewDevice else null
Expand Down Expand Up @@ -162,16 +168,18 @@ fun ColumnScope.Controls(device: Device?) {
CIconButton(
R.drawable.baseline_power_24,
"Power",
enabled = device.hasCapability(PowerControl.Any),
modifier = Modifier
.weight(1F)
.aspectRatio(1F, true)
.aspectRatio(1F, true),
) {
device?.powerOff()
}

CIconButton(
R.drawable.baseline_settings_24,
"Settings",
enabled = device.hasCapability(KeyControl.Any),
modifier = Modifier
.aspectRatio(1F, true)
.weight(1F)
Expand All @@ -193,6 +201,7 @@ fun ColumnScope.Controls(device: Device?) {
CIconButton(
R.drawable.baseline_home_24,
"Home",
enabled = device.hasCapability(KeyControl.Home),
modifier = Modifier
.aspectRatio(1F, true)
.weight(1F)
Expand All @@ -204,6 +213,7 @@ fun ColumnScope.Controls(device: Device?) {
CIconButton(
R.drawable.baseline_keyboard_arrow_up_24,
"Up",
enabled = device.hasCapability(KeyControl.Up),
shape = RoundedCornerShape(4.0.dp, 4.0.dp, 0.0.dp, 0.0.dp),
modifier = Modifier
.aspectRatio(1F, true)
Expand All @@ -213,7 +223,9 @@ fun ColumnScope.Controls(device: Device?) {
}

CIconButton(
R.drawable.baseline_input_24, "Source",
R.drawable.baseline_input_24,
"Source",
enabled = device.hasCapability(ExternalInputControl.Any),
modifier = Modifier
.aspectRatio(1F, true)
.weight(1F)
Expand All @@ -230,6 +242,7 @@ fun ColumnScope.Controls(device: Device?) {
CIconButton(
R.drawable.baseline_keyboard_arrow_left_24,
"Left",
enabled = device.hasCapability(KeyControl.Left),
shape = RoundedCornerShape(4.dp, 0.dp, 0.dp, 4.dp),
modifier = Modifier
.aspectRatio(1F, true)
Expand All @@ -241,6 +254,7 @@ fun ColumnScope.Controls(device: Device?) {
CTextButton(
"OK",
shape = CutCornerShape(0.dp),
enabled = device.hasCapability(KeyControl.OK),
modifier = Modifier
.aspectRatio(1F, true)
.weight(1F),
Expand All @@ -251,6 +265,7 @@ fun ColumnScope.Controls(device: Device?) {
CIconButton(
R.drawable.baseline_keyboard_arrow_right_24, "Right",
shape = RoundedCornerShape(0.dp, 4.dp, 4.dp, 0.dp),
enabled = device.hasCapability(KeyControl.Right),
modifier = Modifier
.aspectRatio(1F, true)
.weight(1F),
Expand All @@ -267,6 +282,7 @@ fun ColumnScope.Controls(device: Device?) {
CIconButton(
R.drawable.baseline_arrow_back_24,
"Back",
enabled = device.hasCapability(KeyControl.Back),
modifier = Modifier
.aspectRatio(1F, true)
.weight(1F)
Expand All @@ -279,6 +295,7 @@ fun ColumnScope.Controls(device: Device?) {
R.drawable.baseline_keyboard_arrow_down_24,
"Down",
shape = RoundedCornerShape(0.dp, 0.dp, 4.dp, 4.dp),
enabled = device.hasCapability(KeyControl.Down),
modifier = Modifier
.aspectRatio(1F, true)
.weight(1F),
Expand All @@ -289,6 +306,7 @@ fun ColumnScope.Controls(device: Device?) {
CIconButton(
R.drawable.netflix,
"Netflix",
enabled = device.hasCapability(Launcher.Netflix),
modifier = Modifier
.aspectRatio(1F, true)
.weight(1F)
Expand All @@ -303,15 +321,18 @@ fun ColumnScope.Controls(device: Device?) {

@Composable
fun RowScope.VolumeControls(device: Device?) {
val isEnabled = device.hasCapability(VolumeControl.Volume_Up_Down)
VerticalControls(
centerText = "VOL",
enabled = isEnabled,
topButton = {
CTextButton(
"+",
fontSize = 6.em,
enabled = isEnabled,
modifier = Modifier
.weight(1F)
.fillMaxWidth()
.fillMaxWidth(),
) {
device?.volumeUp()
}
Expand All @@ -320,9 +341,10 @@ fun RowScope.VolumeControls(device: Device?) {
CTextButton(
"-",
fontSize = 5.em,
enabled = isEnabled,
modifier = Modifier
.weight(1F)
.fillMaxWidth()
.fillMaxWidth(),
) {
device?.volumeDown()
}
Expand All @@ -334,10 +356,12 @@ fun RowScope.VolumeControls(device: Device?) {
fun RowScope.ChannelControls(device: Device?) {
VerticalControls(
centerText = "CH",
enabled = device.hasCapability(TVControl.Channel_Up) || device.hasCapability(TVControl.Channel_Down),
topButton = {
CIconButton(
R.drawable.baseline_keyboard_arrow_up_24,
"Channel Up",
enabled = device.hasCapability(TVControl.Channel_Up),
modifier = Modifier
.weight(1F)
.fillMaxWidth()
Expand All @@ -349,6 +373,7 @@ fun RowScope.ChannelControls(device: Device?) {
CIconButton(
R.drawable.baseline_keyboard_arrow_down_24,
"Channel Down",
enabled = device.hasCapability(TVControl.Channel_Down),
modifier = Modifier
.weight(1F)
.fillMaxWidth()
Expand All @@ -359,6 +384,8 @@ fun RowScope.ChannelControls(device: Device?) {
)
}

fun Device?.hasCapability(capability: String) = this?.hasCapability(capability) ?: false

object PreviewDevice : Device {
override val id: String
get() = "asddgres--sdfsdf-sdf"
Expand All @@ -367,6 +394,7 @@ object PreviewDevice : Device {
override val status: Flow<DeviceStatus>
get() = flowOf(DeviceStatus.CONNECTED)

override fun hasCapability(capability: String): Boolean = true
override fun powerOff() {}
override fun volumeUp() {}
override fun volumeDown() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ interface Device {

val status: Flow<DeviceStatus>

fun hasCapability(capability: String): Boolean

fun powerOff()

fun volumeUp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class LgDevice(
val service
get() = device.getServiceByName(WebOSTVService.ID) as WebOSTVService

override fun hasCapability(capability: String): Boolean {
return device.hasCapability(capability)
}

override fun powerOff() {
service.powerOff(null)
disconnect()
Expand Down

0 comments on commit 0c442eb

Please sign in to comment.