Files
opencode-mobile/app/(tabs)/settings.tsx
Den 54ddc64c74 feat(update): tell sideloaded installs that a newer version exists (#179)
AGE-110: 64% of 30d-active users sit on v0.4.10 and 0.2% on the newest
build, so a client-side fix (the AGE-105 Sentry noise gate) reaches almost
nobody. Play hygiene fixes one channel; the direct APK, the self-hosted
F-Droid repo and third-party mirrors have no update mechanism at all — a
device installed from a downloaded APK has literally no way to learn a newer
version exists.

- src/lib/update-check-policy.ts: pure, node --test'able decision logic —
  numeric version compare (a string compare puts 0.4.10 BEFORE 0.4.9, i.e.
  it would have told the largest stale cohort it was current), a 24h check
  throttle that survives a backwards clock, per-version dismissal, and a
  cached last-known-latest so the affordance survives between checks
- src/lib/update-check.ts: Android-only runtime wiring. One unauthenticated
  GET per 24h to the GitHub releases API (every non-Play channel is
  downstream of a GitHub release; expo-updates cannot replace a native
  binary, which is what this cohort needs). Never throws.
- UpdateBanner on the sessions list: one dismissible strip, no modal.
  "Not now" sticks for that version only.
- Settings "Version" row showed a hard-coded "1.0.0" for every build ever
  shipped. It now shows the real version, plus "0.4.10 -> 0.4.14" when an
  update exists (ignoreDismissed: dismissal silences the banner, not the
  place a user goes to check).
- en/zh-Hans strings, catalog parity kept.

Tests: 19 new cases in update-check-policy.test.ts; full suite 300 pass,
tsc --noEmit clean.

Co-authored-by: engineer <engineer@macbookpro.lan>
Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-08-14 09:45:54 -07:00

399 lines
13 KiB
TypeScript

import { useCallback, useEffect, useState } from "react"
import {
View,
Text,
ScrollView,
TouchableOpacity,
Switch,
StyleSheet,
useColorScheme,
Linking,
Alert,
} from "react-native"
import { Ionicons } from "@expo/vector-icons"
import { useTranslation } from "react-i18next"
import { useAuth } from "../../src/stores/auth"
import { useSettings } from "../../src/stores/settings"
import {
categories,
categoryMeta,
setup as setupNotifications,
granted as notificationsGranted,
} from "../../src/lib/notifications"
import type { Category } from "../../src/lib/notifications"
import { hasTelemetryConsent, setTelemetryConsent } from "../../src/lib/telemetry"
import { PRIVACY_POLICY_URL } from "../../src/lib/links"
import { CURRENT_VERSION, checkForUpdate, type AvailableUpdate } from "../../src/lib/update-check"
import type { LocalePreference } from "../../src/lib/i18n/locale-resolve"
function SettingRow({
icon,
label,
description,
isDark,
right,
onPress,
}: {
icon: keyof typeof Ionicons.glyphMap
label: string
description?: string
isDark: boolean
right?: React.ReactNode
onPress?: () => void
}) {
const content = (
<View style={[styles.settingRow, isDark && styles.settingRowDark]}>
<View style={[styles.settingIcon, isDark && styles.settingIconDark]}>
<Ionicons name={icon} size={22} color={isDark ? "#ffffff" : "#0a0a0a"} />
</View>
<View style={styles.settingContent}>
<Text style={[styles.settingLabel, isDark && styles.textDark]}>{label}</Text>
{description && <Text style={[styles.settingDescription, isDark && styles.metaDark]}>{description}</Text>}
</View>
{right}
</View>
)
if (onPress) {
return <TouchableOpacity onPress={onPress}>{content}</TouchableOpacity>
}
return content
}
function SettingSection({ title, children, isDark }: { title: string; children: React.ReactNode; isDark: boolean }) {
return (
<View style={styles.section}>
<Text style={[styles.sectionTitle, isDark && styles.sectionTitleDark]}>{title}</Text>
<View style={[styles.sectionContent, isDark && styles.sectionContentDark]}>{children}</View>
</View>
)
}
export default function SettingsScreen() {
const colorScheme = useColorScheme()
const isDark = colorScheme === "dark"
const { t } = useTranslation()
const { settings, hasBiometrics, updateSettings, lock } = useAuth()
const { notifications, setNotification, locale, setLocale } = useSettings()
const [osGranted, setOsGranted] = useState<boolean | null>(null)
const [telemetryUpdating, setTelemetryUpdating] = useState(false)
// Settings is where a user goes to ask "what am I running?". Answer it, and if
// a newer build exists say so here too — the banner on the sessions list is
// dismissible, this row is not (AGE-110). Uses the same 24h-throttled check,
// so opening Settings repeatedly costs no extra requests.
const [updateAvailable, setUpdateAvailable] = useState<AvailableUpdate | null>(null)
useEffect(() => {
let cancelled = false
checkForUpdate({ ignoreDismissed: true })
.then((result) => {
if (!cancelled) setUpdateAvailable(result)
})
.catch(() => undefined)
return () => {
cancelled = true
}
}, [])
// Telemetry consent: hasTelemetryConsent() returns null (unknown), true, or false.
// We initialise local state from in-memory value; updates call setTelemetryConsent().
const [crashReporting, setCrashReporting] = useState<boolean>(hasTelemetryConsent() ?? false)
const handleCrashReportingToggle = useCallback(
async (value: boolean) => {
setTelemetryUpdating(true)
try {
await setTelemetryConsent(value)
setCrashReporting(value)
} catch {
setCrashReporting(hasTelemetryConsent() ?? false)
Alert.alert(t("settings.alerts.privacyNotSavedTitle"), t("settings.alerts.privacyNotSavedMessage"))
} finally {
setTelemetryUpdating(false)
}
},
[t],
)
// Check OS permission state on first toggle attempt
const handleToggle = useCallback(
async (category: Category, enabled: boolean) => {
if (enabled) {
const ok = await setupNotifications()
setOsGranted(ok)
if (!ok) {
Alert.alert(t("settings.alerts.notificationsDisabledTitle"), t("settings.alerts.notificationsDisabledMessage"))
return
}
}
setNotification(category, enabled)
},
[setNotification, t],
)
// Lazy-check OS permission for status display
if (osGranted === null) {
notificationsGranted()
.then(setOsGranted)
.catch(() => setOsGranted(false))
}
const localeLabels: Record<LocalePreference, string> = {
system: t("settings.language.system"),
en: t("settings.language.en"),
"zh-Hans": t("settings.language.zhHans"),
}
const handleLanguagePress = useCallback(() => {
Alert.alert(t("settings.language.title"), undefined, [
{ text: localeLabels.system, onPress: () => setLocale("system") },
{ text: localeLabels.en, onPress: () => setLocale("en") },
{ text: localeLabels["zh-Hans"], onPress: () => setLocale("zh-Hans") },
{ text: t("common.cancel"), style: "cancel" },
])
}, [t, setLocale, localeLabels])
return (
<ScrollView style={[styles.container, isDark && styles.containerDark]} contentContainerStyle={styles.content}>
<SettingSection title={t("settings.sections.security")} isDark={isDark}>
<SettingRow
icon="finger-print"
label={t("settings.security.biometricOpen.label")}
description={
hasBiometrics
? t("settings.security.biometricOpen.descriptionEnabled")
: t("settings.security.biometricOpen.descriptionUnavailable")
}
isDark={isDark}
right={
<Switch
value={settings.requireBiometric}
onValueChange={(value) => updateSettings({ requireBiometric: value })}
disabled={!hasBiometrics}
trackColor={{ false: "#767577", true: "#22c55e" }}
/>
}
/>
<SettingRow
icon="lock-closed"
label={t("settings.security.biometricSend.label")}
description={t("settings.security.biometricSend.description")}
isDark={isDark}
right={
<Switch
value={settings.requireBiometricForMessages}
onValueChange={(value) => updateSettings({ requireBiometricForMessages: value })}
disabled={!hasBiometrics || !settings.requireBiometric}
trackColor={{ false: "#767577", true: "#22c55e" }}
/>
}
/>
{settings.requireBiometric && (
<SettingRow
icon="exit"
label={t("settings.security.lockNow.label")}
description={t("settings.security.lockNow.description")}
isDark={isDark}
onPress={lock}
right={<Ionicons name="chevron-forward" size={20} color={isDark ? "#666666" : "#999999"} />}
/>
)}
</SettingSection>
<SettingSection title={t("settings.sections.notifications")} isDark={isDark}>
{categories.map((category) => {
const meta = categoryMeta[category]
return (
<SettingRow
key={category}
icon={meta.icon as keyof typeof Ionicons.glyphMap}
label={t(meta.labelKey)}
description={t(meta.descriptionKey)}
isDark={isDark}
right={
<Switch
value={notifications[category]}
onValueChange={(value) => handleToggle(category, value)}
trackColor={{ false: "#767577", true: "#22c55e" }}
/>
}
/>
)
})}
{osGranted === false && (
<View style={[styles.settingRow, isDark && styles.settingRowDark]}>
<Text style={[styles.settingDescription, { color: "#ef4444", paddingLeft: 48 }]}>
{t("settings.notifications.disabledNotice")}
</Text>
</View>
)}
</SettingSection>
<SettingSection title={t("settings.sections.privacy")} isDark={isDark}>
<SettingRow
icon="shield-checkmark"
label={t("settings.privacy.crashReporting.label")}
description={t("settings.privacy.crashReporting.description")}
isDark={isDark}
right={
<Switch
value={crashReporting}
onValueChange={handleCrashReportingToggle}
disabled={telemetryUpdating}
trackColor={{ false: "#767577", true: "#22c55e" }}
/>
}
/>
<SettingRow
icon="document-text"
label={t("settings.privacy.privacyPolicy.label")}
description={t("settings.privacy.privacyPolicy.description")}
isDark={isDark}
onPress={() => Linking.openURL(PRIVACY_POLICY_URL)}
right={<Ionicons name="open-outline" size={20} color={isDark ? "#666666" : "#999999"} />}
/>
</SettingSection>
<SettingSection title={t("settings.sections.about")} isDark={isDark}>
<SettingRow
icon="language"
label={t("settings.language.label")}
description={localeLabels[locale]}
isDark={isDark}
onPress={handleLanguagePress}
right={<Ionicons name="chevron-forward" size={20} color={isDark ? "#666666" : "#999999"} />}
/>
<SettingRow
icon="information-circle"
label={t("settings.about.version")}
// Was hard-coded "1.0.0" — wrong for every build ever shipped, and the
// one place a user could have checked what they are running while 64%
// of the base sat on a four-week-old build (AGE-110).
description={
updateAvailable
? `${CURRENT_VERSION} → ${updateAvailable.version}`
: `${CURRENT_VERSION} · ${t("update.upToDate")}`
}
isDark={isDark}
onPress={updateAvailable ? () => Linking.openURL(updateAvailable.url) : undefined}
right={
updateAvailable ? (
<Ionicons name="arrow-up-circle" size={20} color={isDark ? "#7dd3fc" : "#0369a1"} />
) : undefined
}
/>
<SettingRow
icon="logo-github"
label={t("settings.about.github.label")}
description={t("settings.about.github.description")}
isDark={isDark}
onPress={() => Linking.openURL("https://github.com/anomalyco/opencode")}
right={<Ionicons name="open-outline" size={20} color={isDark ? "#666666" : "#999999"} />}
/>
<SettingRow
icon="document-text"
label={t("settings.about.docs.label")}
description={t("settings.about.docs.description")}
isDark={isDark}
onPress={() => Linking.openURL("https://opencode.ai/docs")}
right={<Ionicons name="open-outline" size={20} color={isDark ? "#666666" : "#999999"} />}
/>
</SettingSection>
<View style={styles.footer}>
<Text style={[styles.footerText, isDark && styles.metaDark]}>{t("settings.footer.appName")}</Text>
<Text style={[styles.footerText, isDark && styles.metaDark]}>{t("settings.footer.tagline")}</Text>
</View>
</ScrollView>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#f5f5f5",
},
containerDark: {
backgroundColor: "#0a0a0a",
},
content: {
paddingBottom: 32,
},
section: {
marginTop: 24,
},
sectionTitle: {
fontSize: 13,
fontWeight: "600",
color: "#666666",
marginLeft: 16,
marginBottom: 8,
textTransform: "uppercase",
},
sectionTitleDark: {
color: "#888888",
},
sectionContent: {
backgroundColor: "#ffffff",
borderTopWidth: 1,
borderBottomWidth: 1,
borderColor: "#e5e5e5",
},
sectionContentDark: {
backgroundColor: "#1a1a1a",
borderColor: "#2a2a2a",
},
settingRow: {
flexDirection: "row",
alignItems: "center",
paddingVertical: 12,
paddingHorizontal: 16,
borderBottomWidth: 1,
borderBottomColor: "#e5e5e5",
},
settingRowDark: {
borderBottomColor: "#2a2a2a",
},
settingIcon: {
width: 36,
height: 36,
borderRadius: 8,
backgroundColor: "#f5f5f5",
justifyContent: "center",
alignItems: "center",
marginRight: 12,
},
settingIconDark: {
backgroundColor: "#2a2a2a",
},
settingContent: {
flex: 1,
},
settingLabel: {
fontSize: 16,
color: "#0a0a0a",
},
textDark: {
color: "#ffffff",
},
settingDescription: {
fontSize: 13,
color: "#666666",
marginTop: 2,
},
metaDark: {
color: "#888888",
},
footer: {
alignItems: "center",
padding: 32,
},
footerText: {
fontSize: 13,
color: "#999999",
textAlign: "center",
},
})