1
0

index.android.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. var characterList = require('./dev/characterlist.json')
  2. let uniqueCharacters = characterList.filter((item, pos, self) => self.findIndex(c => c["Athenea"] == item["Athenea"]) == pos)
  3. var newState = {}
  4. import React, { Component } from 'react';
  5. import {
  6. AppRegistry,
  7. StyleSheet,
  8. Text,
  9. View,
  10. WebView,
  11. Dimensions,
  12. ToolbarAndroid,
  13. StatusBar,
  14. ScrollView,
  15. Animated,
  16. FlatList,
  17. Image,
  18. TouchableNativeFeedback
  19. } from 'react-native';
  20. import ActionButton from 'react-native-action-button';
  21. import Interactable from 'react-native-interactable';
  22. import WebViewBridge from 'react-native-webview-bridge';
  23. export default class TeXDraw extends Component {
  24. state = {
  25. _deltaY: new Animated.Value(0),
  26. _drawerPadding: new Animated.Value(38),
  27. recognitions:[],
  28. NNHasLoaded:false,
  29. toBeRecognized:""
  30. }
  31. render() {
  32. return (
  33. <View style={{flex: 1}}>
  34. <View style={{flex: 1}}>
  35. <StatusBar
  36. backgroundColor="#8b0032"
  37. barStyle="light-content"
  38. style={{zIndex:1005}}
  39. />
  40. <ToolbarAndroid
  41. title="TeXDraw"
  42. titleColor="#ffffff"
  43. actions={[{title: 'Settings', icon: require('./icons/settings.png'), show: 'always'}]}
  44. style={{
  45. height: 56,
  46. backgroundColor: '#c1185b',
  47. zIndex:200,
  48. elevation:3,
  49. }}
  50. />
  51. <Animated.View style={{
  52. flex:1,
  53. backgroundColor: this.state._deltaY.interpolate({
  54. inputRange: [0, 0, Dimensions.get('window').height-130, Dimensions.get('window').height-130],
  55. outputRange: ['rgba(255, 0, 0, 1)','rgba(255, 0, 0, 1)', 'rgba(0, 255, 0, 1)', 'rgba(0, 255, 0, 1)']
  56. }),
  57. transform: [{
  58. translateY: this.state._deltaY.interpolate({
  59. inputRange: [0, 0, Dimensions.get('window').height-180, Dimensions.get('window').height-180],
  60. outputRange: [-250, -250, 0, 0]
  61. })
  62. }],
  63. }}>
  64. <WebView
  65. ref="webviewbridge"
  66. onMessage={this.onBridgeMessage.bind(this)}
  67. style={{marginTop: 0}}
  68. scrollEnabled={false}
  69. source={{uri:"file:///android_asset/main.html"}} />
  70. </Animated.View>
  71. </View>
  72. <Interactable.View
  73. ref="interactableView"
  74. dragEnabled={this.state.recognitions.length!=0}
  75. verticalOnly={true}
  76. initialPosition={{y: Dimensions.get('window').height-this.getDrawerHeight()}}
  77. //snapPoints={[{y: 200}, {y: Dimensions.get('window').height-130}]}
  78. springPoints={[{y: Dimensions.get('window').height-this.getDrawerHeight(), tension: 500, damping: 0.5, influenceArea: {top: Dimensions.get('window').height-250}}]}
  79. frictionAreas={[{damping: 0.9}]}
  80. alertAreas={[{id: 'myArea', influenceArea: {bottom: 56}}]}
  81. onAlert = {this.menuOpened.bind(this)}
  82. animatedValueY={this.state._deltaY}
  83. boundaries={{top: -2000, bottom:Dimensions.get('window').height, bounce:0}}
  84. style = {{
  85. position: 'absolute',
  86. backgroundColor:'#fdfdfd',
  87. zIndex: 5,
  88. width:'100%',
  89. elevation:2
  90. }}>
  91. <Animated.View style={{
  92. transform: [{
  93. translateY: this.state._deltaY.interpolate({
  94. inputRange: [0, Dimensions.get('window').height-250, Dimensions.get('window').height-130, Dimensions.get('window').height-130],
  95. outputRange: [-38, -38, 0, 0]
  96. })
  97. }],
  98. }}>
  99. <Animated.View style={{padding:this.state._drawerPadding, backgroundColor:'#fdf1f6'}}>
  100. <Text style={{textAlign:'center', fontSize:18, padding:0, backgroundColor:'#fdf1f6'}}>{this.textToShow()}</Text>
  101. </Animated.View>
  102. <FlatList
  103. data={this.state.recognitions}
  104. renderItem={({item}) =>
  105. <TouchableNativeFeedback>
  106. <View style={{flexDirection: 'row', padding:5}}>
  107. <View style={{width: 80, height:80}}>
  108. <Image source={{'uri': 'tex'+item.key}}
  109. resizeMode="contain"
  110. style={{alignSelf:'stretch', width: undefined, height: undefined, flex:1}}/>
  111. </View>
  112. <View style={{flexDirection: 'column'}}>
  113. <Text style={{fontSize:20, fontWeight:'normal', paddingTop:5, paddingLeft:10}}>{characterList[item.key]["Latex"]}</Text>
  114. <Text style={{fontSize:15, fontWeight:'normal', paddingTop:5, paddingLeft:10, color:"777777"}}>{characterList[item.key]["Package"]}</Text>
  115. </View>
  116. </View>
  117. </TouchableNativeFeedback>
  118. }
  119. />
  120. </Animated.View>
  121. </Interactable.View>
  122. <View style={{height:0}}>
  123. <WebViewBridge
  124. ref="texdrawwebview"
  125. style={{height:0}}
  126. allowUniversalAccessFromFileURLs={true}
  127. onBridgeMessage={this.onTeXDrawMessage.bind(this)}
  128. scrollEnabled={false}
  129. source={{uri:"file:///android_asset/texdraw.html"}}/>
  130. </View>
  131. <ActionButton
  132. offSetY={250}
  133. buttonColor="#c1185b"
  134. position="left"
  135. buttonText="x"
  136. degrees={45}
  137. onPress={this.clearCanvas.bind(this)}
  138. style={{zIndex:5, position:'absolute', bottom:100}}
  139. />
  140. </View>
  141. );
  142. }
  143. onBridgeMessage(webViewData) {
  144. var str
  145. if(webViewData==undefined) str = this.state.toBeRecognized
  146. else str = webViewData.nativeEvent.data
  147. if(str=="[]"){
  148. this.setState({'recognitions':[]})
  149. newState = []
  150. }
  151. else{
  152. if(this.state.NNHasLoaded){
  153. this.refs.texdrawwebview.sendToBridge(str)
  154. }
  155. else{
  156. this.setState({'toBeRecognized':str})
  157. setTimeout(this.onBridgeMessage.bind(this),300)
  158. }
  159. }
  160. }
  161. onTeXDrawMessage(webViewData) {
  162. if(webViewData == "loaded"){
  163. //alert("loaded")
  164. this.refs.webviewbridge.postMessage("loaded");
  165. this.setState({'NNHasLoaded':true})
  166. }
  167. else{
  168. let jsonData = JSON.parse(webViewData);
  169. let result = Object.keys(jsonData).map(k => jsonData[k])
  170. let sorted = result.slice(0,).sort((a,b) => b-a)
  171. newState = sorted.slice(0,30).map(v => {
  172. let index = result.indexOf(v)
  173. return {key: index}
  174. })
  175. newState = newState.map(item => {
  176. return characterList.filter(c => c["Athenea"]==uniqueCharacters[item.key]["Athenea"]).map(c => characterList.findIndex(p => p["Latex"]==c["Latex"]))
  177. })
  178. //alert(JSON.stringify(newState))
  179. newState = Array.prototype.concat(...newState).map(n => ({"key":n}))
  180. //alert(JSON.stringify(newState))
  181. var ra = Math.floor(Math.random()*800)
  182. this.setState({'recognitions':newState.slice(0,5)})
  183. setTimeout((function(){
  184. this.setState({'recognitions':newState})
  185. }).bind(this), 400)
  186. setTimeout(Animated.timing(
  187. this.state._drawerPadding, {toValue: 10, duration:300}
  188. ).start, 50)
  189. }
  190. }
  191. clearCanvas() {
  192. this.refs.webviewbridge.postMessage("clear");
  193. setTimeout((function(){
  194. this.setState({'recognitions':[]})
  195. newState = []
  196. }).bind(this), 200)
  197. }
  198. menuOpened(e) {
  199. this.refs.interactableView.changePosition({x: 0, y: 56});
  200. this.refs.interactableView.setVelocity({y: 0});
  201. }
  202. textToShow(){
  203. if(this.state.recognitions.length==0){
  204. return 'Draw a symbol'
  205. }
  206. else{
  207. return 'Pull up for results'
  208. }
  209. }
  210. getDrawerHeight(){
  211. return 130
  212. }
  213. }
  214. const styles = StyleSheet.create({
  215. });
  216. AppRegistry.registerComponent('TeXDraw', () => TeXDraw);