MainSwiper.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import { View, Swiper, SwiperItem, Text } from "@tarojs/components";
  2. import './MainCard.scss';
  3. import MainDayNightCard from "./MainDayNightCard";
  4. import MainFastEatCard from "./MainFastEatCard";
  5. import MainSleepActiveCard from "./MainSleepActiveCard";
  6. import { useEffect, useState } from "react";
  7. import { WindowType } from "@/utils/types";
  8. import { useDispatch, useSelector } from "react-redux";
  9. import { setMode, setTab } from "@/store/health";
  10. import { MainColorType } from "@/context/themes/color";
  11. import { rpxToPx } from "@/utils/tools";
  12. import dayjs from "dayjs";
  13. import { useTranslation } from "react-i18next";
  14. let scrollPage = 0;
  15. let maxScale = 0.66
  16. export default function MainSwiper(props: { count: number, pageChanged: Function, typeChanged: Function }) {
  17. const [current, setCurrent] = useState(1)
  18. const [type, setType] = useState(WindowType.day)
  19. const health = useSelector((state: any) => state.health);
  20. const [scale0, setScale0] = useState(1.0)
  21. const [scale1, setScale1] = useState(maxScale)
  22. const [scale2, setScale2] = useState(maxScale)
  23. const {t} = useTranslation()
  24. const dispatch = useDispatch()
  25. useEffect(()=>{
  26. dispatch(setTab(1))
  27. },[])
  28. function pageChanged(e) {
  29. const page = e.detail.current
  30. console.log(page)
  31. setCurrent(page)
  32. props.pageChanged(page)
  33. var strType = WindowType.day
  34. if (page == 0) {
  35. strType = WindowType.day
  36. }
  37. else if (page == 1) {
  38. strType = WindowType.fast
  39. }
  40. else {
  41. strType = WindowType.sleep
  42. }
  43. props.typeChanged(strType)
  44. dispatch(setTab(page % 3))
  45. }
  46. function typeChanged(e) {
  47. setType(e)
  48. props.typeChanged(e)
  49. }
  50. function onTransition(e) {
  51. let x = e.detail.dx
  52. let scale0 = 0
  53. let scale1 = 0
  54. let scale2 = 0
  55. const pageWidth = 143
  56. if (x >= -pageWidth && x <= pageWidth) {
  57. switch (scrollPage) {
  58. case 0:
  59. case 3:
  60. {
  61. scale0 = 1 - Math.abs(x) / pageWidth * 0.2
  62. scale1 = x > 0 ? 1 - (pageWidth - x) / pageWidth * 0.2 : maxScale
  63. scale2 = x < 0 ? 1 - (pageWidth + x) / pageWidth * 0.2 : maxScale
  64. }
  65. break;
  66. case 1:
  67. case 4:
  68. {
  69. scale0 = x < 0 ? 1 - (pageWidth + x) / pageWidth * 0.2 : maxScale
  70. scale1 = 1 - Math.abs(x) / pageWidth * 0.2
  71. scale2 = x > 0 ? 1 - (pageWidth - x) / pageWidth * 0.2 : maxScale
  72. }
  73. break;
  74. case 2:
  75. case 5:
  76. {
  77. scale0 = x > 0 ? 1 - (pageWidth - x) / pageWidth * 0.2 : maxScale
  78. scale1 = x < 0 ? 1 - (pageWidth + x) / pageWidth * 0.2 : maxScale
  79. scale2 = 1 - Math.abs(x) / pageWidth * 0.2
  80. }
  81. break;
  82. }
  83. }
  84. else if (x >= -pageWidth * 2 && x <= pageWidth * 2) {
  85. switch (scrollPage) {
  86. case 0:
  87. case 3:
  88. {
  89. scale1 = 1 - (Math.abs(x) - pageWidth) / pageWidth * 0.2
  90. scale2 = x > 0 ? 1 - (pageWidth - x + pageWidth) / pageWidth * 0.2 : maxScale
  91. scale0 = x < 0 ? 1 - (pageWidth + x + pageWidth) / pageWidth * 0.2 : maxScale
  92. }
  93. break;
  94. case 1:
  95. case 4:
  96. {
  97. scale1 = x < 0 ? 1 - (pageWidth + x + pageWidth) / pageWidth * 0.2 : maxScale
  98. scale2 = 1 - (Math.abs(x) - pageWidth) / pageWidth * 0.2
  99. scale0 = x > 0 ? 1 - (pageWidth - x + pageWidth) / pageWidth * 0.2 : maxScale
  100. }
  101. break;
  102. case 2:
  103. case 5:
  104. {
  105. scale1 = x > 0 ? 1 - (pageWidth - x + pageWidth) / pageWidth * 0.2 : maxScale
  106. scale2 = x < 0 ? 1 - (pageWidth + x + pageWidth) / pageWidth * 0.2 : maxScale
  107. scale0 = 1 - (Math.abs(x) - pageWidth) / pageWidth * 0.2
  108. }
  109. break;
  110. }
  111. }
  112. else if (x >= -pageWidth * 3 && x <= pageWidth * 3) {
  113. switch (scrollPage) {
  114. case 0:
  115. case 3:
  116. {
  117. scale2 = 1 - (Math.abs(x) - pageWidth * 2) / pageWidth * 0.2
  118. scale0 = x > 0 ? 1 - (pageWidth - x + pageWidth * 2) / pageWidth * 0.2 : maxScale
  119. scale1 = x < 0 ? 1 - (pageWidth + x + pageWidth * 2) / pageWidth * 0.2 : maxScale
  120. }
  121. break;
  122. case 1:
  123. case 4:
  124. {
  125. scale2 = x < 0 ? 1 - (pageWidth + x + pageWidth * 2) / pageWidth * 0.2 : maxScale
  126. scale0 = 1 - (Math.abs(x) - pageWidth * 2) / pageWidth * 0.2
  127. scale1 = x > 0 ? 1 - (pageWidth - x + pageWidth * 2) / pageWidth * 0.2 : maxScale
  128. }
  129. break;
  130. case 2:
  131. case 5:
  132. {
  133. scale2 = x > 0 ? 1 - (pageWidth - x + pageWidth * 2) / pageWidth * 0.2 : maxScale
  134. scale0 = x < 0 ? 1 - (pageWidth + x + pageWidth * 2) / pageWidth * 0.2 : maxScale
  135. scale1 = 1 - (Math.abs(x) - pageWidth * 2) / pageWidth * 0.2
  136. }
  137. break;
  138. }
  139. }
  140. setScale0(scale0)
  141. setScale1(scale1)
  142. setScale2(scale2)
  143. }
  144. function onTransitionend() {
  145. scrollPage = current
  146. }
  147. function switchMode() {
  148. switch (health.mode) {
  149. case 'FAST':
  150. dispatch(setMode('EAT'));
  151. break
  152. case 'EAT':
  153. dispatch(setMode('FAST'));
  154. break
  155. case 'DAY':
  156. dispatch(setMode('NIGHT'));
  157. break
  158. case 'NIGHT':
  159. dispatch(setMode('DAY'));
  160. break
  161. case 'SLEEP':
  162. dispatch(setMode('ACTIVE'));
  163. break
  164. case 'ACTIVE':
  165. dispatch(setMode('SLEEP'));
  166. break
  167. }
  168. }
  169. return <View style={{ flexDirection: 'column', display: 'flex', alignItems: 'center' }}>
  170. <Text className="today">{t('health.today')}</Text>
  171. <Text className="main_date">{global.language == 'en' ? dayjs().format('dddd, MMM D') : dayjs().format('MMMD日, dddd')}</Text>
  172. {/* <View className="tabs">
  173. <View className="tab_item" style={{ backgroundColor: current != 0 ? '#B2B2B2' : isDay() ? MainColorType.day : MainColorType.night }} onClick={() => tapScroll(0)}></View>
  174. <View style={{ width: rpxToPx(40) }} />
  175. <View className="tab_item" style={{ backgroundColor: current != 1 ? '#B2B2B2' : isFast() ? MainColorType.fast : MainColorType.eat }} onClick={() => tapScroll(1)}></View>
  176. <View style={{ width: rpxToPx(40) }} />
  177. <View className="tab_item" style={{ backgroundColor: current != 2 ? '#B2B2B2' : isSleep() ? MainColorType.sleep : MainColorType.active }} onClick={() => tapScroll(2)}></View>
  178. </View> */}
  179. <Swiper
  180. onTransition={onTransition}
  181. onAnimationFinish={onTransitionend}
  182. style={{ height: 110, width: rpxToPx(634) }} circular
  183. previousMargin={rpxToPx(634/3)}
  184. nextMargin={rpxToPx(634/3)}
  185. onChange={pageChanged}
  186. current={current}>
  187. <SwiperItem>
  188. <MainDayNightCard scale={current == 0 ? 1 : maxScale} count={props.count} typeChanged={typeChanged} id={1} onClick={() => {
  189. if (current == 0) {
  190. switchMode()
  191. }
  192. else {
  193. setCurrent(0)
  194. }
  195. }} />
  196. </SwiperItem>
  197. <SwiperItem>
  198. <MainFastEatCard scale={current == 1 ? 1 : maxScale} count={props.count} typeChanged={typeChanged} id={2} onClick={() => {
  199. if (current == 1) {
  200. switchMode()
  201. }
  202. else {
  203. setCurrent(1)
  204. }
  205. }} />
  206. </SwiperItem>
  207. <SwiperItem>
  208. <MainSleepActiveCard scale={current == 2 ? 1 : maxScale} count={props.count} typeChanged={typeChanged} id={3} onClick={() => {
  209. if (current == 2) {
  210. switchMode()
  211. }
  212. else {
  213. setCurrent(2)
  214. }
  215. }} />
  216. </SwiperItem>
  217. {/* <SwiperItem>
  218. <MainDayNightCard scale={current==3?1:maxScale} count={props.count} typeChanged={typeChanged} id={7} onClick={() => {
  219. setCurrent(3)
  220. }} />
  221. </SwiperItem>
  222. <SwiperItem>
  223. <MainFastEatCard scale={current==4?1:maxScale} count={props.count} typeChanged={typeChanged} id={8} onClick={() => {
  224. setCurrent(4)
  225. }} />
  226. </SwiperItem>
  227. <SwiperItem>
  228. <MainSleepActiveCard scale={current==5?1:maxScale} count={props.count} typeChanged={typeChanged} id={9} onClick={() => {
  229. setCurrent(5)
  230. }} />
  231. </SwiperItem> */}
  232. </Swiper>
  233. {/* <View style={{flexDirection:'row',display:'flex'}}>
  234. <View style={{backgroundColor:current==0?'#f0f0f0':'#fff'}} onClick={()=>setCurrent(0)}>
  235. <MainDayNightCard count={props.count} typeChanged={typeChanged} id={4}/>
  236. </View>
  237. <View style={{backgroundColor:current==1?'#f0f0f0':'#fff'}} onClick={()=>setCurrent(1)}>
  238. <MainFastEatCard count={props.count} typeChanged={typeChanged} id={5}/>
  239. </View>
  240. <View style={{backgroundColor:current==2?'#f0f0f0':'#fff'}} onClick={()=>setCurrent(2)}>
  241. <MainSleepActiveCard count={props.count} typeChanged={typeChanged} id={6}/>
  242. </View>
  243. </View> */}
  244. </View>
  245. }