commit
Let swipes be set in Settings, and show the account's SSO picture
Swiping left or right can now mark read or unread, star, move to the Bin or do nothing, chosen in Settings and shared between the web and the app. The account's picture comes from the SSO's picture claim (or userinfo) at each sign-in and refresh, and shows in the drawer; Settings can switch it off for the default initial.
diff --git a/email/app/src/main/java/gay/doughmination/mail/data/Api.kt b/email/app/src/main/java/gay/doughmination/mail/data/Api.kt
index 653c89b..66fc25f 100644
--- a/email/app/src/main/java/gay/doughmination/mail/data/Api.kt
+++ b/email/app/src/main/java/gay/doughmination/mail/data/Api.kt
@@ -42,6 +42,8 @@ class Api(val baseUrl: String, private val tokens: TokenStore, private val onSig
val json = Json {
ignoreUnknownKeys = true
encodeDefaults = true
+ // An enum value this build doesn't know (a newer swipe) takes the field's default.
+ coerceInputValues = true
}
private val jsonType = "application/json".toMediaType()
@@ -101,6 +103,7 @@ class Api(val baseUrl: String, private val tokens: TokenStore, private val onSig
suspend fun me(): Me = get("/api/me")
suspend fun unread(): Map<String, Int> = get<Unread>("/api/unread").unread
suspend fun setName(name: String) { call<JsonObject, Name>("PATCH", "/api/me", Name(name)) }
+ suspend fun setPreferences(prefs: Preferences): Preferences = call("PATCH", "/api/me/preferences", prefs)
suspend fun threads(folder: ListFolder, mailbox: String?, cursor: String?): Page<ThreadSummary> {
val q = url("/api/threads").newBuilder().addQueryParameter("folder", folder.path)
diff --git a/email/app/src/main/java/gay/doughmination/mail/data/Models.kt b/email/app/src/main/java/gay/doughmination/mail/data/Models.kt
index 6c671ed..e29e01d 100644
--- a/email/app/src/main/java/gay/doughmination/mail/data/Models.kt
+++ b/email/app/src/main/java/gay/doughmination/mail/data/Models.kt
@@ -1,5 +1,6 @@
package gay.doughmination.mail.data
+import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
// Mirrors email/src/shared/types.ts. Keep the two in step.
@@ -13,7 +14,30 @@ data class Me(
/** Every address this user can send from. */
val addresses: List<String>,
val domains: List<String>,
-)
+ /** The account's picture on the SSO (https), whether or not it's in use. */
+ val picture: String? = null,
+ val usePicture: Boolean = true,
+ val swipeLeft: SwipeAction = SwipeAction.Bin,
+ val swipeRight: SwipeAction = SwipeAction.Read,
+) {
+ val preferences get() = Preferences(usePicture, swipeLeft, swipeRight)
+
+ /** The picture to show for the account, or null for the default (an initial). */
+ val shownPicture get() = picture?.takeIf { usePicture }
+}
+
+/** What swiping a row does. An unknown value (a newer server) reads as None. */
+@Serializable
+enum class SwipeAction(val label: String) {
+ @SerialName("none") None("Nothing"),
+ @SerialName("read") Read("Mark as read or unread"),
+ @SerialName("star") Star("Star or unstar"),
+ @SerialName("bin") Bin("Move to Bin"),
+}
+
+/** Settings that follow the user between the web and the app. */
+@Serializable
+data class Preferences(val usePicture: Boolean, val swipeLeft: SwipeAction, val swipeRight: SwipeAction)
@Serializable
data class Page<T>(val items: List<T>, val nextCursor: String? = null)
diff --git a/email/app/src/main/java/gay/doughmination/mail/ui/AccountPicture.kt b/email/app/src/main/java/gay/doughmination/mail/ui/AccountPicture.kt
new file mode 100644
index 0000000..120c0d3
--- /dev/null
+++ b/email/app/src/main/java/gay/doughmination/mail/ui/AccountPicture.kt
@@ -0,0 +1,72 @@
+package gay.doughmination.mail.ui
+
+import android.graphics.BitmapFactory
+import androidx.compose.foundation.Image
+import androidx.compose.foundation.background
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.size
+import androidx.compose.foundation.shape.CircleShape
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.produceState
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.draw.clip
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.graphics.ImageBitmap
+import androidx.compose.ui.graphics.asImageBitmap
+import androidx.compose.ui.layout.ContentScale
+import androidx.compose.ui.platform.LocalContext
+import androidx.compose.ui.text.font.FontWeight
+import androidx.compose.ui.unit.Dp
+import androidx.compose.ui.unit.sp
+import gay.doughmination.mail.InboxApp
+import gay.doughmination.mail.data.Me
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.withContext
+import okhttp3.OkHttpClient
+import okhttp3.Request
+import java.util.concurrent.ConcurrentHashMap
+
+/**
+ * The signed-in account's picture: the SSO's, when there is one and Settings
+ * says to use it, or the default, their initial on the accent colour. Also the
+ * default while the picture loads, or if it can't be loaded. Same as the web.
+ */
+@Composable
+fun AccountPicture(me: Me?, size: Dp) {
+ val url = me?.shownPicture
+ val http = (LocalContext.current.applicationContext as InboxApp).api.http
+ val picture by produceState(url?.let { loaded[it] }, url) {
+ value = url?.let { loadPicture(http, it) }
+ }
+ val shape = Modifier.size(size).clip(CircleShape)
+ picture?.let {
+ Image(it, null, shape, contentScale = ContentScale.Crop)
+ return
+ }
+ val initial = (me?.name?.ifBlank { null } ?: me?.username ?: "?").firstOrNull { it.isLetterOrDigit() }?.uppercaseChar() ?: '?'
+ Box(shape.background(Accent), contentAlignment = Alignment.Center) {
+ Text(initial.toString(), color = Color.White, fontSize = (size.value * 0.42f).sp, fontWeight = FontWeight.SemiBold)
+ }
+}
+
+/** Pictures already fetched this run, so the drawer doesn't flash its initial each time. */
+private val loaded = ConcurrentHashMap<String, ImageBitmap>()
+
+/** Fetches a picture without the session token (it's the SSO's, not ours), scaled down to avatar size. */
+private suspend fun loadPicture(http: OkHttpClient, url: String): ImageBitmap? = loaded[url] ?: withContext(Dispatchers.IO) {
+ runCatching {
+ http.newCall(Request.Builder().url(url).build()).execute().use { res ->
+ if (!res.isSuccessful) return@use null
+ val bytes = res.body?.bytes() ?: return@use null
+ val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
+ BitmapFactory.decodeByteArray(bytes, 0, bytes.size, bounds)
+ var sample = 1
+ while (minOf(bounds.outWidth, bounds.outHeight) / (sample * 2) >= 256) sample *= 2
+ BitmapFactory.decodeByteArray(bytes, 0, bytes.size, BitmapFactory.Options().apply { inSampleSize = sample })
+ ?.asImageBitmap()
+ }
+ }.getOrNull()?.also { loaded[url] = it }
+}
diff --git a/email/app/src/main/java/gay/doughmination/mail/ui/MailViewModel.kt b/email/app/src/main/java/gay/doughmination/mail/ui/MailViewModel.kt
index 20c965f..49f8c99 100644
--- a/email/app/src/main/java/gay/doughmination/mail/ui/MailViewModel.kt
+++ b/email/app/src/main/java/gay/doughmination/mail/ui/MailViewModel.kt
@@ -18,6 +18,7 @@ import gay.doughmination.mail.data.ListFolder
import gay.doughmination.mail.data.MailThread
import gay.doughmination.mail.data.Me
import gay.doughmination.mail.data.Message
+import gay.doughmination.mail.data.Preferences
import gay.doughmination.mail.data.SendRequest
import gay.doughmination.mail.data.SignedOutException
import gay.doughmination.mail.data.ThreadSummary
@@ -611,6 +612,19 @@ class MailViewModel(application: Application) : AndroidViewModel(application) {
}
}
+ /** Shows the change straight away, and puts it back if the server says no. */
+ fun setPreferences(prefs: Preferences) = viewModelScope.launch {
+ val before = me ?: return@launch
+ me = before.copy(usePicture = prefs.usePicture, swipeLeft = prefs.swipeLeft, swipeRight = prefs.swipeRight)
+ try {
+ val saved = api.setPreferences(prefs)
+ me = me?.copy(usePicture = saved.usePicture, swipeLeft = saved.swipeLeft, swipeRight = saved.swipeRight)
+ } catch (e: Exception) {
+ me = me?.copy(usePicture = before.usePicture, swipeLeft = before.swipeLeft, swipeRight = before.swipeRight)
+ fail(e)
+ }
+ }
+
fun testPush() = viewModelScope.launch {
try {
val n = api.pushTest()
diff --git a/email/app/src/main/java/gay/doughmination/mail/ui/SettingsScreen.kt b/email/app/src/main/java/gay/doughmination/mail/ui/SettingsScreen.kt
index 67fcea6..d96f918 100644
--- a/email/app/src/main/java/gay/doughmination/mail/ui/SettingsScreen.kt
+++ b/email/app/src/main/java/gay/doughmination/mail/ui/SettingsScreen.kt
@@ -29,6 +29,11 @@ import androidx.compose.material.icons.outlined.Notifications
import androidx.compose.material.icons.outlined.PrivacyTip
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
+import androidx.compose.material3.DropdownMenuItem
+import androidx.compose.material3.ExposedDropdownMenuBox
+import androidx.compose.material3.ExposedDropdownMenuDefaults
+import androidx.compose.material3.MenuAnchorType
+import androidx.compose.material3.Switch
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
@@ -42,6 +47,7 @@ import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
@@ -52,6 +58,7 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import gay.doughmination.mail.BuildConfig
import gay.doughmination.mail.Push
+import gay.doughmination.mail.data.SwipeAction
/** Laid out like the web's settings page: one card per topic, under a small pink heading. */
@OptIn(ExperimentalMaterial3Api::class, ExperimentalLayoutApi::class)
@@ -94,6 +101,38 @@ fun SettingsScreen(vm: MailViewModel) {
}
}
+ Section("Profile picture") {
+ val picture = me?.picture
+ Row(verticalAlignment = Alignment.CenterVertically) {
+ AccountPicture(me, 56.dp)
+ Spacer(Modifier.width(16.dp))
+ Column(Modifier.weight(1f)) {
+ Text("Use your account's picture")
+ Note(
+ if (picture != null) "The one on your Doughmination account. Off shows your initial instead."
+ else "Your Doughmination account has no picture, so your initial shows.",
+ )
+ }
+ Spacer(Modifier.width(12.dp))
+ Switch(
+ checked = me?.shownPicture != null,
+ onCheckedChange = { on -> me?.let { vm.setPreferences(it.preferences.copy(usePicture = on)) } },
+ enabled = picture != null,
+ )
+ }
+ }
+
+ Section("Swipe actions") {
+ Note(
+ "What swiping a conversation in the list does, here and on the web. Read and unread only " +
+ "applies to received mail, and in the Bin a swipe right always restores.",
+ )
+ me?.let {
+ SwipePicker("Swipe right", it.swipeRight) { choice -> vm.setPreferences(it.preferences.copy(swipeRight = choice)) }
+ SwipePicker("Swipe left", it.swipeLeft) { choice -> vm.setPreferences(it.preferences.copy(swipeLeft = choice)) }
+ }
+ }
+
Section("Your addresses") {
Note("You receive and can send from each of these.")
for (addr in me?.addresses.orEmpty()) Text("• $addr")
@@ -179,5 +218,27 @@ private fun Section(title: String, icon: ImageVector? = null, content: @Composab
}
}
+/** A dropdown of the swipe actions, as an outlined field. */
+@OptIn(ExperimentalMaterial3Api::class)
+@Composable
+private fun SwipePicker(label: String, selected: SwipeAction, onPick: (SwipeAction) -> Unit) {
+ var open by remember { mutableStateOf(false) }
+ ExposedDropdownMenuBox(expanded = open, onExpandedChange = { open = it }) {
+ OutlinedTextField(
+ value = selected.label,
+ onValueChange = {},
+ readOnly = true,
+ label = { Text(label) },
+ trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(open) },
+ modifier = Modifier.fillMaxWidth().menuAnchor(MenuAnchorType.PrimaryNotEditable),
+ )
+ ExposedDropdownMenu(expanded = open, onDismissRequest = { open = false }) {
+ for (choice in SwipeAction.entries) {
+ DropdownMenuItem(text = { Text(choice.label) }, onClick = { open = false; onPick(choice) })
+ }
+ }
+ }
+}
+
@Composable
private fun Note(text: String) = Text(text, style = MaterialTheme.typography.bodyMedium, color = Muted)
diff --git a/email/app/src/main/java/gay/doughmination/mail/ui/ThreadListScreen.kt b/email/app/src/main/java/gay/doughmination/mail/ui/ThreadListScreen.kt
index 9be58fb..cd09381 100644
--- a/email/app/src/main/java/gay/doughmination/mail/ui/ThreadListScreen.kt
+++ b/email/app/src/main/java/gay/doughmination/mail/ui/ThreadListScreen.kt
@@ -34,7 +34,6 @@ import androidx.compose.material.icons.outlined.Drafts
import androidx.compose.material.icons.outlined.Edit
import androidx.compose.material.icons.outlined.Inbox
import androidx.compose.material.icons.outlined.Language
-import androidx.compose.material.icons.outlined.Mail
import androidx.compose.material.icons.outlined.MarkEmailRead
import androidx.compose.material.icons.outlined.MarkEmailUnread
import androidx.compose.material.icons.outlined.Menu
@@ -94,6 +93,7 @@ import androidx.compose.ui.unit.sp
import androidx.browser.customtabs.CustomTabsIntent
import android.net.Uri
import gay.doughmination.mail.data.ListFolder
+import gay.doughmination.mail.data.SwipeAction
import gay.doughmination.mail.data.ThreadSummary
import kotlinx.coroutines.launch
@@ -229,70 +229,81 @@ private fun Centered(modifier: Modifier = Modifier.fillMaxSize(), content: @Comp
) { content() }
}
+/** One way to swipe a row: what it shows underneath, and what it does. */
+private class Swipe(val label: String, val icon: ImageVector, val color: Color, val dismiss: Boolean, val run: () -> Unit)
+
+/** What a swipe set up in Settings does to this row; null where it can't apply. Same as the web. */
+private fun swipeFor(choice: SwipeAction, vm: MailViewModel, t: ThreadSummary): Swipe? = when (choice) {
+ // Only received mail has a read state.
+ SwipeAction.Read -> if (vm.folder != ListFolder.Inbox && vm.folder != ListFolder.Starred) null else Swipe(
+ if (t.unread > 0) "Read" else "Unread",
+ if (t.unread > 0) Icons.Outlined.MarkEmailRead else Icons.Outlined.MarkEmailUnread,
+ Accent,
+ dismiss = false,
+ ) { vm.toggleRead(t) }
+ SwipeAction.Star -> Swipe(
+ if (t.starred) "Unstar" else "Star",
+ if (t.starred) Icons.Outlined.StarOutline else Icons.Filled.Star,
+ Accent,
+ // Unstarring takes it out of Starred.
+ dismiss = vm.folder == ListFolder.Starred && t.starred,
+ ) { vm.toggleStar(t.id, !t.starred) }
+ SwipeAction.Bin -> Swipe("Bin", Icons.Outlined.Delete, Danger, dismiss = true) { vm.moveToBin(t.id) }
+ SwipeAction.None -> null
+}
+
/**
- * Swipe right to toggle read (or restore, in the Bin); swipe left to bin it.
- * Same as the web app.
+ * Swipes do what Settings says (toggle read and bin it, unless changed). In
+ * the Bin, swiping right restores and left does nothing. Same as the web app.
*/
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun SwipeRow(vm: MailViewModel, t: ThreadSummary, onHold: (ThreadSummary) -> Unit) {
val inBin = vm.folder == ListFolder.Bin
- val canToggleRead = vm.folder == ListFolder.Inbox || vm.folder == ListFolder.Starred
+ val right = if (inBin) Swipe("Restore", Icons.Outlined.RestoreFromTrash, Accent, dismiss = true) { vm.restore(t.id) }
+ else swipeFor(vm.me?.swipeRight ?: SwipeAction.Read, vm, t)
+ val left = if (inBin) null else swipeFor(vm.me?.swipeLeft ?: SwipeAction.Bin, vm, t)
val view = LocalView.current
- // Material can ask more than once for one swipe; a toggle must only run once.
+ // Material can ask more than once for one swipe; an action must only run once.
var firedAt by remember { mutableStateOf(0L) }
val state = rememberSwipeToDismissBoxState(
confirmValueChange = { value ->
val now = System.currentTimeMillis()
if (value != SwipeToDismissBoxValue.Settled && now - firedAt < 600) return@rememberSwipeToDismissBoxState false
if (value != SwipeToDismissBoxValue.Settled) firedAt = now
- when (value) {
- SwipeToDismissBoxValue.StartToEnd -> {
- view.performHapticFeedback(swipeHaptic)
- if (inBin) {
- vm.restore(t.id)
- true
- } else {
- // Not dismissed: the row springs back, showing its new state.
- vm.toggleRead(t)
- false
- }
- }
- SwipeToDismissBoxValue.EndToStart -> {
- view.performHapticFeedback(swipeHaptic)
- vm.moveToBin(t.id)
- true
- }
- SwipeToDismissBoxValue.Settled -> true
- }
+ val swipe = when (value) {
+ SwipeToDismissBoxValue.StartToEnd -> right
+ SwipeToDismissBoxValue.EndToStart -> left
+ SwipeToDismissBoxValue.Settled -> return@rememberSwipeToDismissBoxState true
+ } ?: return@rememberSwipeToDismissBoxState false
+ view.performHapticFeedback(swipeHaptic)
+ swipe.run()
+ // Not dismissed: the row springs back, showing its new state.
+ swipe.dismiss
},
positionalThreshold = { it * 0.35f },
)
SwipeToDismissBox(
state = state,
- enableDismissFromStartToEnd = inBin || canToggleRead,
- enableDismissFromEndToStart = !inBin,
+ enableDismissFromStartToEnd = right != null,
+ enableDismissFromEndToStart = left != null,
backgroundContent = {
val direction = state.dismissDirection
- val (icon, label, color) = when (direction) {
- SwipeToDismissBoxValue.StartToEnd -> when {
- inBin -> Triple(Icons.Outlined.RestoreFromTrash, "Restore", Accent)
- t.unread > 0 -> Triple(Icons.Outlined.MarkEmailRead, "Read", Accent)
- else -> Triple(Icons.Outlined.MarkEmailUnread, "Unread", Accent)
- }
- SwipeToDismissBoxValue.EndToStart -> Triple(Icons.Outlined.Delete, "Bin", Danger)
- SwipeToDismissBoxValue.Settled -> Triple(null, "", Color.Transparent)
+ val swipe = when (direction) {
+ SwipeToDismissBoxValue.StartToEnd -> right
+ SwipeToDismissBoxValue.EndToStart -> left
+ SwipeToDismissBoxValue.Settled -> null
}
Row(
- Modifier.fillMaxSize().background(color).padding(horizontal = 24.dp),
+ Modifier.fillMaxSize().background(swipe?.color ?: Color.Transparent).padding(horizontal = 24.dp),
horizontalArrangement = if (direction == SwipeToDismissBoxValue.EndToStart) Arrangement.End else Arrangement.Start,
verticalAlignment = Alignment.CenterVertically,
) {
- if (icon != null) {
- Icon(icon, null, tint = Color.White)
+ if (swipe != null) {
+ Icon(swipe.icon, null, tint = Color.White)
Spacer(Modifier.width(8.dp))
- Text(label, color = Color.White, fontWeight = FontWeight.SemiBold)
+ Text(swipe.label, color = Color.White, fontWeight = FontWeight.SemiBold)
}
}
},
@@ -486,7 +497,7 @@ private fun Drawer(vm: MailViewModel, close: () -> Unit) {
ModalDrawerSheet {
Column(Modifier.verticalScroll(rememberScrollState()).padding(vertical = 12.dp)) {
Row(Modifier.padding(horizontal = 24.dp, vertical = 12.dp), verticalAlignment = Alignment.CenterVertically) {
- Icon(Icons.Outlined.Mail, null, tint = Accent)
+ AccountPicture(me, 40.dp)
Spacer(Modifier.width(12.dp))
Column {
Text(me?.name ?: me?.username ?: "Inbox", style = MaterialTheme.typography.titleMedium)