[text] React

Viewer

  1. import React, {useEffect, useRef, useState} from 'react';
  2. import {
  3.   Text,
  4.   View,
  5.   StyleSheet,
  6.   ScrollView,
  7.   SafeAreaView,
  8.   TouchableOpacity,
  9.   Touchable,
  10.   Linking,
  11.   Dimensions,
  12.   Share,
  13. } from 'react-native';
  14. import {theme, theme as Theme} from '../constants/theme';
  15. import CustomHeader from '../components/CustomHeader';
  16. import {apiRadioBase} from '../api/endpoints';
  17. import {
  18.   API_RADIO_STREAM_URL,
  19.   URL_PROGRAMS,
  20.   URL_PROGRAMMING,
  21. } from '../api/index';
  22. import Marquee from '../components/shared/marquee';
  23. import Icon from '../components/shared/Icon';
  24. import TrackPlayer, {
  25.   Capability,
  26.   State,
  27.   usePlaybackState,
  28. } from 'react-native-track-player';
  29.  
  30. interface RadioProps {
  31.   navigation: any;
  32. }
  33.  
  34. export function Radio(props: RadioProps) {
  35.   const {navigation} = props;
  36.   const [loadPolling, setLoadingPolling] = useState<any>([]);
  37.   const [loadPub, setLoadPub] = useState<any>([]);
  38.  
  39.   let polling = loadPolling.live;
  40.   let pub = loadPub.pub;
  41.  
  42.   const windowHeight = Dimensions.get('window').height;
  43.  
  44.   const uri = API_RADIO_STREAM_URL;
  45.   const [audioStatus, setAudioStatus] = useState(false);
  46.   const [data, setData] = useState([]);
  47.   const playbackState = usePlaybackState();
  48.  
  49.   //state to manage whether track player is initialized or not
  50.   const [isTrackPlayerInit, setIsTrackPlayerInit] = useState(false);
  51.   const [isPlaying, setIsPlaying] = useState(false);
  52.  
  53.   const stream = {
  54.     'id': '1',
  55.     'url': uri,
  56.     'artwork': require('../assets/images/icon_radio.png'),
  57.     'artist': 'Observador',
  58.     'title': polling ? polling : 'Aconteça o que acontecer',
  59.   };
  60.  
  61.   // Share
  62.   const onShare = async () => {
  63.     try {
  64.       const result = await Share.share({
  65.         message:
  66.           'Oiça a Rádio Observador em https://observador.pt/radio/. Acontença o que acontecer.',
  67.       });
  68.     } catch (error) {
  69.       console.log(error);
  70.     }
  71.   };
  72.  
  73.   const trackPlayerInit = async () => {
  74.     await TrackPlayer.setupPlayer();
  75.     await TrackPlayer.add(stream);
  76.   };
  77.  
  78.   useEffect(() => {
  79.     trackPlayerInit();
  80.   }, []);
  81.  
  82.   const musicPlay = async () => {
  83.     if (!isPlaying) {
  84.       await TrackPlayer.play();
  85.       setIsPlaying(true);
  86.     } else if (isPlaying) {
  87.       await TrackPlayer.reset();
  88.       await TrackPlayer.stop();
  89.       setIsPlaying(false);
  90.     } else {
  91.       TrackPlayer.destroy;
  92.     }
  93.   };
  94.  
  95.   // Loop Polling and Pub
  96.   useEffect(() => {
  97.     getPolling();
  98.     getPub();
  99.     const intervalId = setInterval(() => {
  100.       getPolling();
  101.     }, 10000);
  102.     return () => clearInterval(intervalId);
  103.   }, []);
  104.  
  105.   // Get Polling
  106.   const getPolling = async () => {
  107.     const response = await apiRadioBase
  108.       .get('/live')
  109.       .then(response => {
  110.         setLoadingPolling(response.data);
  111.         return response.data;
  112.       })
  113.       .catch(err => {
  114.         console.log('Erro Pooling', err);
  115.       });
  116.   };
  117.  
  118.   // Get Pub
  119.   const getPub = async () => {
  120.     const response = await apiRadioBase
  121.       .get('/pub')
  122.       .then(response => {
  123.         setLoadPub(response.data);
  124.         return response.data;
  125.       })
  126.       .catch(err => {
  127.         console.log('Erro Pub', err);
  128.       });
  129.   };
  130.  
  131.   return (
  132.     <SafeAreaView style={styles.safeArea}>
  133.       <CustomHeader
  134.         isHome={false}
  135.         navigation={navigation}
  136.         menuColor={Theme.colors.white}
  137.       />
  138.       <View style={styles.container}>
  139.         <View
  140.           style={{
  141.             marginTop: 32,
  142.             flexDirection: 'row',
  143.             width: '100%',
  144.             justifyContent: 'center',
  145.             alignItems: 'center',
  146.             paddingHorizontal: 20,
  147.           }}>
  148.           {!isPlaying ? (
  149.             <Text
  150.               style={{
  151.                 fontFamily: theme.fonts.halyardRegular,
  152.                 fontSize: 18,
  153.                 color: theme.colors.white,
  154.               }}>
  155.               Acontença o que acontecer
  156.             </Text>
  157.           ) : windowHeight < 569 || polling.length > 40 ? (
  158.             <Marquee>
  159.               <Text
  160.                 style={{
  161.                   fontFamily: theme.fonts.halyardRegular,
  162.                   fontSize: 18,
  163.                   color: theme.colors.white,
  164.                 }}>
  165.                 {polling}
  166.               </Text>
  167.             </Marquee>
  168.           ) : (
  169.             <Text
  170.               style={{
  171.                 fontFamily: theme.fonts.halyardRegular,
  172.                 fontSize: 18,
  173.                 color: theme.colors.white,
  174.               }}>
  175.               {polling}
  176.             </Text>
  177.           )}
  178.         </View>
  179.         <View
  180.           style={{
  181.             marginTop: 14,
  182.             justifyContent: 'center',
  183.             alignItems: 'center',
  184.           }}>
  185.           {isPlaying ? (
  186.             <View
  187.               style={{
  188.                 backgroundColor: theme.colors.primary,
  189.                 borderRadius: 5,
  190.                 width: 71,
  191.                 padding: 4,
  192.               }}>
  193.               <Text
  194.                 style={{
  195.                   fontSize: 14,
  196.                   fontFamily: theme.fonts.halyardSemBd,
  197.                   textTransform: 'uppercase',
  198.                   color: theme.colors.white,
  199.                   textAlign: 'center',
  200.                 }}>
  201.                 Direto
  202.               </Text>
  203.             </View>
  204.           ) : (
  205.             <View />
  206.           )}
  207.  
  208.           <View
  209.             style={{
  210.               flex: 1,
  211.               justifyContent: 'center',
  212.               alignItems: 'center',
  213.             }}>
  214.             <TouchableOpacity
  215.               onPress={() => {
  216.                 musicPlay();
  217.               }}
  218.               activeOpacity={1}
  219.               style={styles.btnMedia}>
  220.               <Icon
  221.                 name={isPlaying ? 'stop' : 'play'}
  222.                 size={24}
  223.                 color={theme.colors.white}
  224.               />
  225.             </TouchableOpacity>
  226.             <TouchableOpacity
  227.               onPress={onShare}
  228.               style={{
  229.                 marginTop: 20,
  230.                 justifyContent: 'center',
  231.                 alignItems: 'center',
  232.               }}>
  233.               <Icon name="share" size={18} color={theme.colors.white} />
  234.               <Text
  235.                 style={{
  236.                   fontSize: 10,
  237.                   fontFamily: theme.fonts.halyardRegular,
  238.                   color: theme.colors.white,
  239.                   marginTop: 4,
  240.                 }}>
  241.                 Partilhar
  242.               </Text>
  243.             </TouchableOpacity>
  244.           </View>
  245.  
  246.           <View
  247.             style={{
  248.               justifyContent: 'flex-end',
  249.               marginBottom: 80,
  250.             }}>
  251.             <View
  252.               style={{
  253.                 flexDirection: 'row',
  254.                 paddingHorizontal: 20,
  255.               }}>
  256.               <View>
  257.                 <Marquee>
  258.                   <Text
  259.                     style={{
  260.                       fontFamily: theme.fonts.halyardRegular,
  261.                       fontSize: 12,
  262.                       color: theme.colors.white,
  263.                     }}>
  264.                     {pub && pub.length > 0 ? pub : ' '}
  265.                   </Text>
  266.                 </Marquee>
  267.               </View>
  268.             </View>
  269.  
  270.             <View
  271.               style={{
  272.                 justifyContent: 'center',
  273.                 alignItems: 'center',
  274.                 borderColor: 'white',
  275.               }}>
  276.               <View
  277.                 style={{
  278.                   flexDirection: 'row',
  279.                   justifyContent: 'space-between',
  280.                   marginTop: 20,
  281.                 }}>
  282.                 <View style={{marginLeft: 20}}>
  283.                   <TouchableOpacity
  284.                     onPress={() => {
  285.                       Linking.openURL(URL_PROGRAMS);
  286.                     }}
  287.                     style={styles.btn}>
  288.                     <View
  289.                       style={{
  290.                         flex: 1,
  291.                         flexDirection: 'row',
  292.                         justifyContent: 'center',
  293.                         alignItems: 'center',
  294.                       }}>
  295.                       <Icon
  296.                         name="programas"
  297.                         size={14}
  298.                         color={theme.colors.white}
  299.                         style={{marginRight: 6}}
  300.                       />
  301.                       <Text style={styles.textButtons}>Programas</Text>
  302.                     </View>
  303.                   </TouchableOpacity>
  304.                 </View>
  305.                 <View style={{marginLeft: 20}}>
  306.                   <TouchableOpacity
  307.                     onPress={() => {
  308.                       Linking.openURL(URL_PROGRAMMING);
  309.                     }}
  310.                     style={styles.btn}>
  311.                     <View
  312.                       style={{
  313.                         flex: 1,
  314.                         flexDirection: 'row',
  315.                         justifyContent: 'center',
  316.                         alignItems: 'center',
  317.                       }}>
  318.                       <Icon
  319.                         name="programacao"
  320.                         size={14}
  321.                         color={theme.colors.white}
  322.                         style={{marginRight: 6}}
  323.                       />
  324.                       <Text style={styles.textButtons}>Programação</Text>
  325.                     </View>
  326.                   </TouchableOpacity>
  327.                 </View>
  328.               </View>
  329.             </View>
  330.           </View>
  331.         </View>
  332.       </View>
  333.     </SafeAreaView>
  334.   );
  335. }
  336.  
  337. const styles = StyleSheet.create({
  338.   safeArea: {
  339.     flex: 1,
  340.     backgroundColor: theme.colors.raisinBlack,
  341.   },
  342.   container: {
  343.     flex: 1,
  344.     alignItems: 'center',
  345.   },
  346.   btnMedia: {
  347.     borderWidth: 2,
  348.     borderColor: theme.colors.white,
  349.     borderRadius: 100,
  350.     width: 100,
  351.     height: 100,
  352.     justifyContent: 'center',
  353.     alignItems: 'center',
  354.   },
  355.   btn: {
  356.     width: 117,
  357.     height: 26,
  358.     borderRadius: 5,
  359.     borderWidth: 1,
  360.     borderColor: theme.colors.languidLavender,
  361.     backgroundColor: 'rgba(206, 206, 219, 0.1)',
  362.   },
  363.   textButtons: {
  364.     fontFamily: theme.fonts.halyardRegular,
  365.     color: theme.colors.white,
  366.     fontSize: 13,
  367.   },
  368. });

Editor

You can edit this paste and save as new: