Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.58 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { View, WebView, ScrollView, LayoutAnimation, Dimensions } from 'react-native';
  3. import { ElliotNewsStyles } from './styles/elliotNewsDetailed.styles';
  4. import Fonts from '../../../styles/fonts';
  5. import Colors from '../../../styles/colors';
  6. import Navbar from '../../../shared/components/navbar/navbar';
  7. import SourceSansProText from '../../../shared/components/text/source-sans-pro/source-sans-pro';
  8. import ApiDispatcher from '../../../shared/dispatcher/api-dispatcher';
  9. import RaisedButton from '../../../shared/components/button/raised/raised-button';
  10.  
  11. const injectedJS =
  12. `window.onscroll = function() {
  13. var goToEnd = false
  14. // if(this.scrollTop + this.height() == document.height()) {
  15. // goToEnd = true
  16. // }
  17.  
  18. const message = {
  19. scrollTop: document.documentElement.scrollTop || document.body.scrollTop, //this.scrollY,
  20. offsetHeight: document.documentElement.clientHeight || document.body.clientHeight,// this.innerHeight,
  21. goToEnd
  22. }
  23. window.postMessage(JSON.stringify(message))
  24. }`
  25.  
  26.  
  27. export default class ElliotNewsDetailed extends React.Component {
  28. constructor(props) {
  29. super(props)
  30. this.dispatcher = new ApiDispatcher()
  31.  
  32. this.state = {
  33. elliotNews: {},
  34. isTitlesVisible: true,
  35. isButtonOkElliotVisible: false,
  36. webviewHeight: 300,
  37. _viewOffset: 0,
  38. _viewHeight: 0,
  39. _viewContentHeight: 0,
  40. _screenHeight: 0,
  41. isFirstTime: true,
  42. currentScroll: 0,
  43. maxScroll: 0,
  44. teste: ''
  45. }
  46. }
  47.  
  48. componentDidMount() {
  49. const { elliotNews } = this.props.navigation.state.params;
  50. this.setState({
  51. elliotNews: elliotNews
  52. });
  53. }
  54.  
  55. setAsRead = (callBack) => {
  56. this.dispatcher.setElliotNewsAsRead(this.state.elliotNews.id)
  57. .then(callBack)
  58. .catch(err => {
  59. Sentry.captureException(JSON.stringify(err))
  60. console.log(err)
  61. })
  62. }
  63.  
  64. goBack = () => {
  65. this.setAsRead()
  66. this.props.navigation.goBack();
  67. }
  68.  
  69. _onLayout = (event) => {
  70. const { height } = event.nativeEvent.layout
  71. const dimensions = Dimensions.get('screen');
  72. const screenHeight = dimensions.height;
  73. this.setState({ _viewHeight: height, _screenHeight: screenHeight })
  74. }
  75.  
  76. _onContentSizeChange = (contentWidth, contentHeight) => {
  77. this.setState({ _viewContentHeight: contentHeight })
  78. }
  79.  
  80. getMaxScroll = (scrollTop, maxScroll, isButtonOkElliotVisible) => {
  81. if (isButtonOkElliotVisible)
  82. scrollTop += 25
  83.  
  84. if (scrollTop > maxScroll)
  85. return scrollTop
  86.  
  87. return maxScroll - 5
  88. }
  89.  
  90. _onMessage(event) {
  91. console.log('data', JSON.parse(event.nativeEvent.data))
  92.  
  93. const {
  94. scrollTop,
  95. offsetHeight,
  96. goToEnd
  97. } = JSON.parse(event.nativeEvent.data)
  98.  
  99. console.log(goToEnd)
  100.  
  101. const CustomLayoutLinear = {
  102. duration: 100,
  103. create: { type: LayoutAnimation.Types.linear, property: LayoutAnimation.Properties.opacity },
  104. update: { type: LayoutAnimation.Types.linear, property: LayoutAnimation.Properties.opacity },
  105. delete: { type: LayoutAnimation.Types.linear, property: LayoutAnimation.Properties.opacity }
  106. }
  107. const isButtonOkElliotVisible = scrollTop == this.state.maxScroll // direction === 'down'
  108. // const maxScrollToComparater = this.state.maxScroll - 5
  109. const maxScroll = this.getMaxScroll(scrollTop, this.state.maxScroll, isButtonOkElliotVisible)
  110.  
  111. const direction = scrollTop > 0 &&
  112. ((scrollTop > this.state.currentScroll && this.state.currentScroll <= maxScroll) ||
  113. scrollTop > maxScroll) ? 'down' : 'up'
  114.  
  115. const isTitlesVisible = true
  116. if (isTitlesVisible !== this.state.isTitlesVisible) {
  117. LayoutAnimation.configureNext(CustomLayoutLinear)
  118. this.setState({ isTitlesVisible })
  119. }
  120.  
  121.  
  122. if (isButtonOkElliotVisible != this.state.isButtonOkElliotVisible) {
  123. LayoutAnimation.configureNext(CustomLayoutLinear)
  124. this.setState({ isButtonOkElliotVisible })
  125. }
  126.  
  127. // Update your scroll position
  128. this.setState({ currentScroll: scrollTop, maxScroll: maxScroll, teste: direction })
  129. }
  130.  
  131.  
  132. render() {
  133. return (
  134. <ScrollView style={ElliotNewsStyles.scrollView} contentContainerStyle={ElliotNewsStyles.scrollViewContent}
  135. onLayout={this._onLayout} onContentSizeChange={this._onContentSizeChange}
  136. showsHorizontalScrollIndicator={false} >
  137.  
  138. <Navbar title="Elliot News" onLeftPress={this.goBack} leftIconIsBack={true} style={ElliotNewsStyles.navbar}/>
  139.  
  140. {this.state.isTitlesVisible ?
  141. // {1 ===1 ?
  142. <View style={ElliotNewsStyles.header}>
  143. {/* <SourceSansProText text={this.state.elliotNews.articleTitle} fontSize={22} color={Colors.BlackGray} fontWeight={Fonts.Weights.SourceSansPro.SemiBold}></SourceSansProText>
  144. <SourceSansProText text={this.state.elliotNews.articleSubtitle} fontSize={18} color={Colors.BlackGray} fontWeight={Fonts.Weights.SourceSansPro.Light}></SourceSansProText>
  145. <SourceSansProText text={this.state.elliotNews.releaseDate} fontSize={14} color={Colors.BlackGray} fontWeight={Fonts.Weights.SourceSansPro.Light}></SourceSansProText> */}
  146. <SourceSansProText text={'_viewHeight: ' + this.state._viewHeight} fontSize={14} color={Colors.BlackGray} fontWeight={Fonts.Weights.SourceSansPro.Light}></SourceSansProText>
  147. {/* <SourceSansProText text={'_viewOffset: ' + this.state._viewOffset} fontSize={14} color={Colors.BlackGray} fontWeight={Fonts.Weights.SourceSansPro.Light}></SourceSansProText> */}
  148. <SourceSansProText text={'_viewContentHeight: ' + this.state._viewContentHeight} fontSize={14} color={Colors.BlackGray} fontWeight={Fonts.Weights.SourceSansPro.Light}></SourceSansProText>
  149. <SourceSansProText text={'_screenHeight: ' + this.state._screenHeight} fontSize={14} color={Colors.BlackGray} fontWeight={Fonts.Weights.SourceSansPro.Light}></SourceSansProText>
  150. <SourceSansProText text={'currentScroll: ' + this.state.currentScroll} fontSize={14} color={Colors.BlackGray} fontWeight={Fonts.Weights.SourceSansPro.Light}></SourceSansProText>
  151. <SourceSansProText text={'maxScroll: ' + this.state.maxScroll} fontSize={14} color={Colors.BlackGray} fontWeight={Fonts.Weights.SourceSansPro.Light}></SourceSansProText>
  152. <SourceSansProText text={'teste: ' + this.state.teste} fontSize={14} color={Colors.BlackGray} fontWeight={Fonts.Weights.SourceSansPro.Light}></SourceSansProText>
  153. </View> : null}
  154.  
  155. <View style={ElliotNewsStyles.content} scrollEnabled={false} >
  156.  
  157. <WebView source={{ html: this.state.elliotNews.articleBody }}
  158. startInLoadingState={true}
  159. scrollEnabled={true}
  160. automaticallyAdjustContentInsets={false}
  161. javaScriptEnabled={true}
  162. injectedJavaScript={injectedJS}
  163. onMessage={this._onMessage.bind(this)}
  164. style={{padding: 5,
  165. backgroundColor:Colors.Transparent, overflow: 'hidden'
  166. }}
  167. />
  168.  
  169. {this.state.isButtonOkElliotVisible ?
  170. <View style={ElliotNewsStyles.button}>
  171. <RaisedButton fontSize={16} text={'Ok, Elliot !'} isRadius={true} isElliot={true}
  172. width="85%" onPress={() => this.goBack()} ></RaisedButton>
  173. </View> : null}
  174. </View>
  175.  
  176. {/* {this.state.isButtonOkElliotVisible ?
  177. <View style={ElliotNewsStyles.button}>
  178. <RaisedButton fontSize={16} text={'Ok, Elliot !'} isRadius={true} isElliot={true}
  179. width="85%" onPress={() => this.goBack()} ></RaisedButton>
  180. </View> : null} */}
  181.  
  182. </ScrollView>
  183. );
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement