A review of the shown files and the diff card: the sheet opens the gate, the diff is bounded first
CI / check (push) Canceled after 0s
CI / check (push) Canceled after 0s
The assistant's sheet is a session's window and no activity, so the gate a reply's picture now waits at never opened for it: the session says when it shows and hides, and the lock still keeps the gate shut. An edit's diff is worked out off the main thread, one file after another, and each text is cut to 2000 lines and 200000 characters before anything is split or compared; a change that was cut says so. Lines are compared without the carriage return before their newline, and a change of line endings is said once. Invisible, direction-changing and line-breaking characters in a line or a path are written as their code point. The call's arguments stay on the card when the diffs say nothing. "Open with" takes the type from the copy's first bytes, else from its name's extension, and no longer from the instance's word. Tests: the too-long case now shares lines out of order and can fail; the device file fixtures are checked by field; the gate with the session; the bounds, the line endings, the escaping, the arguments and the type. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
25 files changed
+554
-87
No files matched your search
@@ -11,6 +11,7 @@ import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withTimeoutOrNull
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
|
||||
/**
|
||||
@@ -20,11 +21,13 @@ import java.util.concurrent.atomic.AtomicInteger
|
||||
* is not touched, so what the person started finishes; the next one waits here, and goes on by
|
||||
* itself when the app is in front of an unlocked phone again.
|
||||
*
|
||||
* The process has one ([LembasApp.gate]). It hears of the app's activities starting and stopping
|
||||
* and of the screen going off and the phone being unlocked; [locked] is asked each time.
|
||||
* The process has one ([LembasApp.gate]). It hears of the app's activities starting and stopping,
|
||||
* of the assistant's sheet showing and hiding (a session's window, which is no activity), and of
|
||||
* the screen going off and the phone being unlocked; [locked] is asked each time.
|
||||
*/
|
||||
class InstanceGate(private val locked: () -> Boolean) {
|
||||
private val started = AtomicInteger(0)
|
||||
private val sheet = AtomicBoolean(false)
|
||||
private val state = MutableStateFlow(false)
|
||||
|
||||
/** Whether a request may start now, as it was last looked at. */
|
||||
@@ -40,9 +43,25 @@ class InstanceGate(private val locked: () -> Boolean) {
|
||||
look()
|
||||
}
|
||||
|
||||
/**
|
||||
* The assistant's sheet is up. It is a window of the system's session and no activity, so it
|
||||
* says so itself: the app is in front while it shows, as while an activity is started. Told
|
||||
* once however often the system shows a sheet that is already up. The lock still shuts the
|
||||
* gate: a sheet over the lock screen asks the instance for nothing.
|
||||
*/
|
||||
fun sessionShown() {
|
||||
sheet.set(true)
|
||||
look()
|
||||
}
|
||||
|
||||
fun sessionHidden() {
|
||||
sheet.set(false)
|
||||
look()
|
||||
}
|
||||
|
||||
/** Looks again (the screen went off, the phone was unlocked) and says whether a request may start. */
|
||||
fun look(): Boolean {
|
||||
val now = started.get() > 0 && !locked()
|
||||
val now = (started.get() > 0 || sheet.get()) && !locked()
|
||||
state.value = now
|
||||
return now
|
||||
}
|
||||
|
||||
@@ -2,15 +2,21 @@ package eu.llembas.android.api
|
||||
|
||||
// An edit's diff on its approval card, worked out on the phone from the two texts the card
|
||||
// carries (`contract/event_ask_edit.json`). Lines only: what is drawn is text with a sign, and
|
||||
// every step is bounded, because both texts are a model's and a file's words of any size.
|
||||
// every step is bounded before it is taken, because both texts are a model's and a file's words
|
||||
// of any size.
|
||||
|
||||
/** What a line of a diff is: in both texts, only in the new one, only in the old one, or the mark of lines left out between two changes. */
|
||||
enum class DiffSign { Same, Added, Removed, Gap }
|
||||
|
||||
data class DiffLine(val sign: DiffSign, val text: String)
|
||||
|
||||
/** One file's change as the card draws it: the changed lines with a little around them, and how many were added and removed. */
|
||||
data class DiffView(val lines: List<DiffLine>, val added: Int, val removed: Int) {
|
||||
/**
|
||||
* One file's change as the card draws it: the changed lines with a little around them, and how
|
||||
* many were added and removed. [endings] says a change of line endings (`"LF" to "CRLF"`), which
|
||||
* is said once instead of as every line going and coming. [cut]: the texts were longer than what
|
||||
* a phone compares, and only their beginning was.
|
||||
*/
|
||||
data class DiffView(val lines: List<DiffLine>, val added: Int, val removed: Int, val endings: Pair<String, String>? = null, val cut: Boolean = false) {
|
||||
/** The lines drawn: the first [DIFF_FOLD] until the person opens it, and never more than [DIFF_MOST]. */
|
||||
fun shown(open: Boolean): List<DiffLine> = lines.take(if (open) DIFF_MOST else DIFF_FOLD)
|
||||
|
||||
@@ -19,6 +25,9 @@ data class DiffView(val lines: List<DiffLine>, val added: Int, val removed: Int)
|
||||
|
||||
/** The lines not drawn even when open. */
|
||||
val beyond: Int get() = (lines.size - DIFF_MOST).coerceAtLeast(0)
|
||||
|
||||
/** Whether the block says anything of the change at all; a card whose diffs say nothing keeps its arguments in view. */
|
||||
val says: Boolean get() = lines.isNotEmpty() || endings != null || cut
|
||||
}
|
||||
|
||||
/** How many lines of a file's diff the card shows before it is opened. */
|
||||
@@ -33,11 +42,40 @@ const val DIFF_CONTEXT = 3
|
||||
/** Past this many pairs of lines the two middles are not compared: the old one is shown going and the new one coming. */
|
||||
private const val DIFF_CELLS = 1_000_000L
|
||||
|
||||
/** A text's lines; the newline that ends the last one starts no empty line. */
|
||||
internal fun textLines(text: String): List<String> {
|
||||
if (text.isEmpty()) return emptyList()
|
||||
val lines = text.split('\n')
|
||||
return if (lines.last().isEmpty()) lines.dropLast(1) else lines
|
||||
/** The most lines of one text that are compared, and the most characters: what is past either is left out before any work is done. */
|
||||
const val DIFF_TEXT_LINES = 2_000
|
||||
const val DIFF_TEXT_CHARS = 200_000
|
||||
|
||||
/** A text's lines as they are compared, and whether the text went on past what is. */
|
||||
data class TextLines(val lines: List<String>, val cut: Boolean)
|
||||
|
||||
/**
|
||||
* The lines of [text], at most [mostLines] of them from at most its first [mostChars]
|
||||
* characters: nothing past either bound is read, split or kept. The newline that ends the last
|
||||
* line starts no empty one, and a line is compared without the `\r` before its newline, so a
|
||||
* file whose line endings change is not a file whose every line changes.
|
||||
*/
|
||||
fun textLines(text: String, mostLines: Int = DIFF_TEXT_LINES, mostChars: Int = DIFF_TEXT_CHARS): TextLines {
|
||||
val end = minOf(text.length, mostChars)
|
||||
val lines = ArrayList<String>()
|
||||
var from = 0
|
||||
while (from < end && lines.size < mostLines) {
|
||||
val newline = text.indexOf('\n', from).let { if (it < 0 || it >= end) -1 else it }
|
||||
val to = if (newline < 0) end else newline
|
||||
lines += text.substring(from, if (to > from && text[to - 1] == '\r') to - 1 else to)
|
||||
from = if (newline < 0) end else newline + 1
|
||||
}
|
||||
return TextLines(lines, cut = from < text.length)
|
||||
}
|
||||
|
||||
/** How [text]'s lines end, where it has any: `CRLF` when its first newline follows a `\r`, else `LF`; null for a text of one line. */
|
||||
internal fun lineEnding(text: String): String? {
|
||||
val newline = text.indexOf('\n')
|
||||
return when {
|
||||
newline < 0 -> null
|
||||
newline > 0 && text[newline - 1] == '\r' -> "CRLF"
|
||||
else -> "LF"
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,8 +145,55 @@ fun hunks(lines: List<DiffLine>, context: Int = DIFF_CONTEXT): List<DiffLine> {
|
||||
return out
|
||||
}
|
||||
|
||||
/** This file's change for the card: a file that is made is all added, one that goes is all removed. */
|
||||
/**
|
||||
* This file's change for the card: a file that is made is all added, one that goes is all
|
||||
* removed. Bounded before it starts ([textLines]) and while it runs ([diffLines]); it is still
|
||||
* work for a background thread, never for a frame.
|
||||
*/
|
||||
fun FileDiff.view(): DiffView {
|
||||
val all = diffLines(textLines(oldText.orEmpty()), textLines(if (deleted) "" else newText))
|
||||
return DiffView(hunks(all), added = all.count { it.sign == DiffSign.Added }, removed = all.count { it.sign == DiffSign.Removed })
|
||||
val before = oldText.orEmpty()
|
||||
val after = if (deleted) "" else newText
|
||||
val old = textLines(before)
|
||||
val new = textLines(after)
|
||||
val all = diffLines(old.lines, new.lines)
|
||||
val from = lineEnding(before)
|
||||
val to = lineEnding(after)
|
||||
return DiffView(
|
||||
hunks(all),
|
||||
added = all.count { it.sign == DiffSign.Added },
|
||||
removed = all.count { it.sign == DiffSign.Removed },
|
||||
endings = if (from != null && to != null && from != to) from to to else null,
|
||||
cut = old.cut || new.cut,
|
||||
)
|
||||
}
|
||||
|
||||
/** Whether an approval's item shows its `detail` (the call's arguments): always without diffs, and with them when none of them says anything. [views] is null while they are worked out. */
|
||||
fun showsDetail(item: CardItem, views: List<DiffView>?): Boolean = item.diffs.isEmpty() || (views != null && views.none { it.says })
|
||||
|
||||
// Characters a person cannot see, or that would move or break a line: on a card where somebody
|
||||
// decides, each is written as its code point.
|
||||
private fun unseen(c: Char): Boolean = when (c.code) {
|
||||
in 0x00..0x1F, in 0x7F..0x9F -> true
|
||||
0x00AD, 0x061C, 0x180E, 0xFEFF -> true
|
||||
in 0x200B..0x200F, in 0x2028..0x202E, in 0x2060..0x206F -> true
|
||||
in 0xFFF9..0xFFFB -> true
|
||||
else -> false
|
||||
}
|
||||
|
||||
/**
|
||||
* [text] with every invisible, direction-changing or line-breaking character written as
|
||||
* `<U+202E>`: a bidi override, a zero-width space, a line separator, a control. A tab is four
|
||||
* spaces. What is drawn reads as what is written.
|
||||
*/
|
||||
fun visibly(text: String): String {
|
||||
if (text.none { it == '\t' || unseen(it) }) return text
|
||||
return buildString(text.length + 16) {
|
||||
for (c in text) {
|
||||
when {
|
||||
c == '\t' -> append(" ")
|
||||
unseen(c) -> append("<U+").append(c.code.toString(16).uppercase().padStart(4, '0')).append('>')
|
||||
else -> append(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -365,3 +365,52 @@ class RangeReader(private val length: Long, private val block: Int = BLOCK, priv
|
||||
const val BLOCK = 512 * 1024
|
||||
}
|
||||
}
|
||||
|
||||
/** How much of a file's beginning says what it is. */
|
||||
const val SNIFF_BYTES = 16
|
||||
|
||||
private fun ByteArray.begins(at: Int, vararg bytes: Int): Boolean =
|
||||
size >= at + bytes.size && bytes.indices.all { (this[at + it].toInt() and 0xFF) == bytes[it] }
|
||||
|
||||
private fun ByteArray.says(at: Int, word: String): Boolean = size >= at + word.length && word.indices.all { this[at + it].toInt() == word[it].code }
|
||||
|
||||
/**
|
||||
* What a file is by its first bytes ([head], [SNIFF_BYTES] of them are enough), for the kinds a
|
||||
* chat hands out: pictures, sound, video, a PDF, a 3D model, the archives. Null for anything
|
||||
* else -- text has no mark, and a guess is worse than none.
|
||||
*/
|
||||
fun sniffType(head: ByteArray): String? = when {
|
||||
head.begins(0, 0x89, 0x50, 0x4E, 0x47) -> "image/png"
|
||||
head.begins(0, 0xFF, 0xD8, 0xFF) -> "image/jpeg"
|
||||
head.says(0, "GIF8") -> "image/gif"
|
||||
head.says(0, "RIFF") && head.says(8, "WEBP") -> "image/webp"
|
||||
head.says(0, "RIFF") && head.says(8, "WAVE") -> "audio/wav"
|
||||
head.says(0, "BM") -> "image/bmp"
|
||||
head.says(0, "%PDF-") -> "application/pdf"
|
||||
head.says(0, "OggS") -> "audio/ogg"
|
||||
head.says(0, "fLaC") -> "audio/flac"
|
||||
head.says(0, "ID3") || head.begins(0, 0xFF, 0xFB) || head.begins(0, 0xFF, 0xF3) -> "audio/mpeg"
|
||||
head.says(4, "ftyp") -> "video/mp4"
|
||||
head.begins(0, 0x1A, 0x45, 0xDF, 0xA3) -> "video/webm"
|
||||
head.says(0, "glTF") -> "model/gltf-binary"
|
||||
head.says(0, "PK\u0003\u0004") -> "application/zip"
|
||||
head.begins(0, 0x1F, 0x8B) -> "application/gzip"
|
||||
head.says(0, "7z") && head.begins(2, 0xBC, 0xAF) -> "application/x-7z-compressed"
|
||||
else -> null
|
||||
}
|
||||
|
||||
/**
|
||||
* The type a copy is offered to another app as ("Open with"): what its bytes say, else what its
|
||||
* name's extension says ([named], the platform's table), else anything. A zip is the one mark
|
||||
* that many kinds of document carry inside, so there a known extension is the better word. The
|
||||
* type the instance said is not asked: it is not the phone's own reading.
|
||||
*/
|
||||
fun openType(head: ByteArray, named: String?): String {
|
||||
val sniffed = sniffType(head)
|
||||
val byName = named?.ifBlank { null }
|
||||
return when {
|
||||
sniffed == null -> byName ?: "*/*"
|
||||
sniffed == "application/zip" && byName != null -> byName
|
||||
else -> sniffed
|
||||
}
|
||||
}
|
||||
@@ -201,6 +201,9 @@ class LembasAssistSession(context: Context) :
|
||||
up = true
|
||||
AssistPresence.shown()
|
||||
}
|
||||
// The sheet is the app in front: what it draws of the account (a reply's picture) may be
|
||||
// asked for, unless the phone is locked. Said each time; the gate keeps it as one.
|
||||
context.lembas.gate.sessionShown()
|
||||
AssistLink.sheetShown(SystemClock.elapsedRealtime())
|
||||
// One sheet, never two: a sheet the wake word opened as a screen of the app, because
|
||||
// this one was slow to come, goes now that this one is here.
|
||||
@@ -230,6 +233,7 @@ class LembasAssistSession(context: Context) :
|
||||
up = false
|
||||
AssistPresence.hidden()
|
||||
}
|
||||
context.lembas.gate.sessionHidden()
|
||||
controller.close()
|
||||
scope.cancel()
|
||||
scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
|
||||
|
||||
@@ -37,6 +37,7 @@ import eu.llembas.android.api.CardAnswer
|
||||
import eu.llembas.android.api.CardItem
|
||||
import eu.llembas.android.api.LembasJson
|
||||
import eu.llembas.android.api.Plan
|
||||
import eu.llembas.android.api.showsDetail
|
||||
import eu.llembas.android.ui.theme.LocalPalette
|
||||
|
||||
/** The sentinel the web card's "Something else" row submits (`interaction.OTHER`). */
|
||||
@@ -73,6 +74,7 @@ private fun Approval(card: Card, onAnswer: (CardAnswer) -> Unit) {
|
||||
var refusing by remember { mutableStateOf(false) }
|
||||
Text(stringResource(R.string.card_approval), style = MaterialTheme.typography.titleMedium)
|
||||
card.items.forEach { item ->
|
||||
val views = diffViews(item.diffs)
|
||||
// A subagent asks, not the chat's own turn: said first, as the page says it.
|
||||
val subagent = item.subagent
|
||||
if (subagent != null) {
|
||||
@@ -100,11 +102,12 @@ private fun Approval(card: Card, onAnswer: (CardAnswer) -> Unit) {
|
||||
if (approvalTexts(listOf(item), edits).isNotEmpty()) {
|
||||
Text(stringResource(R.string.card_edit_runs), style = MaterialTheme.typography.bodySmall, color = palette.inkMuted)
|
||||
}
|
||||
} else if (item.detail.isNotBlank() && item.diffs.isEmpty()) {
|
||||
} else if (item.detail.isNotBlank() && showsDetail(item, views)) {
|
||||
Text(item.detail, fontFamily = FontFamily.Monospace)
|
||||
}
|
||||
// An edit says what it would change: the diff stands where the call's arguments would, as text.
|
||||
if (item.diffs.isNotEmpty()) CardDiffs(item.diffs)
|
||||
// An edit says what it would change: the diff stands where the call's arguments would,
|
||||
// as text. Diffs that say nothing (the same text twice) leave the arguments in view.
|
||||
if (item.diffs.isNotEmpty()) CardDiffs(item.diffs, views)
|
||||
if (item.reason.isNotBlank()) Text(item.reason, style = MaterialTheme.typography.bodySmall, color = palette.inkFaint)
|
||||
}
|
||||
// A corrected command goes with an allow, never with a refusal, and only when it was corrected.
|
||||
|
||||
@@ -199,7 +199,7 @@ fun ChatScreen(
|
||||
|
||||
override fun share(media: GeneratedMedia) = transfers.share(media.raw, media.title, media.mime)
|
||||
|
||||
override fun openWith(media: GeneratedMedia) = transfers.openWith(media.raw, media.title, media.mime)
|
||||
override fun openWith(media: GeneratedMedia) = transfers.openWith(media.raw, media.title)
|
||||
|
||||
override fun cancel(job: MediaJob) = model.cancelJob(job)
|
||||
}
|
||||
|
||||
@@ -21,12 +21,14 @@ import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.key
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.produceState
|
||||
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
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.platform.LocalInspectionMode
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.semantics.clearAndSetSemantics
|
||||
import androidx.compose.ui.semantics.contentDescription
|
||||
@@ -39,10 +41,15 @@ import androidx.compose.ui.unit.dp
|
||||
import eu.llembas.android.R
|
||||
import eu.llembas.android.api.DiffLine
|
||||
import eu.llembas.android.api.DiffSign
|
||||
import eu.llembas.android.api.DiffView
|
||||
import eu.llembas.android.api.FileDiff
|
||||
import eu.llembas.android.api.view
|
||||
import eu.llembas.android.api.visibly
|
||||
import eu.llembas.android.ui.theme.LocalPalette
|
||||
import eu.llembas.android.ui.theme.Palette
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
// An edit's diff on its approval card: per file its path, how many lines come and go, and the
|
||||
// changed lines as text -- a sign before each and the theme's colour behind it, in one width of
|
||||
@@ -54,7 +61,11 @@ internal const val DIFF_LINE_MOST = 400
|
||||
|
||||
private val DiffShape = RoundedCornerShape(12.dp)
|
||||
|
||||
/** A line as the card writes it: its sign, then its text with tabs as spaces and cut at [DIFF_LINE_MOST]. */
|
||||
/**
|
||||
* A line as the card writes it: its sign, then its text cut at [DIFF_LINE_MOST], with whatever
|
||||
* a person could not see in it -- a direction override, a zero-width space, a line separator --
|
||||
* written as its code point ([visibly]).
|
||||
*/
|
||||
internal fun diffRow(line: DiffLine): String {
|
||||
val sign = when (line.sign) {
|
||||
DiffSign.Added -> "+ "
|
||||
@@ -62,8 +73,8 @@ internal fun diffRow(line: DiffLine): String {
|
||||
DiffSign.Same -> " "
|
||||
DiffSign.Gap -> return "⋯"
|
||||
}
|
||||
val text = line.text.replace("\r", "").replace("\t", " ")
|
||||
return sign + if (text.length > DIFF_LINE_MOST) text.take(DIFF_LINE_MOST) + "…" else text
|
||||
val long = line.text.length > DIFF_LINE_MOST
|
||||
return sign + visibly(if (long) line.text.take(DIFF_LINE_MOST) else line.text) + if (long) "…" else ""
|
||||
}
|
||||
|
||||
private fun diffText(lines: List<DiffLine>, palette: Palette): AnnotatedString = buildAnnotatedString {
|
||||
@@ -79,27 +90,48 @@ private fun diffText(lines: List<DiffLine>, palette: Palette): AnnotatedString =
|
||||
}
|
||||
}
|
||||
|
||||
/** The diffs of one item of an approval card, a block per file. */
|
||||
/**
|
||||
* The diffs of an item, worked out: null until they are. Off the main thread -- two texts of a
|
||||
* megabyte are not compared inside a frame -- and one file after another, so fifty files hold
|
||||
* one comparison's memory at a time. A screenshot has no later frame: there it is done at once.
|
||||
*/
|
||||
@Composable
|
||||
fun CardDiffs(diffs: List<FileDiff>, modifier: Modifier = Modifier) {
|
||||
fun diffViews(diffs: List<FileDiff>): List<DiffView>? {
|
||||
if (diffs.isEmpty()) return emptyList()
|
||||
if (LocalInspectionMode.current) return remember(diffs) { diffs.map { it.view() } }
|
||||
val views by produceState<List<DiffView>?>(null, diffs) {
|
||||
value = withContext(Dispatchers.Default) {
|
||||
diffs.map {
|
||||
ensureActive()
|
||||
it.view()
|
||||
}
|
||||
}
|
||||
}
|
||||
return views
|
||||
}
|
||||
|
||||
/** The diffs of one item of an approval card, a block per file; [views] as [diffViews] gave them. */
|
||||
@Composable
|
||||
fun CardDiffs(diffs: List<FileDiff>, views: List<DiffView>?, modifier: Modifier = Modifier) {
|
||||
Column(modifier.fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
diffs.forEachIndexed { at, diff -> key(at, diff.path) { FileDiffBlock(diff) } }
|
||||
diffs.forEachIndexed { at, diff -> key(at, diff.path) { FileDiffBlock(diff, views?.getOrNull(at)) } }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* One file's change. The path gives (it wraps) and the two counts keep their width. The lines
|
||||
* are folded to the first few; "Show all" opens them, up to what a card lays out.
|
||||
* are folded to the first few; "Show all" opens them, up to what a card lays out. Until [view]
|
||||
* is worked out the block is its path alone.
|
||||
*/
|
||||
@Composable
|
||||
private fun FileDiffBlock(diff: FileDiff) {
|
||||
private fun FileDiffBlock(diff: FileDiff, view: DiffView?) {
|
||||
val palette = LocalPalette.current
|
||||
val view = remember(diff) { diff.view() }
|
||||
var open by rememberSaveable(diff.path) { mutableStateOf(false) }
|
||||
val path = remember(diff.path) { visibly(diff.path) }
|
||||
Column(Modifier.fillMaxWidth().clip(DiffShape).background(palette.codeBg).border(1.dp, palette.codeBorder, DiffShape)) {
|
||||
Row(Modifier.fillMaxWidth().padding(horizontal = 12.dp, vertical = 8.dp), verticalAlignment = Alignment.CenterVertically) {
|
||||
Column(Modifier.weight(1f)) {
|
||||
Text(diff.path, style = MaterialTheme.typography.bodySmall, fontFamily = FontFamily.Monospace, color = palette.ink)
|
||||
Text(path, style = MaterialTheme.typography.bodySmall, fontFamily = FontFamily.Monospace, color = palette.ink)
|
||||
val state = when {
|
||||
diff.deleted -> stringResource(R.string.diff_deleted)
|
||||
diff.made -> stringResource(R.string.files_new_file)
|
||||
@@ -107,38 +139,49 @@ private fun FileDiffBlock(diff: FileDiff) {
|
||||
}
|
||||
if (state != null) Text(state, style = MaterialTheme.typography.labelSmall, color = palette.inkMuted)
|
||||
}
|
||||
Spacer(Modifier.width(12.dp))
|
||||
val counts = stringResource(R.string.diff_counts, view.added.toString(), view.removed.toString())
|
||||
Row(Modifier.clearAndSetSemantics { contentDescription = counts }, horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
Text("+${view.added}", style = MaterialTheme.typography.labelMedium, fontFamily = FontFamily.Monospace, color = palette.success)
|
||||
Text("−${view.removed}", style = MaterialTheme.typography.labelMedium, fontFamily = FontFamily.Monospace, color = palette.danger)
|
||||
if (view != null) {
|
||||
Spacer(Modifier.width(12.dp))
|
||||
val counts = stringResource(R.string.diff_counts, view.added.toString(), view.removed.toString())
|
||||
Row(Modifier.clearAndSetSemantics { contentDescription = counts }, horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
Text("+${view.added}", style = MaterialTheme.typography.labelMedium, fontFamily = FontFamily.Monospace, color = palette.success)
|
||||
Text("−${view.removed}", style = MaterialTheme.typography.labelMedium, fontFamily = FontFamily.Monospace, color = palette.danger)
|
||||
}
|
||||
}
|
||||
}
|
||||
val lines = view.shown(open)
|
||||
if (lines.isNotEmpty()) {
|
||||
val text = remember(lines, palette) { diffText(lines, palette) }
|
||||
Box(Modifier.fillMaxWidth().horizontalScroll(rememberScrollState())) {
|
||||
Text(
|
||||
text,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
fontFamily = FontFamily.Monospace,
|
||||
softWrap = false,
|
||||
modifier = Modifier.padding(start = 12.dp, end = 12.dp, bottom = 8.dp),
|
||||
)
|
||||
if (view != null) {
|
||||
// Said once, where every line would otherwise go and come with the same words.
|
||||
view.endings?.let { (from, to) -> DiffNote(stringResource(R.string.diff_endings, from, to)) }
|
||||
val lines = view.shown(open)
|
||||
if (lines.isNotEmpty()) {
|
||||
val text = remember(lines, palette) { diffText(lines, palette) }
|
||||
Box(Modifier.fillMaxWidth().horizontalScroll(rememberScrollState())) {
|
||||
Text(
|
||||
text,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
fontFamily = FontFamily.Monospace,
|
||||
softWrap = false,
|
||||
modifier = Modifier.padding(start = 12.dp, end = 12.dp, bottom = 8.dp),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (open && view.beyond > 0) {
|
||||
Text(
|
||||
stringResource(R.string.diff_more, view.beyond.toString()),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = palette.inkMuted,
|
||||
modifier = Modifier.padding(horizontal = 12.dp),
|
||||
)
|
||||
}
|
||||
if (view.folds) {
|
||||
TextButton(onClick = { open = !open }, modifier = Modifier.heightIn(min = 48.dp).padding(horizontal = 4.dp)) {
|
||||
Text(if (open) stringResource(R.string.diff_show_less) else stringResource(R.string.diff_show_all, view.lines.size.toString()))
|
||||
if (open && view.beyond > 0) DiffNote(stringResource(R.string.diff_more, view.beyond.toString()))
|
||||
if (view.cut) DiffNote(stringResource(R.string.diff_cut))
|
||||
if (view.folds) {
|
||||
TextButton(onClick = { open = !open }, modifier = Modifier.heightIn(min = 48.dp).padding(horizontal = 4.dp)) {
|
||||
Text(if (open) stringResource(R.string.diff_show_less) else stringResource(R.string.diff_show_all, view.lines.size.toString()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** A sentence of the block's own, under its lines. */
|
||||
@Composable
|
||||
private fun DiffNote(text: String) {
|
||||
Text(
|
||||
text,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = LocalPalette.current.inkMuted,
|
||||
modifier = Modifier.padding(start = 12.dp, end = 12.dp, bottom = 8.dp),
|
||||
)
|
||||
}
|
||||
@@ -439,7 +439,7 @@ fun FilesScreen(onBack: () -> Unit, start: String? = null, model: FilesViewModel
|
||||
model.clearSelection()
|
||||
}
|
||||
|
||||
override fun openWith(entry: FileEntry) = transfers.openWith(entry.path, entry.name, entry.mime)
|
||||
override fun openWith(entry: FileEntry) = transfers.openWith(entry.path, entry.name)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -91,13 +91,14 @@ class TransferViewModel(app: Application) : AndroidViewModel(app) {
|
||||
_state.update { it.copy(notice = app.getString(R.string.files_downloaded)) }
|
||||
}
|
||||
|
||||
fun openWith(path: String, name: String, mime: String) = openWith(Raw.Path(path), name, mime)
|
||||
fun openWith(path: String, name: String) = openWith(Raw.Path(path), name)
|
||||
|
||||
fun openWith(what: Raw, name: String, mime: String) = start { api, progress ->
|
||||
/** A copy for another app to open; its type is read from the copy ([Handover.view]), not taken from the instance. */
|
||||
fun openWith(what: Raw, name: String) = start { api, progress ->
|
||||
FileCache.sweepShared(app)
|
||||
turn()
|
||||
val copy = api.copyInto(api.raw(what), FileCache.shared(app, name), progress = progress)
|
||||
_state.update { it.copy(launch = Handover.view(app, copy, mime)) }
|
||||
_state.update { it.copy(launch = Handover.view(app, copy)) }
|
||||
}
|
||||
|
||||
/** One thing that is not named by a path (a generated file by its id), shared as itself. */
|
||||
|
||||
@@ -8,12 +8,15 @@ import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.provider.DocumentsContract
|
||||
import android.provider.OpenableColumns
|
||||
import android.webkit.MimeTypeMap
|
||||
import androidx.activity.result.contract.ActivityResultContract
|
||||
import androidx.core.content.FileProvider
|
||||
import eu.llembas.android.api.ApiClient
|
||||
import eu.llembas.android.api.ApiException
|
||||
import eu.llembas.android.api.FileKinds
|
||||
import eu.llembas.android.api.FileListing
|
||||
import eu.llembas.android.api.SNIFF_BYTES
|
||||
import eu.llembas.android.api.openType
|
||||
import eu.llembas.android.share.SharedSource
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
@@ -185,9 +188,24 @@ suspend fun ApiClient.copyInto(request: okhttp3.Request, file: File, most: Long
|
||||
|
||||
/** Hand [files] (cache copies) to another app: "Open with" for one, Share for one or several. Read-only, for this one grant. */
|
||||
object Handover {
|
||||
fun view(context: Context, file: File, mime: String): Intent {
|
||||
/**
|
||||
* "Open with" for the copy [file]. What it is offered as is read from the copy itself
|
||||
* ([openType]: its first bytes, else its name's extension) -- never the type the instance
|
||||
* said, which for a file an agent showed is one more thing a model had a hand in.
|
||||
*/
|
||||
fun view(context: Context, file: File): Intent {
|
||||
val uri = FileCache.uri(context, file)
|
||||
val intent = Intent(Intent.ACTION_VIEW).setDataAndType(uri, mime.ifBlank { "*/*" }).addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
// A copy that cannot be read again is offered by its name alone.
|
||||
val head = try {
|
||||
file.inputStream().use { stream ->
|
||||
val bytes = ByteArray(SNIFF_BYTES)
|
||||
bytes.copyOf(stream.read(bytes).coerceAtLeast(0))
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
ByteArray(0)
|
||||
}
|
||||
val named = MimeTypeMap.getSingleton().getMimeTypeFromExtension(file.extension.lowercase())
|
||||
val intent = Intent(Intent.ACTION_VIEW).setDataAndType(uri, openType(head, named)).addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
return Intent.createChooser(intent, null)
|
||||
}
|
||||
|
||||
|
||||
@@ -182,7 +182,7 @@ fun TreeFileViewer(path: String, onClose: () -> Unit, startEditing: Boolean = fa
|
||||
object : FileActions {
|
||||
override fun download() = saveTo.launch(CreateFile.Ask(name, file?.mime.orEmpty()))
|
||||
|
||||
override fun openWith() = transfers.openWith(path, name, file?.mime.orEmpty())
|
||||
override fun openWith() = transfers.openWith(path, name)
|
||||
|
||||
override fun share() = transfers.share(listOf(FileEntry(name = name, path = path, mime = file?.mime.orEmpty())))
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user