reservationLocationPageComponent.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. import Svg, { Path, Rect } from 'react-native-svg';
  2. import * as React from 'react';
  3. import {
  4. View,
  5. Text,
  6. useWindowDimensions,
  7. StyleSheet,
  8. Image,
  9. ImageSourcePropType,
  10. Pressable,
  11. ActivityIndicator
  12. } from 'react-native';
  13. import { TabView, SceneMap, TabBar } from 'react-native-tab-view';
  14. import { FlashList } from '@shopify/flash-list';
  15. import { SafeAreaView } from 'react-native-safe-area-context';
  16. import { router } from 'expo-router';
  17. import { CheckMarkLogoSvg, CrossLogoSvg } from '../global/SVG';
  18. import { useEffect, useState } from 'react';
  19. import { chargeStationService } from '../../service/chargeStationService';
  20. import * as Location from 'expo-location';
  21. import { calculateDistance } from '../global/distanceCalculator';
  22. interface TabItem {
  23. imgURL: ImageSourcePropType;
  24. date: string;
  25. time: string;
  26. chargeStationName: string;
  27. chargeStationAddress: string;
  28. distance: string;
  29. stationID: string;
  30. lat: string;
  31. lng: string;
  32. }
  33. interface TabViewComponentProps {
  34. titles: string[];
  35. tabItems: TabItem[];
  36. }
  37. const ReservationLocationPage = () => {
  38. const [stations, setStations] = useState([]);
  39. const [TabItems, setTabItems] = useState<TabItem[]>([]);
  40. const [currentLocation, setCurrentLocation] = useState<Location.LocationObject | null>(null);
  41. const getCurrentLocation = async () => {
  42. let { status } = await Location.requestForegroundPermissionsAsync();
  43. if (status !== 'granted') {
  44. console.error('Permission to access location was denied');
  45. return;
  46. }
  47. let location = await Location.getLastKnownPositionAsync({});
  48. setCurrentLocation(location);
  49. };
  50. useEffect(() => {
  51. getCurrentLocation();
  52. }, []);
  53. const LocationTabComponent: React.FC<TabViewComponentProps> = ({ titles, tabItems }) => {
  54. const FirstRoute = () => (
  55. <View
  56. style={{
  57. flex: 1,
  58. backgroundColor: 'white'
  59. }}
  60. >
  61. {TabItems.length > 0 ? (
  62. <FlashList
  63. data={tabItems}
  64. renderItem={({ item }) => {
  65. return (
  66. <Pressable
  67. onPress={() =>
  68. router.push({
  69. pathname: '/resultDetailPage',
  70. params: {
  71. chargeStationAddress: item.chargeStationAddress,
  72. chargeStationName: item.chargeStationName,
  73. chargeStationID: item.stationID,
  74. chargeStationLng: item.lng,
  75. chargeStationLat: item.lat
  76. }
  77. })
  78. }
  79. style={({ pressed }) => [
  80. styles.container,
  81. {
  82. backgroundColor: pressed ? '#e7f2f8' : '#ffffff'
  83. }
  84. ]}
  85. >
  86. <View style={styles.container} className=" flex-1 flex-row ">
  87. <Image style={styles.image} source={item.imgURL} />
  88. <View style={styles.textContainer} className="flex-1 justify-evenly">
  89. <Text
  90. style={{
  91. fontWeight: 400,
  92. fontSize: 20,
  93. color: '#222222'
  94. }}
  95. >
  96. {item.chargeStationName}
  97. </Text>
  98. <Text
  99. style={{
  100. fontWeight: 400,
  101. fontSize: 14,
  102. color: '#888888'
  103. }}
  104. >
  105. {item.chargeStationAddress}
  106. </Text>
  107. <View className=" flex-row space-x-2 items-center">
  108. <CheckMarkLogoSvg />
  109. <Text
  110. style={{
  111. fontWeight: 400,
  112. fontSize: 14,
  113. color: '#222222'
  114. }}
  115. >
  116. Walk-in
  117. </Text>
  118. </View>
  119. </View>
  120. <Text
  121. style={{
  122. fontWeight: 400,
  123. fontSize: 16,
  124. color: '#888888',
  125. marginTop: 22,
  126. marginLeft: 'auto',
  127. marginRight: 10
  128. }}
  129. >
  130. {item.distance}
  131. </Text>
  132. </View>
  133. </Pressable>
  134. );
  135. }}
  136. />
  137. ) : (
  138. <View className="flex-1 items-center justify-center">
  139. <ActivityIndicator size="large" />
  140. </View>
  141. )}
  142. </View>
  143. );
  144. //tab 2
  145. const SecondRoute = () => (
  146. <View
  147. style={{
  148. flex: 1,
  149. backgroundColor: 'white'
  150. }}
  151. >
  152. <FlashList
  153. data={tabItems}
  154. renderItem={({ item }) => {
  155. return (
  156. <Pressable
  157. onPress={() =>
  158. router.push({
  159. pathname: '/resultDetailPage',
  160. params: {
  161. chargeStationAddress: item.chargeStationAddress,
  162. chargeStationName: item.chargeStationName,
  163. chargeStationLng: item.lng,
  164. chargeStationLat: item.lat
  165. }
  166. })
  167. }
  168. style={({ pressed }) => [
  169. styles.container,
  170. {
  171. backgroundColor: pressed ? '#e7f2f8' : '#ffffff'
  172. }
  173. ]}
  174. >
  175. <View style={styles.container} className=" flex-1 flex-row ">
  176. <Image style={styles.image} source={item.imgURL} />
  177. <View style={styles.textContainer} className="flex-1 justify-evenly">
  178. <Text
  179. style={{
  180. fontWeight: 400,
  181. fontSize: 20,
  182. color: '#222222'
  183. }}
  184. >
  185. {item.chargeStationName}
  186. </Text>
  187. <Text
  188. style={{
  189. fontWeight: 400,
  190. fontSize: 14,
  191. color: '#888888'
  192. }}
  193. >
  194. {item.chargeStationAddress}
  195. </Text>
  196. <View className=" flex-row space-x-2 items-center">
  197. <CheckMarkLogoSvg />
  198. <Text
  199. style={{
  200. fontWeight: 400,
  201. fontSize: 14,
  202. color: '#222222'
  203. }}
  204. >
  205. Walk-in
  206. </Text>
  207. </View>
  208. </View>
  209. <Text
  210. style={{
  211. fontWeight: 400,
  212. fontSize: 16,
  213. color: '#888888',
  214. marginTop: 22,
  215. marginLeft: 'auto',
  216. marginRight: 10
  217. }}
  218. >
  219. {item.distance}
  220. </Text>
  221. </View>
  222. </Pressable>
  223. );
  224. }}
  225. />
  226. </View>
  227. );
  228. const renderScene = SceneMap({
  229. firstRoute: FirstRoute,
  230. secondRoute: SecondRoute
  231. });
  232. const [routes] = React.useState([
  233. { key: 'firstRoute', title: titles[0] },
  234. { key: 'secondRoute', title: titles[1] }
  235. ]);
  236. const [index, setIndex] = React.useState(0);
  237. const renderTabBar = (props: any) => (
  238. <TabBar
  239. {...props}
  240. indicatorStyle={{
  241. backgroundColor: '#025c72'
  242. }}
  243. style={{
  244. backgroundColor: 'white',
  245. borderColor: '#DBE4E8',
  246. elevation: 0,
  247. borderBottomWidth: 0.5
  248. }}
  249. />
  250. );
  251. return (
  252. <TabView
  253. navigationState={{ index, routes }}
  254. renderScene={renderScene}
  255. onIndexChange={setIndex}
  256. // initialLayout={{ width: layout.width }}
  257. renderTabBar={renderTabBar}
  258. commonOptions={{
  259. label: ({ route, focused }) => (
  260. <Text
  261. style={{
  262. color: focused ? '#025c72' : '#888888',
  263. fontWeight: focused ? '900' : 'thin',
  264. fontSize: 20
  265. }}
  266. >
  267. {route.title}
  268. </Text>
  269. )
  270. }}
  271. />
  272. );
  273. };
  274. const styles = StyleSheet.create({
  275. container: { flexDirection: 'row' },
  276. image: {
  277. width: 100,
  278. height: 100,
  279. marginTop: 15,
  280. marginRight: 15,
  281. borderRadius: 10
  282. },
  283. textContainer: {
  284. flexDirection: 'column',
  285. gap: 4,
  286. marginTop: 15
  287. }
  288. });
  289. // const fetchStations = async () => {
  290. // const fetchedStations = await chargeStationService.fetchChargeStations();
  291. // setStations(fetchedStations);
  292. // const TabItems = fetchedStations.map((station) => ({
  293. // chargeStationAddress: station.Address,
  294. // chargeStationName: station.StationName,
  295. // lng: station.StationLng,
  296. // lat: station.StationLat,
  297. // date: '今天',
  298. // stationID: station.StationID,
  299. // imgURL: require('../../assets/dummyStationPicture.png'),
  300. // distance: '400米'
  301. // }));
  302. // setTabItems(TabItems);
  303. // };
  304. // fetchStations();
  305. // }, []);
  306. const fetchStations = async () => {
  307. const fetchedStations = await chargeStationService.fetchChargeStations();
  308. setStations(fetchedStations);
  309. if (currentLocation) {
  310. const TabItems = await Promise.all(
  311. fetchedStations.map(async (station) => {
  312. const distance = await calculateDistance(
  313. Number(station.StationLat),
  314. Number(station.StationLng),
  315. currentLocation
  316. );
  317. return {
  318. chargeStationAddress: station.Address,
  319. chargeStationName: station.StationName,
  320. lng: station.StationLng,
  321. lat: station.StationLat,
  322. date: '今天',
  323. stationID: station.StationID,
  324. imgURL: require('../../assets/dummyStationPicture.png'),
  325. distance: distance !== null ? formatDistance(distance) : 'N/A'
  326. };
  327. })
  328. );
  329. setTabItems(TabItems);
  330. }
  331. };
  332. useEffect(() => {
  333. if (currentLocation) {
  334. fetchStations();
  335. }
  336. }, [currentLocation]);
  337. const formatDistance = (distanceInMeters: number): string => {
  338. if (distanceInMeters < 1000) {
  339. return `${Math.round(distanceInMeters)}米`;
  340. } else {
  341. const distanceInKm = distanceInMeters / 1000;
  342. return `${distanceInKm.toFixed(1)}公里`;
  343. }
  344. };
  345. return (
  346. <SafeAreaView className="flex-1 bg-white" edges={['top', 'left', 'right']}>
  347. <View className="flex-1">
  348. <View className="min-h-[250px] flex-column mx-[5%]">
  349. <View className="flex-1 justify-center">
  350. <View className="pt-5 pl-4">
  351. <Pressable
  352. onPress={() => {
  353. if (router.canGoBack()) {
  354. router.back();
  355. } else {
  356. router.replace('/');
  357. }
  358. }}
  359. >
  360. <CrossLogoSvg />
  361. </Pressable>
  362. </View>
  363. </View>
  364. <View className="flex-1 justify-center ">
  365. <Text className="text-[50px] font-normal ">預約地點</Text>
  366. </View>
  367. <View className="flex-1 justify-center ">
  368. <Pressable onPress={() => router.push('searchPage')}>
  369. <View
  370. style={{
  371. borderWidth: 1,
  372. padding: 24,
  373. borderRadius: 12,
  374. borderColor: '#bbbbbb',
  375. maxWidth: '100%'
  376. }}
  377. >
  378. <Text
  379. style={{
  380. color: '#888888',
  381. fontSize: 16
  382. }}
  383. >
  384. 搜尋充電站或地區..
  385. </Text>
  386. </View>
  387. </Pressable>
  388. </View>
  389. </View>
  390. <View className="flex-1 mt-2">
  391. <LocationTabComponent titles={['附近的充電站', '所有的充電站']} tabItems={TabItems} />
  392. </View>
  393. </View>
  394. </SafeAreaView>
  395. );
  396. };
  397. export default ReservationLocationPage;