From ab18a2d72220701083d2ca2267b8bad8f33e5983 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 16 Oct 2026 16:23:39 +0000 Subject: [PATCH] UI: a dashboard as Home In the Modem tab's place: a card with what the modem is doing (off, starting, ready, off hook, connecting, online with its rates down and up and the call's time, hanging up) and the action for it (set up SIP, start, hang up, stop, the terminal); the line (the SIP account, incoming calls, the default network as Wi-Fi or mobile data, fax); quick dial of the four systems last dialled (or a card to add one); shortcuts to the terminal and the directory; the modem's console, folded, with its AT command line. StatusScreen is gone (its console is the dashboard's). DashboardShotsTest draws it first run, ready with a directory and online into app/build/screens/. 37 tests pass; lint: no issues. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0172Hejd12i4aYikYW9vgpZs --- README.md | 8 +- .../neodesktop/dmodem/ui/DashboardScreen.kt | 422 ++++++++++++++++++ .../com/neodesktop/dmodem/ui/MainActivity.kt | 11 +- .../com/neodesktop/dmodem/ui/StatusScreen.kt | 128 ------ .../dmodem/ui/DashboardShotsTest.kt | 80 ++++ .../neodesktop/dmodem/ui/ScreenshotsTest.kt | 11 +- 6 files changed, 522 insertions(+), 138 deletions(-) create mode 100644 app/src/main/java/com/neodesktop/dmodem/ui/DashboardScreen.kt delete mode 100644 app/src/main/java/com/neodesktop/dmodem/ui/StatusScreen.kt create mode 100644 app/src/test/java/com/neodesktop/dmodem/ui/DashboardShotsTest.kt diff --git a/README.md b/README.md index 3d2fc59..add338c 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,12 @@ network: the phone's own cellular calls go through speech codecs ## The app -- **Modem** tab: start and stop the modem, the call's state and rates, and - a command line to the modem with what it answered. +- **Home**: what the modem is doing (off, ready, connecting, online with + its rates down and up and the call's time) and the one thing to do about + it (set up SIP, start, hang up, stop); the line (the SIP account, + incoming calls, the network, fax); quick dial of the systems last + dialled; the terminal and the directory; the modem's console (folded, + with an AT command line). - **Settings**: the SIP account (or paste `user:password@host`, as dmodemd's `SIP_LOGIN`), taking incoming calls, T.38, the AT commands sent at start (`ATX3` by default: SIP has no dial tone), the log levels. diff --git a/app/src/main/java/com/neodesktop/dmodem/ui/DashboardScreen.kt b/app/src/main/java/com/neodesktop/dmodem/ui/DashboardScreen.kt new file mode 100644 index 0000000..ab75d8e --- /dev/null +++ b/app/src/main/java/com/neodesktop/dmodem/ui/DashboardScreen.kt @@ -0,0 +1,422 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Copyright (C) 2026 Neo-Desktop +package com.neodesktop.dmodem.ui + +import android.content.Context +import android.net.ConnectivityManager +import android.net.Network +import android.net.NetworkCapabilities +import android.text.format.DateUtils +import android.widget.Toast +import androidx.compose.animation.animateContentSize +import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +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.rememberScrollState +import androidx.compose.foundation.shape.CircleShape +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.foundation.text.KeyboardActions +import androidx.compose.foundation.text.KeyboardOptions +import androidx.compose.foundation.verticalScroll +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.automirrored.filled.List +import androidx.compose.material.icons.filled.Add +import androidx.compose.material.icons.filled.Call +import androidx.compose.material.icons.filled.KeyboardArrowDown +import androidx.compose.material.icons.filled.KeyboardArrowUp +import androidx.compose.material3.Button +import androidx.compose.material3.ButtonDefaults +import androidx.compose.material3.Card +import androidx.compose.material3.CardDefaults +import androidx.compose.material3.FilledTonalButton +import androidx.compose.material3.Icon +import androidx.compose.material3.IconButton +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.OutlinedButton +import androidx.compose.material3.OutlinedTextField +import androidx.compose.material3.Text +import androidx.compose.material3.TextButton +import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableLongStateOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.saveable.rememberSaveable +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.platform.LocalContext +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.input.ImeAction +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import androidx.lifecycle.compose.collectAsStateWithLifecycle +import com.neodesktop.dmodem.NativeModem +import com.neodesktop.dmodem.modem.ModemService +import com.neodesktop.dmodem.modem.ModemState +import com.neodesktop.dmodem.settings.ModemSettings +import com.neodesktop.dmodem.settings.SettingsStore +import com.neodesktop.dmodem.terminal.DialEntry +import com.neodesktop.dmodem.terminal.Terminal +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch + +/** + * Home: what the modem is doing and the one thing to do about it, the + * line it is on, the systems last dialled, and the way to the rest. + */ +@Composable +fun DashboardScreen( + modifier: Modifier = Modifier, + onOpenTerminal: () -> Unit, + onOpenDirectory: () -> Unit, + onOpenSettings: () -> Unit, +) { + val context = LocalContext.current + val scope = rememberCoroutineScope() + val run by ModemState.run.collectAsStateWithLifecycle() + val status by ModemState.status.collectAsStateWithLifecycle() + val error by ModemState.error.collectAsStateWithLifecycle() + val store = remember { SettingsStore(context.applicationContext) } + val settings by store.settings.collectAsStateWithLifecycle(initialValue = ModemSettings()) + val entries by Terminal.directory.items.collectAsStateWithLifecycle() + val network = rememberNetwork(context) + + // the call's time online + var since by remember { mutableLongStateOf(0L) } + var now by remember { mutableLongStateOf(System.currentTimeMillis()) } + LaunchedEffect(status.link) { + since = if (status.link == NativeModem.Link.ONLINE) System.currentTimeMillis() else 0L + while (since != 0L) { + now = System.currentTimeMillis() + delay(1000) + } + } + + Column( + modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(16.dp), + verticalArrangement = Arrangement.spacedBy(12.dp), + ) { + HeroCard( + run = run, + status = status, + elapsed = if (since != 0L) (now - since) / 1000 else -1, + error = error, + configured = settings.complete, + onStart = { ModemService.start(context) }, + onStop = { ModemService.stop(context) }, + onHangUp = { scope.launch { Terminal.session.hangUp(status.link == NativeModem.Link.ONLINE) } }, + onTerminal = onOpenTerminal, + onSetUp = onOpenSettings, + ) + + LineCard(settings, run, network, onOpenSettings) + + SectionTitle("Quick dial", action = "Directory", onAction = onOpenDirectory) + val recent = entries.sortedWith(compareByDescending { it.lastDialed }.thenBy { it.name }).take(4) + if (recent.isEmpty()) { + Card(Modifier.fillMaxWidth().clickable(onClick = onOpenDirectory)) { + Row(Modifier.padding(16.dp), verticalAlignment = Alignment.CenterVertically) { + Icon(Icons.Filled.Add, null) + Spacer(Modifier.width(12.dp)) + Column { + Text("Add a system to dial", style = MaterialTheme.typography.titleSmall) + Text("A BBS, a host or an ISP: one tap dials it", style = MaterialTheme.typography.bodySmall) + } + } + } + } else { + for (e in recent) { + QuickDial(e, enabled = status.link == NativeModem.Link.IDLE && !status.offhook) { + scope.launch { + val err = Terminal.dial(context, e) + if (err != null) Toast.makeText(context, err, Toast.LENGTH_LONG).show() else onOpenTerminal() + } + } + } + } + + SectionTitle("Open") + Row(horizontalArrangement = Arrangement.spacedBy(12.dp)) { + Shortcut(">_", "Terminal", "VT100 / ANSI-BBS", Modifier.weight(1f), onOpenTerminal) + Shortcut(null, "Directory", "${entries.size} system${if (entries.size == 1) "" else "s"}", Modifier.weight(1f), onOpenDirectory) { + Icon(Icons.AutoMirrored.Filled.List, null) + } + } + + ConsoleCard(enabled = run == ModemState.Run.RUNNING) + } +} + +// ---------------------------------------------------------------------- + +private val Online = Color(0xff2e7d32) +private val Busy = Color(0xffef6c00) +private val Off = Color(0xff757575) + +@Composable +private fun HeroCard( + run: ModemState.Run, + status: NativeModem.Status, + elapsed: Long, + error: String?, + configured: Boolean, + onStart: () -> Unit, + onStop: () -> Unit, + onHangUp: () -> Unit, + onTerminal: () -> Unit, + onSetUp: () -> Unit, +) { + val online = run == ModemState.Run.RUNNING && status.link == NativeModem.Link.ONLINE + val (dot, title, sub) = when { + run == ModemState.Run.STARTING -> Triple(Busy, "Starting…", "Bringing up SIP on this network") + run == ModemState.Run.STOPPING -> Triple(Busy, "Stopping…", "") + run == ModemState.Run.STOPPED -> Triple(Off, "Modem off", if (configured) "Ready to start" else "Set up a SIP account to start") + online -> Triple(Online, "Online", "Connected") + status.link == NativeModem.Link.CONNECTING -> Triple(Busy, "Connecting…", "Training the line") + status.link == NativeModem.Link.HANGING_UP -> Triple(Busy, "Hanging up…", "") + status.offhook -> Triple(Busy, "Off hook", "Dialling") + else -> Triple(Online, "Ready", "On hook, waiting for a dial") + } + Card( + Modifier.fillMaxWidth().animateContentSize(), + colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.primaryContainer), + ) { + Column(Modifier.padding(20.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) { + Row(verticalAlignment = Alignment.CenterVertically) { + Box(Modifier.size(12.dp).background(dot, CircleShape)) + Spacer(Modifier.width(10.dp)) + Text(title, style = MaterialTheme.typography.headlineMedium, fontWeight = FontWeight.SemiBold) + Spacer(Modifier.weight(1f)) + if (elapsed >= 0) { + Text( + "%d:%02d".format(elapsed / 60, elapsed % 60), + style = MaterialTheme.typography.titleLarge, fontFamily = FontFamily.Monospace, + ) + } + } + if (online) { + Row(horizontalArrangement = Arrangement.spacedBy(24.dp)) { + Rate("↓ down", status.rxRate) + Rate("↑ up", status.txRate) + } + } else if (sub.isNotEmpty()) { + Text(sub, style = MaterialTheme.typography.bodyLarge) + } + error?.let { + Text(it, color = MaterialTheme.colorScheme.error, style = MaterialTheme.typography.bodyMedium) + } + Spacer(Modifier.height(4.dp)) + Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { + when { + run == ModemState.Run.STOPPED && !configured -> + Button(onClick = onSetUp) { Text("Set up SIP") } + run == ModemState.Run.STOPPED -> + Button(onClick = onStart) { Text("Start modem") } + online || status.offhook -> + Button( + onClick = onHangUp, + colors = ButtonDefaults.buttonColors(containerColor = MaterialTheme.colorScheme.error), + ) { Text("Hang up") } + else -> + OutlinedButton(onClick = onStop, enabled = run == ModemState.Run.RUNNING) { Text("Stop modem") } + } + if (run == ModemState.Run.RUNNING) { + FilledTonalButton(onClick = onTerminal) { Text("Terminal") } + } + } + } + } +} + +@Composable +private fun Rate(label: String, bps: Int) { + Column { + Text(label, style = MaterialTheme.typography.labelMedium) + Row(verticalAlignment = Alignment.Bottom) { + Text( + if (bps >= 1000) "%.1f".format(bps / 1000.0) else bps.toString(), + fontSize = 34.sp, fontWeight = FontWeight.Bold, + ) + Text(if (bps >= 1000) " kbit/s" else " bit/s", style = MaterialTheme.typography.bodyMedium, modifier = Modifier.padding(bottom = 6.dp)) + } + } +} + +@Composable +private fun LineCard(s: ModemSettings, run: ModemState.Run, network: String?, onSettings: () -> Unit) { + Card(Modifier.fillMaxWidth().clickable(onClick = onSettings)) { + Column(Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) { + Text("Line", style = MaterialTheme.typography.labelLarge, color = MaterialTheme.colorScheme.primary) + InfoRow("SIP", if (s.complete) "${s.user}@${s.domain}" else "not set up") + InfoRow("Calls in", if (s.listen) "answered (registered)" else "off") + InfoRow("Network", network ?: "none") + InfoRow("Fax", if (s.t38) "audio or T.38" else "audio") + if (run == ModemState.Run.RUNNING && s.listen) { + Text("Waiting for calls: RING shows in the terminal", style = MaterialTheme.typography.bodySmall) + } + } + } +} + +@Composable +private fun InfoRow(label: String, value: String) { + Row { + Text(label, Modifier.width(88.dp), style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant) + Text(value, style = MaterialTheme.typography.bodyMedium, maxLines = 1, overflow = TextOverflow.Ellipsis) + } +} + +@Composable +private fun SectionTitle(text: String, action: String? = null, onAction: () -> Unit = {}) { + Row(Modifier.fillMaxWidth().padding(top = 4.dp), verticalAlignment = Alignment.CenterVertically) { + Text(text, style = MaterialTheme.typography.titleMedium, modifier = Modifier.weight(1f)) + if (action != null) TextButton(onClick = onAction) { Text(action) } + } +} + +@Composable +private fun QuickDial(e: DialEntry, enabled: Boolean, onDial: () -> Unit) { + Card(Modifier.fillMaxWidth()) { + Row(Modifier.padding(start = 16.dp, end = 8.dp, top = 10.dp, bottom = 10.dp), verticalAlignment = Alignment.CenterVertically) { + Box( + Modifier.size(40.dp).background(MaterialTheme.colorScheme.secondaryContainer, RoundedCornerShape(10.dp)), + contentAlignment = Alignment.Center, + ) { + Text(e.name.ifBlank { e.number }.take(1).uppercase(), style = MaterialTheme.typography.titleMedium) + } + Spacer(Modifier.width(12.dp)) + Column(Modifier.weight(1f)) { + Text(e.name.ifBlank { e.number }, style = MaterialTheme.typography.titleSmall, maxLines = 1, overflow = TextOverflow.Ellipsis) + val last = if (e.lastDialed > 0) " · " + DateUtils.getRelativeTimeSpanString(e.lastDialed) else "" + Text(e.number + last, style = MaterialTheme.typography.bodySmall, fontFamily = FontFamily.Monospace, maxLines = 1) + } + FilledTonalButton(onClick = onDial, enabled = enabled) { + Icon(Icons.Filled.Call, null, Modifier.size(18.dp)) + Spacer(Modifier.width(6.dp)) + Text("Dial") + } + } + } +} + +@Composable +private fun Shortcut( + glyph: String?, + title: String, + sub: String, + modifier: Modifier, + onClick: () -> Unit, + icon: @Composable (() -> Unit)? = null, +) { + Card(modifier.clickable(onClick = onClick)) { + Column(Modifier.padding(16.dp)) { + if (icon != null) icon() else Text(glyph ?: "", style = MaterialTheme.typography.titleLarge, fontFamily = FontFamily.Monospace) + Spacer(Modifier.height(8.dp)) + Text(title, style = MaterialTheme.typography.titleSmall) + Text(sub, style = MaterialTheme.typography.bodySmall, maxLines = 1) + } + } +} + +/** the modem's raw output and a command line to it: folded away until wanted */ +@Composable +private fun ConsoleCard(enabled: Boolean) { + var open by rememberSaveable { mutableStateOf(false) } + val console by ModemState.console.flow.collectAsStateWithLifecycle() + val lines = console.replace("\r\n", "\n").replace("\r", "").lines().filter { it.isNotBlank() } + Card(Modifier.fillMaxWidth().animateContentSize()) { + Column(Modifier.padding(start = 16.dp, end = 4.dp, top = 4.dp, bottom = 12.dp)) { + Row(verticalAlignment = Alignment.CenterVertically) { + Text("Modem console", style = MaterialTheme.typography.titleSmall, modifier = Modifier.weight(1f)) + IconButton(onClick = { open = !open }) { + Icon(if (open) Icons.Filled.KeyboardArrowUp else Icons.Filled.KeyboardArrowDown, if (open) "Fold" else "Unfold") + } + } + val shown = if (open) lines.takeLast(40) else lines.takeLast(3) + Text( + if (shown.isEmpty()) "(nothing yet)" else shown.joinToString("\n"), + fontFamily = FontFamily.Monospace, + style = MaterialTheme.typography.bodySmall, + modifier = Modifier.padding(end = 12.dp), + ) + if (open) { + var line by remember { mutableStateOf("") } + val send = { + if (line.isNotEmpty()) { + ModemState.dte.write(line + "\r") + line = "" + } + } + Row(Modifier.padding(top = 8.dp, end = 12.dp), verticalAlignment = Alignment.CenterVertically) { + OutlinedTextField( + value = line, onValueChange = { line = it }, label = { Text("AT command") }, + singleLine = true, enabled = enabled, modifier = Modifier.weight(1f), + keyboardOptions = KeyboardOptions(imeAction = ImeAction.Send), + keyboardActions = KeyboardActions(onSend = { send() }), + ) + Spacer(Modifier.width(8.dp)) + Button(onClick = send, enabled = enabled) { Text("Send") } + } + TextButton(onClick = { ModemState.console.clear() }) { Text("Clear") } + } + } + } +} + +/** the default network as words ("Wi-Fi", "Mobile data"), following it */ +@Composable +private fun rememberNetwork(context: Context): String? { + var name by remember { mutableStateOf(networkName(context, null)) } + DisposableEffect(Unit) { + val cm = context.getSystemService(ConnectivityManager::class.java) + val cb = object : ConnectivityManager.NetworkCallback() { + override fun onCapabilitiesChanged(network: Network, caps: NetworkCapabilities) { + name = networkName(context, caps) + } + + override fun onLost(network: Network) { + name = null + } + } + try { + cm?.registerDefaultNetworkCallback(cb) + } catch (_: RuntimeException) { + } + onDispose { + try { + cm?.unregisterNetworkCallback(cb) + } catch (_: RuntimeException) { + } + } + } + return name +} + +private fun networkName(context: Context, given: NetworkCapabilities?): String? { + val cm = context.getSystemService(ConnectivityManager::class.java) ?: return null + val caps = given ?: cm.activeNetwork?.let { cm.getNetworkCapabilities(it) } ?: return null + return when { + caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> "Wi-Fi" + caps.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> "Ethernet" + caps.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> "Mobile data" + caps.hasTransport(NetworkCapabilities.TRANSPORT_VPN) -> "VPN" + else -> "Connected" + } +} diff --git a/app/src/main/java/com/neodesktop/dmodem/ui/MainActivity.kt b/app/src/main/java/com/neodesktop/dmodem/ui/MainActivity.kt index 8b0e81d..6655bd0 100644 --- a/app/src/main/java/com/neodesktop/dmodem/ui/MainActivity.kt +++ b/app/src/main/java/com/neodesktop/dmodem/ui/MainActivity.kt @@ -16,7 +16,7 @@ import androidx.compose.foundation.layout.padding import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.filled.List import androidx.compose.material.icons.filled.Call -import androidx.compose.material.icons.filled.Info +import androidx.compose.material.icons.filled.Home import androidx.compose.material.icons.filled.Settings import androidx.compose.material3.Icon import androidx.compose.material3.NavigationBar @@ -62,7 +62,7 @@ class MainActivity : ComponentActivity() { bottomBar = { if (!full) { NavigationBar { - Item(tab == TAB_MODEM, "Modem", { Icon(Icons.Filled.Info, null) }) { tab = TAB_MODEM } + Item(tab == TAB_MODEM, "Home", { Icon(Icons.Filled.Home, null) }) { tab = TAB_MODEM } Item(tab == TAB_TERMINAL, "Terminal", { Text(">_") }) { tab = TAB_TERMINAL } Item(tab == TAB_DIRECTORY, "Dial", { Icon(Icons.Filled.Call, null) }) { tab = TAB_DIRECTORY } Item(tab == TAB_SETTINGS, "Settings", { Icon(Icons.Filled.Settings, null) }) { tab = TAB_SETTINGS } @@ -73,7 +73,12 @@ class MainActivity : ComponentActivity() { ) { padding -> val m = Modifier.padding(padding).consumeWindowInsets(padding) when (tab) { - TAB_MODEM -> StatusScreen(m) + TAB_MODEM -> DashboardScreen( + m, + onOpenTerminal = { tab = TAB_TERMINAL }, + onOpenDirectory = { tab = TAB_DIRECTORY }, + onOpenSettings = { tab = TAB_SETTINGS }, + ) TAB_TERMINAL -> TerminalScreen(m, fullscreen = full, onFullscreen = { fullscreen = it }) TAB_DIRECTORY -> DirectoryScreen(m, onDialed = { tab = TAB_TERMINAL }) TAB_SETTINGS -> SettingsScreen(m) diff --git a/app/src/main/java/com/neodesktop/dmodem/ui/StatusScreen.kt b/app/src/main/java/com/neodesktop/dmodem/ui/StatusScreen.kt deleted file mode 100644 index 5dca921..0000000 --- a/app/src/main/java/com/neodesktop/dmodem/ui/StatusScreen.kt +++ /dev/null @@ -1,128 +0,0 @@ -// SPDX-License-Identifier: AGPL-3.0-only -// Copyright (C) 2026 Neo-Desktop -package com.neodesktop.dmodem.ui - -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.rememberScrollState -import androidx.compose.foundation.text.KeyboardActions -import androidx.compose.foundation.text.KeyboardOptions -import androidx.compose.foundation.verticalScroll -import androidx.compose.material3.Button -import androidx.compose.material3.Card -import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.OutlinedButton -import androidx.compose.material3.OutlinedTextField -import androidx.compose.material3.Text -import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue -import androidx.compose.ui.Modifier -import androidx.compose.ui.platform.LocalContext -import androidx.compose.ui.text.font.FontFamily -import androidx.compose.ui.text.input.ImeAction -import androidx.compose.ui.unit.dp -import androidx.lifecycle.compose.collectAsStateWithLifecycle -import com.neodesktop.dmodem.NativeModem -import com.neodesktop.dmodem.modem.ModemService -import com.neodesktop.dmodem.modem.ModemState - -/** The modem: on or off, the call, and a command line to it with what it answered. */ -@Composable -fun StatusScreen(modifier: Modifier = Modifier) { - val context = LocalContext.current - val run by ModemState.run.collectAsStateWithLifecycle() - val status by ModemState.status.collectAsStateWithLifecycle() - val error by ModemState.error.collectAsStateWithLifecycle() - val console by ModemState.console.flow.collectAsStateWithLifecycle() - - Column(modifier.fillMaxSize().padding(16.dp), verticalArrangement = Arrangement.spacedBy(12.dp)) { - Card(Modifier.fillMaxWidth()) { - Column(Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) { - Text(runText(run), style = MaterialTheme.typography.titleLarge) - if (run == ModemState.Run.RUNNING) { - Text(linkText(status), style = MaterialTheme.typography.bodyLarge) - } - error?.let { - Text(it, color = MaterialTheme.colorScheme.error) - } - } - } - Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { - when (run) { - ModemState.Run.STOPPED -> - Button(onClick = { ModemService.start(context) }) { Text("Start modem") } - else -> - Button( - onClick = { ModemService.stop(context) }, - enabled = run != ModemState.Run.STOPPING, - ) { Text("Stop modem") } - } - OutlinedButton(onClick = { ModemState.console.clear() }) { Text("Clear") } - } - CommandLine(enabled = run == ModemState.Run.RUNNING) - ConsoleText(console, Modifier.weight(1f).fillMaxWidth()) - } -} - -@Composable -private fun CommandLine(enabled: Boolean) { - var line by remember { mutableStateOf("") } - val send = { - if (line.isNotEmpty()) { - ModemState.dte.write(line + "\r") - line = "" - } - } - Row(verticalAlignment = androidx.compose.ui.Alignment.CenterVertically) { - OutlinedTextField( - value = line, - onValueChange = { line = it }, - label = { Text("AT command") }, - singleLine = true, - enabled = enabled, - keyboardOptions = KeyboardOptions(imeAction = ImeAction.Send), - keyboardActions = KeyboardActions(onSend = { send() }), - modifier = Modifier.weight(1f), - ) - Button(onClick = send, enabled = enabled, modifier = Modifier.padding(start = 8.dp)) { - Text("Send") - } - } -} - -@Composable -private fun ConsoleText(text: String, modifier: Modifier) { - val scroll = rememberScrollState() - LaunchedEffect(text) { scroll.scrollTo(scroll.maxValue) } - Card(modifier) { - Text( - // (CR LF a line's end; a CR alone, the modem's echo of one, nothing) - text.replace("\r\n", "\n").replace("\r", ""), - fontFamily = FontFamily.Monospace, - style = MaterialTheme.typography.bodySmall, - modifier = Modifier.fillMaxSize().padding(8.dp).verticalScroll(scroll), - ) - } -} - -private fun runText(r: ModemState.Run) = when (r) { - ModemState.Run.STOPPED -> "Modem off" - ModemState.Run.STARTING -> "Starting…" - ModemState.Run.RUNNING -> "Modem on" - ModemState.Run.STOPPING -> "Stopping…" -} - -private fun linkText(s: NativeModem.Status) = when (s.link) { - NativeModem.Link.ONLINE -> "Online: ${s.rxRate} bit/s down, ${s.txRate} bit/s up" - NativeModem.Link.CONNECTING -> "Connecting…" - NativeModem.Link.HANGING_UP -> "Hanging up…" - NativeModem.Link.IDLE -> if (s.offhook) "Off hook" else "On hook" -} diff --git a/app/src/test/java/com/neodesktop/dmodem/ui/DashboardShotsTest.kt b/app/src/test/java/com/neodesktop/dmodem/ui/DashboardShotsTest.kt new file mode 100644 index 0000000..58ac408 --- /dev/null +++ b/app/src/test/java/com/neodesktop/dmodem/ui/DashboardShotsTest.kt @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Copyright (C) 2026 Neo-Desktop +package com.neodesktop.dmodem.ui + +import android.graphics.Bitmap +import androidx.compose.ui.graphics.asAndroidBitmap +import androidx.compose.ui.test.captureToImage +import androidx.compose.ui.test.junit4.createAndroidComposeRule +import androidx.compose.ui.test.onRoot +import androidx.test.core.app.ApplicationProvider +import com.neodesktop.dmodem.NativeModem +import com.neodesktop.dmodem.modem.ModemState +import com.neodesktop.dmodem.settings.ModemSettings +import com.neodesktop.dmodem.settings.SettingsStore +import com.neodesktop.dmodem.terminal.DialEntry +import com.neodesktop.dmodem.terminal.TermCharset +import com.neodesktop.dmodem.terminal.Terminal +import kotlinx.coroutines.runBlocking +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import org.robolectric.annotation.GraphicsMode +import java.io.File + +/** Home in its states, drawn into build/screens/ to look at. */ +@RunWith(RobolectricTestRunner::class) +@GraphicsMode(GraphicsMode.Mode.NATIVE) +@Config(sdk = [35], qualifiers = "w411dp-h891dp-xxhdpi") +class DashboardShotsTest { + + @get:Rule + val compose = createAndroidComposeRule() + + private fun shot(name: String) { + compose.waitForIdle() + val bmp = compose.onRoot().captureToImage().asAndroidBitmap() + val dir = File("build/screens").apply { mkdirs() } + File(dir, "$name.png").outputStream().use { bmp.compress(Bitmap.CompressFormat.PNG, 100, it) } + } + + @Test + fun firstRun() { + // (the store and the directory are the process's: as a fresh install has them) + val ctx = ApplicationProvider.getApplicationContext() + runBlocking { SettingsStore(ctx).save(ModemSettings()) } + Terminal.directory.items.value.forEach { Terminal.directory.remove(it) } + ModemState.console.clear() + ModemState.setRun(ModemState.Run.STOPPED) + ModemState.setStatus(ModemState.IDLE) + compose.waitForIdle() + shot("home-1-first-run") + } + + @Test + fun readyAndOnline() { + val ctx = ApplicationProvider.getApplicationContext() + runBlocking { + SettingsStore(ctx).save(ModemSettings(user = "alice", password = "x", domain = "sip.example.net", listen = true)) + } + val hour = 3_600_000L + val now = System.currentTimeMillis() + Terminal.directory.put(DialEntry(name = "Level 29 BBS", number = "18005551229", charset = TermCharset.CP437, lastDialed = now - 2 * hour, dialCount = 7)) + Terminal.directory.put(DialEntry(name = "NIST time service", number = "3034944774", initCommands = "AT+MS=132,0,4800,9600", lastDialed = now - 50 * hour, dialCount = 2)) + Terminal.directory.put(DialEntry(name = "Dial-up ISP", number = "5551000", lastDialed = now - 200 * hour, dialCount = 15)) + Terminal.directory.put(DialEntry(name = "Friend's D-Modem", number = "bob", dialCount = 0)) + ModemState.console.append("ATX3\r\r\nOK\r\n") + ModemState.setRun(ModemState.Run.RUNNING) + ModemState.setStatus(NativeModem.Status(NativeModem.Link.IDLE, false, 0, 0)) + shot("home-2-ready") + + ModemState.console.append("ATD18005551229\r\r\nCONNECT 52000/V92\r\n") + ModemState.setStatus(NativeModem.Status(NativeModem.Link.ONLINE, true, 33600, 52000)) + compose.mainClock.advanceTimeBy(1000) + shot("home-3-online") + ModemState.setRun(ModemState.Run.STOPPED) + ModemState.setStatus(ModemState.IDLE) + } +} diff --git a/app/src/test/java/com/neodesktop/dmodem/ui/ScreenshotsTest.kt b/app/src/test/java/com/neodesktop/dmodem/ui/ScreenshotsTest.kt index a26ca4a..6e16666 100644 --- a/app/src/test/java/com/neodesktop/dmodem/ui/ScreenshotsTest.kt +++ b/app/src/test/java/com/neodesktop/dmodem/ui/ScreenshotsTest.kt @@ -7,6 +7,7 @@ import androidx.compose.ui.graphics.asAndroidBitmap import androidx.compose.ui.test.captureToImage import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onRoot +import androidx.compose.ui.test.onAllNodesWithText import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import com.neodesktop.dmodem.modem.ModemState @@ -51,14 +52,14 @@ class ScreenshotsTest { "\u001b[32mSelect:\u001b[0m ", ) } - shot("1-modem") - compose.onNodeWithText("Terminal").performClick() + shot("1-home") + compose.onAllNodesWithText("Terminal")[0].performClick() shot("2-terminal") - compose.onNodeWithText("Dial").performClick() + compose.onAllNodesWithText("Dial")[0].performClick() shot("3-dial") - compose.onNodeWithText("Settings").performClick() + compose.onAllNodesWithText("Settings")[0].performClick() shot("4-settings") - compose.onNodeWithText("Log").performClick() + compose.onAllNodesWithText("Log")[0].performClick() shot("5-log") } }