|
| 1 | +package com.castle.sefirah.presentation.home.components |
| 2 | + |
| 3 | +import androidx.compose.foundation.combinedClickable |
| 4 | +import androidx.compose.foundation.layout.Arrangement |
| 5 | +import androidx.compose.foundation.layout.Column |
| 6 | +import androidx.compose.foundation.layout.Row |
| 7 | +import androidx.compose.foundation.layout.Spacer |
| 8 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 9 | +import androidx.compose.foundation.layout.padding |
| 10 | +import androidx.compose.foundation.layout.width |
| 11 | +import androidx.compose.foundation.text.KeyboardOptions |
| 12 | +import androidx.compose.material.icons.Icons |
| 13 | +import androidx.compose.material.icons.automirrored.filled.Logout |
| 14 | +import androidx.compose.material.icons.filled.Lock |
| 15 | +import androidx.compose.material.icons.filled.PowerSettingsNew |
| 16 | +import androidx.compose.material.icons.filled.RestartAlt |
| 17 | +import androidx.compose.material.icons.filled.Schedule |
| 18 | +import androidx.compose.material3.AlertDialog |
| 19 | +import androidx.compose.material3.Button |
| 20 | +import androidx.compose.material3.Card |
| 21 | +import androidx.compose.material3.Icon |
| 22 | +import androidx.compose.material3.MaterialTheme |
| 23 | +import androidx.compose.material3.OutlinedTextField |
| 24 | +import androidx.compose.material3.Text |
| 25 | +import androidx.compose.material3.TextButton |
| 26 | +import androidx.compose.runtime.Composable |
| 27 | +import androidx.compose.ui.Alignment |
| 28 | +import androidx.compose.ui.Modifier |
| 29 | +import androidx.compose.ui.graphics.vector.ImageVector |
| 30 | +import androidx.compose.ui.text.input.KeyboardType |
| 31 | +import androidx.compose.ui.unit.dp |
| 32 | +import sefirah.domain.model.CommandType |
| 33 | + |
| 34 | +@Composable |
| 35 | +fun DeviceControlCard( |
| 36 | + onCommandSend: (CommandType) -> Unit, |
| 37 | + onLongClick: (CommandType) -> Unit, |
| 38 | + modifier: Modifier = Modifier |
| 39 | +) { |
| 40 | + Card( |
| 41 | + modifier = modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 8.dp) |
| 42 | + ) { |
| 43 | + Row( |
| 44 | + modifier = Modifier.fillMaxWidth().padding(vertical = 16.dp), |
| 45 | + horizontalArrangement = Arrangement.SpaceEvenly |
| 46 | + ) { |
| 47 | + DeviceControlButton( |
| 48 | + onClick = { onCommandSend(CommandType.Lock) }, |
| 49 | + onLongClick = { onLongClick(CommandType.Lock) }, |
| 50 | + icon = Icons.Default.Lock, |
| 51 | + contentDescription = "Lock Device" |
| 52 | + ) |
| 53 | + |
| 54 | + DeviceControlButton( |
| 55 | + onClick = { onCommandSend(CommandType.Hibernate) }, |
| 56 | + onLongClick = { onLongClick(CommandType.Hibernate) }, |
| 57 | + icon = Icons.Default.Schedule, |
| 58 | + contentDescription = "Hibernate Device" |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | + DeviceControlButton( |
| 63 | + onClick = { onCommandSend(CommandType.Logoff) }, |
| 64 | + onLongClick = { onLongClick(CommandType.Logoff) }, |
| 65 | + icon = Icons.AutoMirrored.Filled.Logout, |
| 66 | + contentDescription = "Logoff Device" |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | + DeviceControlButton( |
| 71 | + onClick = { onCommandSend(CommandType.Restart) }, |
| 72 | + onLongClick = { onLongClick(CommandType.Restart) }, |
| 73 | + icon = Icons.Default.RestartAlt, |
| 74 | + contentDescription = "Restart Device" |
| 75 | + ) |
| 76 | + |
| 77 | + DeviceControlButton( |
| 78 | + onClick = { onCommandSend(CommandType.Shutdown) }, |
| 79 | + onLongClick = { onLongClick(CommandType.Shutdown) }, |
| 80 | + icon = Icons.Default.PowerSettingsNew, |
| 81 | + contentDescription = "Shutdown Device" |
| 82 | + ) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +@Composable |
| 88 | +fun DeviceControlButton( |
| 89 | + onClick: () -> Unit, |
| 90 | + onLongClick: () -> Unit, |
| 91 | + icon: ImageVector, |
| 92 | + contentDescription: String?, |
| 93 | +) { |
| 94 | + Icon( |
| 95 | + imageVector = icon, |
| 96 | + contentDescription = contentDescription, |
| 97 | + tint = MaterialTheme.colorScheme.surfaceTint, |
| 98 | + modifier = Modifier.combinedClickable( |
| 99 | + onClick = onClick, |
| 100 | + onLongClick = onLongClick, |
| 101 | + ) |
| 102 | + ) |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | +@Composable |
| 107 | +fun TimerDialog( |
| 108 | + hours: String, |
| 109 | + minutes: String, |
| 110 | + seconds: String, |
| 111 | + onHoursChange: (String) -> Unit, |
| 112 | + onMinutesChange: (String) -> Unit, |
| 113 | + onSecondsChange: (String) -> Unit, |
| 114 | + onConfirm: () -> Unit, |
| 115 | + onDismiss: () -> Unit |
| 116 | +) { |
| 117 | + AlertDialog( |
| 118 | + onDismissRequest = onDismiss, |
| 119 | + title = { Text("Set timer") }, |
| 120 | + text = { |
| 121 | + Column { |
| 122 | + Row( |
| 123 | + modifier = Modifier.fillMaxWidth(), |
| 124 | + horizontalArrangement = Arrangement.SpaceEvenly, |
| 125 | + verticalAlignment = Alignment.CenterVertically |
| 126 | + ) { |
| 127 | + OutlinedTextField( |
| 128 | + value = hours, |
| 129 | + onValueChange = { onHoursChange(it.filter { char -> char.isDigit() }) }, |
| 130 | + label = { Text("Hours") }, |
| 131 | + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), |
| 132 | + modifier = Modifier.weight(1f) |
| 133 | + ) |
| 134 | + |
| 135 | + Spacer(modifier = Modifier.width(8.dp)) |
| 136 | + |
| 137 | + OutlinedTextField( |
| 138 | + value = minutes, |
| 139 | + onValueChange = { onMinutesChange(it.filter { char -> char.isDigit() }) }, |
| 140 | + label = { Text("Minutes") }, |
| 141 | + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), |
| 142 | + modifier = Modifier.weight(1f) |
| 143 | + ) |
| 144 | + |
| 145 | + Spacer(modifier = Modifier.width(8.dp)) |
| 146 | + |
| 147 | + OutlinedTextField( |
| 148 | + value = seconds, |
| 149 | + onValueChange = { onSecondsChange(it.filter { char -> char.isDigit() }) }, |
| 150 | + label = { Text("Seconds") }, |
| 151 | + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), |
| 152 | + modifier = Modifier.weight(1f) |
| 153 | + ) |
| 154 | + } |
| 155 | + } |
| 156 | + }, |
| 157 | + confirmButton = { |
| 158 | + Button(onClick = onConfirm) { |
| 159 | + Text("Confirm") |
| 160 | + } |
| 161 | + }, |
| 162 | + dismissButton = { |
| 163 | + TextButton(onClick = onDismiss) { |
| 164 | + Text("Cancel") |
| 165 | + } |
| 166 | + } |
| 167 | + ) |
| 168 | +} |
0 commit comments