|
| 1 | +import React, {useState, useEffect} from 'react'; |
| 2 | +import {View, StyleSheet, Image, TouchableOpacity} from 'react-native'; |
| 3 | +import Torch from 'react-native-torch'; |
| 4 | +import RNShake from 'react-native-shake'; |
| 5 | + |
| 6 | +const App = () => { |
| 7 | + const [toggle, setToggle] = useState(false); |
| 8 | + |
| 9 | + const handleChangeToggle = () => setToggle(oldToggle => !oldToggle); |
| 10 | + |
| 11 | + useEffect(() => { |
| 12 | + Torch.switchState(toggle); |
| 13 | + }, [toggle]); |
| 14 | + |
| 15 | + useEffect(() => { |
| 16 | + //Quando chacoalhado o celular muda-se o toggle |
| 17 | + const subscription = RNShake.addListener(() => { |
| 18 | + setToggle(oldToggle => !oldToggle); |
| 19 | + }); |
| 20 | + //Função será chamada quando componente for desmontado |
| 21 | + return () => subscription.remove(); |
| 22 | + }, []); |
| 23 | + |
| 24 | + return ( |
| 25 | + <View style={toggle ? style.containerLight : style.container}> |
| 26 | + <TouchableOpacity onPress={handleChangeToggle}> |
| 27 | + <Image |
| 28 | + style={toggle ? style.lightingOn : style.lightingOff} |
| 29 | + source={ |
| 30 | + toggle |
| 31 | + ? require('./assets/icons/eco-light.png') |
| 32 | + : require('./assets/icons/eco-light-off.png') |
| 33 | + } |
| 34 | + /> |
| 35 | + <Image |
| 36 | + style={style.dioLogo} |
| 37 | + source={ |
| 38 | + toggle |
| 39 | + ? require('./assets/icons/logo-dio.png') |
| 40 | + : require('./assets/icons/logo-dio-white.png') |
| 41 | + } |
| 42 | + /> |
| 43 | + </TouchableOpacity> |
| 44 | + </View> |
| 45 | + ); |
| 46 | +}; |
| 47 | + |
| 48 | +export default App; |
| 49 | + |
| 50 | +const style = StyleSheet.create({ |
| 51 | + container: { |
| 52 | + flex: 1, |
| 53 | + backgroundColor: 'black', |
| 54 | + alignItems: 'center', |
| 55 | + justifyContent: 'center', |
| 56 | + }, |
| 57 | + containerLight: { |
| 58 | + flex: 1, |
| 59 | + backgroundColor: 'white', |
| 60 | + alignItems: 'center', |
| 61 | + justifyContent: 'center', |
| 62 | + }, |
| 63 | + lightingOn: { |
| 64 | + resizeMode: 'contain', |
| 65 | + alignSelf: 'center', |
| 66 | + width: 150, |
| 67 | + height: 150, |
| 68 | + }, |
| 69 | + lightingOff: { |
| 70 | + resizeMode: 'contain', |
| 71 | + alignSelf: 'center', |
| 72 | + tintColor: 'white', |
| 73 | + width: 150, |
| 74 | + height: 150, |
| 75 | + }, |
| 76 | + dioLogo: { |
| 77 | + resizeMode: 'contain', |
| 78 | + alignSelf: 'center', |
| 79 | + width: 250, |
| 80 | + height: 250, |
| 81 | + }, |
| 82 | +}); |
0 commit comments