-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
846 lines (780 loc) · 26.4 KB
/
Copy pathApp.js
File metadata and controls
846 lines (780 loc) · 26.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
import React, { useEffect, useRef, useState, useCallback } from 'react';
import {
StyleSheet,
Text,
View,
TouchableOpacity,
ActivityIndicator,
Platform,
Linking,
useWindowDimensions,
Modal,
TextInput,
KeyboardAvoidingView,
} from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import * as Location from 'expo-location';
import { activateKeepAwakeAsync } from 'expo-keep-awake';
import * as Haptics from 'expo-haptics';
import { StatusBar } from 'expo-status-bar';
// CarPlay / Android Auto — lazy-require to avoid errors in Expo Go.
let HybridAutoPlay, AutoPlayInformationTemplate;
if (Platform.OS === 'ios') {
try {
const ap = require('@iternio/react-native-auto-play');
HybridAutoPlay = ap.HybridAutoPlay;
AutoPlayInformationTemplate = ap.InformationTemplate;
} catch (_) {
// Not available (e.g. Expo Go)
}
}
// ---- Unit conversions ----
const MPS_TO_MPH = 2.2369362921;
const MPS_TO_KMH = 3.6;
// ---- Speed limit providers ----
// Each provider returns { value: number, unit: 'mph' | 'kmh', source: 'here' | 'osm' }
// or null. The main fetchSpeedLimit() tries HERE first (better coverage) and falls
// back to Overpass/OSM (free, open) if HERE has no data or errors.
// Get a key at https://developer.here.com. The free "Base" plan allows 1,000
// transactions/day. For production: proxy this through a small backend — API
// keys shipped inside a mobile app can be extracted by anyone who installs it.
const HERE_API_KEY = '6jTbJVWZkUP4tAtJ-mVBeS24nhj-vs_UD0XVkOK44y4';
// Set this to your deployed Cloudflare Worker URL to proxy HERE API requests
// (keeps the API key server-side). Leave null to call HERE directly (local dev).
// Example: 'https://speed-limit-proxy.your-subdomain.workers.dev'
const PROXY_URL = 'https://speed-limit-proxy.speed-limit-proxy.workers.dev';
const OVERPASS_ENDPOINTS = [
'https://overpass-api.de/api/interpreter',
'https://overpass.kumi.systems/api/interpreter',
];
function parseMaxspeed(raw) {
if (!raw) return null;
const s = String(raw).trim().toLowerCase();
// e.g. "50", "50 mph", "50mph", "50 km/h", "50kmh"
const mphMatch = s.match(/^(\d+(?:\.\d+)?)\s*mph$/);
if (mphMatch) return { value: parseFloat(mphMatch[1]), unit: 'mph' };
const kmhMatch = s.match(/^(\d+(?:\.\d+)?)\s*(km\/h|kmh|kph)$/);
if (kmhMatch) return { value: parseFloat(kmhMatch[1]), unit: 'kmh' };
const numMatch = s.match(/^(\d+(?:\.\d+)?)$/);
if (numMatch) return { value: parseFloat(numMatch[1]), unit: 'kmh' }; // OSM default
// Ignore implicit values like "RU:urban", "walk", "none", "signals", etc.
return null;
}
async function fetchSpeedLimitOverpass(lat, lon, signal) {
// Query ways with highway+maxspeed near the point, ordered implicitly.
const radius = 25; // meters
const query = `[out:json][timeout:8];
way(around:${radius},${lat},${lon})[highway][maxspeed];
out tags center 1;`;
for (const endpoint of OVERPASS_ENDPOINTS) {
try {
const res = await fetch(endpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: 'data=' + encodeURIComponent(query),
signal,
});
if (!res.ok) continue;
const data = await res.json();
if (!data.elements || data.elements.length === 0) return null;
// Pick the closest element to (lat, lon) by great-circle distance on the center.
let best = null;
let bestDist = Infinity;
for (const el of data.elements) {
const c = el.center || { lat: el.lat, lon: el.lon };
if (c == null || c.lat == null || c.lon == null) continue;
const d = haversine(lat, lon, c.lat, c.lon);
if (d < bestDist) {
bestDist = d;
best = el;
}
}
if (!best || !best.tags || !best.tags.maxspeed) return null;
const parsed = parseMaxspeed(best.tags.maxspeed);
return parsed ? { ...parsed, source: 'osm' } : null;
} catch (e) {
if (e.name === 'AbortError') throw e;
// try next endpoint
}
}
return null;
}
// HERE Routing v8 returns speedLimit (in m/s) per route span. We construct a
// minimal ~10 m route from (lat, lon) to a nearby point, then read the
// speedLimit of the first span that has one.
async function fetchSpeedLimitHere(lat, lon, signal) {
if (!PROXY_URL && (!HERE_API_KEY || HERE_API_KEY.startsWith('YOUR_'))) return null;
const dLat = 0.00009; // ~10 m north
const lat2 = lat + dLat;
const lon2 = lon;
const params =
`?transportMode=car&origin=${lat},${lon}&destination=${lat2},${lon2}` +
'&return=polyline&spans=speedLimit';
// Use proxy if configured, otherwise call HERE directly with embedded key.
const url = PROXY_URL
? `${PROXY_URL}/here-route${params}`
: `https://router.hereapi.com/v8/routes${params}&apikey=${encodeURIComponent(HERE_API_KEY)}`;
try {
const res = await fetch(url, { signal });
if (!res.ok) return null;
const data = await res.json();
const spans = data && data.routes && data.routes[0] && data.routes[0].sections && data.routes[0].sections[0] && data.routes[0].sections[0].spans;
if (!Array.isArray(spans)) return null;
const span = spans.find((s) => typeof s.speedLimit === 'number' && s.speedLimit > 0);
if (!span) return null;
// HERE returns speedLimit in m/s. Convert to km/h as our canonical unit.
const kmh = span.speedLimit * 3.6;
return { value: Math.round(kmh), unit: 'kmh', source: 'here' };
} catch (e) {
if (e.name === 'AbortError') throw e;
return null;
}
}
// Try HERE first (better coverage), fall back to OSM/Overpass.
async function fetchSpeedLimit(lat, lon, signal) {
const here = await fetchSpeedLimitHere(lat, lon, signal);
if (here) return here;
return await fetchSpeedLimitOverpass(lat, lon, signal);
}
function haversine(lat1, lon1, lat2, lon2) {
const R = 6371000;
const toRad = (d) => (d * Math.PI) / 180;
const dLat = toRad(lat2 - lat1);
const dLon = toRad(lon2 - lon1);
const a =
Math.sin(dLat / 2) ** 2 +
Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(dLon / 2) ** 2;
return 2 * R * Math.asin(Math.sqrt(a));
}
// Convert a limit given in one unit to the user's display unit.
function convertLimit(limit, displayUnit) {
if (!limit) return null;
if (limit.unit === displayUnit) return Math.round(limit.value);
if (limit.unit === 'mph' && displayUnit === 'kmh') {
return Math.round(limit.value * 1.609344);
}
if (limit.unit === 'kmh' && displayUnit === 'mph') {
return Math.round(limit.value / 1.609344);
}
return Math.round(limit.value);
}
const KEEP_AWAKE_TAG = 'speed-limit-keep-awake';
const DEFAULT_THRESHOLDS = [
{ over: 3, color: '#FFD700' }, // yellow
{ over: 8, color: '#FF8C00' }, // orange
{ over: 10, color: '#FF0000' }, // red
];
const STORAGE_KEY = '@speed_limit_thresholds';
export default function App() {
const [unit, setUnit] = useState('mph'); // 'mph' | 'kmh'
// Always keep screen on
useEffect(() => {
activateKeepAwakeAsync(KEEP_AWAKE_TAG).catch(() => {});
}, []);
const [thresholds, setThresholds] = useState(DEFAULT_THRESHOLDS);
const [settingsVisible, setSettingsVisible] = useState(false);
const [editThresholds, setEditThresholds] = useState(DEFAULT_THRESHOLDS);
useEffect(() => {
AsyncStorage.getItem(STORAGE_KEY).then((val) => {
if (val) {
try {
const parsed = JSON.parse(val);
if (Array.isArray(parsed) && parsed.length === 3) {
setThresholds(parsed);
}
} catch (_) {}
}
});
}, []);
const saveThresholds = useCallback((t) => {
setThresholds(t);
AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(t)).catch(() => {});
}, []);
const [permStatus, setPermStatus] = useState('pending'); // pending | granted | denied
const [speedMps, setSpeedMps] = useState(0);
const [accuracyMps, setAccuracyMps] = useState(null);
const [limit, setLimit] = useState(null); // { value, unit, source } in source unit
const [limitLoading, setLimitLoading] = useState(false);
const [errorMsg, setErrorMsg] = useState(null);
const subRef = useRef(null);
const lastQueriedRef = useRef({ lat: null, lon: null, t: 0 });
const abortRef = useRef(null);
const lastOverRef = useRef(false);
// ---- CarPlay ----
const carplayTemplateRef = useRef(null);
const carplayConnectedRef = useRef(false);
const carplayLastTextRef = useRef('');
useEffect(() => {
if (!HybridAutoPlay) return;
const onConnect = () => {
carplayConnectedRef.current = true;
const tpl = new AutoPlayInformationTemplate({
title: { text: 'Speed & Limit' },
items: [
{ type: 'text', title: { text: '-- MPH' }, detailedText: { text: 'Current speed' } },
{ type: 'text', title: { text: '--' }, detailedText: { text: 'Speed limit' } },
],
});
carplayTemplateRef.current = tpl;
carplayLastTextRef.current = ''; // force the next update to push real values
tpl.setRootTemplate();
};
const onDisconnect = () => {
carplayConnectedRef.current = false;
carplayTemplateRef.current = null;
carplayLastTextRef.current = '';
};
const cleanupConnect = HybridAutoPlay.addListener('didConnect', onConnect);
const cleanupDisconnect = HybridAutoPlay.addListener('didDisconnect', onDisconnect);
// If already connected when component mounts
if (HybridAutoPlay.isConnected()) {
onConnect();
}
return () => {
cleanupConnect();
cleanupDisconnect();
};
}, []);
// Request permission + start streaming location
useEffect(() => {
let cancelled = false;
(async () => {
try {
// Check existing status first — on iOS a prior denial means we must
// send the user to Settings instead of calling request again (which
// would silently return 'denied' without showing a prompt).
const existing = await Location.getForegroundPermissionsAsync();
if (cancelled) return;
let status = existing.status;
if (status !== 'granted') {
if (existing.canAskAgain === false) {
// User previously denied and the OS won't show the prompt again
setPermStatus('denied');
setErrorMsg('Location permission was denied. Enable it in Settings to use this app.');
return;
}
const result = await Location.requestForegroundPermissionsAsync();
if (cancelled) return;
status = result.status;
}
if (status !== 'granted') {
setPermStatus('denied');
setErrorMsg('Location permission was denied. Enable it in Settings to use this app.');
return;
}
setPermStatus('granted');
const sub = await Location.watchPositionAsync(
{
accuracy: Location.Accuracy.BestForNavigation,
timeInterval: 1000,
distanceInterval: 1,
},
(loc) => {
const s = loc.coords.speed;
// expo-location returns m/s; can be negative or null if unknown
setSpeedMps(s != null && s >= 0 ? s : 0);
setAccuracyMps(loc.coords.accuracy != null ? loc.coords.accuracy : null);
const { latitude, longitude } = loc.coords;
maybeFetchLimit(latitude, longitude);
}
);
subRef.current = sub;
} catch (e) {
setErrorMsg('Could not start location updates: ' + e.message);
}
})();
return () => {
cancelled = true;
if (subRef.current) {
subRef.current.remove();
subRef.current = null;
}
if (abortRef.current) abortRef.current.abort();
};
}, []);
// Throttle: query if moved > 40 m or > 15 s since last query.
const maybeFetchLimit = useCallback(async (lat, lon) => {
const now = Date.now();
const last = lastQueriedRef.current;
if (
last.lat != null &&
haversine(last.lat, last.lon, lat, lon) < 40 &&
now - last.t < 15000
) {
return;
}
lastQueriedRef.current = { lat, lon, t: now };
try {
if (abortRef.current) abortRef.current.abort();
const controller = new AbortController();
abortRef.current = controller;
setLimitLoading(true);
const result = await fetchSpeedLimit(lat, lon, controller.signal);
setLimit(result);
} catch (e) {
if (e.name !== 'AbortError') {
// Silent failure for network issues; display will show a dash
}
} finally {
setLimitLoading(false);
}
}, []);
// Derived display values
const currentSpeed =
unit === 'mph' ? speedMps * MPS_TO_MPH : speedMps * MPS_TO_KMH;
const displayLimit = convertLimit(limit, unit);
const isOver =
displayLimit != null && currentSpeed > displayLimit + 1; // +1 tolerance
// Compute speed text color from thresholds
const getSpeedColor = () => {
if (displayLimit == null) return '#fff';
const overBy = Math.round(currentSpeed) - displayLimit;
if (overBy <= 0) return '#fff';
// Sort thresholds descending so we match the highest applicable one
const sorted = [...thresholds].sort((a, b) => b.over - a.over);
for (const t of sorted) {
if (overBy >= t.over) return t.color;
}
return '#fff';
};
const speedColor = getSpeedColor();
// Haptic feedback on crossing the limit
useEffect(() => {
if (isOver && !lastOverRef.current) {
Haptics.notificationAsync(Haptics.NotificationFeedbackType.Warning).catch(
() => {}
);
}
lastOverRef.current = isOver;
}, [isOver]);
// Update CarPlay display when speed, limit, or unit changes
useEffect(() => {
if (!carplayTemplateRef.current) return;
const unitLabel = unit === 'mph' ? 'MPH' : 'KM/H';
const hasSpeed = permStatus === 'granted';
const speedText = hasSpeed
? `${Math.round(currentSpeed)} ${unitLabel}`
: `-- ${unitLabel}`;
// CarPlay can't recolor anything, so the over-limit alert has to live in text.
const overBy = isOver ? Math.round(currentSpeed) - displayLimit : 0;
const speedSubtitle = !hasSpeed
? 'Waiting for GPS'
: isOver
? `${overBy} over the limit`
: 'Current speed';
const limitText = displayLimit != null ? `${displayLimit} ${unitLabel}` : 'Unknown';
// Only push to CarPlay when the displayed text actually changes — the head
// unit can't keep up with a per-render refresh and would flicker otherwise.
const signature = `${speedText}|${speedSubtitle}|${limitText}`;
if (signature === carplayLastTextRef.current) return;
carplayLastTextRef.current = signature;
carplayTemplateRef.current.updateItems([
{ type: 'text', title: { text: speedText }, detailedText: { text: speedSubtitle } },
{ type: 'text', title: { text: limitText }, detailedText: { text: 'Speed limit' } },
]);
});
const bgColor = '#0b0d12';
const sourceLabel = limit
? (limit.source === 'here' ? 'Limit Data: HERE' : 'Limit Data: OpenStreetMap')
: 'Limit Data: HERE (primary), OpenStreetMap (fallback)';
const { width, height } = useWindowDimensions();
const isLandscape = width > height;
// Waiting for permission — show a loading indicator
if (permStatus === 'pending') {
return (
<View style={[styles.container, { backgroundColor: '#0b0d12', justifyContent: 'center', alignItems: 'center' }]}>
<StatusBar style="light" />
<ActivityIndicator size="large" color="#fff" style={{ marginBottom: 20 }} />
<Text style={{ color: '#aaa', fontSize: 16, textAlign: 'center', paddingHorizontal: 30 }}>
Waiting for location permission…
</Text>
</View>
);
}
// Permission denied — show a full-screen message with an Open Settings button
if (permStatus === 'denied') {
return (
<View style={[styles.container, { backgroundColor: '#0b0d12', justifyContent: 'center', alignItems: 'center', paddingHorizontal: 30 }]}>
<StatusBar style="light" />
<Text style={{ color: '#fff', fontSize: 22, fontWeight: '700', textAlign: 'center', marginBottom: 16 }}>
Location Permission Required
</Text>
<Text style={{ color: '#aaa', fontSize: 15, textAlign: 'center', lineHeight: 22, marginBottom: 30 }}>
Speed Limit needs access to your location to show your current speed and the posted speed limit. Please enable location access in Settings.
</Text>
<TouchableOpacity
style={{ backgroundColor: '#2563eb', paddingVertical: 14, paddingHorizontal: 36, borderRadius: 12 }}
onPress={() => Linking.openSettings()}
>
<Text style={{ color: '#fff', fontSize: 17, fontWeight: '600' }}>Open Settings</Text>
</TouchableOpacity>
</View>
);
}
return (
<View style={[styles.container, { backgroundColor: '#0b0d12' }, isLandscape && styles.containerLandscape]}>
<StatusBar style="light" />
{isLandscape ? (
<View style={styles.landscapeBody}>
<View style={[styles.landscapeLeftBase, { backgroundColor: '#0b0d12' }]}>
<Text style={styles.label}>CURRENT SPEED</Text>
<Text style={[styles.speedValueLandscape, { color: speedColor }]}>
{permStatus === 'granted' ? Math.round(currentSpeed) : '--'}
</Text>
</View>
<View style={styles.landscapeRight}>
<View style={styles.landscapeSignBorder}>
<Text style={[styles.label, { color: '#666' }]}>SPEED LIMIT</Text>
{limitLoading && displayLimit == null ? (
<ActivityIndicator color="#000" size="large" />
) : (
<Text style={styles.limitValueLandscape}>
{displayLimit != null ? displayLimit : '--'}
</Text>
)}
</View>
</View>
</View>
) : (
<>
<View style={[styles.centerBlock, { backgroundColor: '#0b0d12' }]}>
<Text style={styles.label}>CURRENT SPEED</Text>
<Text style={[styles.speedValue, { color: speedColor }]}>
{permStatus === 'granted' ? Math.round(currentSpeed) : '--'}
</Text>
</View>
<View style={styles.limitBlockWhite}>
<View style={styles.signBorder}>
<Text style={[styles.label, { color: '#666' }]}>SPEED LIMIT</Text>
{limitLoading && displayLimit == null ? (
<ActivityIndicator color="#000" size="large" />
) : (
<Text style={styles.limitValuePlain}>
{displayLimit != null ? displayLimit : '--'}
</Text>
)}
</View>
</View>
</>
)}
<View style={[styles.footer, isLandscape && styles.footerLandscape, !isLandscape && { backgroundColor: '#fff', paddingHorizontal: 20 }]}>
<View style={styles.footerRow}>
<TouchableOpacity
style={[styles.unitBtn, !isLandscape && styles.unitBtnLight]}
onPress={() => setUnit((u) => (u === 'mph' ? 'kmh' : 'mph'))}
>
<Text style={[styles.unitBtnText, !isLandscape && { color: '#000' }]}>
{unit === 'mph' ? 'MPH' : 'KM/H'}
</Text>
</TouchableOpacity>
<View style={styles.footerCenter}>
<Text style={[styles.footerText, !isLandscape && { color: '#333' }]}>
{accuracyMps != null
? 'GPS +/- ' + Math.round(accuracyMps) + ' m'
: 'Acquiring GPS...'}
</Text>
<Text style={[styles.footerTextDim, !isLandscape && { color: '#999' }]}>{sourceLabel}</Text>
</View>
<TouchableOpacity
onPress={() => {
setEditThresholds([...thresholds]);
setSettingsVisible(true);
}}
>
<Text style={[styles.gearText, !isLandscape && { color: '#000' }]}>
{'\u2699'}
</Text>
</TouchableOpacity>
</View>
</View>
<Modal
visible={settingsVisible}
animationType="slide"
transparent={true}
supportedOrientations={['portrait', 'landscape', 'landscape-left', 'landscape-right']}
onRequestClose={() => setSettingsVisible(false)}
>
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
style={styles.modalOverlay}
>
<View style={styles.modalContent}>
<Text style={styles.modalTitle}>Speed Alert Thresholds</Text>
<Text style={styles.modalSubtitle}>
{unit === 'mph' ? 'MPH' : 'KM/H'} over the limit
</Text>
{editThresholds.map((t, i) => (
<View key={i} style={styles.thresholdRow}>
<View style={[styles.colorSwatch, { backgroundColor: t.color }]} />
<Text style={styles.thresholdLabel}>+</Text>
<TextInput
style={styles.thresholdInput}
keyboardType="number-pad"
value={String(t.over)}
onChangeText={(val) => {
const num = parseInt(val, 10);
const updated = [...editThresholds];
updated[i] = { ...updated[i], over: isNaN(num) ? 0 : num };
setEditThresholds(updated);
}}
/>
<Text style={styles.thresholdLabel}>{unit === 'mph' ? 'MPH' : 'KM/H'} over</Text>
</View>
))}
<View style={styles.modalButtons}>
<TouchableOpacity
style={[styles.modalBtn, { backgroundColor: '#666' }]}
onPress={() => setSettingsVisible(false)}
>
<Text style={styles.modalBtnText}>Cancel</Text>
</TouchableOpacity>
<TouchableOpacity
style={[styles.modalBtn, { backgroundColor: '#2563eb' }]}
onPress={() => {
const sorted = [...editThresholds].sort((a, b) => a.over - b.over);
saveThresholds(sorted);
setSettingsVisible(false);
}}
>
<Text style={styles.modalBtnText}>Save</Text>
</TouchableOpacity>
</View>
</View>
</KeyboardAvoidingView>
</Modal>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: 0,
paddingHorizontal: 0,
paddingBottom: 0,
},
footerRow: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
width: '100%',
},
footerCenter: {
flex: 1,
alignItems: 'center',
},
unitBtnLight: {
borderColor: '#000',
},
unitBtn: {
borderWidth: 1.5,
borderColor: '#fff',
paddingVertical: 5,
paddingHorizontal: 12,
borderRadius: 8,
},
unitBtnText: {
color: '#fff',
fontSize: 14,
fontWeight: '700',
textAlign: 'center',
},
gearText: {
color: '#ccc',
fontSize: 22,
},
unitBtnSub: {
color: '#aaa',
fontSize: 10,
textAlign: 'center',
marginTop: 2,
},
centerBlock: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
paddingHorizontal: 20,
},
label: {
color: '#aaa',
fontSize: 14,
letterSpacing: 3,
marginBottom: 6,
},
speedValue: {
color: '#fff',
fontSize: 160,
fontWeight: '800',
lineHeight: 160,
includeFontPadding: false,
},
limitBlockWhite: {
flex: 1,
backgroundColor: '#fff',
justifyContent: 'center',
alignItems: 'center',
padding: 16,
},
signBorder: {
borderWidth: 4,
borderColor: '#000',
borderRadius: 8,
paddingVertical: 8,
paddingHorizontal: 20,
alignItems: 'center',
justifyContent: 'center',
flex: 1,
width: '100%',
},
limitValuePlain: {
color: '#000',
fontSize: 140,
fontWeight: '800',
lineHeight: 140,
includeFontPadding: false,
},
footer: {
paddingTop: 10,
paddingBottom: Platform.OS === 'ios' ? 30 : 16,
},
footerLandscape: {
paddingTop: 6,
paddingBottom: 6,
paddingHorizontal: 16,
},
footerText: {
color: '#ddd',
fontSize: 13,
},
footerTextDim: {
color: '#777',
fontSize: 11,
marginTop: 4,
textAlign: 'center',
},
errorText: {
color: '#ffb4b4',
fontSize: 13,
textAlign: 'center',
},
// Landscape styles
containerLandscape: {
paddingTop: 0,
paddingBottom: 0,
},
landscapeBody: {
flex: 1,
flexDirection: 'row',
alignItems: 'stretch',
justifyContent: 'center',
},
landscapeLeftBase: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
alignSelf: 'stretch',
},
landscapeRight: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
alignSelf: 'stretch',
backgroundColor: '#fff',
padding: 16,
borderLeftWidth: 2,
borderLeftColor: '#ccc',
},
landscapeSignBorder: {
borderWidth: 4,
borderColor: '#000',
borderRadius: 8,
paddingVertical: 8,
paddingHorizontal: 20,
alignItems: 'center',
justifyContent: 'center',
flex: 1,
width: '100%',
},
speedValueLandscape: {
color: '#fff',
fontSize: 120,
fontWeight: '800',
lineHeight: 120,
includeFontPadding: false,
},
limitValueLandscape: {
color: '#000',
fontSize: 120,
fontWeight: '800',
lineHeight: 120,
includeFontPadding: false,
},
modalOverlay: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'rgba(0,0,0,0.6)',
},
modalContent: {
backgroundColor: '#1a1d24',
borderRadius: 16,
padding: 24,
width: '85%',
maxWidth: 400,
alignItems: 'center',
},
modalTitle: {
color: '#fff',
fontSize: 20,
fontWeight: '700',
marginBottom: 4,
},
modalSubtitle: {
color: '#999',
fontSize: 13,
marginBottom: 20,
},
thresholdRow: {
flexDirection: 'row',
alignItems: 'center',
marginBottom: 14,
gap: 8,
},
colorSwatch: {
width: 28,
height: 28,
borderRadius: 6,
borderWidth: 1,
borderColor: '#555',
},
thresholdLabel: {
color: '#ccc',
fontSize: 16,
},
thresholdInput: {
backgroundColor: '#2a2d34',
color: '#fff',
fontSize: 18,
fontWeight: '700',
width: 50,
textAlign: 'center',
paddingVertical: 6,
borderRadius: 8,
borderWidth: 1,
borderColor: '#444',
},
modalButtons: {
flexDirection: 'row',
gap: 12,
marginTop: 16,
},
modalBtn: {
paddingVertical: 12,
paddingHorizontal: 28,
borderRadius: 10,
},
modalBtnText: {
color: '#fff',
fontSize: 16,
fontWeight: '600',
},
});