MainSwiper.tsx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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.75
  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="h50 bold" style={{marginTop:rpxToPx(24)}}>{t('health.today')}</Text>
  171. <Text className="main_date g01" >{global.language == 'en' ? dayjs().format('dddd, MMM D') : dayjs().format('MMMD日 dddd')}</Text>
  172. <Swiper
  173. onTransition={onTransition}
  174. onAnimationFinish={onTransitionend}
  175. style={{ height: rpxToPx(218), width: rpxToPx(634) }} circular
  176. previousMargin={rpxToPx(634/3)}
  177. nextMargin={rpxToPx(634/3)}
  178. onChange={pageChanged}
  179. current={current}>
  180. <SwiperItem>
  181. <MainDayNightCard scale={current == 0 ? 1 : maxScale} count={props.count} typeChanged={typeChanged} id={1} onClick={() => {
  182. if (current == 0) {
  183. switchMode()
  184. }
  185. else {
  186. setCurrent(0)
  187. }
  188. }} />
  189. </SwiperItem>
  190. <SwiperItem>
  191. <MainFastEatCard scale={current == 1 ? 1 : maxScale} count={props.count} typeChanged={typeChanged} id={2} onClick={() => {
  192. if (current == 1) {
  193. switchMode()
  194. }
  195. else {
  196. setCurrent(1)
  197. }
  198. }} />
  199. </SwiperItem>
  200. <SwiperItem>
  201. <MainSleepActiveCard scale={current == 2 ? 1 : maxScale} count={props.count} typeChanged={typeChanged} id={3} onClick={() => {
  202. if (current == 2) {
  203. switchMode()
  204. }
  205. else {
  206. setCurrent(2)
  207. }
  208. }} />
  209. </SwiperItem>
  210. {/* <SwiperItem>
  211. <MainDayNightCard scale={current==3?1:maxScale} count={props.count} typeChanged={typeChanged} id={7} onClick={() => {
  212. setCurrent(3)
  213. }} />
  214. </SwiperItem>
  215. <SwiperItem>
  216. <MainFastEatCard scale={current==4?1:maxScale} count={props.count} typeChanged={typeChanged} id={8} onClick={() => {
  217. setCurrent(4)
  218. }} />
  219. </SwiperItem>
  220. <SwiperItem>
  221. <MainSleepActiveCard scale={current==5?1:maxScale} count={props.count} typeChanged={typeChanged} id={9} onClick={() => {
  222. setCurrent(5)
  223. }} />
  224. </SwiperItem> */}
  225. </Swiper>
  226. {/* <View style={{flexDirection:'row',display:'flex'}}>
  227. <View style={{backgroundColor:current==0?'#f0f0f0':'#fff'}} onClick={()=>setCurrent(0)}>
  228. <MainDayNightCard count={props.count} typeChanged={typeChanged} id={4}/>
  229. </View>
  230. <View style={{backgroundColor:current==1?'#f0f0f0':'#fff'}} onClick={()=>setCurrent(1)}>
  231. <MainFastEatCard count={props.count} typeChanged={typeChanged} id={5}/>
  232. </View>
  233. <View style={{backgroundColor:current==2?'#f0f0f0':'#fff'}} onClick={()=>setCurrent(2)}>
  234. <MainSleepActiveCard count={props.count} typeChanged={typeChanged} id={6}/>
  235. </View>
  236. </View> */}
  237. </View>
  238. }