MainFastEatCard.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. import { View, Text, Image } from "@tarojs/components";
  2. import './MainCard.scss'
  3. import { useEffect, useRef, useState } from "react";
  4. import Modal from "@/components/layout/Modal.weapp";
  5. import { rpxToPx } from "@/utils/tools";
  6. import Rings, { RingCommon, BgRing, TargetRing, CurrentDot } from "@/features/trackTimeDuration/components/Rings";
  7. import dayjs from "dayjs";
  8. import moment from 'moment-timezone'
  9. import { MainColorType } from "@/context/themes/color";
  10. import { fastWindow, setSchedule, updateRecord } from "@/services/trackTimeDuration";
  11. import { useDispatch, useSelector } from "react-redux";
  12. import { jumpPage } from "../trackTimeDuration/hooks/Common";
  13. import ConsolePicker from "../trackTimeDuration/components/ConsolePicker";
  14. import { endFast, startFast } from "../trackTimeDuration/actions/TrackTimeActions";
  15. import formatMilliseconds from "@/utils/format_time";
  16. import TimePicker from "@/features/common/TimePicker";
  17. import showAlert from "@/components/basic/Alert";
  18. import showActionSheet from "@/components/basic/ActionSheet";
  19. import { records } from "@/services/health";
  20. import MainHistory from "./MainHistory";
  21. import { WindowStatusType, WindowType } from "@/utils/types";
  22. import { durationArc, isCurrentTimeInRange, startArc } from "./util";
  23. import { setMode } from "@/store/health";
  24. import { IconSwitch1, IconSwitch2 } from "@/components/basic/Icons";
  25. import { getDurationArc, getScenario, getStartArc, getWindowStatus } from "./hooks/health_hooks";
  26. import { useTranslation } from "react-i18next";
  27. let useNavigation;
  28. let checkNotification;
  29. let min = 0
  30. let max = 0
  31. let defaultTimestamp = 0
  32. let useActionSheet;
  33. if (process.env.TARO_ENV == 'rn') {
  34. useNavigation = require("@react-navigation/native").useNavigation
  35. checkNotification = require('@/utils/native_permission_check').checkNotification;
  36. useActionSheet = require('@expo/react-native-action-sheet').useActionSheet
  37. }
  38. export default function MainFastEatCard(props: {
  39. count: any,
  40. typeChanged: Function,
  41. id: number,
  42. onClick: Function,
  43. scale: number
  44. }) {
  45. const [showModal, setShowModal] = useState(false)
  46. const [showTimePicker, setShowTimePicker] = useState(false);
  47. const limitPickerRef = useRef(null)
  48. const [operateType, setOperateType] = useState('startFast')
  49. const [btnDisable, setBtnDisable] = useState(false)
  50. const [logEvent, setLogEvent] = useState('LOG_ONCE');
  51. const [eatData, setEatData] = useState<any>(null)
  52. const [fastData, setFastData] = useState<any>(null)
  53. const [status, setStatus] = useState<any>('upcoming')
  54. const [showPicker, setShowPicker] = useState(false)
  55. const [isStart, setIsStart] = useState(true)
  56. const user = useSelector((state: any) => state.user);
  57. const health = useSelector((state: any) => state.health);
  58. const dispatch = useDispatch()
  59. const { fast, eat } = health.windows.fast_eat
  60. const t1 = new Date().getTime()
  61. // const isTempFast = true
  62. // const isTempFast = eat.status!='OG'&&(fast.status == 'WAIT_FOR_END' ||
  63. // fast.target.start_time <= t1 && fast.target.end_time >= t1) ||
  64. // isCurrentTimeInRange(fast.period.start_time, fast.period.end_time)
  65. // const isTempFast = eat.status != 'OG' && (isCurrentTimeInRange(fast.period.start_time, fast.period.end_time) ||
  66. // fast.target.start_time <= t1 && fast.target.end_time >= t1 ||
  67. // fast.status == 'WAIT_FOR_END'
  68. // )
  69. const isTempFast = fast.status == 'OG' || (isCurrentTimeInRange(fast.period.start_time, fast.period.end_time) && eat.status == 'WFS')
  70. const [isFastMode, setIsFastMode] = useState(isTempFast)
  71. const { t } = useTranslation()
  72. useEffect(() => {
  73. if (health.mode == 'FAST') {
  74. setIsFastMode(true)
  75. }
  76. else if (health.mode == 'EAT') {
  77. setIsFastMode(false)
  78. }
  79. }, [health.mode])
  80. useEffect(() => {
  81. const { fast, eat } = health.windows.fast_eat
  82. if (isTempFast){
  83. setIsFastMode(true)
  84. dispatch(setMode('FAST'))
  85. }
  86. else {
  87. setIsFastMode(false)
  88. dispatch(setMode('EAT'))
  89. }
  90. // var now = new Date().getTime()
  91. // if (eat.status == 'OG') {
  92. // setIsFastMode(false)
  93. // debugger
  94. // dispatch(setMode('EAT'))
  95. // }
  96. // else if ((fast.status == 'WAIT_FOR_END' || fast.target.start_time <= now && fast.target.end_time >= now) || isCurrentTimeInRange(fast.period.start_time, fast.period.end_time)) {
  97. // setIsFastMode(true)
  98. // dispatch(setMode('FAST'))
  99. // }
  100. // else {
  101. // setIsFastMode(false)
  102. // debugger
  103. // dispatch(setMode('EAT'))
  104. // }
  105. setEatData(eat)
  106. setFastData(fast)
  107. update(fast)
  108. }, [])
  109. useEffect(() => {
  110. if (fastData) {
  111. update(fastData)
  112. }
  113. }, [props.count])
  114. useEffect(() => {
  115. if (health.selTab == 1) {
  116. dispatch(setMode(isFastMode ? 'FAST' : 'EAT'))
  117. }
  118. }, [health.selTab, isFastMode])
  119. function update(fast) {
  120. var now = new Date().getTime()
  121. if (fast.status == 'WAIT_FOR_END') {
  122. setStatus('process')
  123. }
  124. else if ((fast.target.start_timestamp <= now && fast.target.end_timestamp >= now) || isCurrentTimeInRange(fast.period.start_time, fast.period.end_time)) {
  125. // setStartTime(fast.period.start_time)
  126. setStatus('new')
  127. }
  128. else {
  129. setStatus('upcoming')
  130. }
  131. }
  132. const common: RingCommon = {
  133. useCase: 'ChooseScenario',
  134. radius: 27,
  135. lineWidth: 12,
  136. isFast: true,
  137. status: 'WAIT_FOR_START'
  138. }
  139. const bgRing: BgRing = {
  140. color: MainColorType.ringBg
  141. }
  142. function targetRing() {
  143. const { fast, eat } = health.windows.fast_eat
  144. var starts: any = fast.period.start_time.split(':')
  145. var ends: any = fast.period.end_time.split(':')
  146. const startSeconds: any = parseInt(starts[0] + '') * 60 + parseInt(starts[1] + '')
  147. const endSeconds: any = parseInt(ends[0] + '') * 60 + parseInt(ends[1] + '')
  148. const color = isFastMode ? MainColorType.fastLight : MainColorType.eatLight
  149. const startArc = isFastMode ? startSeconds / 1440 * 2 * Math.PI - Math.PI / 2 : endSeconds / 1440 * 2 * Math.PI - Math.PI / 2
  150. const fastCount = endSeconds - startSeconds > 0 ? endSeconds - startSeconds : endSeconds - startSeconds + 1440
  151. const eatCount = 1440 - fastCount
  152. const durationArc = isFastMode ? fastCount / 1440 * 2 * Math.PI : eatCount / 1440 * 2 * Math.PI
  153. if (isFastMode && getWindowStatus(health.windows, 'FAST') == WindowStatusType.process) {
  154. const scenario = getScenario(health.windows, 'FAST')
  155. return {
  156. color,
  157. startArc: getStartArc(scenario.target.start_timestamp),
  158. durationArc: getDurationArc(scenario.target.start_timestamp, scenario.target.end_timestamp)
  159. }
  160. }
  161. if (!isFastMode && getScenario(health.windows, 'EAT').real) {
  162. const scenario = getScenario(health.windows, 'EAT')
  163. return {
  164. color,
  165. startArc: getStartArc(scenario.target.start_timestamp),
  166. durationArc: getDurationArc(scenario.target.start_timestamp, scenario.target.end_timestamp)
  167. }
  168. }
  169. return {
  170. color,
  171. startArc,
  172. durationArc
  173. }
  174. }
  175. function realRing() {
  176. const status = getWindowStatus(health.windows, isFastMode ? 'FAST' : 'EAT')
  177. const scenario = getScenario(health.windows, isFastMode ? 'FAST' : 'EAT')
  178. if (status == WindowStatusType.upcoming) {
  179. return null
  180. // return {
  181. // color: '#cccccc',
  182. // startArc: getStartArc(new Date().getTime()),
  183. // durationArc: getDurationArc(new Date().getTime(), scenario.target.start_timestamp),
  184. // }
  185. }
  186. return {
  187. color: isFastMode ? MainColorType.fast : MainColorType.eat,
  188. startArc: getStartArc(scenario.target.start_timestamp),
  189. durationArc: getDurationArc(scenario.target.start_timestamp, new Date().getTime())
  190. }
  191. }
  192. function currentDot() {
  193. if (health.mode == 'FAST' || health.mode == 'EAT') {
  194. const status = getWindowStatus(health.windows, isFastMode ? 'FAST' : 'EAT')
  195. return {
  196. color: status == WindowStatusType.upcoming ? '#B2B2B2' : isFastMode ? MainColorType.fast : MainColorType.eat,
  197. lineWidth: 2,
  198. borderColor: '#F5F5F5',
  199. offset: 0
  200. }
  201. }
  202. return null;
  203. }
  204. function ring() {
  205. return <Rings common={common}
  206. bgRing={bgRing}
  207. targetRing={targetRing()}
  208. realRing={realRing()}
  209. canvasId={'smal11l' + props.id}
  210. currentDot={currentDot()}
  211. scale={props.scale ?? 1.0}
  212. />
  213. }
  214. function modalContent() {
  215. global.set_time = new Date().getTime()
  216. return <Modal
  217. testInfo={null}
  218. dismiss={() => {
  219. setShowTimePicker(false)
  220. }}
  221. confirm={() => { }}>
  222. {
  223. timePickerContent()
  224. }
  225. </Modal>
  226. }
  227. function timeContent() {
  228. return <Modal
  229. testInfo={null}
  230. dismiss={() => {
  231. setShowPicker(false)
  232. }}
  233. confirm={() => { }}>
  234. {
  235. pickerContent()
  236. }
  237. </Modal>
  238. }
  239. function timePickerContent() {
  240. var title = operateType == 'endFast' ? '结束断食' : '开始断食'
  241. var color = MainColorType.fast
  242. var endTimestamp = 0
  243. if (operateType == 'endFast') {
  244. endTimestamp = fastData.target.end_time
  245. }
  246. var duration = fastData.target.duration
  247. return <View className="modal_content">
  248. <ConsolePicker ref={limitPickerRef}
  249. themeColor={color}
  250. title={title}
  251. onCancel={() => {
  252. setShowTimePicker(false)
  253. }}
  254. min={min}
  255. max={max}
  256. current={defaultTimestamp}
  257. duration={duration}
  258. endTimestamp={endTimestamp}
  259. isFast={true}
  260. isEnd={operateType == 'endFast'}
  261. isTimeout={false}
  262. isLoading={btnDisable}
  263. onChange={(e) => {
  264. pickerConfirm(e, null)
  265. global.pauseIndexTimer = false
  266. }}
  267. />
  268. </View>
  269. }
  270. function pickerContent() {
  271. const timestamp = isStart ? fastData.target.start_time : fastData.target.end_time
  272. const strTime = dayjs(timestamp).format('HH:mm')
  273. return <TimePicker time={strTime}
  274. color={MainColorType.fast}
  275. title={isStart ? '开始断食' : '结束断食'}
  276. confirm={(e) => {
  277. confirmPickerTime(e)
  278. }}
  279. cancel={() => {
  280. setShowPicker(false)
  281. }} />
  282. }
  283. function confirmPickerTime(strTime) {
  284. if (status != 'process') {
  285. setSchedule({
  286. code: isStart ? 'FAST_START' : 'FAST_END',
  287. time: strTime
  288. }).then(res => {
  289. setShowPicker(false)
  290. })
  291. }
  292. else {
  293. var startTime = dayjs(fastData.target.start_time).format('HH:mm:ss')
  294. var endTime = strTime + ':00'
  295. console.log(startTime, endTime)
  296. if (startTime == endTime) {
  297. showAlert({
  298. title: '',
  299. content: '开始时间不能与结束时间相同'
  300. })
  301. return;
  302. }
  303. updateRecord({
  304. fast: {
  305. target_duration: getIntervalSeconds(startTime, endTime) * 1000
  306. }
  307. }, fastData.id).then(res => {
  308. setShowPicker(false)
  309. })
  310. }
  311. }
  312. function getIntervalSeconds(time1, time2) {
  313. // 将时间字符串转换为 Date 对象
  314. const date1 = new Date(`2000-01-01T${time1}Z`);
  315. const date2 = new Date(`2000-01-01T${time2}Z`);
  316. // 计算两个 Date 对象之间的时间差
  317. let intervalMs = date2.getTime() - date1.getTime();
  318. // 如果 time2 比 time1 小, 说明跨天了, 需要加上一天的毫秒数
  319. if (date2 < date1) {
  320. intervalMs += 24 * 60 * 60 * 1000;
  321. }
  322. // 返回间隔秒数
  323. return Math.floor(intervalMs / 1000);
  324. }
  325. function pickerConfirm(t1: number, event: any) {
  326. if (btnDisable) {
  327. return
  328. }
  329. global.scenario = 'FAST'
  330. setBtnDisable(true)
  331. var date = new Date(t1)
  332. var setDate = new Date(global.set_time);
  333. date.setMilliseconds(setDate.getMilliseconds());
  334. date.setSeconds(setDate.getSeconds());
  335. t1 = date.getTime();
  336. if (operateType == 'startFast') {
  337. startFast(t1, fastData.target_duration, event ? event : logEvent).then(res => {
  338. setBtnDisable(false)
  339. setShowTimePicker(false)
  340. }).catch(e => {
  341. setBtnDisable(false)
  342. })
  343. }
  344. else {
  345. endFast(t1, event ? event : logEvent).then(res => {
  346. setBtnDisable(false)
  347. setShowTimePicker(false)
  348. }).catch(e => {
  349. setBtnDisable(false)
  350. })
  351. }
  352. }
  353. if (!fastData)
  354. return <View />
  355. return <View style={{ alignItems: 'center', display: 'flex', flexDirection: 'column', width: rpxToPx(634 / 3), flexShrink: 0 }} onClick={() => props.onClick()}>
  356. <View style={{ width: rpxToPx(634 / 3), }} />
  357. <View style={{ position: 'relative', }}>
  358. {
  359. ring()
  360. }
  361. <View className={health.selTab == 1 ? 'window_name window_name_sel' : 'window_name g01'}>{isFastMode ? t('health.fast') : t('health.eat')}</View>
  362. </View>
  363. {
  364. showModal && <Modal dismiss={() => setShowModal(false)}>
  365. <View style={{ width: 100, height: 100, backgroundColor: 'red' }}>{props.count}</View>
  366. </Modal>
  367. }
  368. {
  369. showTimePicker && modalContent()
  370. }
  371. {
  372. showPicker && timeContent()
  373. }
  374. </View>
  375. }