146 lines
5.0 KiB
Kotlin
146 lines
5.0 KiB
Kotlin
|
|
package com.example.netshareecohub.ui
|
||
|
|
|
||
|
|
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.width
|
||
|
|
import androidx.compose.foundation.lazy.LazyColumn
|
||
|
|
import androidx.compose.foundation.lazy.items
|
||
|
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||
|
|
import androidx.compose.material3.Card
|
||
|
|
import androidx.compose.material3.CardDefaults
|
||
|
|
import androidx.compose.material3.MaterialTheme
|
||
|
|
import androidx.compose.material3.Surface
|
||
|
|
import androidx.compose.material3.Text
|
||
|
|
import androidx.compose.runtime.Composable
|
||
|
|
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.example.netshareecohub.dhcp.DhcpClientLease
|
||
|
|
import com.example.netshareecohub.dhcp.LeaseStatus
|
||
|
|
import com.example.netshareecohub.dhcp.MacVendorResolver
|
||
|
|
|
||
|
|
@Composable
|
||
|
|
fun ConnectedDevicesScreen(
|
||
|
|
leases: List<DhcpClientLease>
|
||
|
|
) {
|
||
|
|
Column(
|
||
|
|
modifier = Modifier
|
||
|
|
.fillMaxSize()
|
||
|
|
.padding(16.dp)
|
||
|
|
) {
|
||
|
|
Row(
|
||
|
|
modifier = Modifier.fillMaxWidth(),
|
||
|
|
horizontalArrangement = Arrangement.SpaceBetween,
|
||
|
|
verticalAlignment = Alignment.CenterVertically
|
||
|
|
) {
|
||
|
|
Text(
|
||
|
|
text = "Connected Clients (${leases.size})",
|
||
|
|
style = MaterialTheme.typography.titleMedium,
|
||
|
|
fontWeight = FontWeight.Bold
|
||
|
|
)
|
||
|
|
}
|
||
|
|
|
||
|
|
Spacer(modifier = Modifier.height(12.dp))
|
||
|
|
|
||
|
|
if (leases.isEmpty()) {
|
||
|
|
Box(
|
||
|
|
modifier = Modifier.fillMaxSize(),
|
||
|
|
contentAlignment = Alignment.Center
|
||
|
|
) {
|
||
|
|
Text(
|
||
|
|
text = "No active DHCP client leases found.",
|
||
|
|
style = MaterialTheme.typography.bodyMedium,
|
||
|
|
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||
|
|
)
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
LazyColumn(
|
||
|
|
verticalArrangement = Arrangement.spacedBy(10.dp)
|
||
|
|
) {
|
||
|
|
items(leases) { lease ->
|
||
|
|
ConnectedDeviceCard(lease = lease)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Composable
|
||
|
|
fun ConnectedDeviceCard(lease: DhcpClientLease) {
|
||
|
|
val statusColor = when (lease.status) {
|
||
|
|
LeaseStatus.ONLINE -> Color(0xFF2E7D32)
|
||
|
|
LeaseStatus.OFFERED -> Color(0xFFF57F17)
|
||
|
|
LeaseStatus.EXPIRED -> Color.Gray
|
||
|
|
LeaseStatus.RELEASED -> Color(0xFFC62828)
|
||
|
|
}
|
||
|
|
|
||
|
|
val remainingTimeStr = formatRemainingTime(lease.leaseExpirationTime - System.currentTimeMillis())
|
||
|
|
|
||
|
|
Card(
|
||
|
|
modifier = Modifier.fillMaxWidth(),
|
||
|
|
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surfaceVariant),
|
||
|
|
elevation = CardDefaults.cardElevation(2.dp)
|
||
|
|
) {
|
||
|
|
Column(modifier = Modifier.padding(14.dp)) {
|
||
|
|
Row(
|
||
|
|
modifier = Modifier.fillMaxWidth(),
|
||
|
|
horizontalArrangement = Arrangement.SpaceBetween,
|
||
|
|
verticalAlignment = Alignment.CenterVertically
|
||
|
|
) {
|
||
|
|
Text(
|
||
|
|
text = MacVendorResolver.formatDeviceName(lease.hostName, lease.macAddress),
|
||
|
|
fontWeight = FontWeight.Bold,
|
||
|
|
fontSize = 16.sp,
|
||
|
|
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||
|
|
)
|
||
|
|
|
||
|
|
Surface(
|
||
|
|
color = statusColor,
|
||
|
|
shape = RoundedCornerShape(12.dp)
|
||
|
|
) {
|
||
|
|
Text(
|
||
|
|
text = lease.status.name,
|
||
|
|
color = Color.White,
|
||
|
|
fontSize = 11.sp,
|
||
|
|
fontWeight = FontWeight.Bold,
|
||
|
|
modifier = Modifier.padding(horizontal = 8.dp, vertical = 2.dp)
|
||
|
|
)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Spacer(modifier = Modifier.height(6.dp))
|
||
|
|
|
||
|
|
DashboardDetailRow("Assigned IP", lease.ipAddress)
|
||
|
|
DashboardDetailRow("MAC Address", lease.macAddress)
|
||
|
|
DashboardDetailRow("Lease Expiry", remainingTimeStr)
|
||
|
|
DashboardDetailRow("Interface", lease.interfaceName ?: "N/A")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private fun formatRemainingTime(millis: Long): String {
|
||
|
|
if (millis <= 0) return "Expired"
|
||
|
|
val seconds = millis / 1000
|
||
|
|
val hours = seconds / 3600
|
||
|
|
val minutes = (seconds % 3600) / 60
|
||
|
|
val secs = seconds % 60
|
||
|
|
return if (hours > 0) {
|
||
|
|
"${hours}h ${minutes}m ${secs}s"
|
||
|
|
} else if (minutes > 0) {
|
||
|
|
"${minutes}m ${secs}s"
|
||
|
|
} else {
|
||
|
|
"${secs}s"
|
||
|
|
}
|
||
|
|
}
|