The settings dialog had outgrown a dialog: it scrolled inside itself, covered the session it is about, and had nowhere to put a second tab. It is a screen now, drawn over the session like the file explorer so the session under it stays composed, with the back gesture to leave it. The second tab is ProviderScreen itself -- the same composable the machines tab opens -- so a provider's settings have two ways in and one implementation. Every text field in the app goes through LabelledField: the label is a line above the box rather than a thing floating inside it, the hint says what leaving it blank means, and the padding is one line's worth. Material's outlined field spends the height of three lines to hold one, which on a form of a dozen settings is a screen and a half of scrolling. The value's own text is unchanged -- the framing was what cost. A session also gets a system prompt, which for llama.cpp is one entry in the params table and no app change: it rides in front of the conversation on every request rather than being recorded as the first thing in it, so changing it takes effect on the next message. ParamKind::Prose is new because a paragraph in a one-line box shows six words of itself.
183 lines
7.9 KiB
Kotlin
183 lines
7.9 KiB
Kotlin
package com.example.aiapp
|
|
|
|
import android.Manifest
|
|
import android.content.pm.PackageManager
|
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
|
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.material3.Button
|
|
import androidx.compose.material3.MaterialTheme
|
|
import androidx.compose.material3.OutlinedButton
|
|
import androidx.compose.material3.Text
|
|
import androidx.compose.runtime.Composable
|
|
import androidx.compose.runtime.getValue
|
|
import androidx.compose.runtime.mutableStateOf
|
|
import androidx.compose.runtime.remember
|
|
import androidx.compose.runtime.setValue
|
|
import androidx.compose.ui.Alignment
|
|
import androidx.compose.ui.Modifier
|
|
import androidx.compose.ui.platform.LocalContext
|
|
import androidx.compose.ui.unit.dp
|
|
import androidx.core.net.toUri
|
|
import com.example.wgapplink.EnrollmentScanActivity
|
|
import com.google.zxing.client.android.Intents
|
|
import com.journeyapps.barcodescanner.ScanContract
|
|
import com.journeyapps.barcodescanner.ScanIntentResult
|
|
import com.journeyapps.barcodescanner.ScanOptions
|
|
|
|
/**
|
|
* Server address and token. The normal path is the "Scan QR code" button below, which decodes the
|
|
* server's terminal QR itself; these fields are the fallback for typing the same three values by
|
|
* hand. [onBack] is null on first run, when there is nothing to go back to.
|
|
*/
|
|
@Composable
|
|
fun SettingsScreen(
|
|
existing: ServerSettings?,
|
|
onSaved: (ServerSettings) -> Unit,
|
|
onBack: (() -> Unit)?,
|
|
) {
|
|
val context = LocalContext.current
|
|
var host by remember { mutableStateOf(existing?.host ?: "10.66.0.1") }
|
|
var port by remember { mutableStateOf((existing?.port ?: 8443).toString()) }
|
|
// Never pre-filled from the stored token: this screen shouldn't be a way to read the credential
|
|
// back off the device.
|
|
var token by remember { mutableStateOf("") }
|
|
var error by remember { mutableStateOf<String?>(null) }
|
|
|
|
val scanLauncher =
|
|
rememberLauncherForActivityResult(ScanContract()) { result: ScanIntentResult ->
|
|
// Null contents means the user backed out of the scanner -- not an error.
|
|
val contents = result.contents ?: return@rememberLauncherForActivityResult
|
|
val settings = parseEnrollmentUri(contents.toUri())
|
|
if (settings == null) {
|
|
error = "Not a valid enrollment code"
|
|
} else {
|
|
saveServerSettings(context, settings)
|
|
onSaved(settings)
|
|
}
|
|
}
|
|
|
|
val requestCamera =
|
|
rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
|
|
if (granted) {
|
|
scanLauncher.launch(enrollmentScanOptions())
|
|
} else {
|
|
error =
|
|
"Scanning needs the camera. Grant it in the system settings, " +
|
|
"or type the host, port and token in below."
|
|
}
|
|
}
|
|
|
|
Column(Modifier.fillMaxSize().padding(16.dp)) {
|
|
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) {
|
|
// Leading, where a back arrow points at what it returns to. Trailing it would put a
|
|
// left-pointing arrow at the right edge, aimed across the title it sits beside.
|
|
//
|
|
// Absent rather than disabled on first run, which is the one place this app lets a
|
|
// control come and go: there is no screen underneath yet, so a Back here would not be a
|
|
// capability being withheld but a promise it could not keep.
|
|
if (onBack != null) {
|
|
GlyphButton(BACK_GLYPH, "Back", onBack)
|
|
Spacer(Modifier.width(GLYPH_BUTTON_MARGIN))
|
|
}
|
|
Text(
|
|
"Server",
|
|
style = MaterialTheme.typography.headlineSmall,
|
|
modifier = Modifier.weight(1f),
|
|
)
|
|
}
|
|
Spacer(Modifier.height(8.dp))
|
|
Text(
|
|
"The easy way: run ai-server on the backend and scan the QR it prints. " +
|
|
"Or type the same values here.",
|
|
style = MaterialTheme.typography.bodyMedium,
|
|
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
|
)
|
|
Spacer(Modifier.height(16.dp))
|
|
|
|
OutlinedButton(
|
|
onClick = {
|
|
// Hold the camera permission before the scanner starts. Letting its activity ask on
|
|
// our behalf is what the library does by default, and it opens the camera without
|
|
// waiting for the answer: the first-ever scan comes up as a live preview with
|
|
// "Sorry, the Android camera encountered a problem" over it, and works on the
|
|
// second try.
|
|
if (
|
|
context.checkSelfPermission(Manifest.permission.CAMERA) ==
|
|
PackageManager.PERMISSION_GRANTED
|
|
) {
|
|
scanLauncher.launch(enrollmentScanOptions())
|
|
} else {
|
|
requestCamera.launch(Manifest.permission.CAMERA)
|
|
}
|
|
},
|
|
modifier = Modifier.fillMaxWidth(),
|
|
) {
|
|
Text("Scan QR code")
|
|
}
|
|
Spacer(Modifier.height(16.dp))
|
|
|
|
LabelledField(label = "Host", value = host, onValueChange = { host = it })
|
|
Spacer(Modifier.height(8.dp))
|
|
LabelledField(label = "Port", value = port, onValueChange = { port = it })
|
|
Spacer(Modifier.height(8.dp))
|
|
LabelledField(
|
|
label = if (existing != null) "Token (unchanged if left blank)" else "Token",
|
|
value = token,
|
|
onValueChange = { token = it },
|
|
)
|
|
Spacer(Modifier.height(24.dp))
|
|
|
|
error?.let {
|
|
Text(it, color = MaterialTheme.colorScheme.error)
|
|
Spacer(Modifier.height(8.dp))
|
|
}
|
|
|
|
Button(
|
|
onClick = {
|
|
val portNumber = port.trim().toIntOrNull()
|
|
val effectiveToken = token.trim().ifEmpty { existing?.token ?: "" }
|
|
when {
|
|
host.isBlank() -> error = "Host is required"
|
|
portNumber == null || portNumber !in 1..65535 -> error = "Port must be 1-65535"
|
|
effectiveToken.isEmpty() ->
|
|
error = "Token is required -- scan the server's QR or paste it"
|
|
else -> {
|
|
val settings = ServerSettings(host.trim(), portNumber, effectiveToken)
|
|
saveServerSettings(context, settings)
|
|
onSaved(settings)
|
|
}
|
|
}
|
|
}
|
|
) {
|
|
Text("Save")
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* How the enrollment QR is scanned, in one place because two callers reach it -- straight from the
|
|
* button when the camera permission is already held, and from the permission result when it has
|
|
* just been granted.
|
|
*
|
|
* MIXED_SCAN is the load-bearing part: ZXing otherwise looks only for a dark code on a light
|
|
* ground, and ai-server's QR is block characters in the terminal's foreground colour, so on a dark-
|
|
* themed terminal it comes out as a photographic negative the scanner silently never matches. The
|
|
* mixed decoder alternates normal and inverted frames, costing half the frame rate at each
|
|
* polarity.
|
|
*/
|
|
private fun enrollmentScanOptions(): ScanOptions =
|
|
ScanOptions()
|
|
.setDesiredBarcodeFormats(ScanOptions.QR_CODE)
|
|
.setCaptureActivity(EnrollmentScanActivity::class.java)
|
|
// Follow the phone, not the library's landscape pin.
|
|
.setOrientationLocked(false)
|
|
.addExtra(Intents.Scan.SCAN_TYPE, Intents.Scan.MIXED_SCAN)
|