Skip to content

Colota - Android GPS Location tracker

7.7 KB
Raw Blame
/**
 * Copyright (C) 2026 Max Dietrich
 * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
 */

import NativeLocationService from "../services/NativeLocationService"
import { formatDecimal } from "./format"
import { t } from "../i18n/t"

const EARTH_RADIUS_METERS = 6_371_000
const FEET_PER_METER = 3.28084
const MPH_PER_MPS = 2.23694

/** Haversine formula - mirrors GeofenceHelper.kt */
export function haversine(lat1: number, lon1: number, lat2: number, lon2: number): number {
  const dLat = ((lat2 - lat1) * Math.PI) / 180
  const dLon = ((lon2 - lon1) * Math.PI) / 180
  const a =
    Math.sin(dLat / 2) ** 2 +
    Math.cos((lat1 * Math.PI) / 180) * Math.cos((lat2 * Math.PI) / 180) * Math.sin(dLon / 2) ** 2
  return EARTH_RADIUS_METERS * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
}

/** Sum of Haversine distances between consecutive points, in meters. */
export function computeTotalDistance(locations: { latitude: number; longitude: number }[]): number {
  let total = 0
  for (let i = 1; i < locations.length; i++) {
    total += haversine(
      locations[i - 1].latitude,
      locations[i - 1].longitude,
      locations[i].latitude,
      locations[i].longitude
    )
  }
  return total
}

// -- Display preferences (cached from SQLite) --

export type UnitSystem = "metric" | "imperial"
export type TimeFormat = "12h" | "24h"

let cachedUnitSystem: UnitSystem | null = null
let cachedTimeFormat: TimeFormat | null = null

/** Detect whether the device locale uses 12h time. */
function localeUses12h(): boolean {
  try {
    const sample = new Date(2000, 0, 1, 14).toLocaleTimeString(undefined, { hour: "numeric" })
    return /am|pm/i.test(sample)
  } catch {
    return false
  }
}

/** Load display preferences from native storage. Call on app start and after saving. */
export async function loadDisplayPreferences(): Promise<void> {
  try {
    const [unit, time] = await Promise.all([
      NativeLocationService.getSetting("unitSystem", ""),
      NativeLocationService.getSetting("timeFormat", "")
    ])
    cachedUnitSystem = unit === "metric" || unit === "imperial" ? unit : null
    cachedTimeFormat = time === "12h" || time === "24h" ? time : null
  } catch {
    // Keep defaults on error
  }
}

// -- Unit detection --

const MILE_LOCALES = new Set(["en-US", "en-GB", "en-MM", "en-LR"])

function localeUsesMiles(): boolean {
  try {
    const locale = Intl.NumberFormat().resolvedOptions().locale.split("-").slice(0, 2).join("-")
    return MILE_LOCALES.has(locale)
  } catch {
    return false
  }
}

export function getUnitSystem(): UnitSystem {
  return cachedUnitSystem ?? (localeUsesMiles() ? "imperial" : "metric")
}

export function getTimeFormat(): TimeFormat {
  return cachedTimeFormat ?? (localeUses12h() ? "12h" : "24h")
}

function usesMiles(): boolean {
  return getUnitSystem() === "imperial"
}

// -- Formatting functions --

/** Format m/s into a human-readable speed string. */
export function formatSpeed(metersPerSecond: number, decimals = 1): string {
  if (usesMiles()) {
    const mph = metersPerSecond * MPH_PER_MPS
    return `${formatDecimal(mph, decimals)} ${t("unit.mph")}`
  }
  const kmh = metersPerSecond * 3.6
  return `${formatDecimal(kmh, decimals)} ${t("unit.kmh")}`
}

/** Return the speed unit info (used by TrackMap). */
// Module-level so the identity tracks the unit; the getter resolves the label at read time.
const MPH = Object.freeze({
  factor: MPH_PER_MPS,
  get unit() {
    return t("unit.mph")
  }
})
const KMH = Object.freeze({
  factor: 3.6,
  get unit() {
    return t("unit.kmh")
  }
})

export function getSpeedUnit(): { factor: number; unit: string } {
  return usesMiles() ? MPH : KMH
}

