|
| 1 | +import { useState } from 'react'; |
| 2 | +import { Pressable, StyleSheet, Text, View } from 'react-native'; |
| 3 | +import { useSafeAreaInsets } from 'react-native-safe-area-context'; |
| 4 | +import { EaseView } from 'react-native-ease'; |
| 5 | + |
| 6 | +// Audit finding M4: a loop cancelled through the all-'none' transition path |
| 7 | +// resurrects when the view re-attaches to the window (e.g. after a tab |
| 8 | +// switch), because the saved loop snapshot was not cleared on cancel. |
| 9 | +// |
| 10 | +// Steps to reproduce: |
| 11 | +// 1. Observe the spinner — it should loop continuously. |
| 12 | +// 2. Press "Cancel loop". The spinner stops. |
| 13 | +// 3. Switch to the "Other" tab, then come back. |
| 14 | +// 4. Without the fix, the spinner is spinning again even though the |
| 15 | +// transition is still 'none'. With the fix, it stays stopped. |
| 16 | + |
| 17 | +export default function LoopCancelTab() { |
| 18 | + const insets = useSafeAreaInsets(); |
| 19 | + const [cancelled, setCancelled] = useState(false); |
| 20 | + const [mountKey, setMountKey] = useState(0); |
| 21 | + |
| 22 | + return ( |
| 23 | + <View style={[styles.root, { paddingTop: insets.top + 16 }]}> |
| 24 | + <Text style={styles.heading}>Cancelled loop reproducer</Text> |
| 25 | + <Text style={styles.body}> |
| 26 | + Press Cancel loop, switch to the Other tab and come back. The spinner |
| 27 | + must stay stopped. |
| 28 | + </Text> |
| 29 | + <Text style={styles.status}> |
| 30 | + transition: {cancelled ? "'none' (cancelled)" : 'timing + loop'} |
| 31 | + </Text> |
| 32 | + |
| 33 | + <View style={styles.demo}> |
| 34 | + <EaseView |
| 35 | + key={mountKey} |
| 36 | + initialAnimate={{ rotate: 0 }} |
| 37 | + animate={{ rotate: 360 }} |
| 38 | + transition={ |
| 39 | + cancelled |
| 40 | + ? { type: 'none' } |
| 41 | + : { |
| 42 | + type: 'timing', |
| 43 | + duration: 1000, |
| 44 | + easing: 'linear', |
| 45 | + loop: 'repeat', |
| 46 | + } |
| 47 | + } |
| 48 | + style={styles.box} |
| 49 | + > |
| 50 | + <View style={styles.indicator} /> |
| 51 | + </EaseView> |
| 52 | + </View> |
| 53 | + |
| 54 | + <View style={styles.buttons}> |
| 55 | + <Pressable |
| 56 | + style={[styles.button, cancelled && styles.buttonDisabled]} |
| 57 | + disabled={cancelled} |
| 58 | + onPress={() => setCancelled(true)} |
| 59 | + > |
| 60 | + <Text style={styles.buttonText}>Cancel loop</Text> |
| 61 | + </Pressable> |
| 62 | + <Pressable |
| 63 | + style={styles.button} |
| 64 | + onPress={() => { |
| 65 | + setCancelled(false); |
| 66 | + setMountKey((k) => k + 1); |
| 67 | + }} |
| 68 | + > |
| 69 | + <Text style={styles.buttonText}>Restart (remount)</Text> |
| 70 | + </Pressable> |
| 71 | + </View> |
| 72 | + </View> |
| 73 | + ); |
| 74 | +} |
| 75 | + |
| 76 | +const styles = StyleSheet.create({ |
| 77 | + root: { |
| 78 | + flex: 1, |
| 79 | + backgroundColor: '#1a1a2e', |
| 80 | + paddingHorizontal: 20, |
| 81 | + }, |
| 82 | + heading: { |
| 83 | + fontSize: 22, |
| 84 | + fontWeight: '700', |
| 85 | + color: '#fff', |
| 86 | + marginBottom: 8, |
| 87 | + }, |
| 88 | + body: { |
| 89 | + fontSize: 14, |
| 90 | + color: '#aaaacc', |
| 91 | + marginBottom: 8, |
| 92 | + lineHeight: 20, |
| 93 | + }, |
| 94 | + status: { |
| 95 | + fontSize: 12, |
| 96 | + fontFamily: 'monospace', |
| 97 | + color: '#6666aa', |
| 98 | + marginBottom: 32, |
| 99 | + }, |
| 100 | + demo: { |
| 101 | + alignItems: 'center', |
| 102 | + marginBottom: 32, |
| 103 | + }, |
| 104 | + box: { |
| 105 | + width: 80, |
| 106 | + height: 80, |
| 107 | + backgroundColor: '#4a90d9', |
| 108 | + borderRadius: 12, |
| 109 | + alignItems: 'center', |
| 110 | + justifyContent: 'flex-start', |
| 111 | + paddingTop: 8, |
| 112 | + }, |
| 113 | + indicator: { |
| 114 | + width: 12, |
| 115 | + height: 12, |
| 116 | + borderRadius: 6, |
| 117 | + backgroundColor: '#fff', |
| 118 | + }, |
| 119 | + buttons: { |
| 120 | + flexDirection: 'row', |
| 121 | + gap: 12, |
| 122 | + justifyContent: 'center', |
| 123 | + }, |
| 124 | + button: { |
| 125 | + paddingHorizontal: 20, |
| 126 | + paddingVertical: 12, |
| 127 | + borderRadius: 10, |
| 128 | + backgroundColor: '#16213e', |
| 129 | + }, |
| 130 | + buttonDisabled: { |
| 131 | + opacity: 0.4, |
| 132 | + }, |
| 133 | + buttonText: { |
| 134 | + color: '#e0e0ff', |
| 135 | + fontSize: 15, |
| 136 | + fontWeight: '600', |
| 137 | + }, |
| 138 | +}); |
0 commit comments