feat(ui): implement DevicePage, AddDevicePage, MainScreen with HorizontalPager, and MainActivity
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package com.expapp.trigger
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.ui.Modifier
|
||||
import com.expapp.trigger.data.repository.DeviceRepository
|
||||
import com.expapp.trigger.data.repository.SharedPrefsStorage
|
||||
import com.expapp.trigger.network.TriggerClient
|
||||
import com.expapp.trigger.ui.screens.MainScreen
|
||||
import com.expapp.trigger.ui.theme.DarkBackground
|
||||
import com.expapp.trigger.ui.theme.ExpAppTheme
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
|
||||
val repository = DeviceRepository(SharedPrefsStorage(applicationContext))
|
||||
val triggerClient = TriggerClient()
|
||||
|
||||
setContent {
|
||||
ExpAppTheme {
|
||||
Surface(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
color = DarkBackground
|
||||
) {
|
||||
MainScreen(
|
||||
repository = repository,
|
||||
triggerClient = triggerClient
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,287 @@
|
||||
package com.expapp.trigger.ui.screens
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Add
|
||||
import androidx.compose.material.icons.filled.Key
|
||||
import androidx.compose.material.icons.filled.Link
|
||||
import androidx.compose.material.icons.filled.Memory
|
||||
import androidx.compose.material.icons.filled.Storage
|
||||
import androidx.compose.material.icons.filled.Visibility
|
||||
import androidx.compose.material.icons.filled.VisibilityOff
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.ButtonDefaults
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.OutlinedTextFieldDefaults
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.input.KeyboardType
|
||||
import androidx.compose.ui.text.input.PasswordVisualTransformation
|
||||
import androidx.compose.ui.text.input.VisualTransformation
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import com.expapp.trigger.data.model.DeviceConfig
|
||||
import com.expapp.trigger.network.TriggerClient
|
||||
import com.expapp.trigger.ui.components.StatusBanner
|
||||
import com.expapp.trigger.ui.theme.DarkCardBorder
|
||||
import com.expapp.trigger.ui.theme.DarkSurface
|
||||
import com.expapp.trigger.ui.theme.PowerOnGreen
|
||||
import com.expapp.trigger.ui.theme.TextPrimary
|
||||
import com.expapp.trigger.ui.theme.TextSecondary
|
||||
|
||||
@Composable
|
||||
fun AddDevicePage(
|
||||
triggerClient: TriggerClient,
|
||||
onDeviceAdded: (DeviceConfig) -> Unit,
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
var deviceName by remember { mutableStateOf("") }
|
||||
var nodeName by remember { mutableStateOf("debian") }
|
||||
var r2Url by remember { mutableStateOf("") }
|
||||
var secretKey by remember { mutableStateOf("") }
|
||||
var passwordVisible by remember { mutableStateOf(false) }
|
||||
|
||||
var isFetching by remember { mutableStateOf(false) }
|
||||
var errorMessage by remember { mutableStateOf<String?>(null) }
|
||||
var successMessage by remember { mutableStateOf<String?>(null) }
|
||||
|
||||
Column(
|
||||
modifier = modifier
|
||||
.fillMaxSize()
|
||||
.verticalScroll(rememberScrollState())
|
||||
.padding(horizontal = 20.dp, vertical = 12.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
Card(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
shape = RoundedCornerShape(20.dp),
|
||||
colors = CardDefaults.cardColors(containerColor = DarkSurface),
|
||||
border = CardDefaults.outlinedCardBorder().copy(brush = androidx.compose.ui.graphics.SolidColor(DarkCardBorder))
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(20.dp)
|
||||
) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Add,
|
||||
contentDescription = null,
|
||||
tint = PowerOnGreen,
|
||||
modifier = Modifier.size(28.dp)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(10.dp))
|
||||
Text(
|
||||
text = "Add Remote Device",
|
||||
fontSize = 22.sp,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = TextPrimary
|
||||
)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(6.dp))
|
||||
|
||||
Text(
|
||||
text = "Provide your Cloudflare R2 JSON endpoint URL and authentication key to register a new remote trigger.",
|
||||
fontSize = 13.sp,
|
||||
color = TextSecondary,
|
||||
lineHeight = 18.sp
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(18.dp))
|
||||
|
||||
StatusBanner(
|
||||
status = errorMessage ?: successMessage,
|
||||
isSuccess = errorMessage == null,
|
||||
onDismiss = {
|
||||
errorMessage = null
|
||||
successMessage = null
|
||||
}
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
|
||||
// Device Name Field
|
||||
OutlinedTextField(
|
||||
value = deviceName,
|
||||
onValueChange = { deviceName = it },
|
||||
label = { Text("Device Friendly Name") },
|
||||
placeholder = { Text("e.g. Living Room Server") },
|
||||
leadingIcon = {
|
||||
Icon(Icons.Default.Storage, contentDescription = null, tint = TextSecondary)
|
||||
},
|
||||
singleLine = true,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
colors = OutlinedTextFieldDefaults.colors(
|
||||
focusedBorderColor = PowerOnGreen,
|
||||
unfocusedBorderColor = DarkCardBorder
|
||||
)
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(14.dp))
|
||||
|
||||
// Node Identifier Field
|
||||
OutlinedTextField(
|
||||
value = nodeName,
|
||||
onValueChange = { nodeName = it },
|
||||
label = { Text("Node Identifier") },
|
||||
placeholder = { Text("e.g. debian or pop-os") },
|
||||
leadingIcon = {
|
||||
Icon(Icons.Default.Memory, contentDescription = null, tint = TextSecondary)
|
||||
},
|
||||
singleLine = true,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
colors = OutlinedTextFieldDefaults.colors(
|
||||
focusedBorderColor = PowerOnGreen,
|
||||
unfocusedBorderColor = DarkCardBorder
|
||||
)
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(14.dp))
|
||||
|
||||
// Cloudflare R2 JSON URL Field
|
||||
OutlinedTextField(
|
||||
value = r2Url,
|
||||
onValueChange = { r2Url = it },
|
||||
label = { Text("Cloudflare R2 JSON URL") },
|
||||
placeholder = { Text("https://.../endpoints.json") },
|
||||
leadingIcon = {
|
||||
Icon(Icons.Default.Link, contentDescription = null, tint = TextSecondary)
|
||||
},
|
||||
singleLine = true,
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
colors = OutlinedTextFieldDefaults.colors(
|
||||
focusedBorderColor = PowerOnGreen,
|
||||
unfocusedBorderColor = DarkCardBorder
|
||||
)
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(14.dp))
|
||||
|
||||
// Secret Key Field
|
||||
OutlinedTextField(
|
||||
value = secretKey,
|
||||
onValueChange = { secretKey = it },
|
||||
label = { Text("Secret Key") },
|
||||
placeholder = { Text("Carbon@12") },
|
||||
leadingIcon = {
|
||||
Icon(Icons.Default.Key, contentDescription = null, tint = TextSecondary)
|
||||
},
|
||||
trailingIcon = {
|
||||
IconButton(onClick = { passwordVisible = !passwordVisible }) {
|
||||
Icon(
|
||||
imageVector = if (passwordVisible) Icons.Default.Visibility else Icons.Default.VisibilityOff,
|
||||
contentDescription = if (passwordVisible) "Hide password" else "Show password",
|
||||
tint = TextSecondary
|
||||
)
|
||||
}
|
||||
},
|
||||
visualTransformation = if (passwordVisible) VisualTransformation.None else PasswordVisualTransformation(),
|
||||
singleLine = true,
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
colors = OutlinedTextFieldDefaults.colors(
|
||||
focusedBorderColor = PowerOnGreen,
|
||||
unfocusedBorderColor = DarkCardBorder
|
||||
)
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(24.dp))
|
||||
|
||||
// Fetch & Add Button
|
||||
Button(
|
||||
onClick = {
|
||||
errorMessage = null
|
||||
successMessage = null
|
||||
|
||||
if (deviceName.isBlank()) {
|
||||
errorMessage = "Please enter a device name"
|
||||
return@Button
|
||||
}
|
||||
if (r2Url.isBlank()) {
|
||||
errorMessage = "Please provide the Cloudflare R2 JSON URL"
|
||||
return@Button
|
||||
}
|
||||
if (secretKey.isBlank()) {
|
||||
errorMessage = "Please enter the secret key for this device"
|
||||
return@Button
|
||||
}
|
||||
|
||||
isFetching = true
|
||||
triggerClient.fetchR2Config(r2Url.trim()) { result ->
|
||||
isFetching = false
|
||||
result.onSuccess { endpoints ->
|
||||
val newDevice = DeviceConfig(
|
||||
name = deviceName.trim(),
|
||||
node = if (nodeName.isBlank()) "debian" else nodeName.trim(),
|
||||
secretKey = secretKey.trim(),
|
||||
r2Url = r2Url.trim(),
|
||||
powerOnUrl = endpoints.powerOn,
|
||||
powerOffUrl = endpoints.powerOff,
|
||||
rebootUrl = endpoints.reboot
|
||||
)
|
||||
onDeviceAdded(newDevice)
|
||||
successMessage = "Device \"${newDevice.name}\" added successfully!"
|
||||
deviceName = ""
|
||||
r2Url = ""
|
||||
secretKey = ""
|
||||
}.onFailure { ex ->
|
||||
errorMessage = "Failed to fetch endpoints: ${ex.message}"
|
||||
}
|
||||
}
|
||||
},
|
||||
enabled = !isFetching,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(56.dp),
|
||||
shape = RoundedCornerShape(14.dp),
|
||||
colors = ButtonDefaults.buttonColors(containerColor = PowerOnGreen)
|
||||
) {
|
||||
if (isFetching) {
|
||||
CircularProgressIndicator(
|
||||
color = Color.White,
|
||||
modifier = Modifier.size(24.dp),
|
||||
strokeWidth = 2.dp
|
||||
)
|
||||
Spacer(modifier = Modifier.width(10.dp))
|
||||
Text("Fetching Endpoints...", color = Color.White)
|
||||
} else {
|
||||
Text(
|
||||
text = "Fetch & Add Device",
|
||||
fontSize = 17.sp,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = Color.White
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
package com.expapp.trigger.ui.screens
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Delete
|
||||
import androidx.compose.material.icons.filled.PowerOff
|
||||
import androidx.compose.material.icons.filled.PowerSettingsNew
|
||||
import androidx.compose.material.icons.filled.RestartAlt
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.ButtonDefaults
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import com.expapp.trigger.data.model.DeviceConfig
|
||||
import com.expapp.trigger.network.TriggerAction
|
||||
import com.expapp.trigger.ui.components.ActionConfirmDialog
|
||||
import com.expapp.trigger.ui.components.HugeActionButton
|
||||
import com.expapp.trigger.ui.components.StatusBanner
|
||||
import com.expapp.trigger.ui.theme.DarkCardBorder
|
||||
import com.expapp.trigger.ui.theme.DarkSurface
|
||||
import com.expapp.trigger.ui.theme.DarkSurfaceVariant
|
||||
import com.expapp.trigger.ui.theme.PowerOffRed
|
||||
import com.expapp.trigger.ui.theme.PowerOnGreen
|
||||
import com.expapp.trigger.ui.theme.RebootAmber
|
||||
import com.expapp.trigger.ui.theme.TextPrimary
|
||||
import com.expapp.trigger.ui.theme.TextSecondary
|
||||
|
||||
@Composable
|
||||
fun DevicePage(
|
||||
device: DeviceConfig,
|
||||
isLoading: Boolean,
|
||||
lastStatus: String?,
|
||||
lastSuccess: Boolean?,
|
||||
onDismissStatus: () -> Unit,
|
||||
onTrigger: (TriggerAction) -> Unit,
|
||||
onDelete: (DeviceConfig) -> Unit,
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
var pendingAction by remember { mutableStateOf<TriggerAction?>(null) }
|
||||
var showDeleteConfirm by remember { mutableStateOf(false) }
|
||||
|
||||
if (pendingAction != null) {
|
||||
ActionConfirmDialog(
|
||||
action = pendingAction!!,
|
||||
deviceName = device.name,
|
||||
onConfirm = {
|
||||
val actionToExecute = pendingAction!!
|
||||
pendingAction = null
|
||||
onTrigger(actionToExecute)
|
||||
},
|
||||
onDismiss = { pendingAction = null }
|
||||
)
|
||||
}
|
||||
|
||||
if (showDeleteConfirm) {
|
||||
AlertDialog(
|
||||
onDismissRequest = { showDeleteConfirm = false },
|
||||
title = { Text("Delete Device") },
|
||||
text = { Text("Are you sure you want to remove \"${device.name}\" from your trigger list?") },
|
||||
confirmButton = {
|
||||
Button(
|
||||
onClick = {
|
||||
showDeleteConfirm = false
|
||||
onDelete(device)
|
||||
},
|
||||
colors = ButtonDefaults.buttonColors(containerColor = PowerOffRed)
|
||||
) {
|
||||
Text("Delete", color = Color.White)
|
||||
}
|
||||
},
|
||||
dismissButton = {
|
||||
TextButton(onClick = { showDeleteConfirm = false }) {
|
||||
Text("Cancel")
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
Column(
|
||||
modifier = modifier
|
||||
.fillMaxSize()
|
||||
.padding(horizontal = 20.dp, vertical = 12.dp),
|
||||
verticalArrangement = Arrangement.SpaceBetween
|
||||
) {
|
||||
// Device Header Card
|
||||
Card(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
shape = RoundedCornerShape(20.dp),
|
||||
colors = CardDefaults.cardColors(containerColor = DarkSurface),
|
||||
border = CardDefaults.outlinedCardBorder().copy(brush = androidx.compose.ui.graphics.SolidColor(DarkCardBorder))
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 18.dp, vertical = 14.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween
|
||||
) {
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(
|
||||
text = device.name,
|
||||
fontSize = 24.sp,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = TextPrimary
|
||||
)
|
||||
Spacer(modifier = Modifier.height(4.dp))
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.background(DarkSurfaceVariant, RoundedCornerShape(8.dp))
|
||||
.border(1.dp, DarkCardBorder, RoundedCornerShape(8.dp))
|
||||
.padding(horizontal = 8.dp, vertical = 2.dp)
|
||||
) {
|
||||
Text(
|
||||
text = "node: ${device.node}",
|
||||
fontSize = 12.sp,
|
||||
color = TextSecondary,
|
||||
fontWeight = FontWeight.SemiBold
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
IconButton(
|
||||
onClick = { showDeleteConfirm = true },
|
||||
modifier = Modifier
|
||||
.size(42.dp)
|
||||
.background(Color.White.copy(alpha = 0.05f), CircleShape)
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Delete,
|
||||
contentDescription = "Delete device",
|
||||
tint = Color(0xFFE57373)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(10.dp))
|
||||
|
||||
// Status Banner if any
|
||||
StatusBanner(
|
||||
status = lastStatus,
|
||||
isSuccess = lastSuccess,
|
||||
onDismiss = onDismissStatus
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
|
||||
// 3 Massive Action Buttons
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.weight(1f),
|
||||
verticalArrangement = Arrangement.SpaceEvenly
|
||||
) {
|
||||
HugeActionButton(
|
||||
text = "POWER ON",
|
||||
subtext = "Wake / Turn on remote system",
|
||||
containerColor = PowerOnGreen,
|
||||
icon = Icons.Default.PowerSettingsNew,
|
||||
isLoading = isLoading && pendingAction == null,
|
||||
onClick = { onTrigger(TriggerAction.POWER_ON) }
|
||||
)
|
||||
|
||||
HugeActionButton(
|
||||
text = "REBOOT",
|
||||
subtext = "Graceful restart (Key authenticated)",
|
||||
containerColor = RebootAmber,
|
||||
icon = Icons.Default.RestartAlt,
|
||||
isLoading = isLoading && pendingAction == null,
|
||||
onClick = { pendingAction = TriggerAction.REBOOT }
|
||||
)
|
||||
|
||||
HugeActionButton(
|
||||
text = "POWER OFF",
|
||||
subtext = "Shut down machine (Key authenticated)",
|
||||
containerColor = PowerOffRed,
|
||||
icon = Icons.Default.PowerOff,
|
||||
isLoading = isLoading && pendingAction == null,
|
||||
onClick = { pendingAction = TriggerAction.POWER_OFF }
|
||||
)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
package com.expapp.trigger.ui.screens
|
||||
|
||||
import android.widget.Toast
|
||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.pager.HorizontalPager
|
||||
import androidx.compose.foundation.pager.rememberPagerState
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Add
|
||||
import androidx.compose.material.icons.filled.Dns
|
||||
import androidx.compose.material3.CenterAlignedTopAppBar
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBarDefaults
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateMapOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import com.expapp.trigger.data.model.DeviceConfig
|
||||
import com.expapp.trigger.data.repository.DeviceRepository
|
||||
import com.expapp.trigger.network.TriggerAction
|
||||
import com.expapp.trigger.network.TriggerClient
|
||||
import com.expapp.trigger.ui.theme.DarkBackground
|
||||
import com.expapp.trigger.ui.theme.DarkSurface
|
||||
import com.expapp.trigger.ui.theme.PowerOnGreen
|
||||
import com.expapp.trigger.ui.theme.TextPrimary
|
||||
import com.expapp.trigger.ui.theme.TextSecondary
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@OptIn(ExperimentalFoundationApi::class, ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun MainScreen(
|
||||
repository: DeviceRepository,
|
||||
triggerClient: TriggerClient,
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
|
||||
var devices by remember { mutableStateOf(repository.getDevices()) }
|
||||
val pagerState = rememberPagerState(pageCount = { devices.size + 1 })
|
||||
|
||||
// Device ID -> In-Flight Loading State
|
||||
val loadingStates = remember { mutableStateMapOf<String, Boolean>() }
|
||||
// Device ID -> Pair(StatusMessage, IsSuccess)
|
||||
val statusStates = remember { mutableStateMapOf<String, Pair<String, Boolean>>() }
|
||||
|
||||
fun executeAction(device: DeviceConfig, action: TriggerAction) {
|
||||
val actionLabel = when (action) {
|
||||
TriggerAction.POWER_ON -> "Power On"
|
||||
TriggerAction.POWER_OFF -> "Power Off"
|
||||
TriggerAction.REBOOT -> "Reboot"
|
||||
}
|
||||
|
||||
loadingStates[device.id] = true
|
||||
statusStates.remove(device.id)
|
||||
|
||||
triggerClient.sendTrigger(device, action) { response ->
|
||||
loadingStates[device.id] = false
|
||||
val statusText = if (response.isSuccess) {
|
||||
"$actionLabel sent: HTTP ${response.statusCode} - ${response.message.take(60)}"
|
||||
} else {
|
||||
"Failed: ${response.message.take(60)}"
|
||||
}
|
||||
statusStates[device.id] = Pair(statusText, response.isSuccess)
|
||||
|
||||
Toast.makeText(
|
||||
context,
|
||||
if (response.isSuccess) "$actionLabel successful (${response.statusCode})" else "Failed: ${response.message}",
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
}
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
modifier = modifier.fillMaxSize(),
|
||||
containerColor = DarkBackground,
|
||||
topBar = {
|
||||
CenterAlignedTopAppBar(
|
||||
title = {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Dns,
|
||||
contentDescription = null,
|
||||
tint = PowerOnGreen,
|
||||
modifier = Modifier.size(24.dp)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Text(
|
||||
text = "Device Trigger",
|
||||
fontSize = 20.sp,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = TextPrimary
|
||||
)
|
||||
}
|
||||
},
|
||||
actions = {
|
||||
// Quick jump to Add Device tab
|
||||
IconButton(
|
||||
onClick = {
|
||||
coroutineScope.launch {
|
||||
pagerState.animateScrollToPage(devices.size)
|
||||
}
|
||||
}
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Add,
|
||||
contentDescription = "Add Device",
|
||||
tint = PowerOnGreen
|
||||
)
|
||||
}
|
||||
},
|
||||
colors = TopAppBarDefaults.centerAlignedTopAppBarColors(
|
||||
containerColor = DarkSurface
|
||||
)
|
||||
)
|
||||
},
|
||||
bottomBar = {
|
||||
// Horizontal Pager Dots Indicator
|
||||
val totalPages = devices.size + 1
|
||||
if (totalPages > 1) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(vertical = 16.dp),
|
||||
horizontalArrangement = Arrangement.Center,
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
repeat(totalPages) { pageIndex ->
|
||||
val isSelected = pagerState.currentPage == pageIndex
|
||||
val isAddPage = pageIndex == devices.size
|
||||
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.padding(horizontal = 4.dp)
|
||||
.height(8.dp)
|
||||
.width(if (isSelected) 22.dp else 8.dp)
|
||||
.clip(if (isSelected) RoundedCornerShape(4.dp) else CircleShape)
|
||||
.background(
|
||||
when {
|
||||
isSelected -> PowerOnGreen
|
||||
isAddPage -> TextSecondary.copy(alpha = 0.5f)
|
||||
else -> TextSecondary.copy(alpha = 0.3f)
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
) { paddingValues ->
|
||||
HorizontalPager(
|
||||
state = pagerState,
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(paddingValues)
|
||||
) { page ->
|
||||
if (page < devices.size) {
|
||||
val currentDevice = devices[page]
|
||||
val isLoading = loadingStates[currentDevice.id] == true
|
||||
val statusInfo = statusStates[currentDevice.id]
|
||||
|
||||
DevicePage(
|
||||
device = currentDevice,
|
||||
isLoading = isLoading,
|
||||
lastStatus = statusInfo?.first,
|
||||
lastSuccess = statusInfo?.second,
|
||||
onDismissStatus = { statusStates.remove(currentDevice.id) },
|
||||
onTrigger = { action -> executeAction(currentDevice, action) },
|
||||
onDelete = { dev ->
|
||||
repository.deleteDevice(dev.id)
|
||||
devices = repository.getDevices()
|
||||
}
|
||||
)
|
||||
} else {
|
||||
AddDevicePage(
|
||||
triggerClient = triggerClient,
|
||||
onDeviceAdded = { newDevice ->
|
||||
repository.addDevice(newDevice)
|
||||
devices = repository.getDevices()
|
||||
coroutineScope.launch {
|
||||
val newPageIndex = devices.indexOfFirst { it.id == newDevice.id }
|
||||
if (newPageIndex >= 0) {
|
||||
pagerState.animateScrollToPage(newPageIndex)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user