/** Format seconds duration as "Xh Ymin" or "Ymin" */
export function formatDuration(seconds: number): string {
  const s = Math.max(0, seconds)
  const hours = Math.floor(s / 3600)
  const minutes = Math.floor((s % 3600) / 60)
  if (hours > 0) return `${hours}${t("unit.h")} ${minutes}${t("unit.min")}`
  return `${minutes}${t("unit.min")}`
}

/** Format a Unix-seconds timestamp as a localized time string. */
/** The unit spelled out, for a screen reader. */
export function spokenDistance(meters: number): string {
  if (usesMiles()) return t("unit.mi.spoken", { n: formatDecimal(meters / 1609.344, 1) })
  return t("unit.km.spoken", { n: formatDecimal(meters / 1000, 1) })
}

/** The app's clock, with the format passed in, so a sample of a format cannot drift from it. */
export function formatTimeIn(unixSeconds: number, format: TimeFormat | null, showSeconds = false): string {
  const d = new Date(unixSeconds * 1000)
  return d.toLocaleTimeString(undefined, {
    hour: "2-digit",
    minute: "2-digit",
    ...(showSeconds && { second: "2-digit" }),
    ...(format && { hour12: format === "12h" })
  })
}

export function formatTime(unixSeconds: number, showSeconds = false): string {
  return formatTimeIn(unixSeconds, cachedTimeFormat, showSeconds)
}

/** Format a Unix-seconds timestamp as a localized date string (e.g. "Wed, Feb 27"). */
export function formatDate(unixSeconds: number): string {
  return new Date(unixSeconds * 1000).toLocaleDateString(undefined, {
    weekday: "short",
    month: "short",
    day: "numeric"
  })
}

/** Format meters as a long distance string (e.g. "12.3 km" / "7.6 mi"). */
export function formatDistance(meters: number): string {
  if (usesMiles()) return `${formatDecimal(meters / 1609.344, 1)} ${t("unit.mi")}`
  return `${formatDecimal(meters / 1000, 1)} ${t("unit.km")}`
}

/** Format meters as a short distance string (e.g. "50m" / "164 ft"). */
export function formatShortDistance(meters: number): string {
  if (usesMiles()) return `${Math.round(meters * FEET_PER_METER)} ${t("unit.ft")}`
  return `${Math.round(meters)}${t("unit.m")}`
}

/** Returns the short distance unit label for input fields ("m" or "ft"). */
export function shortDistanceUnit(): string {
  return usesMiles() ? t("unit.ft") : t("unit.m")
}

/** A stored speed in m/s as the whole number the speed field shows, in the user's unit. */
export function speedToInput(metersPerSecond: number): number {
  return Math.round(metersPerSecond * getSpeedUnit().factor)
}

/** A typed speed in the user's unit back to the m/s the profile stores. */
export function inputToSpeed(value: number): number {
  return value / getSpeedUnit().factor
}

/** Convert a user-entered short distance to meters. */
export function inputToMeters(value: number): number {
  return usesMiles() ? value / FEET_PER_METER : value
}

/** Convert meters to the user's short distance unit for pre-filling inputs. */
export function metersToInput(meters: number): number {
  return usesMiles() ? Math.round(meters * FEET_PER_METER) : meters
}

/** A calendar date with its year, for a date that can be years away, such as a certificate's expiry. */
export function formatDateWithYear(unixSeconds: number): string {
  return new Date(unixSeconds * 1000).toLocaleDateString(undefined, { year: "numeric", month: "short", day: "numeric" })
}

/** A time alone for today, the date otherwise, so a caption stays short where the day is implied. */
export function formatWhen(unixSeconds: number, now: Date = new Date()): string {
  const sameDay = startOfDaySec(new Date(unixSeconds * 1000)) === startOfDaySec(now)
  return sameDay ? formatTime(unixSeconds) : `${formatDate(unixSeconds)} · ${formatTime(unixSeconds)}`
}

/** Start of `date`'s local day, in Unix seconds. */
export function startOfDaySec(date: Date): number {
  const d = new Date(date)
  d.setHours(0, 0, 0, 0)
  return Math.floor(d.getTime() / 1000)
}

/** End of `date`'s local day (23:59:59.999), in Unix seconds. */
export function endOfDaySec(date: Date): number {
  const d = new Date(date)
  d.setHours(23, 59, 59, 999)
  return Math.floor(d.getTime() / 1000)
}