VehicleListV2.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. showTypeCapacity(item) {
  81. let str = "";
  82. if (item.connectorType) {
  83. str += item.connectorType;
  84. }
  85. if (item.batteryCapacity) {
  86. if (str) {
  87. str += ", ";
  88. }
  89. str += item.batteryCapacity + "KWH";
  90. }
  91. return str;
  92. }
  93. listItem = ({item, index, separators}) => {
  94. return (
  95. <Pressable
  96. style={styles.vehicleView}
  97. android_ripple={ripple}
  98. onPress={() => {
  99. startPage(PageList.editVehicleV2, {id: item.vehiclePk});
  100. }}
  101. onLongPress={() => {
  102. this.removeVehicle(item.vehiclePk)
  103. }}>
  104. <View style={styles.vehicleRow}>
  105. <TextView style={styles.vehicleName}>{item.brand} {item.model}</TextView>
  106. </View>
  107. <TextView style={styles.vehicleModle}>{item.licensePlate}</TextView>
  108. <View style={styles.vehicleRow}>
  109. <TextView style={styles.vehicleType}>{this.showTypeCapacity(item)}</TextView>
  110. {/* <View style={styles.vehicleTypeRow}>
  111. <View style={styles.vehicleTypeIcon}>
  112. <VehicleType size={10}/>
  113. </View>
  114. </View> */}
  115. </View>
  116. {/* <Pressable
  117. style={styles.closeIcon}
  118. android_ripple={rippleLess}
  119. onPress={() => this.removeVehicle(item.vehiclePk)}>
  120. <MaterialCommunityIcons
  121. name="close"
  122. size={20}
  123. color={textCancel}
  124. />
  125. </Pressable> */}
  126. { utils.isNotEmpty(item.vehiclePhoto) &&
  127. <Image
  128. resizeMode="contain"
  129. style={styles.vehicleViewBg}
  130. source={{uri: utils.getImageUrl(item.vehiclePhoto)}}/>
  131. }
  132. </Pressable>
  133. )
  134. }
  135. listHeader = () => {
  136. return (
  137. <View style={$padding(8, 16)}>
  138. <TextView style={styles.titleText}>{$t('profile.titleVehicles')}</TextView>
  139. <Button
  140. style={styles.addButton}
  141. onClick={() => startPage(PageList.addVehicleV2)}>
  142. <MaterialIcons
  143. name="add-circle"
  144. size={18}
  145. color={colorPrimary}
  146. />
  147. <TextView style={styles.addBtnText}>{$t('profile.btnAdd')}</TextView>
  148. </Button>
  149. </View>
  150. )
  151. }
  152. render() {
  153. return (
  154. <FlatList
  155. style={ui.flex1}
  156. data={this.state.vehicleList}
  157. contentContainerStyle={$padding(8, 0)}
  158. renderItem={this.listItem}
  159. keyExtractor={item => item?.vehiclePk}
  160. //onEndReached={() => this.getVehicleListPage()}
  161. refreshControl={
  162. <RefreshControl
  163. {...MyRefreshProps()}
  164. refreshing={this.state.refreshing}
  165. onRefresh={() => this.onRefresh()}
  166. />
  167. }
  168. ListHeaderComponent={this.listHeader}
  169. ListEmptyComponent={<Text style={ui.noData}>{$t('profile.noVehicles')}</Text>}
  170. />
  171. );
  172. }
  173. }
  174. const styles = StyleSheet.create({
  175. titleText: {
  176. fontSize: 14,
  177. color: textPrimary,
  178. fontWeight: 'bold'
  179. },
  180. vehicleView: {
  181. borderWidth: 1,
  182. borderRadius: 4,
  183. borderStyle: 'solid',
  184. borderColor: '#EAEAEA',
  185. overflow: 'hidden',
  186. //...ElevationObject(5),
  187. ...$margin(8, 16),
  188. ...$padding(12, 16, 12),
  189. backgroundColor: colorLight
  190. },
  191. vehicleViewBg: {
  192. top: 16,
  193. right: 16,
  194. width: 40,
  195. height: 40,
  196. position: 'absolute'
  197. },
  198. vehicleRow: {
  199. alignItems: 'center',
  200. flexDirection: 'row',
  201. },
  202. vehicleIcon: {
  203. width: 32,
  204. height: 32,
  205. },
  206. vehicleName: {
  207. color: textDark,
  208. fontSize: 14,
  209. fontWeight: 'bold'
  210. },
  211. vehicleModle: {
  212. color: textPrimary,
  213. fontSize: 14,
  214. paddingTop: 4,
  215. paddingBottom: 4
  216. },
  217. vehicleTypeRow: {
  218. height: 20,
  219. marginLeft: 44,
  220. borderRadius: 4,
  221. overflow: 'hidden',
  222. alignItems: 'center',
  223. flexDirection: 'row',
  224. backgroundColor: '#E5EFDE'
  225. },
  226. vehicleTypeIcon: {
  227. width: 20,
  228. height: 20,
  229. alignItems: 'center',
  230. justifyContent: 'center',
  231. backgroundColor: colorAccent
  232. },
  233. vehicleType: {
  234. color: colorAccent,
  235. fontSize: 14
  236. },
  237. closeIcon: {
  238. top: 0,
  239. right: 0,
  240. padding: 12,
  241. position: 'absolute'
  242. },
  243. addButton: {
  244. marginTop: 16,
  245. borderWidth: 1,
  246. borderRadius: 4,
  247. borderStyle: 'solid',
  248. borderColor: colorPrimary,
  249. backgroundColor: 'transparent'
  250. },
  251. addBtnText: {
  252. fontSize: 15,
  253. color: colorPrimary,
  254. fontWeight: 'bold',
  255. paddingLeft: 8
  256. }
  257. });