Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions app/src/main/java/com/excp/podroid/ui/components/LiveLineChart.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Podroid - Rootless Podman for Android
* Copyright (C) 2024-2026 Podroid contributors
*/
package com.excp.podroid.ui.components

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.unit.dp

data class ChartSeries(
val values: List<Float>,
val color: Color,
)

@Composable
fun LiveLineChart(
series: List<ChartSeries>,
modifier: Modifier = Modifier,
yMax: Float? = null,
heightDp: Int = 120,
) {
val gridColor = MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.55f)
Canvas(
modifier = modifier
.fillMaxWidth()
.height(heightDp.dp),
) {
val w = size.width
val h = size.height
if (w <= 0f || h <= 0f) return@Canvas

// Horizontal grid (Azure-like)
for (i in 0..3) {
val y = h * i / 3f
drawLine(gridColor, Offset(0f, y), Offset(w, y), strokeWidth = 1f)
}

val peak = series.flatMap { it.values }.maxOrNull()?.coerceAtLeast(0.01f) ?: 1f
val maxY = (yMax ?: (peak * 1.15f)).coerceAtLeast(0.01f)

series.forEach { s ->
if (s.values.size < 2) return@forEach
val path = Path()
val n = s.values.size
s.values.forEachIndexed { i, v ->
val x = if (n == 1) 0f else w * i / (n - 1)
val y = h - (v.coerceIn(0f, maxY) / maxY) * h
if (i == 0) path.moveTo(x, y) else path.lineTo(x, y)
}
drawPath(
path = path,
color = s.color,
style = Stroke(width = 2.5.dp.toPx(), cap = StrokeCap.Round),
)
}
}
}
130 changes: 130 additions & 0 deletions app/src/main/java/com/excp/podroid/ui/components/MetricChartCard.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Podroid - Rootless Podman for Android
* Copyright (C) 2024-2026 Podroid contributors
*
* Azure-style metric tile: title, live line chart, colored legend rows.
*/
package com.excp.podroid.ui.components

import androidx.compose.foundation.background
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.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
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.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.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.excp.podroid.ui.theme.PodroidTokens

data class MetricLegend(
val label: String,
val value: String,
val color: Color,
val subtitle: String? = null,
)

@Composable
fun MetricChartCard(
title: String,
legends: List<MetricLegend>,
series: List<ChartSeries>,
modifier: Modifier = Modifier,
yMax: Float? = null,
unavailable: String? = null,
timeHint: String? = null,
) {
Card(
modifier = modifier.fillMaxWidth(),
shape = RoundedCornerShape(PodroidTokens.Radius.Chip),
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.surface,
),
elevation = CardDefaults.cardElevation(defaultElevation = 1.dp),
border = androidx.compose.foundation.BorderStroke(
1.dp,
MaterialTheme.colorScheme.outlineVariant,
),
) {
Column(
modifier = Modifier.padding(PodroidTokens.Spacing.MD),
verticalArrangement = Arrangement.spacedBy(PodroidTokens.Spacing.SM),
) {
Text(
text = title,
style = MaterialTheme.typography.titleSmall,
fontWeight = FontWeight.SemiBold,
color = MaterialTheme.colorScheme.onSurface,
)

if (unavailable != null) {
Text(
text = unavailable,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
modifier = Modifier
.fillMaxWidth()
.height(120.dp)
.padding(top = PodroidTokens.Spacing.LG),
)
} else {
LiveLineChart(series = series, yMax = yMax)
if (timeHint != null) {
Text(
text = timeHint,
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
}

legends.forEach { legend ->
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically,
) {
Spacer(
modifier = Modifier
.width(3.dp)
.height(28.dp)
.background(legend.color, RoundedCornerShape(1.dp)),
)
Spacer(modifier = Modifier.width(PodroidTokens.Spacing.SM))
Column(modifier = Modifier.weight(1f)) {
Text(
text = legend.label,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurface,
)
if (legend.subtitle != null) {
Text(
text = legend.subtitle,
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
}
Text(
text = legend.value,
style = MaterialTheme.typography.bodyMedium,
fontFamily = FontFamily.Monospace,
fontWeight = FontWeight.Medium,
color = MaterialTheme.colorScheme.onSurface,
)
}
}
}
}
}
Loading