VehicleListV2.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**
  2. * 车辆列表V2
  3. * @邠心vbe on 2024/01/30
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, Image, StyleSheet, Pressable, FlatList, RefreshControl } from 'react-native';
  7. import apiUser from '../../api/apiUser';
  8. import Button, { ElevationObject } from '../../components/Button';
  9. import Dialog from '../../components/Dialog';
  10. import TextView from '../../components/TextView';
  11. import { PageList } from '../Router';
  12. import { MyRefreshProps } from '../../components/ThemesConfig';
  13. import utils from '../../utils/utils';
  14. export default class VehicleListV2 extends Component {
  15. constructor(props) {
  16. super(props);
  17. this.state = {
  18. vehicleList: [],
  19. refreshing: false
  20. };
  21. }
  22. componentDidMount() {
  23. //this.getVehicleList();
  24. this.props.navigation.addListener('focus', () => {
  25. this.getVehicleList();
  26. })
  27. }
  28. onRefresh() {
  29. this.setState({
  30. refreshing: true
  31. })
  32. this.getVehicleList();
  33. }
  34. getVehicleList() {
  35. apiUser.getVehicles().then(res => {
  36. if (res.data) {
  37. this.setState({
  38. vehicleList: res.data,
  39. refreshing: false
  40. });
  41. if (this.props.onResult) {
  42. this.props.onResult(res.data.length)
  43. }
  44. }
  45. }).catch(err => {
  46. this.setState({
  47. vehicleList: [],
  48. refreshing: false
  49. });
  50. if (this.props.onResult) {
  51. this.props.onResult(0)
  52. }
  53. });
  54. }
  55. removeVehicle(id) {
  56. Dialog.showDialog({
  57. title: $t('profile.removeVehicle'),
  58. message: $t('profile.tipRemoveVehicle'),
  59. callback: btn => {
  60. if (btn == 'ok') {
  61. Dialog.dismissLoading();
  62. this.deleteVehicle(id);
  63. }
  64. }
  65. })
  66. }
  67. deleteVehicle(id) {
  68. Dialog.showProgressDialog();
  69. apiUser.deleteVehicle({
  70. vehiclePk: id
  71. }).then(res => {
  72. Dialog.dismissLoading();
  73. toastShort($t('common.deleteSuccess'));
  74. this.getVehicleList();
  75. }).catch(err => {
  76. Dialog.dismissLoading();
  77. toastShort(err);
  78. });
  79. }
  80. listItem = ({item, index, separators}) => {
  81. return (
  82. <Pressable
  83. style={styles.vehicleView}
  84. android_ripple={ripple}
  85. onPress={() => {
  86. startPage(PageList.editVehicleV2, {id: item.vehiclePk});
  87. }}
  88. onLongPress={() => {
  89. this.removeVehicle(item.vehiclePk)
  90. }}>
  91. <View style={styles.vehicleRow}>
  92. <TextView style={styles.vehicleName}>{item.brand} {item.model}</TextView>
  93. </View>
  94. <TextView style={styles.vehicleModle}>{item.licensePlate}</TextView>
  95. <View style={styles.vehicleRow}>
  96. <TextView style={styles.vehicleType}>{item.connectorType + ", " + item.batteryCapacity + "KWH"}</TextView>
  97. {/* <View style={styles.vehicleTypeRow}>
  98. <View style={styles.vehicleTypeIcon}>
  99. <VehicleType size={10}/>
  100. </View>
  101. </View> */}
  102. </View>
  103. {/* <Pressable
  104. style={styles.closeIcon}
  105. android_ripple={rippleLess}
  106. onPress={() => this.removeVehicle(item.vehiclePk)}>
  107. <MaterialCommunityIcons
  108. name="close"
  109. size={20}
  110. color={textCancel}
  111. />
  112. </Pressable> */}
  113. { utils.isNotEmpty(item.vehiclePhoto) &&
  114. <Image
  115. resizeMode="contain"
  116. style={styles.vehicleViewBg}
  117. source={{uri: utils.getImageUrl(item.vehiclePhoto)}}/>
  118. }
  119. </Pressable>
  120. )
  121. }
  122. listHeader = () => {
  123. return (
  124. <View style={$padding(8, 16)}>
  125. <TextView style={styles.titleText}>{$t('profile.titleVehicles')}</TextView>
  126. <Button
  127. style={styles.addButton}
  128. onClick={() => startPage(PageList.addVehicleV2)}>
  129. <MaterialIcons
  130. name="add-circle"
  131. size={18}
  132. color={colorPrimary}
  133. />
  134. <TextView style={styles.addBtnText}>{$t('profile.btnAdd')}</TextView>
  135. </Button>
  136. </View>
  137. )
  138. }
  139. render() {
  140. return (
  141. <FlatList
  142. style={ui.flex1}
  143. data={this.state.vehicleList}
  144. contentContainerStyle={$padding(8, 0)}
  145. renderItem={this.listItem}
  146. keyExtractor={item => item?.vehiclePk}
  147. //onEndReached={() => this.getVehicleListPage()}
  148. refreshControl={
  149. <RefreshControl
  150. {...MyRefreshProps()}
  151. refreshing={this.state.refreshing}
  152. onRefresh={() => this.onRefresh()}
  153. />
  154. }
  155. ListHeaderComponent={this.listHeader}
  156. ListEmptyComponent={<Text style={ui.noData}>{$t('profile.noVehicles')}</Text>}
  157. />
  158. );
  159. }
  160. }
  161. const styles = StyleSheet.create({
  162. titleText: {
  163. fontSize: 14,
  164. color: textPrimary,
  165. fontWeight: 'bold'
  166. },
  167. vehicleView: {
  168. borderWidth: 1,
  169. borderRadius: 4,
  170. borderStyle: 'solid',
  171. borderColor: '#EAEAEA',
  172. overflow: 'hidden',
  173. //...ElevationObject(5),
  174. ...$margin(8, 16),
  175. ...$padding(12, 16, 12),
  176. backgroundColor: colorLight
  177. },
  178. vehicleViewBg: {
  179. top: 16,
  180. right: 16,
  181. width: 40,
  182. height: 40,
  183. position: 'absolute'
  184. },
  185. vehicleRow: {
  186. alignItems: 'center',
  187. flexDirection: 'row',
  188. },
  189. vehicleIcon: {
  190. width: 32,
  191. height: 32,
  192. },
  193. vehicleName: {
  194. color: textDark,
  195. fontSize: 14,
  196. fontWeight: 'bold'
  197. },
  198. vehicleModle: {
  199. color: textPrimary,
  200. fontSize: 14,
  201. paddingTop: 4,
  202. paddingBottom: 4
  203. },
  204. vehicleTypeRow: {
  205. height: 20,
  206. marginLeft: 44,
  207. borderRadius: 4,
  208. overflow: 'hidden',
  209. alignItems: 'center',
  210. flexDirection: 'row',
  211. backgroundColor: '#E5EFDE'
  212. },
  213. vehicleTypeIcon: {
  214. width: 20,
  215. height: 20,
  216. alignItems: 'center',
  217. justifyContent: 'center',
  218. backgroundColor: colorAccent
  219. },
  220. vehicleType: {
  221. color: colorAccent,
  222. fontSize: 14
  223. },
  224. closeIcon: {
  225. top: 0,
  226. right: 0,
  227. padding: 12,
  228. position: 'absolute'
  229. },
  230. addButton: {
  231. marginTop: 16,
  232. borderWidth: 1,
  233. borderRadius: 4,
  234. borderStyle: 'solid',
  235. borderColor: colorPrimary,
  236. backgroundColor: 'transparent'
  237. },
  238. addBtnText: {
  239. fontSize: 15,
  240. color: colorPrimary,
  241. fontWeight: 'bold',
  242. paddingLeft: 8
  243. }
  244. });