Files
LLeMbas-Android/app/src/main/java/eu/llembas/android/ui/Markdown.kt
T
HomerandClaude Opus 5.5 4f09bc41bb
CI / check (push) Canceled after 0s
LLeMbas for Android 0.1.0 (alpha, unreleased): the first commit
A native client for a LLeMbas instance on the server's /api/v2, and the
phone's digital assistant.

- Sign in by device code; the token sealed in the Android Keystore; several
  instances.
- Chats and agent chats: the list, a chat with its reply streaming as JSON
  server-sent events (reconnecting, whole blocks), thinking, tool calls,
  steered messages, the plan, jobs, stop, edit, regenerate, continue, the
  model, effort and Plan mode, attachments and dictation.
- Cards in a sheet, answered as the page's form says it; first answer wins.
- Notifications through UnifiedPush, no Google services; an approval can be
  answered from its notification.
- The digital assistant: a VoiceInteractionService with its session (a sheet
  over the current app, "Include screen" off by default), a recognition
  service through the instance's speech-to-text, the lock-screen path that
  asks to unlock before sending, a Quick Settings tile and an Ask shortcut.
- Share to LLeMbas; Moria and Shire from the server's tokens; nine
  languages; a self-updater in the `full` flavour, none in `fdroid`.

Generated and pinned from the server, each checked in CI: the theme
(tokens.lock.json), the contract (contract/lock.json, server 2124ba9), the
strings and the icons. Built and tested only on GitHub Actions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 13:44:30 +00:00

80 lines
3.2 KiB
Kotlin

package eu.llembas.android.ui
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.text.method.LinkMovementMethod
import android.util.TypedValue
import android.widget.TextView
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.viewinterop.AndroidView
import eu.llembas.android.ui.theme.LocalPalette
import io.noties.markwon.AbstractMarkwonPlugin
import io.noties.markwon.Markwon
import io.noties.markwon.MarkwonConfiguration
import io.noties.markwon.core.MarkwonTheme
import io.noties.markwon.ext.strikethrough.StrikethroughPlugin
import io.noties.markwon.ext.tables.TablePlugin
/** Schemes a link from a model may open. Anything else -- `javascript:`, `intent:`, `file:` -- is inert. */
private val OPENABLE = setOf("http", "https", "mailto")
/**
* Markdown from a model, drawn natively. Model output is untrusted here exactly as on the web
* page, so the rules are the server's: no images are loaded at all (an image URL can carry what
* the model read out of a page to somebody else's server), raw HTML is shown as text (there is
* no HTML plugin), and a link opens outside the app, in the browser, and only for the schemes
* above.
*/
fun markwon(context: Context, codeBg: Int, link: Int): Markwon = Markwon.builder(context)
.usePlugin(StrikethroughPlugin.create())
.usePlugin(TablePlugin.create(context))
.usePlugin(
object : AbstractMarkwonPlugin() {
override fun configureTheme(builder: MarkwonTheme.Builder) {
builder.codeBackgroundColor(codeBg).codeBlockBackgroundColor(codeBg).linkColor(link)
}
override fun configureConfiguration(builder: MarkwonConfiguration.Builder) {
builder.linkResolver { view, url ->
val uri = Uri.parse(url)
if (uri.scheme?.lowercase() in OPENABLE) {
try {
view.context.startActivity(Intent(Intent.ACTION_VIEW, uri).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
} catch (_: ActivityNotFoundException) {
}
}
}
}
},
)
.build()
@Composable
fun MarkdownText(markdown: String, modifier: Modifier = Modifier, color: Color? = null) {
val context = LocalContext.current
val palette = LocalPalette.current
val ink = (color ?: palette.ink).toArgb()
val engine = remember(palette) { markwon(context, palette.codeBg.toArgb(), palette.accent.toArgb()) }
AndroidView(
modifier = modifier,
factory = { ctx ->
TextView(ctx).apply {
setTextSize(TypedValue.COMPLEX_UNIT_SP, 15f)
setTextIsSelectable(true)
movementMethod = LinkMovementMethod.getInstance()
}
},
update = { view ->
view.setTextColor(ink)
engine.setMarkdown(view, markdown)
},
)
}