Nessuna descrizione

LearnViewController.js 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /**
  2. * Created by zack on 2018/4/19.
  3. */
  4. import {
  5. View,
  6. Text,
  7. StyleSheet,
  8. Image,
  9. TouchableOpacity
  10. } from 'react-native'
  11. import React, {Component} from 'react'
  12. import {NavigationBarHeight, TabBarHeight, ScreenDimensions} from '../../utils/DimensionsTools'
  13. import VocabularyTextView from './View/VocabularyTextView'
  14. import VocabularyImageView from './View/VocabularyImageView'
  15. import ListenAndFillSingleWordView from './View/ListenAndFillSingleWordView'
  16. import ListenAndFillMultiWordView from './View/ListenAndFillMultiWordView'
  17. import TranslationView from './View/TranslationView'
  18. import ToastMsg from '../../utils/ToastMsg'
  19. import LearnViewControllerNavigationBar from '../base/LearnViewControllerNavigationBar'
  20. import {GlobalUserStorageTool, UserStorageKey} from '../../utils/GlobalUserStorageTool'
  21. import BaseNavigationBarStyle from '../base/BaseNavigationBarStyle'
  22. export default class LearnViewController extends Component {
  23. static navigatorButtons = {
  24. rightButtons: [
  25. {
  26. icon: require('../../resources/images/BaseNavigationBar/icon-mine.png'), // for icon button, provide the local image asset name
  27. id: 'mine' //Android has four type of left button,you sure specify the button type.
  28. },
  29. ],
  30. leftButtons: [
  31. {
  32. title: 'Edit',
  33. id: 'edit',
  34. disableIconTint: true,
  35. buttonFontSize: 14,
  36. },
  37. ]
  38. };
  39. constructor(props) {
  40. super(props)
  41. this.state = {
  42. navigationBarMenu: [
  43. {title: '单词', index: 0},
  44. {title: '短语', index: 1},
  45. {title: '句子', index: 2},
  46. {title: '语法', index: 3},
  47. {title: '听说', index: 4},
  48. {title: '读写', index: 5},
  49. ],
  50. questionList: [{type: 'Text'},{type: 'Image'},{type: 'SingleWord'},{type: 'MultiWord'},{type: 'Translation'},],
  51. questionSelectedIndex: 0,
  52. isVerified: false,
  53. currentQuestionType: 'Text',
  54. answerSelectedIndex: -1, //
  55. answerCorrectIndex: -1,
  56. }
  57. }
  58. componentDidMount() {
  59. this.props.navigator.setOnNavigatorEvent(event => {
  60. if (event.type === 'NavBarButtonPress') {
  61. if (event.id === 'mine') {
  62. this.goToMinePage()
  63. }else if (event.id === 'edit') {
  64. this.goToEditUserLearnLevelPage()
  65. }
  66. }else if (event.type === 'ScreenChangedEvent') {
  67. if (event.id === 'willAppear') {
  68. this.showTabBar()
  69. }else if (event.id === 'willDisappear') {
  70. }
  71. }
  72. })
  73. }
  74. goToEditUserLearnLevelPage() {
  75. this.hideTabBar()
  76. this.props.navigator.showModal({
  77. screen: 'LearnLevelViewController',
  78. title: '',
  79. backButtonTitle: '',
  80. passProps: {
  81. isPush: true
  82. },
  83. navigatorStyle: {
  84. statusBarTextColorScheme: 'light',
  85. statusBarColor: '#0281c5',
  86. navBarHidden: true
  87. }
  88. })
  89. }
  90. goToMinePage() {
  91. this.hideTabBar()
  92. GlobalUserStorageTool.load(UserStorageKey.UserInfo, (response) => {
  93. if (response && response.nickname) { //已经登录成功
  94. this.props.navigator.push({
  95. screen: 'MineViewController',
  96. title: '基本资料',
  97. backButtonTitle: '',
  98. navigatorStyle: BaseNavigationBarStyle,
  99. passProps: {
  100. UserInfo: response
  101. }
  102. })
  103. } else {
  104. this.props.navigator.push({
  105. screen: 'LoginViewController',
  106. title: '我的',
  107. backButtonTitle: '',
  108. navigatorStyle: {
  109. navBarHidden: true
  110. },
  111. passProps: {
  112. }
  113. })
  114. }
  115. }, (error) => {
  116. this.props.navigator.push({
  117. screen: 'LoginByVerificationCodeViewController',
  118. title: '我的',
  119. backButtonTitle: '',
  120. navigatorStyle: {
  121. navBarHidden: true
  122. },
  123. passProps: {
  124. }
  125. })
  126. })
  127. }
  128. hideTabBar() {
  129. this.props.navigator.toggleTabs({
  130. to:'hidden',
  131. animated: true
  132. })
  133. }
  134. showTabBar() {
  135. this.props.navigator.toggleTabs({
  136. to:'shown',
  137. animated: true
  138. })
  139. }
  140. nextQuestion() {
  141. if (this.state.questionSelectedIndex === this.state.questionList.length - 1) {
  142. alert("完成")
  143. return;
  144. }
  145. this.setState({questionSelectedIndex: this.state.questionSelectedIndex + 1, isVerified: false, answerSelectedIndex: -1, answerCorrectIndex: -1})
  146. }
  147. verifyAnswer() {
  148. let contentViewType = this.state.questionList[this.state.questionSelectedIndex].type
  149. if (contentViewType === 'Text') {
  150. if (this.state.answerSelectedIndex === -1) {
  151. alert("请选择你的答案")
  152. return
  153. }
  154. //从课后答案中设置正确的答案
  155. this.setState({isVerified: true, answerCorrectIndex: 2})
  156. }else if (contentViewType === 'Image') {
  157. if (this.state.answerSelectedIndex === -1) {
  158. ToastMsg.show("请选择你的答案")
  159. return
  160. }
  161. //从课后答案中设置正确的答案
  162. this.setState({isVerified: true, answerCorrectIndex: 2})
  163. }else if (contentViewType === 'SingleWord') {
  164. this.setState({isVerified: true})
  165. }else if (contentViewType === 'MultiWord') {
  166. this.setState({isVerified: true})
  167. }else if (contentViewType === 'Translation') {
  168. this.setState({isVerified: true})
  169. }
  170. }
  171. lastQuestion() {
  172. if (this.state.questionSelectedIndex === 0) {
  173. ToastMsg.show("当前已经是第一题了!")
  174. return;
  175. }
  176. this.setState({questionSelectedIndex: this.state.questionSelectedIndex - 1} )
  177. }
  178. renderVocabularyView() {
  179. let contentViewType = this.state.questionList[this.state.questionSelectedIndex].type
  180. if (contentViewType === 'Text' || contentViewType === 'Image') {
  181. return(
  182. <TouchableOpacity style={styles.VoiceTitleBgView}>
  183. <Image style={styles.VoiceImageView} source={require("../../resources/images/TabBar/Learn/btn_speaker.png")}/>
  184. <Text style={styles.VocabularyText}>{'Speech'}</Text>
  185. <Text style={styles.NounText}>{'[spiːtʃ]'}</Text>
  186. </TouchableOpacity>
  187. )
  188. }else {
  189. return(
  190. null
  191. )
  192. }
  193. }
  194. renderCenterContentView() {
  195. let contentViewType = this.state.questionList[this.state.questionSelectedIndex].type
  196. if (contentViewType === 'Text') {
  197. return (
  198. <VocabularyTextView
  199. correctIndex = {this.state.answerCorrectIndex}
  200. isVerified= {this.state.isVerified}
  201. didSelectedItem = {(index) => {
  202. this.setState({answerSelectedIndex: index})
  203. }}
  204. />
  205. )
  206. }else if (contentViewType === 'Image') {
  207. return (
  208. <VocabularyImageView
  209. correctIndex = {this.state.answerCorrectIndex}
  210. isVerified= {this.state.isVerified}
  211. didSelectedItem = {(index) => {
  212. this.setState({answerSelectedIndex: index})
  213. }}
  214. />
  215. )
  216. }else if (contentViewType === 'SingleWord') {
  217. return (
  218. <ListenAndFillSingleWordView
  219. isVerified= {this.state.isVerified}
  220. />
  221. )
  222. }else if (contentViewType === 'MultiWord') {
  223. return (
  224. <ListenAndFillMultiWordView
  225. isVerified= {this.state.isVerified}
  226. />
  227. )
  228. }else if (contentViewType === 'Translation') {
  229. return (
  230. <TranslationView />
  231. )
  232. }
  233. }
  234. render() {
  235. return (
  236. <View style={styles.View}>
  237. <LearnViewControllerNavigationBar
  238. title = {"单词22"}
  239. subTitle = {"1/6"}
  240. leftTitle = {"初级"}
  241. didClickedMineItem = {() => {
  242. this.goToMinePage()
  243. }}
  244. didClickLeftButton = {() => {
  245. this.goToEditUserLearnLevelPage()
  246. }}
  247. />
  248. {this.renderVocabularyView(true)}
  249. {this.renderCenterContentView()}
  250. <View style={styles.BottomButtonView}>
  251. <TouchableOpacity onPress={() => {
  252. this.lastQuestion()
  253. }} style={styles.LastQuestionButton}>
  254. <Text style={[styles.BottomButtonText, {color: '#666666',}]}>{'上一题'}</Text>
  255. </TouchableOpacity>
  256. <TouchableOpacity onPress={() => {
  257. if (!this.state.isVerified) { //还没有验证答案,先进行答案验证请求
  258. //模拟验证
  259. this.verifyAnswer()
  260. }else { //验证成功之后,就允许用户点击下一题
  261. this.nextQuestion()
  262. }
  263. }} style={[styles.VerifyButton, {backgroundColor: this.state.isVerified === false ? '#e5e5e5' : '#71c135'}]}>
  264. <Text style={[styles.BottomButtonText, {color: '#ffffff'}]}>{this.state.isVerified === false ? '检查' : '下一题'}</Text>
  265. </TouchableOpacity>
  266. </View>
  267. </View>
  268. );
  269. }
  270. }
  271. const styles = StyleSheet.create({
  272. View: {
  273. width: ScreenDimensions.width,
  274. height: ScreenDimensions.height,
  275. backgroundColor: '#ffffff',
  276. justifyContent: 'space-between',
  277. },
  278. VoiceTitleBgView: {
  279. height: 30,
  280. marginLeft: 15,
  281. marginTop: 15 + NavigationBarHeight.height,
  282. flexDirection: 'row',
  283. alignItems: 'center'
  284. },
  285. VoiceImageView: {
  286. width: 30,
  287. height: 30,
  288. backgroundColor: 'white'
  289. },
  290. VocabularyText: {
  291. fontSize: 18,
  292. color: '#333333',
  293. marginLeft: 8,
  294. },
  295. NounText: {
  296. fontSize: 18,
  297. color: '#333333',
  298. marginLeft: 8,
  299. },
  300. BottomButtonView: {
  301. marginLeft: 0,
  302. marginBottom: TabBarHeight.height + 18,
  303. height: 48,
  304. width: ScreenDimensions.width,
  305. flexDirection: 'row',
  306. alignItems: 'center',
  307. justifyContent: 'space-between',
  308. backgroundColor: 'rgba(0,0,0,0)'
  309. },
  310. LastQuestionButton: {
  311. marginLeft: 15,
  312. height: 48,
  313. width: 100,
  314. borderRadius: 24,
  315. alignItems: 'center',
  316. justifyContent: 'center',
  317. backgroundColor: '#e5e5e5',
  318. },
  319. VerifyButton: {
  320. marginRight: 15,
  321. height: 48,
  322. width: ScreenDimensions.width - 40 - 100,
  323. borderRadius: 24,
  324. alignItems: 'center',
  325. justifyContent: 'center',
  326. },
  327. BottomButtonText: {
  328. fontSize: 18,
  329. },
  330. })