Android login: the server starts filled in (last used, else https://forum.towerforever.com), shown as text with Change

a8b6885 · gmorell · 2026-12-03 18:10

26 files +269 -42
Message
{commit_body(@commit)}

Files changed

modified android/README.md
+6 −1
@@ -33,7 +33,12 @@ sheet; back or a swipe down closes it. `ui/login/LoginScreen.kt`,
33 33 `data/AppLogin.kt`; the theme is the app's own (Blueprint and the rest).
34 34
35 35 - **Scan:** pairing, below.
36 −- **Sign in with NEIAM:** type the server once (remembered), and the app opens
36 +- **Server:** the NEIAM and Username sheets start on the last server signed
37 + in to, else the app's default (`DEFAULT_SERVER` in `app/build.gradle.kts`,
38 + https://forum.towerforever.com). They show it as text with a **Change** button; the editor
39 + holds the current address, keeps the old one if the new one isn't valid,
40 + and offers **Reset to default**. Back closes the editor before the sheet.
41 +- **Sign in with NEIAM:** the app opens
37 42 `<server>/auth/oidc/neiam/login?app=1&challenge=…` in a Custom Tab with a
38 43 fresh PKCE pair. The server sends the browser back to
39 44 `recourse://auth?code=…`; the app trades the code and its verifier at
modified android/app/build.gradle.kts
+4 −0
@@ -51,6 +51,10 @@ android {
51 51 // timestamp orders builds at a glance in a list that versionCode
52 52 // alone renders as bare integers.
53 53 versionName = appVersionName
54 +
55 + // The server the login page starts on (until a sign-in remembers
56 + // another). Editable in the app; the same for debug and release.
57 + buildConfigField("String", "DEFAULT_SERVER", "\"https://forum.towerforever.com\"")
54 58 }
55 59
56 60 buildTypes {
modified android/app/src/main/java/org/neiam/recourse/app/MainActivity.kt
+1 −1
@@ -202,7 +202,7 @@ fun RecourseApp(
202 202 /** The login page's words for recourse. */
203 203 val RECOURSE_LOGIN = LoginBranding(
204 204 appName = "recourse",
205 − serverPlaceholder = "https://forum.example",
205 + defaultServer = BuildConfig.DEFAULT_SERVER,
206 206 help = LoginHelp(
207 207 scan = "Pairs this phone with an account you are already signed in to on the web. In a " +
208 208 "browser, open the forum, sign in, and go to Devices (the account menu) → Pair a " +
modified android/app/src/main/java/org/neiam/recourse/app/data/AppLogin.kt
+31 −0
@@ -114,9 +114,14 @@ object AuthClient {
114 114 * `https://forum.example/` -> `https://forum.example`; adds `https://` to a
115 115 * bare host. Null for something that can't be a server address.
116 116 */
117 +private val BROKEN_SCHEME = Regex("(?i)^https?:(?!//[^/])")
118 +
117 119 fun normalizeServer(raw: String): String? {
118 120 var s = raw.trim().trimEnd('/')
119 121 if (s.isEmpty()) return null
122 + // "https://" alone, or "https:/x": a scheme with no host, which would
123 + // otherwise come out as a server called "https".
124 + if (BROKEN_SCHEME.containsMatchIn(s.trim())) return null
120 125 if (!s.contains("://")) s = "https://$s"
121 126 val uri = runCatching { URI(s) }.getOrNull() ?: return null
122 127 if (uri.scheme != "http" && uri.scheme != "https") return null
@@ -124,6 +129,32 @@ fun normalizeServer(raw: String): String? {
124 129 return s
125 130 }
126 131
132 +/**
133 + * The server the login page starts on: the last one signed in to, else the
134 + * app's default (BuildConfig.DEFAULT_SERVER). Never empty, so the methods
135 + * call runs as soon as the page opens.
136 + */
137 +fun initialServer(lastUsed: String?, default: String): String =
138 + lastUsed?.let(::normalizeServer) ?: normalizeServer(default) ?: default
139 +
140 +/** What typing a new server into the Change field comes to. */
141 +sealed interface ServerEdit {
142 + data class Accepted(val server: String) : ServerEdit
143 + /** The previous server stays; [message] says why. */
144 + data class Rejected(val message: String) : ServerEdit
145 +}
146 +
147 +fun editServer(typed: String, default: String): ServerEdit =
148 + normalizeServer(typed)?.let { ServerEdit.Accepted(it) }
149 + ?: ServerEdit.Rejected("That isn't a server address. Try one like $default")
150 +
151 +/** "monster.neiam.org" for display; the port too when there is one. */
152 +fun serverHost(server: String): String = runCatching {
153 + val uri = URI(server)
154 + val host = uri.host ?: return@runCatching null
155 + if (uri.port != -1) "$host:${uri.port}" else host
156 +}.getOrNull() ?: server
157 +
127 158 /** The server's `{error, message}` (or the older `{detail}`), else a plain description. */
128 159 private val errorJson = Json { ignoreUnknownKeys = true }
129 160
modified android/app/src/main/java/org/neiam/recourse/app/ui/login/LoginScreen.kt
+44 −40
@@ -20,6 +20,7 @@ import androidx.compose.material.icons.filled.VerifiedUser
20 20 import androidx.compose.material.icons.filled.Visibility
21 21 import androidx.compose.material.icons.filled.VisibilityOff
22 22 import androidx.compose.material3.*
23 +import androidx.compose.material3.ModalBottomSheetProperties
23 24 import androidx.compose.runtime.*
24 25 import androidx.compose.runtime.saveable.rememberSaveable
25 26 import androidx.compose.ui.Alignment
@@ -44,7 +45,6 @@ import androidx.compose.ui.text.input.VisualTransformation
44 45 import androidx.compose.ui.text.style.TextAlign
45 46 import androidx.compose.ui.text.style.TextOverflow
46 47 import androidx.compose.ui.unit.dp
47 −import kotlinx.coroutines.delay
48 48 import kotlinx.coroutines.launch
49 49 import org.neiam.recourse.app.data.AuthClient
50 50 import org.neiam.recourse.app.data.AuthMethods
@@ -55,6 +55,8 @@ import org.neiam.recourse.app.data.PasswordBody
55 55 import org.neiam.recourse.app.data.Pkce
56 56 import org.neiam.recourse.app.data.TokenStore
57 57 import org.neiam.recourse.app.data.authMessage
58 +import org.neiam.recourse.app.data.initialServer
59 +import org.neiam.recourse.app.data.serverHost
58 60 import org.neiam.recourse.app.data.normalizeServer
59 61 import org.neiam.recourse.app.data.oidcLoginUrl
60 62 import org.neiam.recourse.app.ui.PairPanel
@@ -71,7 +73,8 @@ data class LoginBranding(
71 73 val usernameDescription: String = "Your username or email and password",
72 74 val scanDescription: String = "Pair with the QR from the web",
73 75 val oidcDescription: String = "Use your NEIAM account in the browser",
74 − val serverPlaceholder: String = "https://server.example",
76 + /** The server the page starts on until a sign-in remembers another (BuildConfig.DEFAULT_SERVER). */
77 + val defaultServer: String,
75 78 val help: LoginHelp,
76 79 )
77 80
@@ -109,15 +112,19 @@ fun LoginScreen(
109 112 val context = LocalContext.current
110 113 var sheet by rememberSaveable { mutableStateOf(if (prefilledPair != null) Sheet.Scan else Sheet.None) }
111 114 var showHelp by rememberSaveable { mutableStateOf(false) }
112 − var server by rememberSaveable { mutableStateOf(prefs.lastServer ?: "") }
115 + var server by rememberSaveable { mutableStateOf(initialServer(prefs.lastServer, branding.defaultServer)) }
116 + // The Change field in the NEIAM and Username sheets (they share the
117 + // server). Here so back can close it before the sheet.
118 + var editingServer by rememberSaveable { mutableStateOf(false) }
113 119 var methods by remember { mutableStateOf<AuthMethods?>(null) }
114 120 var oidcStatus by remember { mutableStateOf<FlowStatus>(FlowStatus.Idle) }
115 121
116 − // What the server offers, for the last address typed. A failure (an
117 − // older server, no network) leaves every button on.
122 + // What the server offers, from the moment the page opens (the server is
123 + // never empty: last used, else the default). A failure (an older
124 + // server, no network) leaves every button on.
118 125 LaunchedEffect(server) {
119 − val base = normalizeServer(server) ?: run { methods = null; return@LaunchedEffect }
120 − delay(500)
126 + methods = null
127 + val base = normalizeServer(server) ?: return@LaunchedEffect
121 128 methods = runCatching { AuthClient.build(base).methods() }.getOrNull()
122 129 }
123 130
@@ -160,7 +167,7 @@ fun LoginScreen(
160 167 LoginHelpScreen(
161 168 branding = branding,
162 169 oidcLabel = methods?.oidc?.label?.takeIf { it.isNotBlank() } ?: branding.oidcLabel,
163 − server = normalizeServer(server),
170 + server = server,
164 171 onBack = { showHelp = false },
165 172 )
166 173 return
@@ -193,10 +200,18 @@ fun LoginScreen(
193 200 val state = rememberModalBottomSheetState(skipPartiallyExpanded = true)
194 201 val scope = rememberCoroutineScope()
195 202 val close: () -> Unit = {
196 − scope.launch { state.hide() }.invokeOnCompletion { sheet = Sheet.None }
203 + scope.launch { state.hide() }.invokeOnCompletion { editingServer = false; sheet = Sheet.None }
197 204 }
198 − // Back (and predictive back) and a swipe down both land here.
199 − ModalBottomSheet(onDismissRequest = { sheet = Sheet.None }, sheetState = state) {
205 + // A swipe down (or a tap outside) lands in onDismissRequest. Back is
206 + // handled inside: it closes the server editor first, then the sheet.
207 + // (The sheet's own back handling is fixed when it opens, so it can't
208 + // follow the editor.)
209 + ModalBottomSheet(
210 + onDismissRequest = { editingServer = false; sheet = Sheet.None },
211 + sheetState = state,
212 + properties = ModalBottomSheetProperties(shouldDismissOnBackPress = false),
213 + ) {
214 + BackHandler { if (editingServer) editingServer = false else close() }
200 215 Column(
201 216 Modifier
202 217 .fillMaxWidth()
@@ -215,9 +230,10 @@ fun LoginScreen(
215 230 SheetTitle(oidcLabel)
216 231 OidcPanel(
217 232 label = oidcLabel,
218 − server = server,
219 − onServer = { server = it },
220 − placeholder = branding.serverPlaceholder,
233 + serverSetting = {
234 + ServerSetting(server, branding.defaultServer, { server = it }, editingServer, { editingServer = it })
235 + },
236 + canStart = !editingServer,
221 237 status = oidcStatus,
222 238 onStart = {
223 239 val base = normalizeServer(server)
@@ -236,8 +252,9 @@ fun LoginScreen(
236 252 SheetTitle(branding.usernameTitle)
237 253 UsernamePanel(
238 254 server = server,
239 − onServer = { server = it },
240 − placeholder = branding.serverPlaceholder,
255 + serverSetting = {
256 + ServerSetting(server, branding.defaultServer, { server = it }, editingServer, { editingServer = it })
257 + },
241 258 tokens = tokens,
242 259 prefs = prefs,
243 260 onSignedIn = { close(); onSignedIn() },
@@ -432,25 +449,11 @@ private fun SheetTitle(text: String) {
432 449
433 450 // ------------------------------------------------------------------ sheets --
434 451
435 −@Composable
436 −private fun ServerField(server: String, onServer: (String) -> Unit, placeholder: String, imeAction: ImeAction) {
437 − OutlinedTextField(
438 − value = server,
439 − onValueChange = onServer,
440 − label = { Text("Server") },
441 − placeholder = { Text(placeholder) },
442 − singleLine = true,
443 − modifier = Modifier.fillMaxWidth(),
444 − keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri, imeAction = imeAction),
445 − )
446 −}
447 −
448 452 @Composable
449 453 private fun OidcPanel(
450 454 label: String,
451 − server: String,
452 − onServer: (String) -> Unit,
453 − placeholder: String,
455 + serverSetting: @Composable () -> Unit,
456 + canStart: Boolean,
454 457 status: FlowStatus,
455 458 onStart: () -> Unit,
456 459 ) {
@@ -458,10 +461,10 @@ private fun OidcPanel(
458 461 "Sign in in your browser with your NEIAM account. When it's done, you come back here signed in.",
459 462 style = MaterialTheme.typography.bodyMedium,
460 463 )
461 − ServerField(server, onServer, placeholder, ImeAction.Go)
464 + serverSetting()
462 465 Button(
463 466 onClick = onStart,
464 − enabled = server.isNotBlank() && status !is FlowStatus.Busy,
467 + enabled = canStart && status !is FlowStatus.Busy,
465 468 modifier = Modifier.fillMaxWidth().heightIn(min = 48.dp),
466 469 ) { Text(label) }
467 470 StatusLine(status)
@@ -484,8 +487,7 @@ private fun StatusLine(status: FlowStatus) {
484 487 @Composable
485 488 private fun UsernamePanel(
486 489 server: String,
487 − onServer: (String) -> Unit,
488 − placeholder: String,
490 + serverSetting: @Composable () -> Unit,
489 491 tokens: TokenStore,
490 492 prefs: LoginPrefs,
491 493 onSignedIn: () -> Unit,
@@ -520,7 +522,7 @@ private fun UsernamePanel(
520 522 }
521 523 }
522 524
523 − ServerField(server, onServer, placeholder, ImeAction.Next)
525 + serverSetting()
524 526 OutlinedTextField(
525 527 value = login,
526 528 onValueChange = { login = it },
@@ -563,7 +565,7 @@ private fun UsernamePanel(
563 565
564 566 @OptIn(ExperimentalMaterial3Api::class)
565 567 @Composable
566 −fun LoginHelpScreen(branding: LoginBranding, oidcLabel: String, server: String?, onBack: () -> Unit) {
568 +fun LoginHelpScreen(branding: LoginBranding, oidcLabel: String, server: String, onBack: () -> Unit) {
567 569 val context = LocalContext.current
568 570 val version = remember {
569 571 runCatching { context.packageManager.getPackageInfo(context.packageName, 0).versionName }.getOrNull()
@@ -598,8 +600,10 @@ fun LoginHelpScreen(branding: LoginBranding, oidcLabel: String, server: String?,
598 600 HelpSection(branding.usernameTitle, branding.help.username)
599 601 HelpSection(
600 602 "This server",
601 − server ?: "No server chosen yet. Scan fills it in; for the other two, type the address you " +
602 − "use for ${branding.appName} in a browser.",
603 + "${serverHost(server)} ($server)" +
604 + (if (server == branding.defaultServer) ", the app's default." else ".") +
605 + " To use another, tap Change in the $oidcLabel or ${branding.usernameTitle} sheet; " +
606 + "Reset to default there brings this one back. Scan doesn't need it: the QR carries its own server.",
603 607 )
604 608 HelpSection(
605 609 "About",
added android/app/src/main/java/org/neiam/recourse/app/ui/login/ServerSetting.kt
+132 −0
@@ -0,0 +1,132 @@
1 +package org.neiam.recourse.app.ui.login
2 +
3 +import androidx.compose.foundation.layout.Arrangement
4 +import androidx.compose.foundation.layout.Column
5 +import androidx.compose.foundation.layout.Row
6 +import androidx.compose.foundation.layout.Spacer
7 +import androidx.compose.foundation.layout.fillMaxWidth
8 +import androidx.compose.foundation.layout.heightIn
9 +import androidx.compose.foundation.layout.size
10 +import androidx.compose.foundation.layout.width
11 +import androidx.compose.foundation.text.KeyboardActions
12 +import androidx.compose.foundation.text.KeyboardOptions
13 +import androidx.compose.material.icons.Icons
14 +import androidx.compose.material.icons.filled.Edit
15 +import androidx.compose.material3.Button
16 +import androidx.compose.material3.Icon
17 +import androidx.compose.material3.MaterialTheme
18 +import androidx.compose.material3.OutlinedTextField
19 +import androidx.compose.material3.Text
20 +import androidx.compose.material3.TextButton
21 +import androidx.compose.runtime.Composable
22 +import androidx.compose.runtime.LaunchedEffect
23 +import androidx.compose.runtime.getValue
24 +import androidx.compose.runtime.mutableStateOf
25 +import androidx.compose.runtime.remember
26 +import androidx.compose.runtime.saveable.rememberSaveable
27 +import androidx.compose.runtime.setValue
28 +import androidx.compose.ui.Alignment
29 +import androidx.compose.ui.Modifier
30 +import androidx.compose.ui.focus.FocusRequester
31 +import androidx.compose.ui.focus.focusRequester
32 +import androidx.compose.ui.semantics.contentDescription
33 +import androidx.compose.ui.semantics.semantics
34 +import androidx.compose.ui.text.input.ImeAction
35 +import androidx.compose.ui.text.input.KeyboardType
36 +import androidx.compose.ui.text.style.TextOverflow
37 +import androidx.compose.ui.unit.dp
38 +import org.neiam.recourse.app.data.ServerEdit
39 +import org.neiam.recourse.app.data.editServer
40 +import org.neiam.recourse.app.data.serverHost
41 +
42 +/**
43 + * The server a sheet signs in to: shown as text (the host, with the full
44 + * address under it) and a Change button. Change opens a field holding the
45 + * current address, not a hint; Use (or Done on the keyboard) takes a valid
46 + * address and closes it, and anything else keeps the old server and says
47 + * why. Reset to default appears once the server isn't the app's default.
48 + *
49 + * [editing] is the caller's, so back can close the editor before the sheet.
50 + */
51 +@Composable
52 +fun ServerSetting(
53 + server: String,
54 + default: String,
55 + onServer: (String) -> Unit,
56 + editing: Boolean,
57 + onEditing: (Boolean) -> Unit,
58 +) {
59 + if (!editing) {
60 + Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
61 + Column(
62 + Modifier
63 + .weight(1f)
64 + .semantics(mergeDescendants = true) { contentDescription = "Server: $server" },
65 + ) {
66 + Text("Server", style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.onSurfaceVariant)
67 + Text(
68 + serverHost(server),
69 + style = MaterialTheme.typography.titleMedium,
70 + maxLines = 1,
71 + overflow = TextOverflow.Ellipsis,
72 + )
73 + Text(
74 + server,
75 + style = MaterialTheme.typography.bodySmall,
76 + color = MaterialTheme.colorScheme.onSurfaceVariant,
77 + maxLines = 1,
78 + overflow = TextOverflow.Ellipsis,
79 + )
80 + }
81 + TextButton(onClick = { onEditing(true) }, modifier = Modifier.heightIn(min = 48.dp)) {
82 + Icon(Icons.Default.Edit, contentDescription = null, modifier = Modifier.size(18.dp))
83 + Spacer(Modifier.width(6.dp))
84 + Text("Change")
85 + }
86 + }
87 + return
88 + }
89 +
90 + var typed by rememberSaveable { mutableStateOf(server) }
91 + var error by remember { mutableStateOf<String?>(null) }
92 + val focus = remember { FocusRequester() }
93 + LaunchedEffect(Unit) { runCatching { focus.requestFocus() } }
94 +
95 + fun use() {
96 + when (val edit = editServer(typed, default)) {
97 + is ServerEdit.Accepted -> {
98 + onServer(edit.server)
99 + onEditing(false)
100 + }
101 + is ServerEdit.Rejected -> error = edit.message
102 + }
103 + }
104 +
105 + Column(verticalArrangement = Arrangement.spacedBy(4.dp)) {
106 + OutlinedTextField(
107 + value = typed,
108 + onValueChange = { typed = it; error = null },
109 + label = { Text("Server") },
110 + singleLine = true,
111 + isError = error != null,
112 + supportingText = { Text(error ?: "Now: ${serverHost(server)}") },
113 + keyboardOptions = KeyboardOptions(
114 + keyboardType = KeyboardType.Uri,
115 + imeAction = ImeAction.Done,
116 + autoCorrectEnabled = false,
117 + ),
118 + keyboardActions = KeyboardActions(onDone = { use() }),
119 + modifier = Modifier.fillMaxWidth().focusRequester(focus),
120 + )
121 + Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(8.dp, Alignment.End)) {
122 + if (server != default || typed.trim().trimEnd('/') != default) {
123 + TextButton(
124 + onClick = { onServer(default); onEditing(false) },
125 + modifier = Modifier.heightIn(min = 48.dp),
126 + ) { Text("Reset to default") }
127 + }
128 + TextButton(onClick = { onEditing(false) }, modifier = Modifier.heightIn(min = 48.dp)) { Text("Cancel") }
129 + Button(onClick = { use() }, modifier = Modifier.heightIn(min = 48.dp)) { Text("Use") }
130 + }
131 + }
132 +}
added android/app/src/test/java/org/neiam/recourse/app/data/ServerDefaultTest.kt
+51 −0
@@ -0,0 +1,51 @@
1 +package org.neiam.recourse.app.data
2 +
3 +import org.junit.Assert.assertEquals
4 +import org.junit.Assert.assertTrue
5 +import org.junit.Test
6 +import org.neiam.recourse.app.BuildConfig
7 +
8 +/** The login page's server: where it starts, and what Change accepts. */
9 +class ServerDefaultTest {
10 + private val default = BuildConfig.DEFAULT_SERVER
11 +
12 + @Test fun theDefaultIsTheProductionServer() {
13 + assertEquals("https://forum.towerforever.com", default)
14 + assertEquals(default, normalizeServer(default))
15 + }
16 +
17 + @Test fun noRememberedServerStartsOnTheDefault() {
18 + assertEquals(default, initialServer(null, default))
19 + assertEquals(default, initialServer("", default))
20 + assertEquals(default, initialServer("not a server", default))
21 + }
22 +
23 + @Test fun aRememberedServerWins() {
24 + assertEquals("https://films.example", initialServer("https://films.example/", default))
25 + }
26 +
27 + @Test fun anEditIsNormalised() {
28 + assertEquals(ServerEdit.Accepted("https://films.example"), editServer(" films.example/ ", default))
29 + assertEquals(ServerEdit.Accepted("http://10.0.2.2:8000"), editServer("http://10.0.2.2:8000", default))
30 + }
31 +
32 + /** The UI keeps the previous server on a rejection; only the message changes. */
33 + @Test fun anInvalidEditIsRejected() {
34 + for (typed in listOf("", " ", "ftp://films.example", "https://", "https:/films.example", "http:", "http://exa mple")) {
35 + val edit = editServer(typed, default)
36 + assertTrue("$typed should be rejected", edit is ServerEdit.Rejected)
37 + assertTrue((edit as ServerEdit.Rejected).message.contains(default))
38 + }
39 + }
40 +
41 + @Test fun resetIsTheDefault() {
42 + // Reset hands back exactly the default, which is a valid server.
43 + assertEquals(ServerEdit.Accepted(default), editServer(default, default))
44 + }
45 +
46 + @Test fun theHostIsWhatShows() {
47 + assertEquals("forum.towerforever.com", serverHost("https://forum.towerforever.com"))
48 + assertEquals("10.0.2.2:8000", serverHost("http://10.0.2.2:8000"))
49 + assertEquals("nonsense", serverHost("nonsense"))
50 + }
51 +}

Parents: 01e509e