Colota - Android GPS Location tracker
fix(inspector): format the time since last fix and name the power state in the location table (#876)
Max Dietrich01bd8c47da7d
diff --git a/apps/mobile/src/components/features/inspector/LocationTable.tsx b/apps/mobile/src/components/features/inspector/LocationTable.tsxindex 568029bf..3374e4b9 100644--- a/apps/mobile/src/components/features/inspector/LocationTable.tsx+++ b/apps/mobile/src/components/features/inspector/LocationTable.tsx@@ -14,7 +14,6 @@ import {NativeScrollEvent,NativeSyntheticEvent} from "react-native"-import { BatteryCharging, BatteryFull } from "lucide-react-native"import { fontSizes, fonts, lineHeights, type } from "../../../styles/typography"import { LocationCoords, ThemeColors } from "../../../types/global"import { formatTime, getSpeedUnit, getTimeFormat } from "../../../utils/geo"@@ -34,14 +33,15 @@ interface TableRow extends LocationCoords {}const COLUMN_WIDTHS = {- delta: 56,+ delta: 88,lat: 88,lon: 88,acc: 56,speed: 72,alt: 56,bear: 56,- batt: 80,+ batt: 56,+ power: 96,sync: 72,note: 176}@@ -49,14 +49,20 @@ const COLUMN_WIDTHS = {// "10:17:10 PM" is three characters wider than "22:17:10".const TIME_WIDTH = { "24h": 88, "12h": 112 } as constconst ROW_LENGTH = size.touch + StyleSheet.hairlineWidth-const BATTERY_CHARGING = 2-const BATTERY_FULL = 3+const POWER_WORDS: Record<number, string> = { 1: "Unplugged", 2: "Charging", 3: "Full" }function val(v?: number | null, decimals = 0): string {if (v == null) return "-"return decimals > 0 ? v.toFixed(decimals) : String(Math.round(v))}+function gap(seconds: number): string {+ if (seconds < 60) return `${seconds}s`+ if (seconds < 3600) return `${Math.floor(seconds / 60)}m ${seconds % 60}s`+ if (seconds < 86400) return `${Math.floor(seconds / 3600)}h ${Math.floor((seconds % 3600) / 60)}m`+ return `${Math.floor(seconds / 86400)}d ${Math.floor((seconds % 86400) / 3600)}h`+}+function timeOf(item: TableRow): string {return item.timestamp ? formatTime(item.timestamp, true) : "-"}@@ -99,14 +105,7 @@ const DataRow = React.memo(speedUnit,hasEndpoint}: RowProps & { speedUnit: { factor: number; unit: string }; hasEndpoint: boolean }) => {- const BatteryGlyph =- item.battery_status === BATTERY_CHARGING- ? BatteryCharging- : item.battery_status === BATTERY_FULL- ? BatteryFull- : null- const batteryWord =- item.battery_status === BATTERY_CHARGING ? "charging" : item.battery_status === BATTERY_FULL ? "full" : null+ const power = POWER_WORDS[item.battery_status ?? 0]const battery = item.battery != null ? `${item.battery}%` : "-"const syncWord = item.sent ? "Sent" : "Queued"const facts = [@@ -117,7 +116,7 @@ const DataRow = React.memo(item.speed != null ? `speed ${(item.speed * speedUnit.factor).toFixed(1)} ${speedUnit.unit}` : null,item.altitude != null ? `altitude ${val(item.altitude)} m` : null,item.bearing != null ? `bearing ${val(item.bearing)}` : null,- item.battery != null ? `battery ${battery}${batteryWord ? ` ${batteryWord}` : ""}` : null,+ item.battery != null ? `battery ${battery}${power ? ` ${power.toLowerCase()}` : ""}` : null,hasEndpoint ? syncWord : null,item.note ? `note ${item.note}` : null,"show on map"@@ -134,7 +133,7 @@ const DataRow = React.memo(onPress={() => item.id != null && onSelectPoint(item.id)}><Text style={[mono, { width: COLUMN_WIDTHS.delta }]} numberOfLines={1}>- {item.delta != null ? `+${item.delta}` : ""}+ {item.delta != null ? gap(item.delta) : ""}</Text><Text style={[mono, { width: COLUMN_WIDTHS.lat }]} numberOfLines={1}>{val(item.latitude, 5)}@@ -154,16 +153,12 @@ const DataRow = React.memo(<Text style={[mono, { width: COLUMN_WIDTHS.bear }]} numberOfLines={1}>{val(item.bearing)}</Text>- <View- style={[styles.batteryCell, { width: COLUMN_WIDTHS.batt }]}- accessibilityRole="text"- accessibilityLabel={batteryWord ? `${battery} ${batteryWord}` : battery}- >- <Text style={[styles.mono, { color: content }]} numberOfLines={1}>- {battery}- </Text>- {BatteryGlyph && <BatteryGlyph size={size.icon.sm} color={colors.textSecondary} />}- </View>+ <Text style={[mono, { width: COLUMN_WIDTHS.batt }]} numberOfLines={1}>+ {battery}+ </Text>+ <Text style={[styles.caption, { width: COLUMN_WIDTHS.power, color: content }]} numberOfLines={1}>+ {power ?? "-"}+ </Text>{hasEndpoint && (<Text style={[styles.caption, { width: COLUMN_WIDTHS.sync, color: content }]} numberOfLines={1}>{syncWord}@@ -232,6 +227,7 @@ export function LocationTable({ locations, colors, hasEndpoint, selectedPointId,COLUMN_WIDTHS.alt +COLUMN_WIDTHS.bear +COLUMN_WIDTHS.batt ++ COLUMN_WIDTHS.power +(hasEndpoint ? COLUMN_WIDTHS.sync : 0) +COLUMN_WIDTHS.note@@ -267,7 +263,7 @@ export function LocationTable({ locations, colors, hasEndpoint, selectedPointId,<View style={{ width: dataWidth }}><View style={styles.headerRow}><Text style={[numericHeader, { width: COLUMN_WIDTHS.delta }]} numberOfLines={2}>- Δs+ Since last fix</Text><Text style={[numericHeader, { width: COLUMN_WIDTHS.lat }]} numberOfLines={2}>Lat@@ -290,6 +286,9 @@ export function LocationTable({ locations, colors, hasEndpoint, selectedPointId,<Text style={[numericHeader, { width: COLUMN_WIDTHS.batt }]} numberOfLines={2}>Batt %</Text>+ <Text style={[header, { width: COLUMN_WIDTHS.power }]} numberOfLines={2}>+ Power+ </Text>{hasEndpoint && (<Text style={[header, { width: COLUMN_WIDTHS.sync }]} numberOfLines={2}>Sync@@ -356,12 +355,5 @@ const styles = StyleSheet.create({lineHeight: lineHeights.caption,...fonts.regular,paddingHorizontal: space.md- },- batteryCell: {- flexDirection: "row",- alignItems: "center",- justifyContent: "flex-end",- gap: space.xs,- paddingEnd: space.md}})diff --git a/apps/mobile/src/components/features/inspector/__tests__/LocationTable.test.tsx b/apps/mobile/src/components/features/inspector/__tests__/LocationTable.test.tsxindex 2e4e8399..a383c3af 100644--- a/apps/mobile/src/components/features/inspector/__tests__/LocationTable.test.tsx+++ b/apps/mobile/src/components/features/inspector/__tests__/LocationTable.test.tsx@@ -102,7 +102,7 @@ describe("LocationTable", () => {expect(timePane.getByText("Time")).toBeTruthy()expect(timePane.getAllByText(/^\d\d:\d\d:\d\d$/)).toHaveLength(3)expect(dataPane.queryByText("Time")).toBeNull()- for (const heading of ["Δs", "Lat", "Lon", "Acc m", "Alt m", "Bear", "Batt %", "Note"]) {+ for (const heading of ["Since last fix", "Lat", "Lon", "Acc m", "Alt m", "Bear", "Batt %", "Power", "Note"]) {expect(dataPane.getByText(heading)).toBeTruthy()}})@@ -118,9 +118,27 @@ describe("LocationTable", () => {const dataRows = within(getByTestId("table-data-pane")).getAllByLabelText(rowLabel)expect(dataRows).toHaveLength(3)expect(within(dataRows[0]).getByText("48.30000")).toBeTruthy()- expect(within(dataRows[0]).getByText("+30")).toBeTruthy()+ expect(within(dataRows[0]).getByText("30s")).toBeTruthy()expect(within(dataRows[2]).getByText("48.10000")).toBeTruthy()- expect(within(dataRows[2]).queryByText(/^\+/)).toBeNull()+ expect(within(dataRows[2]).queryByText(/^\d+s$/)).toBeNull()+ })++ it("writes the time since the last fix in units, always two past a minute, so a long pause fits its column and a lone m never reads as metres", () => {+ const at = (id: number, timestamp: number): LocationCoords => ({+ id,+ latitude: 48,+ longitude: 11,+ accuracy: 5,+ timestamp+ })+ const { getByTestId } = renderTable({+ locations: [at(1, 1000), at(2, 1045), at(3, 1345), at(4, 5245), at(5, 188845)]+ })++ const gaps = within(getByTestId("table-data-pane"))+ .getAllByLabelText(rowLabel)+ .map((row) => within(row).queryByText(/^\d+[dhms]( \d+[hms])?$/)?.props.children ?? null)+ expect(gaps).toEqual(["2d 3h", "1h 5m", "5m 0s", "45s", null])})it("names the speed unit in the header so the cells stay bare numbers", () => {@@ -147,19 +165,42 @@ describe("LocationTable", () => {expect(withServer.getAllByText("Queued")).toHaveLength(1)})- it("draws a battery glyph only while charging or full and speaks the word for a screen reader", () => {- const { getAllByTestId, getByTestId, getByLabelText, queryByLabelText } = renderTable()-- expect(getAllByTestId("icon-BatteryCharging")).toHaveLength(1)- expect(getAllByTestId("icon-BatteryFull")).toHaveLength(1)- expect(getByLabelText("80% charging")).toBeTruthy()- expect(getByLabelText("100% full")).toBeTruthy()- expect(getByLabelText("79%")).toBeTruthy()- expect(queryByLabelText(/discharging/i)).toBeNull()+ it("names the power state in words in its own column, because an unlabelled battery glyph left users guessing", () => {+ const { getByTestId, queryByTestId } = renderTable()const dataRows = within(getByTestId("table-data-pane")).getAllByLabelText(rowLabel)+ expect(within(dataRows[0]).getByText("Full")).toBeTruthy()+ expect(within(dataRows[1]).getByText("Unplugged")).toBeTruthy()+ expect(within(dataRows[2]).getByText("Charging")).toBeTruthy()+ expect(queryByTestId("icon-BatteryCharging")).toBeNull()+ expect(queryByTestId("icon-BatteryFull")).toBeNull()+expect(dataRows[2].props.accessibilityLabel).toContain("battery 80% charging")- expect(dataRows[1].props.accessibilityLabel).not.toContain("charging")+ expect(dataRows[1].props.accessibilityLabel).toContain("battery 79% unplugged")+ })++ it("shows a dash when the power state was not recorded, so an unknown never passes for unplugged", () => {+ const { getByTestId } = renderTable({+ locations: [+ {+ id: 1,+ latitude: 48,+ longitude: 11,+ accuracy: 5,+ altitude: 500,+ speed: 5,+ bearing: 90,+ timestamp: 1000,+ battery: 50,+ battery_status: 0+ }+ ]+ })++ const [row] = within(getByTestId("table-data-pane")).getAllByLabelText(rowLabel)+ expect(within(row).getByText("-")).toBeTruthy()+ expect(within(row).queryByText(/Unplugged|Charging|Full/)).toBeNull()+ expect(row.props.accessibilityLabel).toContain("battery 50%, ")})it("hands the point id to the screen from either pane so the map can open it", () => {