notificationTabViewComponent.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //the size of the TabView will follow its parent-container's size.
  2. import * as React from 'react';
  3. import { View, Text, useWindowDimensions, StyleSheet, ScrollView, ActivityIndicator, Pressable } from 'react-native';
  4. import { TabView, SceneMap, TabBar } from 'react-native-tab-view';
  5. import { formatToChineseDateTime } from '../../util/lib';
  6. import { useCallback, useMemo, useEffect, useState } from 'react';
  7. import { notificationStorage } from '../notificationStorage';
  8. import { router, useFocusEffect } from 'expo-router';
  9. export interface TabItem {
  10. title: string;
  11. description: string;
  12. date: string;
  13. }
  14. interface TabViewComponentProps {
  15. titles: string[];
  16. reservationAfter2025: any;
  17. passingThisPromotionToBell: any;
  18. }
  19. const NotificationRow = ({
  20. promotionObj,
  21. index,
  22. viewedNotifications
  23. }: {
  24. promotionObj: any;
  25. index: number;
  26. viewedNotifications: any;
  27. }) => {
  28. const isViewed = viewedNotifications.some(
  29. (notification: any) => notification.id === promotionObj.id && notification.type === 'promotion'
  30. );
  31. const handlePress = async () => {
  32. // Mark promotion as viewed
  33. await notificationStorage.markAsViewed(promotionObj.id, 'promotion');
  34. router.push({
  35. pathname: '/notificationDetailPage',
  36. params: { promotion: JSON.stringify(promotionObj) }
  37. });
  38. };
  39. return (
  40. <Pressable
  41. onPress={handlePress}
  42. style={{ opacity: isViewed ? 0.5 : 1 }}
  43. className={`flex flex-col w-full bg-white space-y-2 p-2 ${index % 2 === 0 ? 'bg-[#e7f2f8]' : 'bg-white'}`}
  44. >
  45. <View className="flex flex-row justify-between">
  46. <Text className="font-[500] text-base">{promotionObj.title}</Text>
  47. <Text>{formatToChineseDateTime(promotionObj.createdAt)}</Text>
  48. </View>
  49. <View>
  50. <Text numberOfLines={2} ellipsizeMode="tail" className="text-sm">
  51. {promotionObj.text}
  52. </Text>
  53. </View>
  54. </Pressable>
  55. );
  56. };
  57. const ReservationRow = ({
  58. reservationObj,
  59. index,
  60. viewedNotifications
  61. }: {
  62. reservationObj: any;
  63. index: number;
  64. viewedNotifications: any;
  65. }) => {
  66. const isViewed = viewedNotifications.some(
  67. (notification: any) => notification?.id === reservationObj?.id && notification.type === 'reservation'
  68. );
  69. const handlePress = async () => {
  70. await notificationStorage.markAsViewed(reservationObj.id, 'reservation');
  71. if (reservationObj.status.id === '7') {
  72. router.push({ pathname: '/chargingPage' });
  73. } else {
  74. router.push({ pathname: '/chargingDetailPage', params: { promotion: JSON.stringify(reservationObj) } });
  75. }
  76. };
  77. const title =
  78. reservationObj.status.id === '8' || reservationObj.status.id === '13'
  79. ? '充電完成'
  80. : reservationObj.status.id === '7'
  81. ? '充電進行中'
  82. : '';
  83. const text =
  84. reservationObj.status.id === '8' || reservationObj.status.id === '13'
  85. ? '親愛的用戶,您的愛車已充滿電完成!請盡快駛離充電站,以便其他車輛使用。感謝您的配合!'
  86. : reservationObj.status.id === '7'
  87. ? `您的車輛正在充電中,當時充電百分比為${reservationObj.Soc}%。詳情請按我進入充電頁面查看。`
  88. : '';
  89. return (
  90. <Pressable
  91. onPress={handlePress}
  92. style={{ opacity: isViewed ? 0.5 : 1 }}
  93. className={`flex flex-col w-full bg-white space-y-2 p-2 ${index % 2 === 0 ? 'bg-[#e7f2f8]' : 'bg-white'}`}
  94. >
  95. <View className="flex flex-row justify-between">
  96. <Text className={`font-[400] text-base ${reservationObj.status.id === '7' ? 'font-[700]' : ''}`}>
  97. {title}
  98. </Text>
  99. <Text>{formatToChineseDateTime(reservationObj.createdAt)}</Text>
  100. </View>
  101. <View>
  102. <Text numberOfLines={2} ellipsizeMode="tail" className="text-sm">
  103. {text}
  104. </Text>
  105. </View>
  106. </Pressable>
  107. );
  108. };
  109. const FirstRoute = ({ loading, reservationAfter2025 }: { loading: boolean; reservationAfter2025: any }) => {
  110. const [viewedNotifications, setViewedNotifications] = useState([]);
  111. useFocusEffect(
  112. useCallback(() => {
  113. const getViewedNotifications = async () => {
  114. const notifications = await notificationStorage.getViewedNotifications();
  115. setViewedNotifications(notifications);
  116. };
  117. getViewedNotifications();
  118. }, [])
  119. );
  120. return (
  121. <ScrollView
  122. style={{ flex: 1, backgroundColor: 'white', marginTop: 14 }}
  123. contentContainerStyle={{ paddingBottom: 120 }}
  124. >
  125. <View className="flex-1 flex-col">
  126. {loading ? (
  127. <View className="items-center justify-center">
  128. <ActivityIndicator />
  129. </View>
  130. ) : (
  131. <View>
  132. <View>
  133. {reservationAfter2025.length === 0 ? (
  134. <Text className="pl-4">暫時沒有充電資訊。</Text>
  135. ) : (
  136. reservationAfter2025
  137. .sort(
  138. (a: any, b: any) =>
  139. new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()
  140. )
  141. .filter(
  142. (reservationObj: any) =>
  143. reservationObj.status.id === '7' ||
  144. reservationObj.status.id === '8' ||
  145. reservationObj.status.id === '13'
  146. )
  147. .slice(0, 30)
  148. .map((reservationObj: any, index: any) => (
  149. <ReservationRow
  150. reservationObj={reservationObj}
  151. index={index}
  152. key={reservationObj.id}
  153. viewedNotifications={viewedNotifications}
  154. />
  155. ))
  156. )}
  157. </View>
  158. </View>
  159. )}
  160. </View>
  161. </ScrollView>
  162. );
  163. };
  164. const SecondRoute = ({ promotions, loading }: { promotions: TabItem[]; loading: boolean }) => {
  165. const [viewedNotifications, setViewedNotifications] = useState([]);
  166. useFocusEffect(
  167. useCallback(() => {
  168. const getViewedNotifications = async () => {
  169. const notifications = await notificationStorage.getViewedNotifications();
  170. setViewedNotifications(notifications);
  171. };
  172. getViewedNotifications();
  173. }, [])
  174. );
  175. return (
  176. <ScrollView
  177. style={{ flex: 1, backgroundColor: 'white', marginTop: 14 }}
  178. contentContainerStyle={{ paddingBottom: 120 }}
  179. >
  180. <View className="flex-1 flex-col">
  181. {loading ? (
  182. <View className="items-center justify-center">
  183. <ActivityIndicator />
  184. </View>
  185. ) : (
  186. <View>
  187. <View>
  188. {promotions.length === 0 ? (
  189. <Text className="pl-4">暫時沒有通知消息。</Text>
  190. ) : (
  191. promotions
  192. .sort(
  193. (a: any, b: any) =>
  194. new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()
  195. )
  196. .slice(0, 30)
  197. .map((promotionObj: any, index: any) => (
  198. <NotificationRow
  199. promotionObj={promotionObj}
  200. index={index}
  201. key={promotionObj.id}
  202. viewedNotifications={viewedNotifications}
  203. />
  204. ))
  205. )}
  206. </View>
  207. </View>
  208. )}
  209. </View>
  210. </ScrollView>
  211. );
  212. };
  213. const NotificationTabView: React.FC<TabViewComponentProps> = ({
  214. titles,
  215. reservationAfter2025,
  216. passingThisPromotionToBell
  217. }) => {
  218. const layout = useWindowDimensions();
  219. const [loading, setLoading] = useState(true);
  220. const renderScene = useCallback(
  221. ({ route }: { route: any }) => {
  222. switch (route.key) {
  223. case 'firstRoute':
  224. return <FirstRoute loading={loading} reservationAfter2025={reservationAfter2025} />;
  225. case 'secondRoute':
  226. return <SecondRoute promotions={passingThisPromotionToBell} loading={loading} />;
  227. default:
  228. return null;
  229. }
  230. },
  231. [reservationAfter2025, passingThisPromotionToBell]
  232. );
  233. useEffect(() => {
  234. setLoading(false)
  235. }, [reservationAfter2025, passingThisPromotionToBell]);
  236. const [routes] = React.useState([
  237. { key: 'firstRoute', title: titles[0] },
  238. { key: 'secondRoute', title: titles[1] }
  239. ]);
  240. const [index, setIndex] = React.useState(0);
  241. const renderTabBar = (props: any) => (
  242. <TabBar
  243. {...props}
  244. indicatorStyle={{
  245. backgroundColor: '#025c72'
  246. }}
  247. style={{
  248. backgroundColor: 'white',
  249. borderColor: '#DBE4E8',
  250. elevation: 0,
  251. marginHorizontal: 15,
  252. borderBottomWidth: 0.5
  253. }}
  254. />
  255. );
  256. return (
  257. <TabView
  258. navigationState={{ index, routes }}
  259. renderTabBar={renderTabBar}
  260. renderScene={renderScene}
  261. onIndexChange={setIndex}
  262. initialLayout={{ width: layout.width }}
  263. commonOptions={{
  264. label: ({ route, focused }) => (
  265. <Text
  266. style={{
  267. color: focused ? '#025c72' : '#888888',
  268. fontWeight: focused ? '900' : 'thin',
  269. fontSize: 20
  270. }}
  271. >
  272. {route.title}
  273. </Text>
  274. )
  275. }}
  276. lazy
  277. />
  278. );
  279. };
  280. export default NotificationTabView;
  281. const styles = StyleSheet.create({
  282. container: { flexDirection: 'row' },
  283. image: { width: 100, height: 100, margin: 15, borderRadius: 10 },
  284. textContainer: { flexDirection: 'column', gap: 8, marginTop: 20 },
  285. floatingButton: {
  286. elevation: 5,
  287. shadowColor: '#000',
  288. shadowOffset: { width: 0, height: 2 },
  289. shadowOpacity: 0.25,
  290. shadowRadius: 3.84
  291. }
  292. });