diff --git a/android/app/src/main/java/com/siriusdevops/onionwire/ui/RosterScreen.kt b/android/app/src/main/java/com/siriusdevops/onionwire/ui/RosterScreen.kt index ed641dc..b3903cd 100644 --- a/android/app/src/main/java/com/siriusdevops/onionwire/ui/RosterScreen.kt +++ b/android/app/src/main/java/com/siriusdevops/onionwire/ui/RosterScreen.kt @@ -11,6 +11,7 @@ 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.material3.Card @@ -27,6 +28,7 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -51,6 +53,10 @@ fun RosterScreen( onOpenWallet: () -> Unit, ) { var confirmWipe by remember { mutableStateOf(false) } + // Closed by default; `rememberSaveable` so a rotation keeps whatever the + // user chose instead of snapping back to (or away from) the summary. + // Nothing about this is worth persisting to the store. + var identityExpanded by rememberSaveable { mutableStateOf(false) } Scaffold( // The content root below consumes systemBars ∪ ime itself (see @@ -79,11 +85,33 @@ fun RosterScreen( ), ) { Column(Modifier.padding(12.dp)) { - Label("YOU (stable forever)") - Mono(fingerprint) - Spacer(Modifier.height(10.dp)) - Label("ONION (locator — rotates)") - Mono(onion) + // The disclosure header. Collapsed, this one line *is* the + // identity block: the label, the abbreviated fingerprint, + // and a chevron saying there is more behind it. + Row( + modifier = Modifier + .fillMaxWidth() + .clickable { identityExpanded = !identityExpanded }, + verticalAlignment = Alignment.CenterVertically, + ) { + Label("YOU (stable forever)") + if (!identityExpanded) { + Spacer(Modifier.width(8.dp)) + Mono(shortId(fingerprint)) + } + Spacer(Modifier.weight(1f)) + Text( + if (identityExpanded) "▾" else "▸", + style = MaterialTheme.typography.labelSmall, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + } + if (identityExpanded) { + Mono(fingerprint) + Spacer(Modifier.height(10.dp)) + Label("ONION (locator — rotates)") + Mono(onion) + } Spacer(Modifier.height(10.dp)) Text( "sdk $sdkVersion · identity = ed25519 pubkey", @@ -194,6 +222,21 @@ private fun FriendRow(f: FriendInfo, onClick: () -> Unit) { } } +/** + * The compact stand-in for a long identity string: `head` characters, an + * ellipsis, then `tail` characters. Pure, so the roster's collapsed line and + * its unit test agree on what "short" means. + * + * A string no longer than the abbreviation is returned unchanged — the + * abbreviation must never grow a value, and a short value must never come back + * mangled. Kotlin strings are UTF-16, but these IDs are hex, so `take`/ + * `takeLast` cannot split a surrogate pair here. + */ +fun shortId(id: String, head: Int = 6, tail: Int = 4): String { + if (id.length < head + tail + 1) return id + return id.take(head) + "…" + id.takeLast(tail) +} + @Composable fun Label(text: String) { Text( diff --git a/android/app/src/test/java/com/siriusdevops/onionwire/ui/RosterIdTest.kt b/android/app/src/test/java/com/siriusdevops/onionwire/ui/RosterIdTest.kt new file mode 100644 index 0000000..d890fb3 --- /dev/null +++ b/android/app/src/test/java/com/siriusdevops/onionwire/ui/RosterIdTest.kt @@ -0,0 +1,84 @@ +package com.siriusdevops.onionwire.ui + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test + +/** + * `shortId`, the roster's collapsed identity line, on the JVM. + * + * The collapsed card shows the fingerprint through this function, and the + * expanded one shows the raw value; the two must never disagree about what the + * fingerprint *is*, so the abbreviation has to be a pure slice of the input + * with nothing invented. Pure logic, no Robolectric — same shape as + * `SeedBackupGateTest` and `WalletChildStatusTextTest`. + */ +class RosterIdTest { + + /** An ed25519 pubkey as the core prints it: 64 lowercase hex characters. */ + private val fingerprint = + "9f2c41ab77de03b5c8e1f0a6d4b39c2e5a7f81c0d3e6b492af17c5d80e3b6a49" + + // --- the long case ------------------------------------------------------- + + @Test + fun a_long_fingerprint_becomes_head_ellipsis_tail() { + assertEquals("9f2c41…6a49", shortId(fingerprint)) + } + + @Test + fun the_abbreviation_is_a_slice_of_the_input_not_a_re_encoding() { + // The head and the tail must be literally the input's own characters: + // a summary that cannot be checked against the full value is worse than + // no summary. + val short = shortId(fingerprint) + assertEquals(fingerprint.take(6), short.substringBefore("…")) + assertEquals(fingerprint.takeLast(4), short.substringAfter("…")) + // Nothing invented to pad the middle: 6 + "…" + 4. + assertEquals(11, short.length) + } + + @Test + fun head_and_tail_are_call_sites_choice() { + assertEquals("9f2c…6a49", shortId(fingerprint, head = 4, tail = 4)) + assertEquals("9f2c41ab77…3b6a49", shortId(fingerprint, head = 10, tail = 6)) + } + + // --- the short case ------------------------------------------------------ + + @Test + fun a_string_shorter_than_head_tail_plus_one_is_returned_unchanged() { + // head + tail + 1 == 11 here, so anything of 10 characters or fewer has + // no middle worth hiding. + assertEquals("abcdefghij", shortId("abcdefghij")) + assertEquals("abc", shortId("abc")) + assertEquals("a", shortId("a")) + } + + @Test + fun the_first_length_that_abbreviates_is_head_tail_plus_one() { + // The boundary, both sides: 10 unchanged, 11 abbreviated to the same + // length with the middle character collapsed. + assertEquals("abcdefghij", shortId("abcdefghij")) + assertEquals("abcdef…hijk", shortId("abcdefghijk")) + } + + // --- the empty case ------------------------------------------------------ + + @Test + fun an_empty_string_stays_empty() { + // `ready.fingerprint` is always populated in practice, but the function + // must not turn "nothing" into "…" on a card that is already on screen. + assertEquals("", shortId("")) + } + + // --- the shape the card relies on --------------------------------------- + + @Test + fun the_ellipsis_is_the_single_character_one() { + // U+2026, not three dots: the collapsed line is monospace and a + // three-dot tail is four cells where the design budgeted one. + assertTrue(shortId(fingerprint).contains("\u2026")) + assertEquals(1, shortId(fingerprint).count { it == '\u2026' }) + } +}