Skip to content
Open
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: 6 additions & 0 deletions ATTRIBUTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,9 @@ Generated-in-code art is original and has no file to list: the UI icon set (`cra
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_AttachmentsScreenshotTest_unfoldedDark200_attachments_unfolded_dark_200.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of the folded and unfolded attachments (MR3, #1962) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_AttachmentsScreenshotTest_unfoldedLight100_attachments_unfolded_light_100.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of the folded and unfolded attachments (MR3, #1962) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_AttachmentsScreenshotTest_unfoldedLight200_attachments_unfolded_light_200.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of the folded and unfolded attachments (MR3, #1962) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_SettingsScreenshotTest_appearance_settings_appearance_dark_100.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of Android Settings › Appearance (#1997) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_SettingsScreenshotTest_appearance_settings_appearance_dark_200.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of Android Settings › Appearance (#1997) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_SettingsScreenshotTest_appearance_settings_appearance_light_100.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of Android Settings › Appearance (#1997) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_SettingsScreenshotTest_appearance_settings_appearance_light_200.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of Android Settings › Appearance (#1997) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_SettingsScreenshotTest_darkChosenOnLightPhone_settings_appearance_forced_dark_on_light_100.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of Android Settings › Appearance (#1997) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_SettingsScreenshotTest_lightChosenOnDarkPhone_settings_appearance_forced_light_on_dark_100.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of Android Settings › Appearance (#1997) |
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import android.content.IntentFilter
import android.net.Uri
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.SystemBarStyle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -83,7 +85,6 @@ class MainActivity : AppCompatActivity() {
if (savedInstanceState == null || window.handoff.readAgain) takeHandedOver(intent)
// A window built again (turned, or its language changed) has already taken a message it was asked for up.
if (savedInstanceState == null) incoming.value = incomingOf(intent)
enableEdgeToEdge()
val application = application as? MailCraftApplication
application?.begin()
val starting = application?.started ?: MutableStateFlow(Phase.Failed)
Expand All @@ -103,13 +104,34 @@ class MainActivity : AppCompatActivity() {
started = phase
}
}
MailCraftTheme {
// The theme is the owner's choice (kept with the reading choices); the bars' icons follow it.
val store = remember { DeviceReadingPrefs(applicationContext) }
var reading by remember { mutableStateOf(store.load()) }
// Another window (split screen) may save a newer choice: take it in, so a save here never undoes it.
DisposableEffect(store) {
val stop = store.watch { reading = store.load() }
onDispose { stop() }
}
val dark = reading.theme.dark(isSystemInDarkTheme())
DisposableEffect(dark) {
val clear = android.graphics.Color.TRANSPARENT
val bars = SystemBarStyle.auto(clear, clear) { dark }
enableEdgeToEdge(statusBarStyle = bars, navigationBarStyle = bars)
onDispose {}
}
MailCraftTheme(dark) {
DeviceDateSettings {
Surface(modifier = Modifier.fillMaxSize()) {
var chosen by remember { mutableStateOf(LanguageChoice.current()) }
val handedOver by window.handoff.file.collectAsState()
val asked = Asked({ incoming.value }, { incoming.value = null })
Root(started, version, chosen, handedOver, window.handoff::handled, window, asked) {
Root(
started, version, chosen, handedOver, window.handoff::handled, window, asked,
ReadingChoice(reading) {
reading = it
store.save(it)
},
) {
chosen = it
LanguageChoice.choose(it)
}
Expand Down Expand Up @@ -186,11 +208,14 @@ internal fun Root(
onHandled: () -> Unit = {},
window: WindowModel? = null,
asked: Asked = Asked.None,
readingChoice: ReadingChoice = ReadingChoice(ReadingPrefs()) {},
onLanguage: (String) -> Unit,
) {
when (started) {
is Phase.Ready ->
CoreShell(started.value, version, chosenLanguage, handedOver, onHandled, window, asked, onLanguage)
CoreShell(
started.value, version, chosenLanguage, handedOver, onHandled, window, asked, readingChoice, onLanguage,
)
Phase.Loading -> Opening()
Phase.Failed -> Centered(stringResource(R.string.start_failed))
}
Expand Down Expand Up @@ -225,6 +250,7 @@ private fun CoreShell(
onHandled: () -> Unit,
window: WindowModel?,
asked: Asked,
readingChoice: ReadingChoice,
onLanguage: (String) -> Unit,
) {
val (core, hub, list) = Triple(opened.core, opened.hub, opened.mail)
Expand All @@ -242,8 +268,6 @@ private fun CoreShell(
var swipe by remember { mutableStateOf(prefs.load()) }
val noticeStore = remember { DeviceNoticeButtons(appContext) }
var noticeButtons by remember { mutableStateOf(noticeStore.load()) }
val readingStore = remember { DeviceReadingPrefs(appContext) }
var readingPrefs by remember { mutableStateOf(readingStore.load()) }
// (A folder of 0 is the first Inbox.)
var query by rememberSaveable(stateSaver = MailQuery.Saver) { mutableStateOf(MailQuery(0uL)) }
val ui by list.ui.collectAsState()
Expand Down Expand Up @@ -289,7 +313,7 @@ private fun CoreShell(
nowMillis = now,
swipe = swipe,
noticeButtons = noticeButtons,
readingPrefs = readingPrefs,
readingPrefs = readingChoice.prefs,
reading = pictured.reading,
pictures = pictured.settings,
avatars = pictured.avatars,
Expand All @@ -314,10 +338,7 @@ private fun CoreShell(
noticeButtons = it
noticeStore.save(it)
},
onReadingPrefs = {
readingPrefs = it
readingStore.save(it)
},
onReadingPrefs = readingChoice.onChange,
reading = pictured.reader.actions(writing::begin),
onPictures = pictures::changed,
onClearPictures = pictures::clear,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ internal enum class SettingsPage {
Notifications,
NotificationButtons,
SwipeActions,
Appearance,
Reading,
Pictures,
SenderImages,
Expand All @@ -45,6 +46,7 @@ internal enum class SettingsPage {
Notifications -> texts.t("Notifications")
NotificationButtons -> texts.t("Notification buttons")
SwipeActions -> texts.t("Swipe actions")
Appearance -> texts.t("Appearance")
Reading -> texts.t("Reading")
Pictures -> texts.t("Pictures")
SenderImages -> texts.t("Sender images")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package work.mailcraft.android

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences

/** What the owner chose about reading, kept on this phone only. */
internal data class ReadingPrefs(
Expand All @@ -15,8 +16,38 @@ internal data class ReadingPrefs(
val foldAttachments: Boolean = true,
/** When an opened message counts as read. */
val markRead: MarkRead = MarkRead.WhenOpened,
/** Light, dark, or the phone's setting. */
val theme: ThemeChoice = ThemeChoice.System,
)

/** The reading prefs the window holds, and how it changes them (kept by the window: the theme is outside the shell). */
internal class ReadingChoice(val prefs: ReadingPrefs, val onChange: (ReadingPrefs) -> Unit)

/** The app's theme: its own light or dark, or whatever the phone is set to. */
internal enum class ThemeChoice(val code: String) {
System("system"),
Light("light"),
Dark("dark"),
;

/** Whether the app is dark, given whether the phone is. */
fun dark(system: Boolean): Boolean = when (this) {
System -> system
Light -> false
Dark -> true
}

fun label(texts: Texts): String = when (this) {
System -> texts.t("Follow system")
Light -> texts.t("Light")
Dark -> texts.t("Dark")
}

companion object {
fun fromCode(code: String?): ThemeChoice = entries.firstOrNull { it.code == code } ?: System
}
}

/** When a message that was opened is marked read. */
@Suppress("MagicNumber")
internal enum class MarkRead(val code: String, private val seconds: Int) {
Expand Down Expand Up @@ -58,6 +89,7 @@ internal fun readingPrefsFrom(
headerOpen = get("header_open", false),
foldAttachments = get("fold_attachments", true),
markRead = MarkRead.fromCode(text("mark_read")),
theme = ThemeChoice.fromCode(text("theme")),
)

/** [ReadingPrefs] in the app's private preferences. A store that cannot be read gives the defaults. */
Expand All @@ -77,15 +109,25 @@ internal class DeviceReadingPrefs(context: Context) {
?.putBoolean(HEADER, value.headerOpen)
?.putBoolean(FOLD, value.foldAttachments)
?.putString(MARK, value.markRead.code)
?.putString(THEME, value.theme.code)
?.apply()
}
}

/** Calls [onChange] whenever any window saves; returns the call that stops watching. */
fun watch(onChange: () -> Unit): () -> Unit {
// Held here: the preferences keep only a weak reference to a listener.
val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, _ -> onChange() }
prefs?.registerOnSharedPreferenceChangeListener(listener)
return { prefs?.unregisterOnSharedPreferenceChangeListener(listener) }
}

private companion object {
const val FILE = "reading"
const val LIGHT = "light_messages"
const val HEADER = "header_open"
const val FOLD = "fold_attachments"
const val MARK = "mark_read"
const val THEME = "theme"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package work.mailcraft.android

import androidx.compose.foundation.background
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
Expand Down Expand Up @@ -89,7 +88,7 @@ internal fun ReadingPane(
draft: Boolean = false,
tools: ReadingTools = ReadingTools.None,
) {
val dark = isSystemInDarkTheme()
val dark = appIsDark()
// In a dark window the body may sit on a light page; the choice for this message alone wins.
var pageChoice by remember(id) { mutableStateOf<Boolean?>(null) }
val light = dark && (pageChoice ?: tools.prefs.lightMessages)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: MailCraft contributors
// SPDX-License-Identifier: MIT OR Apache-2.0
@file:OptIn(ExperimentalMaterial3Api::class)
@file:Suppress("TooManyFunctions")

package work.mailcraft.android

Expand Down Expand Up @@ -79,7 +80,8 @@ internal fun SettingsScreen(
}

@Composable
@Suppress("LongParameterList")
// One flat arm per page: the exhaustive `when` is the clearest form.
@Suppress("LongParameterList", "CyclomaticComplexMethod")
private fun SettingsBody(
texts: Texts,
page: SettingsPage?,
Expand All @@ -97,6 +99,7 @@ private fun SettingsBody(
SettingsPage.Language -> LanguagePage(texts, data.chosenLanguage, actions.onLanguage)
SettingsPage.SwipeActions -> SwipePage(texts, data.swipe, actions.onSwipe)
SettingsPage.NotificationButtons -> NoticeButtonsPage(texts, data.noticeButtons, actions.onNoticeButtons)
SettingsPage.Appearance -> AppearancePage(texts, data.readingPrefs, actions.onReadingPrefs)
SettingsPage.Reading -> ReadingPage(texts, data.readingPrefs, actions.onReadingPrefs)
SettingsPage.RemoveData -> RemoveDataPage(texts, data.settingsUi.removal, actions.settings)
SettingsPage.About -> AboutScreenPage(texts, data.version, actions.settings, licences, onLicences)
Expand All @@ -109,13 +112,15 @@ private fun SettingsBody(
}

@Composable
@Suppress("CyclomaticComplexMethod") // one flat arm per page, as in SettingsBody
private fun SettingsRows(texts: Texts, data: ShellData, onOpen: (SettingsPage) -> Unit) {
SettingsPage.entries.forEach { entry ->
val summary = when (entry) {
SettingsPage.Accounts -> accountsSummary(texts, data.accounts.size)
SettingsPage.Language -> languageName(texts, data.chosenLanguage)
SettingsPage.SwipeActions -> "${data.swipe.start.label(texts)} · ${data.swipe.end.label(texts)}"
SettingsPage.NotificationButtons -> noticeButtonsSummary(texts, data.noticeButtons)
SettingsPage.Appearance -> data.readingPrefs.theme.label(texts)
SettingsPage.Reading -> readingSummary(texts, data.readingPrefs)
SettingsPage.About -> data.version
SettingsPage.RemoveData -> removeDataSummary(texts)
Expand Down Expand Up @@ -204,6 +209,13 @@ private fun ChoiceRow(name: String, selected: Boolean, onClick: () -> Unit) {
HorizontalDivider()
}

@Composable
private fun AppearancePage(texts: Texts, prefs: ReadingPrefs, onPrefs: (ReadingPrefs) -> Unit) {
ThemeChoice.entries.forEach { choice ->
ChoiceRow(choice.label(texts), selected = prefs.theme == choice) { onPrefs(prefs.copy(theme = choice)) }
}
}

/** The summary under the Reading row. */
private fun readingSummary(texts: Texts, prefs: ReadingPrefs): String =
if (prefs.lightMessages) texts.t("Light page in the dark theme") else texts.t("Dark page in the dark theme")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.Color

internal val LightColors: ColorScheme = lightColorScheme(
Expand Down Expand Up @@ -58,8 +60,17 @@ private val DarkColors: ColorScheme = darkColorScheme(
outlineVariant = Color(0xFF41484D),
)

/** The app's colours: light or dark as the system is. */
/** Whether the app is dark, as [MailCraftTheme] set it; null outside it. */
private val LocalDark = staticCompositionLocalOf<Boolean?> { null }

/** Whether the app is dark now: the owner's choice, which every screen follows, not the phone's setting. */
@Composable
internal fun appIsDark(): Boolean = LocalDark.current ?: isSystemInDarkTheme()

/** The app's colours: light or dark as [dark] says (the owner's choice, resolved against the system's). */
@Composable
internal fun MailCraftTheme(dark: Boolean = isSystemInDarkTheme(), content: @Composable () -> Unit) {
MaterialTheme(colorScheme = if (dark) DarkColors else LightColors, content = content)
CompositionLocalProvider(LocalDark provides dark) {
MaterialTheme(colorScheme = if (dark) DarkColors else LightColors, content = content)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class NavigationTest {
fun settingsHasItsSections() {
assertEquals(
listOf(
"Accounts", "Mail to keep offline", "Notifications", "Notification buttons", "Swipe actions", "Reading",
"Accounts", "Mail to keep offline", "Notifications", "Notification buttons", "Swipe actions",
"Appearance", "Reading",
"Pictures", "Sender images", "Signatures", "Language", "Remove local data", "About",
),
SettingsPage.entries.map { it.label(EnglishTexts) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ class SettingsScreenshotTest {
shoot("${name}_dark_200", true, 2f, if (tallAtTwice) TALL else null, content)
}

@Test
fun appearance() = four("settings_appearance") { SettingsShots.Page(SettingsPage.Appearance) }

// The app set to Light on a phone that is dark, and Dark on a phone that is light: the choice wins.
@Test
fun lightChosenOnDarkPhone() =
shoot("settings_appearance_forced_light_on_dark_100", true, 1f) { SettingsShots.Page(SettingsPage.Appearance, theme = ThemeChoice.Light) }

@Test
fun darkChosenOnLightPhone() =
shoot("settings_appearance_forced_dark_on_light_100", false, 1f) { SettingsShots.Page(SettingsPage.Appearance, theme = ThemeChoice.Dark) }

@Test
fun rowsLight100() = shoot("settings_rows_light_100", false, 1f) { SettingsShots.Page(null) }

Expand Down
Loading