Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/src/main/java/com/anindra/messages/data/Repository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4409,9 +4409,9 @@ class Repository(private val context: Context) {
/** Re-resolves display names from ContactsContract for all conversations. */
fun refreshContactNames() {
// A photo changed while the app was closed raises no ContentObserver
// event, so a resume expires the stored URIs; the thumbnails stay on
// disk and an unchanged photo costs only the lookup.
ContactPhotoStore.expire(context)
// event, so a resume re-resolves every URI. The old entries are kept
// until the fresh batch lands: expiring them first made every avatar
// fall back to its initial for as long as the batch took.
Thread {
try {
val ids = mutableListOf<Long>()
Expand Down
219 changes: 88 additions & 131 deletions app/src/main/java/com/anindra/messages/ui/AttachTray.kt
Original file line number Diff line number Diff line change
@@ -1,230 +1,187 @@
package com.anindra.messages.ui

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import com.anindra.messages.ui.theme.LocalReduceMotion
import com.anindra.messages.ui.theme.Motion
import com.anindra.messages.ui.theme.motionSpring
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.anindra.messages.ui.theme.LocalReduceMotion
import com.anindra.messages.ui.theme.inputPill
import com.anindra.messages.ui.theme.motionSpring

/** One action the tray offers, beside the camera tile. */
/** One action the tray offers. */
internal data class AttachAction(
val label: String,
val icon: ImageVector,
val onClick: () -> Unit
)

/**
* Tile geometry of the attachment tray, measured from the approved design.
*
* The camera column is [REFERENCE_CAMERA_DP] wide against a
* [REFERENCE_TILE_DP] square tile, and the columns plus their [GAP_DP] gaps
* fill the content width, so a narrower screen shrinks the grid instead of
* spilling off it.
* A 3x2 grid of rounded tiles, each with its icon above its label, in a panel
* that takes the keyboard's place. Six actions fill the grid exactly — no
* oversized camera column and no tile left alone on a final row.
*/
internal data class AttachTraySizes(
val cameraWidth: Dp,
val tile: Dp,
val tileHeight: Dp,
val gap: Dp,
val pagePad: Dp,
val verticalPad: Dp
) {
/** The camera tile spans both rows plus the gap between them. */
val cameraHeight: Dp get() = tileHeight * 2 + gap

/** What the tray adds to the chat's height while it is open. */
val height: Dp get() = cameraHeight + verticalPad * 2
}
val verticalPad: Dp,
val rowHeight: Dp,
val height: Dp
)

internal object AttachTrayMetrics {

const val COLUMNS = 3
const val GAP_DP = 8f
const val PAGE_PAD_DP = 8f
const val VERTICAL_PAD_DP = 8f
const val REFERENCE_CAMERA_DP = 132f
const val REFERENCE_TILE_DP = 112f

/** Columns of tiles beside the camera tile. */
const val COLUMNS = 2
const val PAGE_PAD_DP = 16f
const val VERTICAL_PAD_DP = 12f
const val ROWS = 2

/** A tile is slightly wider than it is tall, which keeps the tray short. */
/** A tile is a little wider than it is tall, keeping the tray short. */
const val TILE_ASPECT = 0.9f

/** Fraction of a tile's height the icon sits above its centre. */
const val ICON_LIFT = 0.07f

/** The label's distance from the tile's bottom edge, as a fraction of it. */
const val LABEL_INSET_FRACTION = 0.08f
// Material 3: a 24dp icon in a large (16dp) cornered card.
const val ICON_DP = 24f
const val CORNER_DP = 16f

/** Icon size and corner radius, as fractions of the tile they sit in. */
const val ICON_FRACTION = 0.24f
const val RADIUS_FRACTION = 0.107f

private const val UNITS = REFERENCE_CAMERA_DP + REFERENCE_TILE_DP * COLUMNS

/**
* Tiles stop growing past this, measured as height over width. Without a
* cap a tablet's keyboard would turn the tray into a wall of portraits.
*/
/** Tiles stop growing past this, so a tablet keyboard is not a wall of cards. */
const val MAX_TILE_ASPECT = 1.35f

/** Content width is the tray's width after [PAGE_PAD_DP] on each side. */
fun sizes(contentWidthDp: Float, targetHeightDp: Float? = null): AttachTraySizes {
val scalable = (contentWidthDp - GAP_DP * 2).coerceAtLeast(1f)
val scale = scalable / UNITS
val tileWidth = REFERENCE_TILE_DP * scale
val base = AttachTraySizes(
cameraWidth = (REFERENCE_CAMERA_DP * scale).dp,
tile = tileWidth.dp,
tileHeight = (tileWidth * TILE_ASPECT).dp,
fun sizes(
contentWidthDp: Float,
targetHeightDp: Float? = null,
rows: Int = ROWS
): AttachTraySizes {
val column = (contentWidthDp - GAP_DP * (COLUMNS - 1)) / COLUMNS
val tile = column.coerceAtLeast(1f)
var tileHeight = tile * TILE_ASPECT
val natural = VERTICAL_PAD_DP * 2 + tileHeight * rows + GAP_DP * (rows - 1)
// The tray replaces the keyboard, so a taller slot grows the tiles to
// fill it. The cap keeps a tablet keyboard from turning them into
// portraits; the panel still fills the slot below them.
val target = targetHeightDp ?: natural
if (target > natural) {
val roomForRow = (target - VERTICAL_PAD_DP * 2 - GAP_DP * (rows - 1)) / rows
tileHeight = roomForRow.coerceAtMost(tile * MAX_TILE_ASPECT)
}
return AttachTraySizes(
tile = tile.dp,
tileHeight = tileHeight.dp,
gap = GAP_DP.dp,
pagePad = PAGE_PAD_DP.dp,
verticalPad = VERTICAL_PAD_DP.dp
verticalPad = VERTICAL_PAD_DP.dp,
rowHeight = tileHeight.dp,
height = target.coerceAtLeast(
VERTICAL_PAD_DP * 2 + tileHeight * rows + GAP_DP * (rows - 1)
).dp
)
// The keyboard's slot is what the tray replaces, so it is sized to it:
// the two rows grow to fill it and the composer never moves. A slot
// shorter than the grid's natural height leaves the tiles alone rather
// than squashing them.
val target = targetHeightDp ?: return base
if (target <= base.height.value) return base
val roomForRow = (target - base.verticalPad.value * 2 - base.gap.value) / 2f
val tileHeight = roomForRow.coerceAtMost(base.tile.value * MAX_TILE_ASPECT)
return base.copy(tileHeight = tileHeight.dp)
}
}

/**
* The attachment tray that takes the keyboard's place.
*
* It is not a sheet: it sits at the bottom of the chat's own column, so the
* conversation above it stays on screen and undimmed while it is open, and the
* camera tile spans both rows of the grid beside it.
*/
/** A grid of tiles in the chat's own column, with the conversation still on screen above. */
@Composable
internal fun AttachTray(
onCamera: () -> Unit,
cameraLabel: String,
cameraIcon: ImageVector,
actions: List<AttachAction>,
modifier: Modifier = Modifier,
/** The height the keyboard last had, so the tray takes exactly its slot. */
keyboardHeight: Dp? = null
) {
val colors = MaterialTheme.colorScheme
// One accent per tile, cycled from the scheme's three so a dynamic palette
// keeps them related instead of turning the tray into a paint chart.
val accents = listOf(colors.primary, colors.tertiary, colors.secondary)
val tiles = listOf(AttachAction(cameraLabel, cameraIcon, onCamera)) + actions
Column(
modifier
.fillMaxWidth()
// The page's own colour, which is what the composer already sits
// on: painting a container role here made the tray a visible band
// under the chat in the dark scheme.
.background(colors.background)
// The page's own colour: a container role made the tray a visible
// band under the chat in the dark scheme.
.background(colors.inputPill)
) {
BoxWithConstraints(
Modifier
.fillMaxWidth()
.padding(horizontal = AttachTrayMetrics.PAGE_PAD_DP.dp)
) {
val sizes = AttachTrayMetrics.sizes(maxWidth.value, keyboardHeight?.value)
android.util.Log.d(
"AttachTrayHeight",
"content=${maxWidth.value} target=${keyboardHeight?.value} " +
"tray=${sizes.height.value} tile=${sizes.tileHeight.value}"
val sizes = AttachTrayMetrics.sizes(
maxWidth.value,
keyboardHeight?.value,
rows = (tiles.size + AttachTrayMetrics.COLUMNS - 1) / AttachTrayMetrics.COLUMNS
)
Row(
Modifier
.fillMaxWidth()
.padding(vertical = sizes.verticalPad),
horizontalArrangement = Arrangement.spacedBy(sizes.gap)
) {
AttachTile(
label = cameraLabel,
icon = cameraIcon,
width = sizes.cameraWidth,
height = sizes.cameraHeight,
onClick = onCamera,
iconTint = accents[0]
)
Column(verticalArrangement = Arrangement.spacedBy(sizes.gap)) {
actions.chunked(AttachTrayMetrics.COLUMNS)
.forEachIndexed { rowIndex, row ->
Row(horizontalArrangement = Arrangement.spacedBy(sizes.gap)) {
row.forEachIndexed { column, action ->
val index = rowIndex * AttachTrayMetrics.COLUMNS + column
AttachTile(
label = action.label,
icon = action.icon,
width = sizes.tile,
height = sizes.tileHeight,
onClick = action.onClick,
iconTint = accents[(index + 1) % accents.size]
)
}
Column(Modifier.height(sizes.height)) {
tiles.chunked(AttachTrayMetrics.COLUMNS)
.forEachIndexed { rowIndex, row ->
Row(
Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(sizes.gap)
) {
row.forEach { action ->
AttachTile(
label = action.label,
icon = action.icon,
width = sizes.tile,
height = sizes.tileHeight,
onClick = action.onClick
)
}
}
}
if (rowIndex < tiles.size / AttachTrayMetrics.COLUMNS - 1) {
Spacer(Modifier.height(sizes.gap))
}
}
}
}
}
}

/** One tile: a Material surface with its icon above the label. */
/** One tile: a rounded surface with its icon above its label. */
@Composable
internal fun AttachTile(
label: String,
icon: ImageVector,
width: Dp,
height: Dp,
onClick: () -> Unit,
modifier: Modifier = Modifier,
iconTint: Color = Color.Unspecified
modifier: Modifier = Modifier
) {
val colors = MaterialTheme.colorScheme
val reduceMotion = LocalReduceMotion.current
// A pressed tile gives a little: the ripple alone is easy to miss on a
// surface this size, and this is what makes a tap feel answered.
val interaction = remember { MutableInteractionSource() }
val pressed by interaction.collectIsPressedAsState()
val scale by animateFloatAsState(
targetValue = if (pressed) 0.96f else 1f,
animationSpec = motionSpring(reduceMotion, stiffness = androidx.compose.animation.core.Spring.StiffnessMediumLow),
animationSpec = motionSpring(
reduceMotion,
stiffness = androidx.compose.animation.core.Spring.StiffnessMediumLow
),
label = "attachTilePress"
)

Surface(
onClick = onClick,
modifier = modifier
Expand All @@ -233,18 +190,18 @@ internal fun AttachTile(
scaleX = scale
scaleY = scale
},
shape = RoundedCornerShape(width * AttachTrayMetrics.RADIUS_FRACTION),
color = colors.surfaceContainerHighest,
shape = RoundedCornerShape(AttachTrayMetrics.CORNER_DP.dp),
color = colors.inputPill,
contentColor = colors.onSurface,
interactionSource = interaction
) {
Box(contentAlignment = Alignment.Center) {
Icon(
icon,
contentDescription = null,
tint = if (iconTint == Color.Unspecified) colors.onSurface else iconTint,
tint = colors.onSurfaceVariant,
modifier = Modifier
.size(minOf(width, height) * AttachTrayMetrics.ICON_FRACTION)
.size(AttachTrayMetrics.ICON_DP.dp)
.offset(y = -(height * AttachTrayMetrics.ICON_LIFT))
)
Text(
Expand Down
Loading