Wallet.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**
  2. * 钱包页面
  3. * @邠心vbe on 2021/05/07
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, StyleSheet, ScrollView, RefreshControl, Image, Pressable } from 'react-native';
  7. import { Balance } from './Payment';
  8. import { PageList } from '../Router';
  9. import History from './History';
  10. import Overview from './Overview';
  11. import { MyRefreshProps } from '../../components/ThemesConfig';
  12. import { ElevationObject } from '../../components/Button';
  13. import TextView from '../../components/TextView';
  14. import Dialog from '../../components/Dialog';
  15. export default class Wallet extends Component {
  16. constructor(props) {
  17. super(props);
  18. this.state = {
  19. balance: 0,
  20. tabIndex: 0,
  21. refreshing: false,
  22. tabWidth: 0,
  23. tabHeight: 0,
  24. balanceText: '',
  25. pageShown: true
  26. };
  27. }
  28. componentDidMount() {
  29. this.props.navigation.addListener('focus', () => {
  30. this.onRefresh();
  31. this.setState({
  32. pageShown: true
  33. })
  34. getUserInfo(info => {
  35. this.setState({
  36. balance: info.credit,
  37. balanceText: info.creditStr
  38. })
  39. }, true)
  40. });
  41. this.props.navigation.addListener('blur', () => {
  42. this.setState({
  43. pageShown: false
  44. })
  45. });
  46. this.props.navigation.addListener('beforeRemove', (e) => {
  47. if (this.state.pageShown) {
  48. e.preventDefault();
  49. Dialog.showProgressDialog();
  50. this.setState({
  51. pageShown: false
  52. }, () => {
  53. setTimeout(() => {
  54. Dialog.dismissLoading();
  55. goBack();
  56. }, 500);
  57. });
  58. }
  59. });
  60. }
  61. changeTab(index) {
  62. if (this.state.tabIndex !== index) {
  63. this.setState({
  64. tabIndex: index
  65. });
  66. }
  67. }
  68. onRefresh() {
  69. this.setState({
  70. refreshing: true
  71. });
  72. }
  73. stopRefresh() {
  74. this.setState({
  75. refreshing: false
  76. });
  77. }
  78. tabLayout({nativeEvent}) {
  79. //console.log('tab layout\n\n\n\n\n', nativeEvent.layout);
  80. if (nativeEvent.layout.width) {
  81. this.setState({
  82. tabWidth: nativeEvent.layout.width,
  83. tabHeight: 0.171955 * nativeEvent.layout.width
  84. })
  85. }
  86. }
  87. render() {
  88. return (
  89. <ScrollView
  90. style={styles.container}
  91. refreshControl={
  92. <RefreshControl
  93. {...MyRefreshProps()}
  94. refreshing={this.state.refreshing}
  95. onRefresh={() => this.onRefresh()}
  96. />
  97. }>
  98. {/* <View style={styles.balanceView}>
  99. <Payment
  100. balance={this.state.balance}
  101. isPayPerUse={false}
  102. isWallet={true}
  103. payType={"Credit Wallet"}
  104. topup={() => toTopupPage()}/>
  105. </View> */}
  106. <Balance
  107. balance={this.state.balance}
  108. balanceText={this.state.balanceText}
  109. action={$t('wallet.purchaseCredits')/*"Top Up"*/}
  110. page={PageList.topupNew}
  111. />
  112. <View style={styles.contentView}>
  113. <View
  114. style={styles.tabView}
  115. onLayout={props => this.tabLayout(props)}>
  116. { this.state.tabIndex == 0
  117. ? <Image
  118. style={[styles.tabBackgroundLeft, {width: this.state.tabWidth + 1, height: this.state.tabHeight}]}
  119. resizeMode="cover"
  120. source={require('../../images/wallet/tab-left.png')}
  121. />
  122. : <Image
  123. style={[styles.tabBackgroundRight, {width: this.state.tabWidth + 1, height: this.state.tabHeight}]}
  124. resizeMode="cover"
  125. source={require('../../images/wallet/tab-right.png')}
  126. />
  127. }
  128. <Pressable
  129. style={styles.tabItem}
  130. onPress={() => this.changeTab(0)}>
  131. <TextView
  132. style={[
  133. styles.tabText,
  134. this.state.tabIndex == 0 && styles.active
  135. ]}
  136. numberOfLines={1}>
  137. {$t('wallet.tabOverview')}
  138. </TextView>
  139. </Pressable>
  140. <View style={{width: 16}}></View>
  141. <Pressable
  142. style={styles.tabItem}
  143. onPress={() => this.changeTab(1)}>
  144. <TextView
  145. style={[
  146. styles.tabText,
  147. this.state.tabIndex == 1 && styles.active
  148. ]}
  149. numberOfLines={1}>
  150. {$t('wallet.tabHistory')}
  151. </TextView>
  152. </Pressable>
  153. </View>
  154. <Overview
  155. atAglance={false}
  156. skeleton={!this.state.pageShown}
  157. refresh={this.state.refreshing}
  158. refreshed={() => this.stopRefresh()}
  159. shown={this.state.tabIndex == 0}/>
  160. <History
  161. refresh={this.state.refreshing}
  162. refreshed={() => this.stopRefresh()}
  163. shown={this.state.tabIndex == 1}/>
  164. </View>
  165. </ScrollView>
  166. );
  167. }
  168. }
  169. const styles = StyleSheet.create({
  170. container: {
  171. flex: 1,
  172. backgroundColor: '#f5f5f5'
  173. },
  174. balanceView: {
  175. paddingTop: 12,
  176. paddingLeft: 16,
  177. paddingRight: 16,
  178. paddingBottom: 12,
  179. backgroundColor: colorLight
  180. },
  181. tabView: {
  182. height: 60,
  183. padding: 16,
  184. overflow: 'hidden',
  185. alignItems: 'center',
  186. flexDirection: 'row',
  187. justifyContent: 'center'
  188. },
  189. tabItem: {
  190. flex: 1,
  191. marginTop: -14,
  192. alignItems: 'center',
  193. justifyContent: 'center'
  194. },
  195. tabText: {
  196. color: textSecondary,
  197. fontSize: 16,
  198. paddingTop: 5,
  199. paddingLeft: 36,
  200. paddingRight: 36,
  201. paddingBottom: 5,
  202. },
  203. active: {
  204. color: textPrimary,
  205. fontWeight: 'bold'
  206. },
  207. contentView: {
  208. ...$margin(8, 16, 16),
  209. overflow: 'hidden',
  210. borderRadius: 6,
  211. ...ElevationObject(5),
  212. backgroundColor: colorLight
  213. },
  214. tabBackgroundLeft: {
  215. top: -1,
  216. left: -1,
  217. right: 0,
  218. paddingRight: 1,
  219. width: $vw(90),
  220. height: $vw(14),
  221. position: 'absolute'
  222. },
  223. tabBackgroundRight: {
  224. top: -1,
  225. left: 0,
  226. right: -1,
  227. paddingRight: 1,
  228. width: $vw(90),
  229. height: $vw(14),
  230. position: 'absolute'
  231. }
  232. })