Colota - Android GPS Location tracker
fix(theme): keep controls visible with wallpaper colors (#866)
Max Dietrich0f8decc68cad
diff --git a/apps/docs/docs/configuration/appearance.md b/apps/docs/docs/configuration/appearance.mdnew file mode 100644index 00000000..37b85d32--- /dev/null+++ b/apps/docs/docs/configuration/appearance.md@@ -0,0 +1,47 @@+---+sidebar_position: 7+---++# Appearance++Found under **Settings → Appearance**. A choice applies as soon as you tap it. The map style URLs are saved when you leave the field.++| Setting | Options | Default |+| ---------------- | ------------------------- | ------------------------ |+| Theme | System / Light / Dark | System |+| Wallpaper colors | On/Off | Off |+| Units | Metric / Imperial | From the device language |+| Time format | 24h / 12h | From the device language |+| Map tile server | Light and dark style URLs | maps.mxd.codes |++Appearance settings are included in a [backup](/docs/guides/backup-restore).++## Theme++**System** follows the phone's dark theme setting and changes with it. **Light** and **Dark** keep the app in one theme whatever the phone uses. The map loads the dark style in dark theme and the light style otherwise.++## Wallpaper Colors++Android 12 and later only. On older versions the row is not shown.++With **Wallpaper colors** on, accents and surfaces take their colors from the palette Android builds from your wallpaper. It works in light and dark theme alike. Warnings and errors keep their colors, and so do trip colors and the map.++After you change the wallpaper, the new colors appear the next time you return to Colota. The switch stays greyed out until Colota has read the wallpaper palette.++## Units++**Metric** shows kilometers, km/h and meters. **Imperial** shows miles, mph and feet. The row's hint lists the units in use, and every screen switches at once.++Until you pick one, Colota uses imperial when the device language is English and metric for every other language.++The distance fields in tracking settings follow this choice. See [Tracking Settings](./tracking-settings).++## Time Format++The row's hint shows the current time in the chosen format. Until you pick one, Colota follows the clock format of the device language.++## Map Tile Server++Tap **Map tile server** to enter a style URL for light theme and one for dark theme. Leave a field empty to use the default server. A URL that does not start with `http://` or `https://` and name a host is refused. **Reset to default** clears both fields.++Offline areas always download from the light style. See [Map Tile Server](/docs/guides/tile-server) for hosted alternatives and self-hosting.diff --git a/apps/docs/sidebars.ts b/apps/docs/sidebars.tsindex 3074bb7c..3ae861ec 100644--- a/apps/docs/sidebars.ts+++ b/apps/docs/sidebars.ts@@ -21,7 +21,8 @@ const sidebars: SidebarsConfig = {"configuration/server-settings","configuration/field-mapping","configuration/authentication",- "configuration/mtls"+ "configuration/mtls",+ "configuration/appearance"]},{diff --git a/apps/mobile/android/app/src/main/java/com/colota/bridge/BuildConfigModule.kt b/apps/mobile/android/app/src/main/java/com/colota/bridge/BuildConfigModule.ktindex bb7e9a4d..492017ce 100644--- a/apps/mobile/android/app/src/main/java/com/colota/bridge/BuildConfigModule.kt+++ b/apps/mobile/android/app/src/main/java/com/colota/bridge/BuildConfigModule.kt@@ -53,11 +53,9 @@ class BuildConfigModule(reactContext: ReactApplicationContext) :"accent1_300" to android.R.color.system_accent1_300,"accent1_600" to android.R.color.system_accent1_600,"accent1_700" to android.R.color.system_accent1_700,- "accent1_800" to android.R.color.system_accent1_800,"accent1_900" to android.R.color.system_accent1_900,"neutral1_0" to android.R.color.system_neutral1_0,"neutral1_50" to android.R.color.system_neutral1_50,- "neutral1_600" to android.R.color.system_neutral1_600,"neutral1_700" to android.R.color.system_neutral1_700,"neutral1_800" to android.R.color.system_neutral1_800,"neutral1_900" to android.R.color.system_neutral1_900,diff --git a/apps/mobile/android/app/src/test/java/com/Colota/bridge/BuildConfigModuleTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/bridge/BuildConfigModuleTest.ktindex 9542cf67..1f1c2cb4 100644--- a/apps/mobile/android/app/src/test/java/com/Colota/bridge/BuildConfigModuleTest.kt+++ b/apps/mobile/android/app/src/test/java/com/Colota/bridge/BuildConfigModuleTest.kt@@ -35,9 +35,8 @@ class BuildConfigModuleTest {// The theme reads exactly these steps. One dropped here is an undefined colour in a style prop// on the JS side, so the set is asserted rather than sampled.private val expectedSteps = setOf(- "accent1_100", "accent1_200", "accent1_300", "accent1_600", "accent1_700", "accent1_800",- "accent1_900",- "neutral1_0", "neutral1_50", "neutral1_600", "neutral1_700", "neutral1_800", "neutral1_900",+ "accent1_100", "accent1_200", "accent1_300", "accent1_600", "accent1_700", "accent1_900",+ "neutral1_0", "neutral1_50", "neutral1_700", "neutral1_800", "neutral1_900","neutral2_100", "neutral2_200", "neutral2_300", "neutral2_400", "neutral2_500","neutral2_600", "neutral2_700", "neutral2_800")diff --git a/apps/mobile/src/__tests__/contrastGuard.test.ts b/apps/mobile/src/__tests__/contrastGuard.test.tsindex 6ef57454..73ca3ea8 100644--- a/apps/mobile/src/__tests__/contrastGuard.test.ts+++ b/apps/mobile/src/__tests__/contrastGuard.test.ts@@ -1,10 +1,12 @@import { lightColors, darkColors, type ThemeColors } from "@colota/shared"+import { buildDynamicColors, PALETTE_STEPS, type SystemPalette } from "../styles/dynamicColors"+import { SWITCH_TRACK_ALPHA } from "../constants"/*** Contrast is the one colour rule a machine can settle, so it is settled here rather than in a* status page nobody runs.*- * Ten pairs fail today and are pinned at their measured ratio in DEFERRED. A pin is a number, not+ * Five wallpaper pairs fail today and are pinned at their measured ratio in DEFERRED. A pin is a number, not* an exemption: a pair that gets worse fails, and a pair that gets fixed fails too, because its* pin is then stale and has to be deleted. Adding a pin is a deliberate line in a diff.*@@ -15,6 +17,8 @@ import { lightColors, darkColors, type ThemeColors } from "@colota/shared"** textDisabled and placeholder are absent on purpose: WCAG exempts disabled controls and* placeholder text is not the field's accessible name.+ *+ * The wallpaper themes use a grey palette at Android's tones: a ratio depends on luminance alone.*//** WCAG 2.2: 4.5 for text under 18pt, 3.0 for a UI component or meaningful graphic. */@@ -44,6 +48,7 @@ const PAIRS: Pair[] = [["textOnPrimary", "primary", TEXT],["textOnPrimary", "error", TEXT],["onPrimaryContainer", "primaryContainer", TEXT],+ ["primary", "primaryContainer", UI],["primary", "background", TEXT],["primary", "card", TEXT],["primaryDark", "background", TEXT],@@ -62,10 +67,19 @@ const PAIRS: Pair[] = [["primary", "well", UI]]-/** Empty on purpose: every pair clears its floor. A pin here is a palette decision not yet taken. */+/** The brand themes defer nothing. A pin here is a palette decision not yet taken. */const DEFERRED: Record<string, Record<string, number>> = {light: {},- dark: {}+ dark: {},+ "wallpaper light": {+ "error on background": 4.41,+ "info on background": 4.44+ },+ "wallpaper dark": {+ "error on card": 4.32,+ "info on card": 4.33,+ "border on well": 2.95+ }}function luminance(hex: string): number {@@ -79,9 +93,33 @@ function ratio(fg: string, bg: string): number {return Math.round(((hi + 0.05) / (lo + 0.05)) * 100) / 100}+function over(fg: string, alpha: string, bg: string): string {+ const a = parseInt(alpha, 16) / 255+ const channels = [1, 3, 5].map((i) =>+ Math.round(parseInt(fg.slice(i, i + 2), 16) * a + parseInt(bg.slice(i, i + 2), 16) * (1 - a))+ )+ return `#${channels.map((c) => c.toString(16).padStart(2, "0")).join("")}`+}++/** The grey at CIE L* `tone`; Android's step N sits at L* 100 - N/10. */+function greyAtTone(tone: number): string {+ const y = tone > 8 ? Math.pow((tone + 16) / 116, 3) : tone / 903.3+ const encoded = y <= 0.0031308 ? 12.92 * y : 1.055 * Math.pow(y, 1 / 2.4) - 0.055+ const channel = Math.round(encoded * 255)+ .toString(16)+ .padStart(2, "0")+ return `#${channel}${channel}${channel}`.toUpperCase()+}++const greyPalette = Object.fromEntries(+ PALETTE_STEPS.map((step) => [step, greyAtTone(100 - Number(step.split("_")[1]) / 10)])+) as SystemPalette+const THEMES: [name: string, colors: ThemeColors][] = [["light", lightColors],- ["dark", darkColors]+ ["dark", darkColors],+ ["wallpaper light", buildDynamicColors(greyPalette, false)],+ ["wallpaper dark", buildDynamicColors(greyPalette, true)]]describe("contrast", () => {@@ -92,6 +130,14 @@ describe("contrast", () => {expect(ratio("#FFFFFF", "#FFFFFF")).toBe(1)})+ it("builds the wallpaper palette at Android's tones, so its ratios are the device's", () => {+ expect(greyAtTone(50)).toBe("#777777")+ expect(greyPalette.neutral1_0).toBe("#FFFFFF")+ expect(greyPalette.neutral1_50).toBe("#F1F1F1")+ expect(greyPalette.neutral2_500).toBe("#777777")+ expect(greyPalette.neutral1_900).toBe("#1B1B1B")+ })+for (const [themeName, colors] of THEMES) {describe(themeName, () => {const deferred = DEFERRED[themeName]@@ -131,6 +177,25 @@ describe("contrast", () => {expect(stale).toEqual([])})+ it("keeps the switch thumb 3:1 on its track and on the row behind it, because the thumb is the state", () => {+ const failures: string[] = []+ for (const ground of ["card", "background"] as const) {+ const offTrack = over(colors.border, SWITCH_TRACK_ALPHA, colors[ground])+ const checks: [name: string, fg: string, bg: string][] = [+ ["off thumb on its track", colors.textSecondary, offTrack],+ ["off thumb on the row", colors.textSecondary, colors[ground]],+ ["on thumb on its track", colors.primary, colors.primaryContainer],+ ["on thumb on the row", colors.primary, colors[ground]]+ ]+ for (const [name, fg, bg] of checks) {+ const measured = ratio(fg, bg)+ if (measured < UI) failures.push(`${name} on ${ground} is ${measured}`)+ }+ }++ expect(failures).toEqual([])+ })+it("defers nothing that is not in the pair list, so a pin cannot hide an unchecked pairing", () => {const orphans = Object.keys(deferred).filter((key) => !PAIRS.some(([fg, bg]) => `${fg} on ${bg}` === key))diff --git a/apps/mobile/src/components/ui/Toggle.tsx b/apps/mobile/src/components/ui/Toggle.tsxindex 2773991c..e5b70bd4 100644--- a/apps/mobile/src/components/ui/Toggle.tsx+++ b/apps/mobile/src/components/ui/Toggle.tsx@@ -6,6 +6,7 @@import React from "react"import { Switch } from "react-native"import { useTheme } from "../../hooks/useTheme"+import { SWITCH_TRACK_ALPHA } from "../../constants"type ToggleProps = {value: boolean@@ -19,7 +20,8 @@ type ToggleProps = {/*** The one switch. Thirteen call sites each built the same two colour objects by hand, so the* track and thumb live here instead. Every switch takes the same hue: a semantic colour marks- * a state, and a toggle being on is not a warning.+ * a state, and a toggle being on is not a warning. Every state is tinted: the platform drawable+ * follows the system night mode, not the app's.*/export function Toggle({ value, onValueChange, accessibilityLabel, disabled = false, testID }: ToggleProps) {const { colors } = useTheme()@@ -30,8 +32,8 @@ export function Toggle({ value, onValueChange, accessibilityLabel, disabled = faonValueChange={onValueChange}disabled={disabled}accessibilityLabel={accessibilityLabel}- trackColor={{ false: undefined, true: colors.primaryContainer }}- thumbColor={value ? colors.primary : undefined}+ trackColor={{ false: colors.border + SWITCH_TRACK_ALPHA, true: colors.primaryContainer }}+ thumbColor={disabled ? colors.textDisabled : value ? colors.primary : colors.textSecondary}/>)}diff --git a/apps/mobile/src/components/ui/__tests__/Toggle.test.tsx b/apps/mobile/src/components/ui/__tests__/Toggle.test.tsxindex e3c41843..13682c5d 100644--- a/apps/mobile/src/components/ui/__tests__/Toggle.test.tsx+++ b/apps/mobile/src/components/ui/__tests__/Toggle.test.tsx@@ -1,6 +1,8 @@import React from "react"+import { Platform } from "react-native"import { render, fireEvent } from "@testing-library/react-native"import { lightColors } from "@colota/shared"+import { SWITCH_TRACK_ALPHA } from "../../../constants"jest.mock("../../../hooks/useTheme", () => ({useTheme: () => ({ colors: require("@colota/shared").lightColors })@@ -8,61 +10,63 @@ jest.mock("../../../hooks/useTheme", () => ({import { Toggle } from "../Toggle"+// Switch sends different props per platform; these are the ones Android receives.+beforeEach(() => {+ jest.replaceProperty(Platform, "OS", "android")+})++afterEach(() => {+ jest.restoreAllMocks()+})++function renderToggle(props: { value: boolean; disabled?: boolean; onValueChange?: (value: boolean) => void }) {+ return render(+ <Toggle testID="t" onValueChange={jest.fn()} accessibilityLabel="Offline mode" {...props} />+ ).getByTestId("t")+}+describe("Toggle", () => {- it("puts the thumb on the container, not on its own colour at half alpha", () => {- const { getByTestId } = render(- <Toggle testID="t" value={true} onValueChange={jest.fn()} accessibilityLabel="Offline mode" />- )-- // RN resolves trackColor and thumbColor into these before they reach the platform. The track- // was the accent at 50 percent, which left the thumb 2.4:1 against its own bar; the container- // takes that to 4.1 in light and 6.6 in dark.- const el = getByTestId("t")- expect(el.props.onTintColor).toBe(lightColors.primaryContainer)+ it("paints on as a primary thumb on the container, the same for every switch", () => {+ const el = renderToggle({ value: true })++ expect(el.props.trackTintColor).toBe(lightColors.primaryContainer)expect(el.props.thumbTintColor).toBe(lightColors.primary)})- it("leaves the off state to Android, so the thumb is not the colour of its own track", () => {- // Tinting both with colors.border made them identical at 1.00:1, so an off switch was a- // uniform pill with no visible thumb. Undefined clears the colour filter and restores the- // platform drawable, which also fixes how a disabled switch renders.- const { getByTestId } = render(- <Toggle testID="t" value={false} onValueChange={jest.fn()} accessibilityLabel="Offline mode" />- )-- const el = getByTestId("t")- expect(el.props.thumbTintColor).toBeUndefined()- expect(el.props.tintColor).toBeUndefined()+ it("paints off from the app theme, not the system's night mode", () => {+ const el = renderToggle({ value: false })++ expect(el.props.trackTintColor).toBe(lightColors.border + SWITCH_TRACK_ALPHA)+ expect(el.props.thumbTintColor).toBe(lightColors.textSecondary)})- it("gives every switch the same hue, because being on is not a warning", () => {- // The geofence pause toggle used to paint warning. It was the only one, it was not applied- // to the three sibling toggles that also stop GPS, and orange reads as a fault rather than- // as the setting working.- const { getByTestId } = render(- <Toggle testID="t" value={true} onValueChange={jest.fn()} accessibilityLabel="Pause" />- )+ it("hands native both tracks, because a tap swaps the track before JS re-renders", () => {+ const el = renderToggle({ value: false })- const el = getByTestId("t")- expect(el.props.onTintColor).toBe(lightColors.primaryContainer)- expect(el.props.thumbTintColor).toBe(lightColors.primary)+ expect(el.props.trackColorForTrue).toBe(lightColors.primaryContainer)+ expect(el.props.trackColorForFalse).toBe(lightColors.border + SWITCH_TRACK_ALPHA)+ })++ it("keeps its position's track when disabled, so a disabled switch still reads as on or off", () => {+ const on = renderToggle({ value: true, disabled: true })+ const off = renderToggle({ value: false, disabled: true })++ expect(on.props.enabled).toBe(false)+ expect(on.props.thumbTintColor).toBe(lightColors.textDisabled)+ expect(on.props.trackTintColor).toBe(lightColors.primaryContainer)+ expect(off.props.thumbTintColor).toBe(lightColors.textDisabled)+ expect(off.props.trackTintColor).toBe(lightColors.border + SWITCH_TRACK_ALPHA)})it("reports the change so a caller can persist it", () => {const onValueChange = jest.fn()- const { getByTestId } = render(- <Toggle testID="t" value={false} onValueChange={onValueChange} accessibilityLabel="Offline mode" />- )+ const el = renderToggle({ value: false, onValueChange })- fireEvent(getByTestId("t"), "valueChange", true)+ fireEvent(el, "valueChange", true)expect(onValueChange).toHaveBeenCalledWith(true)})it("carries the label so Voice Access can resolve the row by its visible words", () => {- const { getByTestId } = render(- <Toggle testID="t" value={false} onValueChange={jest.fn()} accessibilityLabel="Offline mode" />- )-- expect(getByTestId("t").props.accessibilityLabel).toBe("Offline mode")+ expect(renderToggle({ value: false }).props.accessibilityLabel).toBe("Offline mode")})})diff --git a/apps/mobile/src/constants/index.ts b/apps/mobile/src/constants/index.tsindex 53294098..542603f6 100644--- a/apps/mobile/src/constants/index.ts+++ b/apps/mobile/src/constants/index.ts@@ -51,6 +51,9 @@ export const size = {*/export const STATE_LAYER_ALPHA = "1A"+/** The off switch track is `border` at half, which keeps the thumb 3:1 on it in every theme. */+export const SWITCH_TRACK_ALPHA = "80"+export const HIT_SLOP_SM = { top: 6, right: 6, bottom: 6, left: 6 } as constexport const HIT_SLOP_MD = { top: 8, right: 8, bottom: 8, left: 8 } as constexport const HIT_SLOP_LG = { top: 12, right: 12, bottom: 12, left: 12 } as constdiff --git a/apps/mobile/src/hooks/__tests__/useTheme.test.tsx b/apps/mobile/src/hooks/__tests__/useTheme.test.tsxindex 482172f3..4351d9b9 100644--- a/apps/mobile/src/hooks/__tests__/useTheme.test.tsx+++ b/apps/mobile/src/hooks/__tests__/useTheme.test.tsx@@ -31,11 +31,9 @@ const palette: SystemPalette = {accent1_300: "#8AB4F8",accent1_600: "#2B5CB8",accent1_700: "#12459E",- accent1_800: "#002E6B",accent1_900: "#001B3F",neutral1_0: "#FFFFFF",neutral1_50: "#F3F3F6",- neutral1_600: "#5B5C63",neutral1_700: "#43444B",neutral1_800: "#2C2D33",neutral1_900: "#1A1B1F",diff --git a/apps/mobile/src/services/__tests__/NativeLocationService.test.ts b/apps/mobile/src/services/__tests__/NativeLocationService.test.tsindex 227766aa..a00b170f 100644--- a/apps/mobile/src/services/__tests__/NativeLocationService.test.ts+++ b/apps/mobile/src/services/__tests__/NativeLocationService.test.ts@@ -344,11 +344,9 @@ describe("NativeLocationService", () => {accent1_300: "#8AB4F8",accent1_600: "#2B5CB8",accent1_700: "#12459E",- accent1_800: "#002E6B",accent1_900: "#001B3F",neutral1_0: "#FFFFFF",neutral1_50: "#F3F3F6",- neutral1_600: "#5B5C63",neutral1_700: "#43444B",neutral1_800: "#2C2D33",neutral1_900: "#1A1B1F",diff --git a/apps/mobile/src/styles/__tests__/dynamicColors.test.ts b/apps/mobile/src/styles/__tests__/dynamicColors.test.tsindex be5d30a4..ac83a322 100644--- a/apps/mobile/src/styles/__tests__/dynamicColors.test.ts+++ b/apps/mobile/src/styles/__tests__/dynamicColors.test.ts@@ -8,11 +8,9 @@ const palette: SystemPalette = {accent1_300: "#8AB4F8",accent1_600: "#2B5CB8",accent1_700: "#12459E",- accent1_800: "#002E6B",accent1_900: "#001B3F",neutral1_0: "#FFFFFF",neutral1_50: "#F3F3F6",- neutral1_600: "#5B5C63",neutral1_700: "#43444B",neutral1_800: "#2C2D33",neutral1_900: "#1A1B1F",@@ -82,7 +80,7 @@ describe("buildDynamicColors", () => {expect(light.primaryContainer).toBe(palette.accent1_100)expect(light.onPrimaryContainer).toBe(palette.accent1_900)- expect(dark.primaryContainer).toBe(palette.accent1_800)+ expect(dark.primaryContainer).toBe(palette.accent1_700)expect(dark.onPrimaryContainer).toBe(palette.accent1_100)})diff --git a/apps/mobile/src/styles/dynamicColors.ts b/apps/mobile/src/styles/dynamicColors.tsindex 59f5dd33..676d52e5 100644--- a/apps/mobile/src/styles/dynamicColors.ts+++ b/apps/mobile/src/styles/dynamicColors.ts@@ -8,17 +8,15 @@ import { darkColors, lightColors } from "./colors"// The steps BuildConfigModule.getSystemPalette returns. Android numbers them the other way round// from Material tone: 0 is the lightest step and 900 the darkest.-const PALETTE_STEPS = [+export const PALETTE_STEPS = ["accent1_100","accent1_200","accent1_300","accent1_600","accent1_700",- "accent1_800","accent1_900","neutral1_0","neutral1_50",- "neutral1_600","neutral1_700","neutral1_800","neutral1_900",@@ -69,19 +67,19 @@ export function buildDynamicColors(palette: SystemPalette, isDark: boolean): The...fixed,primary: palette.accent1_200,primaryDark: palette.accent1_300,- primaryContainer: palette.accent1_800,+ primaryContainer: palette.accent1_700,onPrimaryContainer: palette.accent1_100,- well: palette.neutral1_800,+ well: palette.neutral1_700,background: palette.neutral1_900,- backgroundElevated: palette.neutral1_800,- card: palette.neutral1_700,- surfaceRaised: palette.neutral1_600,- surface: palette.neutral1_800,+ backgroundElevated: palette.neutral1_900,+ card: palette.neutral1_800,+ surfaceRaised: palette.neutral1_800,+ surface: palette.neutral1_900,text: palette.neutral1_50,textSecondary: palette.neutral2_200,textLight: palette.neutral2_300,- textDisabled: palette.neutral2_400,- border: palette.neutral2_700,+ textDisabled: palette.neutral2_500,+ border: palette.neutral2_400,borderLight: palette.neutral2_800,divider: palette.neutral2_700,placeholder: palette.neutral2_400,@@ -105,8 +103,8 @@ export function buildDynamicColors(palette: SystemPalette, isDark: boolean): Thetext: palette.neutral1_900,textSecondary: palette.neutral2_700,textLight: palette.neutral2_600,- textDisabled: palette.neutral2_500,- border: palette.neutral2_200,+ textDisabled: palette.neutral2_400,+ border: palette.neutral2_500,borderLight: palette.neutral2_100,divider: palette.neutral2_200,placeholder: palette.neutral2_500,diff --git a/packages/shared/src/colors.ts b/packages/shared/src/colors.tsindex 3cfb97c5..f734fb87 100644--- a/packages/shared/src/colors.ts+++ b/packages/shared/src/colors.ts@@ -29,7 +29,7 @@ export interface ThemeColors {background: stringbackgroundElevated: stringcard: string- /** One tonal step above `card` for what floats over content: docks, dialogs, banners, map discs. Never a list card. */+ /** What floats over content: docks, dialogs, banners, map discs. A step above `card` in brand dark only. Never a list card. */surfaceRaised: stringsurface: string