Repository navigation
Expand file tree
/
Copy pathApp.tsx
More file actions
617 lines (588 loc) · 17.9 KB
/
Copy pathApp.tsx
File metadata and controls
617 lines (588 loc) · 17.9 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
import {
useEffect,
useState,
useCallback,
useRef,
type ComponentRef,
} from 'react';
import {
Text,
View,
StyleSheet,
TouchableOpacity,
SafeAreaView,
StatusBar,
ScrollView,
} from 'react-native';
import {
getThermalState,
onThermalStateChange,
restart,
requestReview,
StoreProduct,
StoreOverlay,
activate,
deactivate,
showColorPicker,
openMail,
Toast,
Alert,
Confetti,
Translation,
type ThermalState,
} from 'react-native-tinykit';
const STORE_APP_ID = '375380948';
export default function App() {
const [thermalState, setThermalState] = useState<ThermalState>(() =>
getThermalState()
);
const [keepAwake, setKeepAwake] = useState(false);
const translationTarget = useRef<ComponentRef<typeof Text>>(null);
const [translationText, setTranslationText] = useState('Hello, world! 👋');
const [translationStatus, setTranslationStatus] = useState('');
const [translating, setTranslating] = useState(false);
const translationSupported = Translation.isSupported();
const [storeProductBusy, setStoreProductBusy] = useState(false);
const [storeProductStatus, setStoreProductStatus] = useState('');
const [storeOverlayBusy, setStoreOverlayBusy] = useState(false);
const [storeOverlayStatus, setStoreOverlayStatus] = useState('');
const handlePresentStoreProduct = async () => {
setStoreProductBusy(true);
setStoreProductStatus('Loading App Store page...');
try {
await StoreProduct.present({ appId: STORE_APP_ID });
setStoreProductStatus('App Store page closed');
} catch (error) {
setStoreProductStatus(
error instanceof Error ? error.message : String(error)
);
} finally {
setStoreProductBusy(false);
}
};
const handlePresentStoreOverlay = async (
position: 'bottom' | 'bottomRaised'
) => {
setStoreOverlayBusy(true);
setStoreOverlayStatus('Presenting App Store overlay...');
try {
await StoreOverlay.present({ appId: STORE_APP_ID, position });
setStoreOverlayStatus('App Store overlay presented');
} catch (error) {
setStoreOverlayStatus(
error instanceof Error ? error.message : String(error)
);
} finally {
setStoreOverlayBusy(false);
}
};
const handleTranslate = async () => {
setTranslating(true);
try {
const result = await Translation.present({
text: translationText,
targetViewNode: translationTarget.current,
allowsReplacement: true,
});
if (result.status === 'replaced' && result.translatedText !== undefined) {
setTranslationText(result.translatedText);
}
setTranslationStatus(result.status);
} catch (error) {
setTranslationStatus(
error instanceof Error ? error.message : String(error)
);
} finally {
setTranslating(false);
}
};
const handleToggleKeepAwake = useCallback(() => {
if (keepAwake) {
deactivate();
} else {
activate();
}
setKeepAwake((prev) => !prev);
}, [keepAwake]);
useEffect(() => {
const subscription = onThermalStateChange((state) => {
console.log('Thermal state changed:', state);
setThermalState(state);
});
return () => {
subscription.remove();
};
}, []);
const handleRestart = () => {
restart();
};
const handleGetThermalState = () => {
const state = getThermalState();
console.log('Manual get thermal state:', state);
setThermalState(state);
};
const handleRequestReview = async () => {
try {
console.log('Requesting review...');
await requestReview();
console.log('Review request completed');
} catch (error) {
console.error('Review request failed:', error);
}
};
const handleShowColorPicker = async () => {
try {
const result = await showColorPicker({
selectedColor: '#007AFF',
supportsAlpha: true,
supportsEyedropper: true,
maximumLinearExposure: 1,
title: 'Pick a Color',
showDoneButton: true,
doneButtonTitle: 'Apply',
detents: [
{ type: 'medium', identifier: 'compact', height: 420 },
{ type: 'large' },
],
selectedDetentIdentifier: 'compact',
prefersGrabberVisible: true,
});
console.log('Selected color:', result);
} catch (error) {
console.error('Color picker failed:', error);
}
};
const handleOpenMail = async () => {
try {
const result = await openMail({
subject: 'TinyKit feedback',
recipients: ['support@example.com'],
body: '<p>Hello from the TinyKit example app.</p>',
isHTML: true,
});
console.log('Mail composer result:', result);
} catch (error) {
console.error('Mail composer failed:', error);
}
};
const thermalColors: Record<ThermalState, string> = {
nominal: '#4CAF50',
fair: '#FFC107',
serious: '#FF9800',
critical: '#F44336',
};
return (
<SafeAreaView style={styles.container}>
<ScrollView>
<StatusBar barStyle="dark-content" />
<View style={styles.header}>
<Text style={styles.title}>Tinykit Example</Text>
</View>
<View style={styles.content}>
<View style={styles.section}>
<Text style={styles.label}>Thermal State API</Text>
<View
style={[
styles.stateBadge,
{ backgroundColor: thermalColors[thermalState] },
]}
>
<Text style={styles.stateText}>{thermalState.toUpperCase()}</Text>
</View>
<Text style={styles.description}>
The thermal state indicates the current thermal condition of the
device.
</Text>
<TouchableOpacity
style={[styles.button, styles.thermalButton]}
onPress={handleGetThermalState}
activeOpacity={0.7}
>
<Text style={styles.buttonText}>Get Thermal State</Text>
</TouchableOpacity>
</View>
<View style={styles.section}>
<Text style={styles.label}>Restart API</Text>
<TouchableOpacity
style={[styles.button, styles.restartButton]}
onPress={handleRestart}
activeOpacity={0.7}
>
<Text style={styles.buttonText}>Restart Application</Text>
</TouchableOpacity>
<Text style={styles.hint}>
This will trigger a bundle reload of the React Native application.
</Text>
</View>
<View style={styles.section}>
<Text style={styles.label}>Review API</Text>
<TouchableOpacity
style={[styles.button, styles.reviewButton]}
onPress={handleRequestReview}
activeOpacity={0.7}
>
<Text style={styles.buttonText}>Request Review</Text>
</TouchableOpacity>
</View>
<View style={styles.section}>
<Text style={styles.label}>App Store Product Page</Text>
<TouchableOpacity
style={styles.button}
disabled={storeProductBusy}
onPress={handlePresentStoreProduct}
activeOpacity={0.7}
>
<Text style={styles.buttonText}>Show App Store Page</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => {
StoreProduct.dismiss().catch((error) => {
setStoreProductStatus(
error instanceof Error ? error.message : String(error)
);
});
}}
activeOpacity={0.7}
>
<Text style={styles.buttonText}>
Dismiss Page / Cancel Loading
</Text>
</TouchableOpacity>
<Text style={styles.hint}>
Loads the Apple Store app page. The Promise resolves when it
closes.
</Text>
{storeProductStatus ? (
<Text style={styles.description}>{storeProductStatus}</Text>
) : null}
</View>
<View style={styles.section}>
<Text style={styles.label}>App Store Overlay</Text>
<TouchableOpacity
style={styles.button}
disabled={storeOverlayBusy}
onPress={() => {
handlePresentStoreOverlay('bottom');
}}
activeOpacity={0.7}
>
<Text style={styles.buttonText}>Show Bottom Overlay</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
disabled={storeOverlayBusy}
onPress={() => {
handlePresentStoreOverlay('bottomRaised');
}}
activeOpacity={0.7}
>
<Text style={styles.buttonText}>Show Raised Overlay</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => {
StoreOverlay.dismiss()
.then(() => setStoreOverlayStatus('Dismissal requested'))
.catch((error) => {
setStoreOverlayStatus(
error instanceof Error ? error.message : String(error)
);
});
}}
activeOpacity={0.7}
>
<Text style={styles.buttonText}>Dismiss Store Overlay</Text>
</TouchableOpacity>
<Text style={styles.hint}>
Requires a physical device. The Promise resolves after
presentation.
</Text>
{storeOverlayStatus ? (
<Text style={styles.description}>{storeOverlayStatus}</Text>
) : null}
</View>
<View style={styles.section}>
<Text style={styles.label}>Color Picker API</Text>
<TouchableOpacity
style={[styles.button, styles.colorPickerButton]}
onPress={handleShowColorPicker}
activeOpacity={0.7}
>
<Text style={styles.buttonText}>Show Color Picker</Text>
</TouchableOpacity>
</View>
<View style={styles.section}>
<Text style={styles.label}>Keep Awake API</Text>
<View
style={[
styles.stateBadge,
keepAwake ? styles.keepAwakeStateOn : styles.keepAwakeStateOff,
]}
>
<Text style={styles.stateText}>{keepAwake ? 'ON' : 'OFF'}</Text>
</View>
<TouchableOpacity
style={[
styles.button,
keepAwake
? styles.keepAwakeOffButton
: styles.keepAwakeOnButton,
]}
onPress={handleToggleKeepAwake}
activeOpacity={0.7}
>
<Text style={styles.buttonText}>
{keepAwake ? 'Disable Keep Awake' : 'Enable Keep Awake'}
</Text>
</TouchableOpacity>
<Text style={styles.hint}>
Prevents the screen from auto-locking when enabled.
</Text>
</View>
<View style={styles.section}>
<Text style={styles.label}>Mail Composer API</Text>
<TouchableOpacity
style={[styles.button, styles.mailButton]}
onPress={handleOpenMail}
activeOpacity={0.7}
>
<Text style={styles.buttonText}>Compose Email</Text>
</TouchableOpacity>
<Text style={styles.hint}>
Requires a Mail account configured on the device.
</Text>
</View>
<View style={styles.section}>
<Text style={styles.label}>Toast API</Text>
<TouchableOpacity
style={styles.button}
onPress={() => {
Toast.show({
title: 'Saved',
message: 'Your changes are ready',
icon: 'done',
haptic: 'success',
}).catch(console.error);
}}
>
<Text style={styles.buttonText}>Show Toast</Text>
</TouchableOpacity>
</View>
<View style={styles.section}>
<Text style={styles.label}>Alert API</Text>
<TouchableOpacity
style={styles.button}
onPress={() => {
Alert.show({
title: 'Done',
icon: 'heart',
haptic: 'success',
duration: 2000,
}).catch(console.error);
}}
>
<Text style={styles.buttonText}>Show Alert</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => {
Alert.show({
title: 'Loading',
icon: 'spinner',
duration: 0,
}).catch(console.error);
}}
>
<Text style={styles.buttonText}>Show Persistent Spinner</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => {
Alert.dismissAll().catch(console.error);
}}
>
<Text style={styles.buttonText}>Dismiss Alerts</Text>
</TouchableOpacity>
</View>
<View style={styles.section}>
<Text style={styles.label}>Confetti API</Text>
<TouchableOpacity
style={styles.button}
onPress={() => {
Confetti.start({ duration: 5000 }).catch(console.error);
}}
>
<Text style={styles.buttonText}>Start Confetti</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => {
Confetti.stop().catch(console.error);
}}
>
<Text style={styles.buttonText}>Stop Confetti</Text>
</TouchableOpacity>
</View>
</View>
<View style={styles.section}>
<Text style={styles.label}>Translation API</Text>
<Text ref={translationTarget} style={styles.description}>
{translationText}
</Text>
<TouchableOpacity
style={styles.button}
onPress={handleTranslate}
disabled={!translationSupported || translating}
accessibilityState={{
disabled: !translationSupported || translating,
}}
>
<Text style={styles.buttonText}>
{translating ? 'Translating…' : 'Translate Text'}
</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => {
setTranslationText('Hello, world! 👋');
setTranslationStatus('');
}}
disabled={translating}
>
<Text style={styles.buttonText}>Reset Text</Text>
</TouchableOpacity>
<Text style={styles.hint}>
{translationSupported
? 'Choose Replace Translation to update the text above.'
: 'Requires a physical iPhone or iPad running iOS 17.4 or later.'}
</Text>
<Text accessibilityLiveRegion="polite" style={styles.hint}>
{translationStatus}
</Text>
</View>
<View style={styles.footer}>
<Text style={styles.version}>react-native-tinykit v0.1.0</Text>
</View>
</ScrollView>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F8F9FA',
},
header: {
paddingVertical: 20,
backgroundColor: '#FFF',
borderBottomWidth: 1,
borderBottomColor: '#EEEEEE',
alignItems: 'center',
shadowColor: '#000',
shadowOffset: { width: 0, height: 1 },
shadowOpacity: 0.05,
shadowRadius: 2,
elevation: 2,
},
title: {
fontSize: 20,
fontWeight: '700',
color: '#1A1A1A',
},
content: {
flex: 1,
padding: 20,
},
section: {
marginBottom: 24,
padding: 24,
backgroundColor: '#FFF',
borderRadius: 16,
alignItems: 'center',
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.05,
shadowRadius: 8,
elevation: 3,
},
label: {
fontSize: 14,
fontWeight: '600',
color: '#666',
textTransform: 'uppercase',
letterSpacing: 1,
marginBottom: 16,
},
stateBadge: {
paddingHorizontal: 24,
paddingVertical: 12,
borderRadius: 12,
marginBottom: 16,
},
stateText: {
color: '#FFF',
fontSize: 24,
fontWeight: '800',
},
description: {
fontSize: 14,
color: '#8E8E93',
textAlign: 'center',
lineHeight: 20,
},
button: {
backgroundColor: '#007AFF',
paddingHorizontal: 24,
paddingVertical: 16,
borderRadius: 12,
width: '100%',
alignItems: 'center',
marginBottom: 12,
},
thermalButton: {
marginTop: 16,
},
restartButton: {
backgroundColor: '#FF3B30',
},
reviewButton: {
backgroundColor: '#007AFF',
},
colorPickerButton: {
backgroundColor: '#34C759',
},
keepAwakeStateOn: {
backgroundColor: '#4CAF50',
},
keepAwakeStateOff: {
backgroundColor: '#9E9E9E',
},
keepAwakeOnButton: {
backgroundColor: '#34C759',
},
keepAwakeOffButton: {
backgroundColor: '#FF3B30',
},
mailButton: {
backgroundColor: '#5856D6',
},
buttonText: {
color: '#FFF',
fontSize: 16,
fontWeight: '600',
},
hint: {
fontSize: 12,
color: '#AEAEB2',
textAlign: 'center',
},
footer: {
padding: 20,
alignItems: 'center',
},
version: {
fontSize: 12,
color: '#C7C7CC',
},
});