Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from "react"
  2. import { SafeAreaView,Text, TouchableOpacity,PanResponder, Button,  FlatList, View, Dimensions, Alert } from "react-native"
  3.  
  4. import VideoComponent from "./video.js"
  5.  
  6. import {setReduxVideoContainerRef} from "../../actions/redux/videocontainer.js"
  7. import SearchBar from "./searchbar.js";
  8. import { Rating } from "react-native-ratings";
  9. import VideoHUD from "./videohud.js";
  10. import { getMost } from "../../actions/api/getMostHandler.js";
  11. import { string } from "prop-types";
  12.  
  13. import {connect} from "react-redux"
  14.  
  15. /*
  16.  
  17.     <NavigationEvents
  18.             onWillFocus={payload => {
  19.               console.log('video focused')
  20.               console.log('video state', this.state)
  21.             }}
  22.             onDidBlur={payload => {
  23.               console.log('video blurred')
  24.               console.log('video state', this.state)
  25.               if (!this.state.paused)
  26.                 this.setState({ paused : true })
  27.             }}
  28.           />
  29.  
  30.  
  31. */
  32.  
  33. function mapStateToProps(state) {
  34.   const reducer = state.cameraReducer
  35.  
  36.   return {
  37.     cameraReducer : reducer
  38.   }
  39. }
  40.  
  41. class StreamContainer extends React.Component {
  42.  
  43.  
  44.     _measureDimensions = (event ) => {
  45.         if (this.state.init == 0) {
  46.             return
  47.         }
  48.  
  49.         this.setState({
  50.             itemWidth: event.nativeEvent.layout.width,
  51.             itemHeight: event.nativeEvent.layout.height, init: 0 } )
  52.  
  53.     }
  54.  
  55.   constructor(props) {
  56.     super(props);
  57.  
  58.     this.flatListRef
  59.  
  60.     this.currIndex = 0
  61.  
  62.     this.state = {data: [],
  63.     maxIndex: 0, itemHeight: 550, itemWidth: 400, init: 1, refInit: 1
  64.   }
  65. }
  66.  
  67.  
  68.  
  69.  
  70.   async componentDidMount() {
  71.     let response = await getMost("TODAY", "new", 0, 100)
  72.  
  73.  
  74.     let res = JSON.parse(response.data)
  75.  
  76.     let urls = this.state.data
  77.  
  78.  
  79.     for(let i = 0; i < res.length; i++) {
  80.       let s = res[i].Member
  81.  
  82.       if(s.charAt(0) == "/")
  83.           s = s.substring(1)
  84.       console.log("here's a source! ", s)
  85.       urls.push({source:s})
  86.     }
  87.  
  88.     let max = urls.length - 1
  89.    
  90.     this.setState( { data: urls, maxIndex: max, itemHeight: 550, itemWidth: 400, init: 1, refInit: 1} )
  91.     this.forceUpdate()
  92.   }
  93.  
  94.     renderItem = (item ) => {
  95.           return (  <View style={{position:"relative",flex:1, height:this.state.itemHeight, width:this.state.itemWidth}}>
  96.               <VideoComponent  source={item.item.source}  index={item.index} upswipe={this.upswipe} downswipe={this.downswipe}/>
  97.               </View>
  98.           )
  99.     }
  100.  
  101.       _onViewableItemsChanged = ({ viewableItems, changed }) => {
  102.         console.log("Visible items are", viewableItems);
  103.         console.log("Changed in this iteration", changed);
  104.       }
  105.    
  106.  
  107.     render() {
  108.         return (<>
  109.             <SafeAreaView onLayout={(event) => this._measureDimensions(event)} style={{zIndex: 3, position:"absolute", top:0, left: 0, right:0, bottom: 0}}>
  110.          
  111.           {this.state.data.length > 0 && <FlatList
  112.                 scrollEnabled={false}
  113.                 style={{flex: 1 }}
  114.                 data={ this.state.data }
  115.  
  116.                 ref={(ref) => { this.flatListRef = ref}}
  117.  
  118.                 onViewableItemsChanged={this._onViewableItemsChanged}
  119.                 renderItem={(item) =>  this.renderItem(item) }
  120.                 keyExtractor={item => item.item}
  121.                 extraData={this.state}
  122.                
  123.             />}
  124.             </SafeAreaView>
  125.                
  126.                 </>)
  127.     }
  128.  
  129.      upswipe = (childClose) => {
  130.        childClose()
  131.        if(this.currIndex < this.state.maxIndex) {
  132.          this.currIndex++
  133.          console.log(this.currIndex , " is the new index", this.state.data[this.currIndex])
  134.          this.flatListRef.scrollToIndex({animated: true, index: this.currIndex})
  135.        }
  136.     }
  137.    
  138.     downswipe = (childClose) => {
  139.        if(this.currIndex > 0) {
  140.          childClose()
  141.          this.currIndex--
  142.  
  143.          console.log(this.currIndex , " is the new index", this.state.data[this.currIndex])
  144.          this.flatListRef.scrollToIndex({animated: true, index: this.currIndex})
  145.        }
  146.     }
  147.    
  148. }
  149.  
  150.  
  151. export default connect(mapStateToProps)(StreamContainer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement