A Files screen (the folder button beside Settings) lists GET /api/v2/files: folders and mounts open as the next listing, back goes up one directory before it leaves, and a refusal keeps the directory already shown with the server's sentence above it. A file opens in the existing viewer through GET /api/v2/files/content, under the viewer's rules: Markdown through Markwon, code and text as numbered lines, HTML as its source. A withheld file (not text, over 1 MB) says why in the server's words and offers the web UI's /files/view. The viewer is now one private Viewer with two doors: FileViewer(chatId, show) for a show card, TreeFileViewer(path) for the tree. A 415 from the show route is the server's "not a text file". Four strings, all already in the server's catalogues, so translated in all eight languages. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
15 files changed
+305
-16
No files matched your search
@@ -28,6 +28,7 @@ import eu.llembas.android.auth.Prefs
|
||||
import eu.llembas.android.push.Notifications
|
||||
import eu.llembas.android.ui.chat.ChatScreen
|
||||
import eu.llembas.android.ui.chats.ChatsScreen
|
||||
import eu.llembas.android.ui.files.FilesScreen
|
||||
import eu.llembas.android.ui.settings.SettingsScreen
|
||||
import eu.llembas.android.ui.signin.SignInScreen
|
||||
import eu.llembas.android.ui.theme.LembasTheme
|
||||
@@ -89,6 +90,7 @@ class MainActivity : ComponentActivity() {
|
||||
onOpen = { nav.navigate("chat/$it") },
|
||||
onNew = { nav.navigate("new") },
|
||||
onSettings = { nav.navigate("settings") },
|
||||
onFiles = { nav.navigate("files") },
|
||||
)
|
||||
}
|
||||
composable("new") {
|
||||
@@ -103,6 +105,7 @@ class MainActivity : ComponentActivity() {
|
||||
composable("chat/{id}") { entry ->
|
||||
ChatScreen(chatId = entry.arguments?.getString("id"), onBack = { nav.popBackStack() })
|
||||
}
|
||||
composable("files") { FilesScreen(onBack = { nav.popBackStack() }) }
|
||||
composable("settings") {
|
||||
SettingsScreen(onBack = { nav.popBackStack() }, onAddInstance = { nav.navigate("add") })
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.filled.Chat
|
||||
import androidx.compose.material.icons.filled.Add
|
||||
import androidx.compose.material.icons.filled.Folder
|
||||
import androidx.compose.material.icons.filled.Settings
|
||||
import androidx.compose.material.icons.filled.Terminal
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
@@ -132,7 +133,7 @@ class ChatsViewModel(app: Application) : AndroidViewModel(app) {
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun ChatsScreen(onOpen: (String) -> Unit, onNew: () -> Unit, onSettings: () -> Unit, model: ChatsViewModel = viewModel()) {
|
||||
fun ChatsScreen(onOpen: (String) -> Unit, onNew: () -> Unit, onSettings: () -> Unit, onFiles: () -> Unit, model: ChatsViewModel = viewModel()) {
|
||||
val state by model.state.collectAsStateWithLifecycle()
|
||||
var tab by rememberSaveable { mutableIntStateOf(0) }
|
||||
val kind = if (tab == 0) "chat" else "agent"
|
||||
@@ -143,6 +144,9 @@ fun ChatsScreen(onOpen: (String) -> Unit, onNew: () -> Unit, onSettings: () -> U
|
||||
TopAppBar(
|
||||
title = { Text(stringResource(R.string.app_name)) },
|
||||
actions = {
|
||||
IconButton(onClick = onFiles) {
|
||||
Icon(Icons.Filled.Folder, contentDescription = stringResource(R.string.files))
|
||||
}
|
||||
IconButton(onClick = onSettings) {
|
||||
Icon(Icons.Filled.Settings, contentDescription = stringResource(R.string.settings))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,195 @@
|
||||
@file:OptIn(androidx.compose.material3.ExperimentalMaterial3Api::class)
|
||||
|
||||
package eu.llembas.android.ui.files
|
||||
|
||||
import android.app.Application
|
||||
import android.text.format.Formatter
|
||||
import androidx.activity.compose.BackHandler
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.filled.ArrowBack
|
||||
import androidx.compose.material.icons.filled.Description
|
||||
import androidx.compose.material.icons.filled.Folder
|
||||
import androidx.compose.material.icons.filled.FolderShared
|
||||
import androidx.compose.material3.HorizontalDivider
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.ListItem
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.material3.pulltorefresh.PullToRefreshBox
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
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.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import eu.llembas.android.R
|
||||
import eu.llembas.android.api.FileEntry
|
||||
import eu.llembas.android.lembas
|
||||
import eu.llembas.android.ui.theme.LocalPalette
|
||||
import eu.llembas.android.ui.viewer.TreeFileViewer
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
/** One directory of the person's tree, and whether it is on its way. */
|
||||
data class FilesState(
|
||||
val path: String = "/",
|
||||
val parent: String? = null,
|
||||
val entries: List<FileEntry> = emptyList(),
|
||||
val loading: Boolean = false,
|
||||
val error: String? = null,
|
||||
)
|
||||
|
||||
/**
|
||||
* The person's files, read-only: `GET /api/v2/files` as the explorer's listing. The tree is read
|
||||
* as the person, with no chat -- their own access, the same the web UI's explorer draws by.
|
||||
*/
|
||||
class FilesViewModel(app: Application) : AndroidViewModel(app) {
|
||||
private val _state = MutableStateFlow(FilesState(loading = true))
|
||||
val state = _state.asStateFlow()
|
||||
private var loading: Job? = null
|
||||
|
||||
/**
|
||||
* Opens a directory. A refusal (a path no longer readable, the server unreachable) keeps the
|
||||
* directory already shown and says why above it, so the way back is never lost.
|
||||
*/
|
||||
fun open(path: String) {
|
||||
loading?.cancel()
|
||||
loading = viewModelScope.launch {
|
||||
_state.update { it.copy(loading = true, error = null) }
|
||||
try {
|
||||
val api = getApplication<Application>().lembas.instances.client()
|
||||
if (api == null) {
|
||||
_state.update { it.copy(loading = false) }
|
||||
return@launch
|
||||
}
|
||||
val listing = api.files(path)
|
||||
_state.value = FilesState(path = listing.path, parent = listing.parent, entries = listing.entries)
|
||||
} catch (e: Exception) {
|
||||
if (e is kotlinx.coroutines.CancellationException) throw e
|
||||
_state.update { it.copy(loading = false, error = e.message ?: e.javaClass.simpleName) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun reload() = open(_state.value.path)
|
||||
|
||||
fun up(): Boolean {
|
||||
val parent = _state.value.parent ?: return false
|
||||
open(parent)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun FilesScreen(onBack: () -> Unit, model: FilesViewModel = viewModel()) {
|
||||
val state by model.state.collectAsStateWithLifecycle()
|
||||
val palette = LocalPalette.current
|
||||
var viewing by rememberSaveable { mutableStateOf<String?>(null) }
|
||||
LaunchedEffect(Unit) { if (state.entries.isEmpty() && state.error == null) model.open(state.path) }
|
||||
|
||||
// Back goes up one directory first, as a file manager's does; at the top it leaves.
|
||||
BackHandler(enabled = state.parent != null) { model.up() }
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopAppBar(
|
||||
navigationIcon = {
|
||||
IconButton(onClick = { if (!model.up()) onBack() }) {
|
||||
Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = stringResource(R.string.back))
|
||||
}
|
||||
},
|
||||
title = {
|
||||
Column {
|
||||
Text(stringResource(R.string.files), maxLines = 1, overflow = TextOverflow.Ellipsis)
|
||||
Text(
|
||||
if (state.parent == null) stringResource(R.string.files_all) else state.path,
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
fontFamily = if (state.parent == null) null else FontFamily.Monospace,
|
||||
color = palette.inkFaint,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.StartEllipsis,
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
},
|
||||
) { padding ->
|
||||
Column(Modifier.padding(padding).fillMaxSize()) {
|
||||
state.error?.let {
|
||||
Text(it, color = MaterialTheme.colorScheme.error, modifier = Modifier.padding(16.dp))
|
||||
}
|
||||
PullToRefreshBox(isRefreshing = state.loading, onRefresh = { model.reload() }, modifier = Modifier.fillMaxSize()) {
|
||||
if (state.entries.isEmpty() && !state.loading && state.error == null) {
|
||||
Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
|
||||
Text(
|
||||
stringResource(R.string.files_empty),
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
modifier = Modifier.padding(32.dp),
|
||||
)
|
||||
}
|
||||
}
|
||||
LazyColumn(Modifier.fillMaxSize()) {
|
||||
items(state.entries, key = { it.path }) { entry ->
|
||||
EntryRow(entry) { if (entry.isFolder) model.open(entry.path) else viewing = entry.path }
|
||||
HorizontalDivider()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
viewing?.let { path -> TreeFileViewer(path, onClose = { viewing = null }) }
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun EntryRow(entry: FileEntry, onOpen: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
ListItem(
|
||||
modifier = Modifier.fillMaxWidth().clickable(onClick = onOpen),
|
||||
leadingContent = {
|
||||
Icon(
|
||||
when (entry.kind) {
|
||||
"mount" -> Icons.Filled.FolderShared
|
||||
"dir" -> Icons.Filled.Folder
|
||||
else -> Icons.Filled.Description
|
||||
},
|
||||
contentDescription = null,
|
||||
tint = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
},
|
||||
headlineContent = { Text(entry.name.ifBlank { entry.path }, maxLines = 1, overflow = TextOverflow.Ellipsis) },
|
||||
supportingContent = if (entry.isFolder) {
|
||||
null
|
||||
} else {
|
||||
{
|
||||
val line = listOfNotNull(Formatter.formatShortFileSize(context, entry.size), entry.mime.ifBlank { null }).joinToString(" · ")
|
||||
Text(line, maxLines = 1, overflow = TextOverflow.Ellipsis)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -55,6 +55,7 @@ import androidx.compose.ui.unit.sp
|
||||
import androidx.compose.ui.window.Dialog
|
||||
import androidx.compose.ui.window.DialogProperties
|
||||
import eu.llembas.android.R
|
||||
import eu.llembas.android.api.ApiClient
|
||||
import eu.llembas.android.api.ApiException
|
||||
import eu.llembas.android.api.ShowCard
|
||||
import eu.llembas.android.lembas
|
||||
@@ -72,12 +73,16 @@ private const val MARKDOWN_MAX_CHARS = 200_000
|
||||
private sealed interface Loaded {
|
||||
data object Loading : Loaded
|
||||
|
||||
data class Text(val text: String) : Loaded
|
||||
/** [kind]: what the server said the file is, when it is only known once it came. */
|
||||
data class Text(val text: String, val kind: String? = null) : Loaded
|
||||
|
||||
/** [unavailable]: this server has no door for the app to read a shown file through. */
|
||||
/** [unavailable]: this server has no door for the app to read the file through. */
|
||||
data class Failed(val message: String, val unavailable: Boolean = false) : Loaded
|
||||
}
|
||||
|
||||
/** What the viewer's title and body need to know before the text comes. */
|
||||
private data class Viewed(val name: String, val path: String, val line: Int, val kind: String)
|
||||
|
||||
/**
|
||||
* What a `show` card opens: the file, full screen, read-only, as the chat sees it now.
|
||||
*
|
||||
@@ -90,26 +95,64 @@ private sealed interface Loaded {
|
||||
*/
|
||||
@Composable
|
||||
fun FileViewer(chatId: String, show: ShowCard, onClose: () -> Unit) {
|
||||
Viewer(
|
||||
file = Viewed(show.name, show.path, show.line, show.viewKind),
|
||||
key = chatId to show,
|
||||
webUrl = { it.webViewer(chatId, show) },
|
||||
load = { api, _ -> Loaded.Text(api.shownText(chatId, show)) },
|
||||
onClose = onClose,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* A file of the person's tree (the Files screen), in the same viewer and under the same rules:
|
||||
* the person's own files are no more trusted than a model's, since a model may have written
|
||||
* them. A file the server withholds (not text, over 1 MB) says why, and offers the web UI.
|
||||
*/
|
||||
@Composable
|
||||
fun TreeFileViewer(path: String, onClose: () -> Unit) {
|
||||
Viewer(
|
||||
file = Viewed(path.substringAfterLast('/').ifBlank { path }, path, 0, "text"),
|
||||
key = path,
|
||||
webUrl = { it.webFile(path) },
|
||||
load = { api, context ->
|
||||
val file = api.fileContent(path)
|
||||
val withheld = file.withheld
|
||||
when {
|
||||
withheld == null -> Loaded.Text(file.text, file.viewKind)
|
||||
withheld.message.isNotBlank() -> Loaded.Failed(withheld.message)
|
||||
withheld.reason == "too_large" -> Loaded.Failed(context.getString(R.string.viewer_too_large))
|
||||
else -> Loaded.Failed(context.getString(R.string.viewer_not_text))
|
||||
}
|
||||
},
|
||||
onClose = onClose,
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun Viewer(file: Viewed, key: Any, webUrl: (ApiClient) -> String?, load: suspend (ApiClient, Context) -> Loaded, onClose: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
val palette = LocalPalette.current
|
||||
var loaded by remember(chatId, show) { mutableStateOf<Loaded>(Loaded.Loading) }
|
||||
var web by remember(chatId, show) { mutableStateOf<String?>(null) }
|
||||
var loaded by remember(key) { mutableStateOf<Loaded>(Loaded.Loading) }
|
||||
var web by remember(key) { mutableStateOf<String?>(null) }
|
||||
|
||||
LaunchedEffect(chatId, show) {
|
||||
LaunchedEffect(key) {
|
||||
val api = context.lembas.instances.client()
|
||||
if (api == null) {
|
||||
loaded = Loaded.Failed(context.getString(R.string.assist_signed_out))
|
||||
return@LaunchedEffect
|
||||
}
|
||||
web = api.webViewer(chatId, show)
|
||||
web = webUrl(api)
|
||||
loaded = try {
|
||||
Loaded.Text(api.shownText(chatId, show))
|
||||
load(api, context)
|
||||
} catch (e: ApiException) {
|
||||
when {
|
||||
e.status == 413 -> Loaded.Failed(context.getString(R.string.viewer_too_large))
|
||||
|
||||
e.status == 415 -> Loaded.Failed(e.message?.ifBlank { null } ?: context.getString(R.string.viewer_not_text))
|
||||
|
||||
// No such route at all (FastAPI's own "Not Found", or the wrong method): a
|
||||
// server without the app's door to shown files. Its own 404 has a sentence.
|
||||
// server without the app's door to the file. Its own 404 has a sentence.
|
||||
e.status == 405 || (e.status == 404 && e.message == "Not Found") ->
|
||||
Loaded.Failed(context.getString(R.string.viewer_unavailable), unavailable = true)
|
||||
|
||||
@@ -131,9 +174,9 @@ fun FileViewer(chatId: String, show: ShowCard, onClose: () -> Unit) {
|
||||
},
|
||||
title = {
|
||||
Column {
|
||||
Text(show.name.ifBlank { show.path }, maxLines = 1, overflow = TextOverflow.Ellipsis)
|
||||
Text(file.name.ifBlank { file.path }, maxLines = 1, overflow = TextOverflow.Ellipsis)
|
||||
Text(
|
||||
show.path + if (show.line > 0) ":${show.line}" else "",
|
||||
file.path + if (file.line > 0) ":${file.line}" else "",
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
fontFamily = FontFamily.Monospace,
|
||||
color = palette.inkFaint,
|
||||
@@ -144,7 +187,7 @@ fun FileViewer(chatId: String, show: ShowCard, onClose: () -> Unit) {
|
||||
},
|
||||
actions = {
|
||||
(loaded as? Loaded.Text)?.let { done ->
|
||||
IconButton(onClick = { copy(context, show.name, done.text) }) {
|
||||
IconButton(onClick = { copy(context, file.name, done.text) }) {
|
||||
Icon(Icons.Filled.ContentCopy, contentDescription = stringResource(R.string.viewer_copy))
|
||||
}
|
||||
}
|
||||
@@ -173,7 +216,7 @@ fun FileViewer(chatId: String, show: ShowCard, onClose: () -> Unit) {
|
||||
}
|
||||
}
|
||||
|
||||
is Loaded.Text -> Body(show, now.text)
|
||||
is Loaded.Text -> Body(now.kind ?: file.kind, file.line, now.text)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -181,9 +224,8 @@ fun FileViewer(chatId: String, show: ShowCard, onClose: () -> Unit) {
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun Body(show: ShowCard, text: String) {
|
||||
private fun Body(kind: String, line: Int, text: String) {
|
||||
val palette = LocalPalette.current
|
||||
val kind = show.viewKind
|
||||
if (kind == "markdown" && text.length <= MARKDOWN_MAX_CHARS) {
|
||||
Column(Modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(16.dp)) {
|
||||
MarkdownText(text, modifier = Modifier.fillMaxWidth())
|
||||
@@ -204,7 +246,7 @@ private fun Body(show: ShowCard, text: String) {
|
||||
modifier = Modifier.fillMaxWidth().background(palette.bgSunken).padding(horizontal = 16.dp, vertical = 10.dp),
|
||||
)
|
||||
}
|
||||
Lines(text, show.line)
|
||||
Lines(text, line)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user