feat(phase-1/m4.4b): image-decode seam; viewer + list move to commonMain
expect/actual decodeImageBytes (§6.3 image-decode): Android actual is the verbatim BitmapFactory.decodeByteArray + asImageBitmap; JVM actual is skia Image.makeFromEncoded().toComposeImageBitmap() with runCatching to keep the null-on-failure contract BitmapFactory gave the screens. Screens switch produceState<Bitmap?> -> produceState<ImageBitmap?> and the three BitmapFactory call sites (list thumbnail, viewer, editor thumbnail stays with the screen next step). Dispatchers.IO -> the existing M4.3c ioDispatcher seam (identical actuals). Verified: ./gradlew :composeApp:compileKotlinJvm :composeApp:compileDebugKotlinAndroid (BUILD SUCCESSFUL); full gate +:desktopApp:run --self-test + :core:data-jvm:smoke run at the end of the step chain.
This commit is contained in:
1 parent
bdb3d7157d
commit
56949c485f
5 files changed
+58
-14
No files matched your search
@@ -1,169 +0,0 @@
|
||||
package org.incido.compose.ui.editor
|
||||
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.gestures.detectTapGestures
|
||||
import androidx.compose.foundation.gestures.rememberTransformableState
|
||||
import androidx.compose.foundation.gestures.transformable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Close
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.produceState
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.asImageBitmap
|
||||
import androidx.compose.ui.graphics.graphicsLayer
|
||||
import androidx.compose.ui.input.pointer.pointerInput
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.window.Dialog
|
||||
import androidx.compose.ui.window.DialogProperties
|
||||
import android.graphics.BitmapFactory
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.incido.domain.repository.NoteRepository
|
||||
|
||||
/**
|
||||
* v0.3.1.0 — Full-screen image viewer with pinch-to-zoom and pan.
|
||||
* Opened from the editor (or any future surface) when the user taps
|
||||
* an attachment thumbnail. Dismissed by tapping the close button,
|
||||
* tapping the scrim, or pressing back (the platform handles back via
|
||||
* the `Dialog`'s default dismiss behaviour).
|
||||
*
|
||||
* [attachmentId] is either a persisted [NoteAttachment.id] (loaded via
|
||||
* [NoteRepository.getAttachmentBytes]) or a pending temp id (loaded
|
||||
* from [NoteEditorUiState.pendingAttachmentBytes]).
|
||||
*
|
||||
* The [onClose] lambda is invoked on any dismiss path. The screen
|
||||
* itself is rendered inside a [Dialog] so it sits above all
|
||||
* Scaffold content with a guaranteed black backdrop.
|
||||
*/
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun ImageViewerScreen(
|
||||
attachmentId: ViewerAttachmentId,
|
||||
repository: NoteRepository,
|
||||
pendingBytes: ByteArray?,
|
||||
onClose: () -> Unit,
|
||||
) {
|
||||
// Full-screen black backdrop, ignore system bars so we get the
|
||||
// full window.
|
||||
Dialog(
|
||||
onDismissRequest = onClose,
|
||||
properties = DialogProperties(
|
||||
usePlatformDefaultWidth = false,
|
||||
dismissOnBackPress = true,
|
||||
dismissOnClickOutside = true,
|
||||
),
|
||||
) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.background(Color.Black),
|
||||
) {
|
||||
// Pinch-zoom + pan state. Pan & zoom compose nicely via
|
||||
// rememberTransformableState + transformable; no new dep.
|
||||
var scale by remember { mutableStateOf(1f) }
|
||||
var offsetX by remember { mutableStateOf(0f) }
|
||||
var offsetY by remember { mutableStateOf(0f) }
|
||||
val transformState = rememberTransformableState { zoomChange, panChange, _ ->
|
||||
scale = (scale * zoomChange).coerceIn(1f, 6f)
|
||||
offsetX += panChange.x
|
||||
offsetY += panChange.y
|
||||
}
|
||||
|
||||
// Decode the image (or display an error message when the
|
||||
// bytes can't be loaded / decoded).
|
||||
var loadFailed by remember { mutableStateOf(false) }
|
||||
val bitmap by produceState<android.graphics.Bitmap?>(initialValue = null, key1 = attachmentId) {
|
||||
loadFailed = false
|
||||
val bytes: ByteArray? = when (attachmentId) {
|
||||
is ViewerAttachmentId.Persisted -> withContext(Dispatchers.IO) {
|
||||
repository.getAttachmentBytes(attachmentId.id)
|
||||
}
|
||||
is ViewerAttachmentId.Pending -> pendingBytes
|
||||
}
|
||||
if (bytes == null) {
|
||||
loadFailed = true
|
||||
return@produceState
|
||||
}
|
||||
val decoded = withContext(Dispatchers.IO) {
|
||||
BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
|
||||
}
|
||||
value = decoded
|
||||
if (decoded == null) loadFailed = true
|
||||
}
|
||||
|
||||
if (bitmap != null) {
|
||||
Image(
|
||||
bitmap = bitmap!!.asImageBitmap(),
|
||||
contentDescription = null,
|
||||
contentScale = ContentScale.Fit,
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.pointerInput(Unit) {
|
||||
detectTapGestures(
|
||||
onTap = { onClose() },
|
||||
onDoubleTap = {
|
||||
// Double-tap to reset zoom + pan.
|
||||
scale = 1f
|
||||
offsetX = 0f
|
||||
offsetY = 0f
|
||||
},
|
||||
)
|
||||
}
|
||||
.graphicsLayer(
|
||||
scaleX = scale,
|
||||
scaleY = scale,
|
||||
translationX = offsetX,
|
||||
translationY = offsetY,
|
||||
)
|
||||
.transformable(state = transformState, lockRotationOnZoomPan = false),
|
||||
)
|
||||
} else {
|
||||
Column(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
verticalArrangement = Arrangement.Center,
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
) {
|
||||
Text(
|
||||
text = if (loadFailed) "Couldn't load image" else "Loading…",
|
||||
color = Color.White,
|
||||
style = MaterialTheme.typography.bodyLarge,
|
||||
fontWeight = FontWeight.SemiBold,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Close button — top-right, always visible.
|
||||
IconButton(
|
||||
onClick = onClose,
|
||||
modifier = Modifier
|
||||
.align(Alignment.TopEnd)
|
||||
.padding(12.dp),
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Filled.Close,
|
||||
contentDescription = "Close image",
|
||||
tint = Color.White,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,837 +0,0 @@
|
||||
package org.incido.compose.ui.notelist
|
||||
|
||||
import android.graphics.BitmapFactory
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.combinedClickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.ExperimentalLayoutApi
|
||||
import androidx.compose.foundation.layout.FlowRow
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxHeight
|
||||
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.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import kotlinx.coroutines.withContext
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.shape.GenericShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Add
|
||||
import androidx.compose.material.icons.filled.AttachFile
|
||||
import androidx.compose.material.icons.filled.CheckBox
|
||||
import androidx.compose.material.icons.filled.Close
|
||||
import androidx.compose.material.icons.filled.Delete
|
||||
import androidx.compose.material.icons.filled.DeleteForever
|
||||
import androidx.compose.material.icons.filled.Lock
|
||||
import androidx.compose.material.icons.filled.Menu
|
||||
import androidx.compose.material.icons.filled.Mic
|
||||
import androidx.compose.material.icons.filled.MoreVert
|
||||
import androidx.compose.material.icons.filled.PhotoCamera
|
||||
import androidx.compose.material.icons.filled.Restore
|
||||
import androidx.compose.material.icons.filled.Search
|
||||
import androidx.compose.material.icons.filled.ShoppingBasket
|
||||
import androidx.compose.material.icons.filled.Star
|
||||
import androidx.compose.material.icons.outlined.StarOutline
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.CenterAlignedTopAppBar
|
||||
import androidx.compose.material3.DropdownMenu
|
||||
import androidx.compose.material3.DropdownMenuItem
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.FloatingActionButton
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.produceState
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.focus.FocusRequester
|
||||
import androidx.compose.ui.focus.focusRequester
|
||||
import androidx.compose.ui.graphics.Brush
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.asImageBitmap
|
||||
import androidx.compose.ui.graphics.lerp
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import org.incido.compose.di.requireAppGraph
|
||||
import org.incido.compose.ui.theme.toComposeColor
|
||||
import org.incido.compose.util.rememberRelativeTimestamp
|
||||
import org.incido.domain.model.Note
|
||||
import org.incido.domain.model.NoteAttachment
|
||||
import org.incido.domain.model.NoteColor
|
||||
import org.incido.domain.model.NoteFilter
|
||||
import org.incido.domain.model.NoteType
|
||||
import org.incido.domain.repository.NoteRepository
|
||||
|
||||
/**
|
||||
* Right-triangle that fills the top-right corner of its bounds — a
|
||||
* "dog-ear" / folded-page effect used to mark the note's colour.
|
||||
*/
|
||||
private val DogEarShape = GenericShape { size, _ ->
|
||||
// Right-triangle anchored to the top-right corner: vertices at
|
||||
// (0, 0), (size.width, 0), (size.width, size.height).
|
||||
moveTo(0f, 0f)
|
||||
lineTo(size.width, 0f)
|
||||
lineTo(size.width, size.height)
|
||||
close()
|
||||
}
|
||||
|
||||
/**
|
||||
* v0.3.3.5: fixed height for every note card on the list (originally
|
||||
* 168.dp). v0.3.3.6: tuned down to 143.dp after feedback that 168.dp
|
||||
* was too tall — ~15 % taller than the pre-0.3.3.5 wrap-content
|
||||
* typical height (~124 dp for a title + 2-line body preview).
|
||||
* Identical for every card whether or not the note carries hashtags,
|
||||
* a body preview, or a checklist summary.
|
||||
*/
|
||||
private val CARD_HEIGHT_DP = 143.dp
|
||||
|
||||
/**
|
||||
* v0.3.4.0: overlay palette for cards carrying an image thumbnail.
|
||||
* The dark scrim (0.45 alpha black) always renders the card background
|
||||
* dark, so every overlay (title, previews, timestamp, icons) must be
|
||||
* light regardless of the active theme — in Light mode the default
|
||||
* `onSurface` / `onSurfaceVariant` are dark and used to render all
|
||||
* but invisible on dark photos.
|
||||
*/
|
||||
private val ThumbTitleColor = Color.White
|
||||
private val ThumbSecondaryColor = Color.White.copy(alpha = 0.85f)
|
||||
private val ThumbFavActiveColor = Color(0xFFFFD54F) // light amber — "favourited" stays distinguishable on dark images
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun NoteListScreen(
|
||||
filterMode: NoteFilter,
|
||||
onOpenDrawer: () -> Unit,
|
||||
onOpenEditor: (Long?) -> Unit,
|
||||
/** v0.3.1.0 — used to fetch image bytes for card thumbnails. */
|
||||
repository: NoteRepository? = null,
|
||||
viewModel: NoteListViewModel = viewModel(factory = NoteListViewModel.factory(requireAppGraph())),
|
||||
) {
|
||||
val state by viewModel.uiState.collectAsStateWithLifecycle()
|
||||
var localQuery by remember { mutableStateOf("") }
|
||||
var showEmptyTrashDialog by remember { mutableStateOf(false) }
|
||||
val searchFocusRequester = remember { FocusRequester() }
|
||||
|
||||
LaunchedEffect(filterMode) { viewModel.setFilter(filterMode) }
|
||||
|
||||
// Auto-focus the search field when it opens so typing can start
|
||||
// immediately.
|
||||
LaunchedEffect(state.isSearchActive) {
|
||||
if (state.isSearchActive) {
|
||||
runCatching { searchFocusRequester.requestFocus() }
|
||||
}
|
||||
}
|
||||
|
||||
// "Empty Trash" confirmation. The whole trash goes, attachments
|
||||
// included — say so plainly before the one tap that does it.
|
||||
if (showEmptyTrashDialog) {
|
||||
AlertDialog(
|
||||
onDismissRequest = { showEmptyTrashDialog = false },
|
||||
title = { Text("Empty Trash?") },
|
||||
text = {
|
||||
Text(
|
||||
"Every note in the Trash, including attachments, will be " +
|
||||
"permanently deleted. This cannot be undone.",
|
||||
)
|
||||
},
|
||||
confirmButton = {
|
||||
TextButton(
|
||||
onClick = {
|
||||
showEmptyTrashDialog = false
|
||||
viewModel.emptyTrash()
|
||||
},
|
||||
) {
|
||||
Text("Empty Trash", color = MaterialTheme.colorScheme.error)
|
||||
}
|
||||
},
|
||||
dismissButton = {
|
||||
TextButton(onClick = { showEmptyTrashDialog = false }) {
|
||||
Text("Cancel")
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
CenterAlignedTopAppBar(
|
||||
title = { Text(screenTitle(filterMode)) },
|
||||
navigationIcon = {
|
||||
IconButton(onClick = onOpenDrawer) {
|
||||
Icon(Icons.Filled.Menu, contentDescription = "Open menu")
|
||||
}
|
||||
},
|
||||
actions = {
|
||||
// v0.3.1.0: search is now context-aware on Home,
|
||||
// Favorites, Untagged, and Trash. ByTag filters already
|
||||
// narrow by a single tag so the additional text
|
||||
// query would be ambiguous.
|
||||
if (filterMode !is NoteFilter.ByTag) {
|
||||
IconButton(onClick = {
|
||||
if (state.isSearchActive) viewModel.closeSearch() else viewModel.openSearch()
|
||||
localQuery = ""
|
||||
}
|
||||
) {
|
||||
Icon(Icons.Default.Search, contentDescription = "Search notes")
|
||||
}
|
||||
}
|
||||
// Trash view gains an "Empty Trash" button whenever it
|
||||
// holds something; the destructive action is confirmed
|
||||
// through a dialog before anything is touched.
|
||||
if (filterMode == NoteFilter.Trash && state.notes.isNotEmpty()) {
|
||||
IconButton(onClick = { showEmptyTrashDialog = true }) {
|
||||
Icon(
|
||||
Icons.Default.DeleteForever,
|
||||
contentDescription = "Empty trash",
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
},
|
||||
floatingActionButton = {
|
||||
if (filterMode == NoteFilter.Home) {
|
||||
FloatingActionButton(onClick = { onOpenEditor(null) }) {
|
||||
Icon(Icons.Default.Add, contentDescription = "New note")
|
||||
}
|
||||
}
|
||||
},
|
||||
) { padding ->
|
||||
Column(modifier = Modifier.padding(padding)) {
|
||||
if (filterMode !is NoteFilter.ByTag && state.isSearchActive) {
|
||||
OutlinedTextField(
|
||||
value = localQuery,
|
||||
onValueChange = {
|
||||
localQuery = it
|
||||
viewModel.onQueryChange(it)
|
||||
},
|
||||
placeholder = {
|
||||
Text(if (filterMode == NoteFilter.Trash) "Search trash…" else "Search notes…")
|
||||
},
|
||||
leadingIcon = { Icon(Icons.Default.Search, contentDescription = null) },
|
||||
trailingIcon = {
|
||||
if (localQuery.isNotEmpty()) {
|
||||
IconButton(onClick = {
|
||||
localQuery = ""
|
||||
viewModel.onQueryChange("")
|
||||
}) {
|
||||
Icon(Icons.Default.Close, contentDescription = "Clear search")
|
||||
}
|
||||
}
|
||||
},
|
||||
singleLine = true,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 16.dp, vertical = 8.dp)
|
||||
.focusRequester(searchFocusRequester),
|
||||
)
|
||||
}
|
||||
|
||||
when {
|
||||
state.isLoading -> Unit
|
||||
state.isEmpty -> EmptyState(filterMode = filterMode)
|
||||
state.noResults -> NoResultsState(filterMode = filterMode)
|
||||
else -> NoteList(
|
||||
notes = state.notes,
|
||||
onClick = { onOpenEditor(it.id) },
|
||||
onDelete = {
|
||||
if (filterMode == NoteFilter.Trash) {
|
||||
viewModel.permanentlyDelete(it)
|
||||
} else {
|
||||
viewModel.softDelete(it)
|
||||
}
|
||||
},
|
||||
onRestore = viewModel::restore,
|
||||
isTrash = filterMode == NoteFilter.Trash,
|
||||
onToggleFavorite = viewModel::toggleFavorite,
|
||||
repository = repository,
|
||||
// v0.3.5.0: ONLY notes whose ciphertext passed the
|
||||
// startup trial-decrypt check earn the badge.
|
||||
verifiedEncryptedIds = state.verifiedEncryptedNoteIds,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun screenTitle(filter: NoteFilter): String = when (filter) {
|
||||
NoteFilter.Home -> "Incido"
|
||||
NoteFilter.Favorites -> "Favorites"
|
||||
NoteFilter.Untagged -> "Untagged"
|
||||
NoteFilter.Drafts -> "Drafts"
|
||||
NoteFilter.Trash -> "Trash"
|
||||
is NoteFilter.ByTag -> "#${filter.hashtag}"
|
||||
is NoteFilter.ByColor -> filter.color.label
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun EmptyState(filterMode: NoteFilter) {
|
||||
val message = when (filterMode) {
|
||||
NoteFilter.Home -> "No notes yet. Tap + to start."
|
||||
NoteFilter.Favorites -> "No favourites yet. Tap ⭐ on a card to add one."
|
||||
NoteFilter.Untagged -> "All notes are tagged. 🎉"
|
||||
NoteFilter.Drafts -> "No drafts yet. Start typing a new note and it lands here."
|
||||
NoteFilter.Trash -> "Trash is empty."
|
||||
is NoteFilter.ByTag -> "No notes tagged #${filterMode.hashtag} yet."
|
||||
is NoteFilter.ByColor -> "No " + filterMode.color.label.lowercase() + " notes yet. Pick a colour in the editor."
|
||||
}
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(24.dp),
|
||||
contentAlignment = Alignment.Center,
|
||||
) {
|
||||
Text(
|
||||
text = message,
|
||||
style = MaterialTheme.typography.bodyLarge,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun NoResultsState(filterMode: NoteFilter) {
|
||||
val message = when (filterMode) {
|
||||
NoteFilter.Home -> "No matching notes."
|
||||
NoteFilter.Drafts -> "No drafts match your search."
|
||||
NoteFilter.Trash -> "No notes in Trash match your search."
|
||||
is NoteFilter.ByColor -> "No matching notes of this colour."
|
||||
else -> "Nothing here."
|
||||
}
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(24.dp),
|
||||
contentAlignment = Alignment.Center,
|
||||
) {
|
||||
Text(
|
||||
text = message,
|
||||
style = MaterialTheme.typography.bodyLarge,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun NoteList(
|
||||
notes: List<Note>,
|
||||
onClick: (Note) -> Unit,
|
||||
onDelete: (Long) -> Unit,
|
||||
onRestore: (Long) -> Unit,
|
||||
isTrash: Boolean,
|
||||
onToggleFavorite: (Long, Boolean) -> Unit,
|
||||
/** v0.3.1.0 — threaded through to [NoteCard] for thumbnail loading. */
|
||||
repository: NoteRepository?,
|
||||
/**
|
||||
* v0.3.5.0 — ids of notes that were double-checked (trial GCM
|
||||
* decrypt succeeded) by the startup / post-migration verification
|
||||
* pass. Drives the "Encrypted" badge — nothing else does.
|
||||
*/
|
||||
verifiedEncryptedIds: Set<Long>,
|
||||
) {
|
||||
LazyColumn(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentPadding = PaddingValues(
|
||||
top = 8.dp,
|
||||
bottom = 88.dp, // FAB clearance
|
||||
start = 16.dp,
|
||||
end = 16.dp,
|
||||
),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||||
) {
|
||||
items(items = notes, key = { it.id }) { note ->
|
||||
NoteCard(
|
||||
note = note,
|
||||
onClick = { if (!isTrash) onClick(note) },
|
||||
onDelete = { onDelete(note.id) },
|
||||
onRestore = { onRestore(note.id) },
|
||||
isTrash = isTrash,
|
||||
onToggleFavorite = { onToggleFavorite(note.id, !note.isFavorite) },
|
||||
repository = repository,
|
||||
showEncryptionBadge = note.id in verifiedEncryptedIds,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* v0.3.0.1 card layout:
|
||||
* - 14 dp vertical gradient stripe down the left edge
|
||||
* - Tinted card surface (12 % colour blend over the M3 surface)
|
||||
* - Dog-ear colour accent in the top-right corner (replaces the old
|
||||
* circle chip next to the star)
|
||||
* - Title + body preview + checklist summary + hashtag row +
|
||||
* timestamp
|
||||
* - Hashtag row is hidden entirely when the note has no tags.
|
||||
*
|
||||
* v0.3.3.0:
|
||||
* - The bottom-right delete `IconButton` is gone.
|
||||
* - A long-press anywhere on the card opens a `DropdownMenu` anchored on
|
||||
* the card itself. Active cards offer "Delete"; Trash cards offer
|
||||
* "Restore" and permanent "Delete" without opening the editor.
|
||||
* - Card now uses `Modifier.combinedClickable` for click + long-click.
|
||||
*
|
||||
* v0.3.3.5:
|
||||
* - Cards now have a FIXED height ([CARD_HEIGHT_DP]) so every card
|
||||
* is the same size regardless of whether the note has tags, a body
|
||||
* preview, or a checklist summary. Shipped at 168 dp.
|
||||
* - A `Spacer(weight(1f))` pushes the timestamp row to the bottom of
|
||||
* the fixed-height card so the layout stays visually consistent.
|
||||
* - The left colour stripe now spans the full card height instead of
|
||||
* a hardcoded 110 dp.
|
||||
*
|
||||
* v0.3.3.6:
|
||||
* - [CARD_HEIGHT_DP] reduced 168 dp → 143 dp (~15 % shorter) after
|
||||
* feedback that the v0.3.3.5 cards were too tall; still ~15 %
|
||||
* taller than the pre-0.3.3.5 wrap-content typical.
|
||||
*
|
||||
* v0.3.4.0:
|
||||
* - Attachment indicator: a paperclip icon appears on the right edge
|
||||
* whenever the note carries at least one attachment (image, PDF,
|
||||
* audio…). It sits in the same vertical column as the ⋮ overflow
|
||||
* button (top) and the favourite star (bottom), exactly halfway
|
||||
* between them via `Alignment.CenterEnd` on the fixed-height card.
|
||||
* - Thumbnail readability: when an image thumbnail is present the
|
||||
* scrim guarantees a dark background, so all overlay text/icons
|
||||
* switch to a light palette regardless of theme (fixes invisible
|
||||
* dark-on-dark text in Light mode).
|
||||
*/
|
||||
@OptIn(ExperimentalLayoutApi::class, androidx.compose.foundation.ExperimentalFoundationApi::class)
|
||||
@Composable
|
||||
private fun NoteCard(
|
||||
note: Note,
|
||||
onClick: () -> Unit,
|
||||
onDelete: () -> Unit,
|
||||
onRestore: () -> Unit,
|
||||
isTrash: Boolean,
|
||||
onToggleFavorite: () -> Unit,
|
||||
/** v0.3.1.0 — used to load the first image attachment as a
|
||||
* thumbnail background. Null disables the feature (still works). */
|
||||
repository: NoteRepository?,
|
||||
/**
|
||||
* v0.3.5.0 — true only when this note's ciphertext was verified
|
||||
* decryptable by the startup integrity check (never inferred from
|
||||
* the settings flag alone).
|
||||
*
|
||||
* v0.3.5.1 — renders a small lock icon immediately to the LEFT of
|
||||
* the timestamp in the card's bottom row (vertically centred with
|
||||
* the date/time). Replaces the previous lock-plus-"Encrypted"-label
|
||||
* Row that was centred at the card's bottom edge.
|
||||
*/
|
||||
showEncryptionBadge: Boolean = false,
|
||||
) {
|
||||
// v0.3.8.0: task / shopping lists render marker-free on cards —
|
||||
// header line becomes the title, items show as clean "Name ×qty @$X"
|
||||
// text. The checkbox state lives in the summary row below instead.
|
||||
// v0.4.2.0: voice notes carry a plain prose body (title = first
|
||||
// line), so they split like TEXT — the cardPresentation list parser
|
||||
// would mangle their content.
|
||||
val (title, body) =
|
||||
if (note.type == NoteType.TEXT || note.type == NoteType.VOICE_NOTE)
|
||||
Note.titleAndBody(note.content)
|
||||
else Note.cardPresentation(note.content, note.type)
|
||||
// Drafts with no title introduce themselves by kind — "Shopping
|
||||
// List - (draft)" beats a bare "Untitled" in the Drafts view. The
|
||||
// stored content is untouched; this is display-only, so typing a
|
||||
// real title (or publishing) reverts the card to normal instantly.
|
||||
val displayTitle =
|
||||
if (note.isDraft && title == Note.UNTITLED)
|
||||
"${note.type.displayName} - (draft)"
|
||||
else title
|
||||
val bodyPreview = Note.bodyPreview(body)
|
||||
val checklistSummary = Note.checklistSummary(note.content)
|
||||
val noteColor = note.color
|
||||
val hasColor = noteColor != NoteColor.NONE
|
||||
val firstImage: NoteAttachment? = note.firstImageAttachment
|
||||
|
||||
// v0.3.1.0: load the first image attachment's bytes + decode
|
||||
// a downsampled bitmap for the card background. The produceState
|
||||
// scopes the work to the card's lifetime, so off-screen cards
|
||||
// don't keep their bitmaps alive.
|
||||
val thumbnailBitmap by produceState<android.graphics.Bitmap?>(initialValue = null, key1 = firstImage?.id) {
|
||||
if (firstImage == null || repository == null) {
|
||||
value = null
|
||||
} else {
|
||||
value = withContext(kotlinx.coroutines.Dispatchers.IO) {
|
||||
val bytes = repository.getAttachmentBytes(firstImage.id) ?: return@withContext null
|
||||
BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// v0.3.1.2: when a thumbnail is present the Card's container goes
|
||||
// transparent so the image (rendered INSIDE the card below) shows
|
||||
// through, and the dark scrim provides the contrast for the text
|
||||
// overlay. When there's no image, fall back to the existing
|
||||
// tinted/surface behaviour so plain notes look unchanged.
|
||||
val containerColor = when {
|
||||
thumbnailBitmap != null -> Color.Transparent
|
||||
hasColor -> lerp(MaterialTheme.colorScheme.surface, noteColor.toComposeColor(), 0.12f)
|
||||
else -> MaterialTheme.colorScheme.surface
|
||||
}
|
||||
|
||||
// v0.3.4.0: overlay colours resolved once. With a thumbnail the
|
||||
// scrim makes the background dark → force the light palette; plain
|
||||
// cards keep the theme defaults.
|
||||
val hasThumbnail = thumbnailBitmap != null
|
||||
val titleColor = if (hasThumbnail) ThumbTitleColor else MaterialTheme.colorScheme.onSurface
|
||||
val secondaryColor = if (hasThumbnail) ThumbSecondaryColor else MaterialTheme.colorScheme.onSurfaceVariant
|
||||
val hashtagColor = if (hasThumbnail) ThumbTitleColor else MaterialTheme.colorScheme.primary
|
||||
val favoriteActiveColor = if (hasThumbnail) ThumbFavActiveColor else MaterialTheme.colorScheme.primary
|
||||
|
||||
// v0.3.3.0: per-card menu visibility. Long-press opens; tapping
|
||||
// outside the menu (or selecting an item) closes it.
|
||||
var showMenu by remember { mutableStateOf(false) }
|
||||
|
||||
Card(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
// v0.3.3.5: fixed card height — consistent size for every
|
||||
// card, with or without tags / body preview / checklist.
|
||||
.height(CARD_HEIGHT_DP)
|
||||
.combinedClickable(
|
||||
onClick = onClick,
|
||||
onLongClick = { showMenu = true },
|
||||
),
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = containerColor,
|
||||
// v0.3.4.0: on an image thumbnail the scrim darkens the
|
||||
// background — default the content colour to light so any
|
||||
// inheriting composables stay readable.
|
||||
contentColor = if (hasThumbnail) Color.White else MaterialTheme.colorScheme.onSurface,
|
||||
),
|
||||
) {
|
||||
// Inner Box hosts the (optional) thumbnail + scrim behind the
|
||||
// existing Row of text content, with the dog-ear overlay
|
||||
// sitting on top of everything. The Card itself clips to its
|
||||
// rounded corners, so the thumbnail can't bleed past the
|
||||
// card's rounded shape.
|
||||
Box(modifier = Modifier.fillMaxWidth()) {
|
||||
// v0.3.1.2: thumbnail + dark scrim live INSIDE the card so
|
||||
// they're not hidden behind an opaque container surface
|
||||
// (the v0.3.1.0 / v0.3.1.1 layout had them as siblings of
|
||||
// the Card, which is why the image looked invisible — the
|
||||
// Card's opaque containerColor was drawn on top of them).
|
||||
if (thumbnailBitmap != null) {
|
||||
Image(
|
||||
bitmap = thumbnailBitmap!!.asImageBitmap(),
|
||||
contentDescription = null,
|
||||
contentScale = ContentScale.Crop,
|
||||
modifier = Modifier.matchParentSize(),
|
||||
)
|
||||
// Dark scrim — the "fill color, like an overlay" —
|
||||
// keeps the text readable on bright photos.
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.matchParentSize()
|
||||
.background(Color.Black.copy(alpha = 0.45f)),
|
||||
)
|
||||
}
|
||||
Row(modifier = Modifier.fillMaxWidth()) {
|
||||
if (hasColor) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
// v0.3.3.5: stripe spans the full height of
|
||||
// the now-fixed-height card.
|
||||
.fillMaxHeight()
|
||||
.width(14.dp)
|
||||
.background(
|
||||
Brush.verticalGradient(
|
||||
colors = listOf(
|
||||
noteColor.toComposeColor().copy(alpha = 0.95f),
|
||||
noteColor.toComposeColor().copy(alpha = 0.05f),
|
||||
),
|
||||
)
|
||||
),
|
||||
)
|
||||
}
|
||||
Column(modifier = Modifier.padding(16.dp).fillMaxWidth()) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
Text(
|
||||
text = displayTitle,
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
color = titleColor,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
modifier = Modifier.weight(1f),
|
||||
)
|
||||
}
|
||||
if (bodyPreview.isNotEmpty()) {
|
||||
Spacer(modifier = Modifier.padding(top = 4.dp))
|
||||
Text(
|
||||
text = bodyPreview,
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = secondaryColor,
|
||||
maxLines = 2,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
)
|
||||
}
|
||||
if (checklistSummary.isNotEmpty()) {
|
||||
Spacer(modifier = Modifier.padding(top = 4.dp))
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Icon(
|
||||
// v0.3.8.0: shopping lists carry a basket
|
||||
// on the card instead of the plain check box.
|
||||
imageVector = if (note.type == NoteType.SHOPPING_LIST)
|
||||
Icons.Default.ShoppingBasket
|
||||
else Icons.Default.CheckBox,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(14.dp),
|
||||
tint = secondaryColor,
|
||||
)
|
||||
Spacer(Modifier.size(6.dp))
|
||||
Text(
|
||||
text = checklistSummary,
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = secondaryColor,
|
||||
)
|
||||
}
|
||||
}
|
||||
if (note.type == NoteType.VOICE_NOTE) {
|
||||
// v0.4.2.0: voice notes carry a mic badge so the
|
||||
// kind is recognisable on the card even when the
|
||||
// body is empty (the take itself is an audio
|
||||
// attachment, not visible as a thumbnail).
|
||||
Spacer(modifier = Modifier.padding(top = 4.dp))
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Icon(
|
||||
Icons.Default.Mic,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(14.dp),
|
||||
tint = secondaryColor,
|
||||
)
|
||||
Spacer(Modifier.size(6.dp))
|
||||
Text(
|
||||
text = note.type.displayName,
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = secondaryColor,
|
||||
)
|
||||
}
|
||||
}
|
||||
if (note.hashtags.isNotEmpty()) {
|
||||
Spacer(modifier = Modifier.padding(top = 4.dp))
|
||||
HashtagRow(
|
||||
hashtags = note.hashtags,
|
||||
tagColor = hashtagColor,
|
||||
overflowColor = secondaryColor,
|
||||
)
|
||||
}
|
||||
// v0.3.3.5: flexible spacer pushes the timestamp to
|
||||
// the bottom of the fixed-height card so every card
|
||||
// has a consistent layout.
|
||||
Spacer(modifier = Modifier.weight(1f))
|
||||
// v0.3.3.0: bottom row now only shows the
|
||||
// timestamp — the overflow (long-press) menu holds
|
||||
// the destructive action.
|
||||
//
|
||||
// v0.3.5.1: when the note is verified-encrypted a
|
||||
// 14 dp lock icon sits immediately to the LEFT of
|
||||
// the timestamp in this same row, vertically
|
||||
// centred via the row's Alignment.CenterVertically
|
||||
// — consistent placement on every card.
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
if (showEncryptionBadge) {
|
||||
Icon(
|
||||
Icons.Filled.Lock,
|
||||
contentDescription = "Encrypted",
|
||||
tint = secondaryColor,
|
||||
modifier = Modifier.size(14.dp),
|
||||
)
|
||||
Spacer(Modifier.size(4.dp))
|
||||
}
|
||||
Text(
|
||||
text = rememberRelativeTimestamp(note.updatedAt),
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = secondaryColor,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// v0.3.0.1: dog-ear colour accent overlays the top-right
|
||||
// corner. Now sits INSIDE the card's inner Box so it stacks
|
||||
// on top of the thumbnail + scrim + content correctly.
|
||||
if (hasColor) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.align(Alignment.TopEnd)
|
||||
.size(28.dp)
|
||||
.background(
|
||||
color = noteColor.toComposeColor(),
|
||||
shape = DogEarShape,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
// v0.3.3.0: overflow menu anchored to the card's top-right
|
||||
// corner. Long-press anywhere on the card pops it open.
|
||||
// Designed to host additional entries (Share, etc.) as we
|
||||
// add them.
|
||||
Box(modifier = Modifier.align(Alignment.TopEnd).padding(end = 4.dp, top = 4.dp)) {
|
||||
IconButton(onClick = { showMenu = true }) {
|
||||
Icon(
|
||||
Icons.Filled.MoreVert,
|
||||
contentDescription = "More actions",
|
||||
tint = secondaryColor,
|
||||
)
|
||||
}
|
||||
DropdownMenu(
|
||||
expanded = showMenu,
|
||||
onDismissRequest = { showMenu = false },
|
||||
) {
|
||||
if (isTrash) {
|
||||
DropdownMenuItem(
|
||||
text = { Text("Restore") },
|
||||
leadingIcon = {
|
||||
Icon(
|
||||
Icons.Filled.Restore,
|
||||
contentDescription = null,
|
||||
)
|
||||
},
|
||||
onClick = {
|
||||
showMenu = false
|
||||
onRestore()
|
||||
},
|
||||
)
|
||||
}
|
||||
DropdownMenuItem(
|
||||
text = { Text(if (isTrash) "Delete permanently" else "Delete") },
|
||||
leadingIcon = {
|
||||
Icon(
|
||||
Icons.Filled.Delete,
|
||||
contentDescription = null,
|
||||
tint = MaterialTheme.colorScheme.error,
|
||||
)
|
||||
},
|
||||
onClick = {
|
||||
showMenu = false
|
||||
onDelete()
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// v0.3.5.1: the "Encrypted" indicator moved from a
|
||||
// bottom-centre Row (lock icon + "Encrypted" label) to a
|
||||
// lock icon sitting immediately to the left of the
|
||||
// timestamp inside the card's bottom row, so its position
|
||||
// stays consistent on every card. Rendered ONLY when the
|
||||
// note passed the verification pass (trial GCM decrypt
|
||||
// with the current Keystore key); re-checked on every app
|
||||
// open, so a corrupted or re-keyed row silently drops it
|
||||
// until it verifies again.
|
||||
|
||||
// Hotfix: favorite star moved from the title row to the
|
||||
// bottom-right corner, vertically aligned with the
|
||||
// three-dot overflow button above.
|
||||
if (!isTrash) {
|
||||
Box(modifier = Modifier.align(Alignment.BottomEnd).padding(end = 4.dp, bottom = 4.dp)) {
|
||||
IconButton(onClick = onToggleFavorite) {
|
||||
Icon(
|
||||
imageVector = if (note.isFavorite) Icons.Filled.Star else Icons.Outlined.StarOutline,
|
||||
contentDescription = if (note.isFavorite) "Unfavorite" else "Favorite",
|
||||
tint = if (note.isFavorite) favoriteActiveColor
|
||||
else secondaryColor,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// v0.3.4.0: attachment indicator — a paperclip shown
|
||||
// whenever the note carries at least one attachment (any
|
||||
// type: image, PDF, audio…). Aligned CenterEnd so it sits
|
||||
// in the same vertical column as the ⋮ overflow button
|
||||
// above and the favourite star below, exactly halfway
|
||||
// between them: the card has a fixed height and both
|
||||
// neighbours are anchored symmetrically in the top / bottom
|
||||
// corners (their 24 dp glyphs centred in 48 dp IconButtons
|
||||
// with end = 4.dp padding ⇒ glyph centres at 28 dp from the
|
||||
// right edge; the midpoint of those two centres IS the card
|
||||
// centre). The `end = 16.dp` offset places this bare 24 dp
|
||||
// Icon's glyph centre at the same 28 dp column. Rendered as
|
||||
// a plain Icon (not an IconButton) so it never intercepts
|
||||
// the whole-card tap that opens the editor.
|
||||
if (note.attachments.isNotEmpty()) {
|
||||
Icon(
|
||||
Icons.Filled.AttachFile,
|
||||
contentDescription = "Has attachments",
|
||||
tint = secondaryColor,
|
||||
modifier = Modifier
|
||||
.align(Alignment.CenterEnd)
|
||||
.padding(end = 16.dp)
|
||||
.size(24.dp),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* v0.3.0.1: hashtag list on the card. Shows up to three tags as a
|
||||
* compact FlowRow of label-styled text; if there are more, renders a
|
||||
* "+N" abbreviation indicator. Hidden entirely by the caller when
|
||||
* [hashtags] is empty.
|
||||
*
|
||||
* v0.3.4.0: colours are supplied by the caller — light palette on
|
||||
* image-thumbnail cards, theme defaults elsewhere.
|
||||
*/
|
||||
@OptIn(ExperimentalLayoutApi::class)
|
||||
@Composable
|
||||
private fun HashtagRow(
|
||||
hashtags: List<String>,
|
||||
/** Colour for the visible "#tag" chips. */
|
||||
tagColor: Color,
|
||||
/** Colour for the "+N" overflow indicator. */
|
||||
overflowColor: Color,
|
||||
) {
|
||||
val visible = hashtags.take(3)
|
||||
val overflow = hashtags.size - visible.size
|
||||
FlowRow(
|
||||
horizontalArrangement = Arrangement.spacedBy(6.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(2.dp),
|
||||
) {
|
||||
visible.forEach { tag ->
|
||||
Text(
|
||||
text = "#$tag",
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = tagColor,
|
||||
)
|
||||
}
|
||||
if (overflow > 0) {
|
||||
Text(
|
||||
text = "+$overflow",
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = overflowColor,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.incido.compose.util
|
||||
|
||||
import android.graphics.BitmapFactory
|
||||
import androidx.compose.ui.graphics.ImageBitmap
|
||||
import androidx.compose.ui.graphics.asImageBitmap
|
||||
|
||||
/**
|
||||
* M4.4b Android actual — the verbatim pre-M4 decode
|
||||
* (`BitmapFactory.decodeByteArray` returns null on undecodable bytes,
|
||||
* exactly the contract [decodeImageBytes] keeps).
|
||||
*/
|
||||
actual fun decodeImageBytes(bytes: ByteArray): ImageBitmap? =
|
||||
BitmapFactory.decodeByteArray(bytes, 0, bytes.size)?.asImageBitmap()
|
||||
Reference in new issue
Block a user