MainFastEatCard.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 [isFastMode, setIsFastMode] = useState(isTempFast)
  70. const { t } = useTranslation()
  71. useEffect(() => {
  72. if (health.mode == 'FAST') {
  73. setIsFastMode(true)
  74. }
  75. else if (health.mode == 'EAT') {
  76. setIsFastMode(false)
  77. }
  78. }, [health.mode])
  79. useEffect(() => {
  80. const { fast, eat } = health.windows.fast_eat
  81. var now = new Date().getTime()
  82. if (eat.status == 'OG') {
  83. setIsFastMode(false)
  84. dispatch(setMode('EAT'))
  85. }
  86. 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)) {
  87. setIsFastMode(true)
  88. dispatch(setMode('FAST'))
  89. }
  90. else {
  91. setIsFastMode(false)
  92. dispatch(setMode('EAT'))
  93. }
  94. setEatData(eat)
  95. setFastData(fast)
  96. update(fast)
  97. }, [])
  98. useEffect(() => {
  99. if (fastData) {
  100. update(fastData)
  101. }
  102. }, [props.count])
  103. useEffect(() => {
  104. if (health.selTab == 1) {
  105. dispatch(setMode(isFastMode ? 'FAST' : 'EAT'))
  106. }
  107. }, [health.selTab, isFastMode])
  108. function update(fast) {
  109. var now = new Date().getTime()
  110. if (fast.status == 'WAIT_FOR_END') {
  111. setStatus('process')
  112. }
  113. else if ((fast.target.start_timestamp <= now && fast.target.end_timestamp >= now) || isCurrentTimeInRange(fast.period.start_time, fast.period.end_time)) {
  114. // setStartTime(fast.period.start_time)
  115. setStatus('new')
  116. }
  117. else {
  118. setStatus('upcoming')
  119. }
  120. }
  121. const common: RingCommon = {
  122. useCase: 'ChooseScenario',
  123. radius: 27,
  124. lineWidth: 12,
  125. isFast: true,
  126. status: 'WAIT_FOR_START'
  127. }
  128. const bgRing: BgRing = {
  129. color: MainColorType.ringBg
  130. }
  131. function targetRing() {
  132. const { fast, eat } = health.windows.fast_eat
  133. var starts: any = fast.period.start_time.split(':')
  134. var ends: any = fast.period.end_time.split(':')
  135. const startSeconds: any = parseInt(starts[0] + '') * 60 + parseInt(starts[1] + '')
  136. const endSeconds: any = parseInt(ends[0] + '') * 60 + parseInt(ends[1] + '')
  137. const color = isFastMode ? MainColorType.fastLight : MainColorType.eatLight
  138. const startArc = isFastMode ? startSeconds / 1440 * 2 * Math.PI - Math.PI / 2 : endSeconds / 1440 * 2 * Math.PI - Math.PI / 2
  139. const fastCount = endSeconds - startSeconds > 0 ? endSeconds - startSeconds : endSeconds - startSeconds + 1440
  140. const eatCount = 1440 - fastCount
  141. const durationArc = isFastMode ? fastCount / 1440 * 2 * Math.PI : eatCount / 1440 * 2 * Math.PI
  142. if (isFastMode && getWindowStatus(health.windows, 'FAST') == WindowStatusType.process) {
  143. const scenario = getScenario(health.windows, 'FAST')
  144. return {
  145. color,
  146. startArc: getStartArc(scenario.target.start_timestamp),
  147. durationArc: getDurationArc(scenario.target.start_timestamp, scenario.target.end_timestamp)
  148. }
  149. }
  150. if (!isFastMode && getScenario(health.windows, 'EAT').real) {
  151. const scenario = getScenario(health.windows, 'EAT')
  152. return {
  153. color,
  154. startArc: getStartArc(scenario.target.start_timestamp),
  155. durationArc: getDurationArc(scenario.target.start_timestamp, scenario.target.end_timestamp)
  156. }
  157. }
  158. return {
  159. color,
  160. startArc,
  161. durationArc
  162. }
  163. }
  164. function realRing() {
  165. const status = getWindowStatus(health.windows, isFastMode ? 'FAST' : 'EAT')
  166. const scenario = getScenario(health.windows, isFastMode ? 'FAST' : 'EAT')
  167. if (status == WindowStatusType.upcoming) {
  168. return null
  169. // return {
  170. // color: '#cccccc',
  171. // startArc: getStartArc(new Date().getTime()),
  172. // durationArc: getDurationArc(new Date().getTime(), scenario.target.start_timestamp),
  173. // }
  174. }
  175. return {
  176. color: isFastMode ? MainColorType.fast : MainColorType.eat,
  177. startArc: getStartArc(scenario.target.start_timestamp),
  178. durationArc: getDurationArc(scenario.target.start_timestamp, new Date().getTime())
  179. }
  180. }
  181. function currentDot() {
  182. if (health.mode == 'FAST' || health.mode == 'EAT') {
  183. const status = getWindowStatus(health.windows, isFastMode ? 'FAST' : 'EAT')
  184. return {
  185. color: status == WindowStatusType.upcoming ? '#B2B2B2' : isFastMode ? MainColorType.fast : MainColorType.eat,
  186. lineWidth: 2,
  187. borderColor: '#F5F5F5',
  188. offset: 0
  189. }
  190. }
  191. return null;
  192. }
  193. function ring() {
  194. return <Rings common={common}
  195. bgRing={bgRing}
  196. targetRing={targetRing()}
  197. realRing={realRing()}
  198. canvasId={'smal11l' + props.id}
  199. currentDot={currentDot()}
  200. scale={props.scale ?? 1.0}
  201. />
  202. }
  203. function modalContent() {
  204. global.set_time = new Date().getTime()
  205. return <Modal
  206. testInfo={null}
  207. dismiss={() => {
  208. setShowTimePicker(false)
  209. }}
  210. confirm={() => { }}>
  211. {
  212. timePickerContent()
  213. }
  214. </Modal>
  215. }
  216. function timeContent() {
  217. return <Modal
  218. testInfo={null}
  219. dismiss={() => {
  220. setShowPicker(false)
  221. }}
  222. confirm={() => { }}>
  223. {
  224. pickerContent()
  225. }
  226. </Modal>
  227. }
  228. function timePickerContent() {
  229. var title = operateType == 'endFast' ? '结束断食' : '开始断食'
  230. var color = MainColorType.fast
  231. var endTimestamp = 0
  232. if (operateType == 'endFast') {
  233. endTimestamp = fastData.target.end_time
  234. }
  235. var duration = fastData.target.duration
  236. return <View className="modal_content">
  237. <ConsolePicker ref={limitPickerRef}
  238. themeColor={color}
  239. title={title}
  240. onCancel={() => {
  241. setShowTimePicker(false)
  242. }}
  243. min={min}
  244. max={max}
  245. current={defaultTimestamp}
  246. duration={duration}
  247. endTimestamp={endTimestamp}
  248. isFast={true}
  249. isEnd={operateType == 'endFast'}
  250. isTimeout={false}
  251. isLoading={btnDisable}
  252. onChange={(e) => {
  253. pickerConfirm(e, null)
  254. global.pauseIndexTimer = false
  255. }}
  256. />
  257. </View>
  258. }
  259. function pickerContent() {
  260. const timestamp = isStart ? fastData.target.start_time : fastData.target.end_time
  261. const strTime = dayjs(timestamp).format('HH:mm')
  262. return <TimePicker time={strTime}
  263. color={MainColorType.fast}
  264. title={isStart ? '开始断食' : '结束断食'}
  265. confirm={(e) => {
  266. confirmPickerTime(e)
  267. }}
  268. cancel={() => {
  269. setShowPicker(false)
  270. }} />
  271. }
  272. function confirmPickerTime(strTime) {
  273. if (status != 'process') {
  274. setSchedule({
  275. code: isStart ? 'FAST_START' : 'FAST_END',
  276. time: strTime
  277. }).then(res => {
  278. setShowPicker(false)
  279. })
  280. }
  281. else {
  282. var startTime = dayjs(fastData.target.start_time).format('HH:mm:ss')
  283. var endTime = strTime + ':00'
  284. console.log(startTime, endTime)
  285. if (startTime == endTime) {
  286. showAlert({
  287. title: '',
  288. content: '开始时间不能与结束时间相同'
  289. })
  290. return;
  291. }
  292. updateRecord({
  293. fast: {
  294. target_duration: getIntervalSeconds(startTime, endTime) * 1000
  295. }
  296. }, fastData.id).then(res => {
  297. setShowPicker(false)
  298. })
  299. }
  300. }
  301. function getIntervalSeconds(time1, time2) {
  302. // 将时间字符串转换为 Date 对象
  303. const date1 = new Date(`2000-01-01T${time1}Z`);
  304. const date2 = new Date(`2000-01-01T${time2}Z`);
  305. // 计算两个 Date 对象之间的时间差
  306. let intervalMs = date2.getTime() - date1.getTime();
  307. // 如果 time2 比 time1 小, 说明跨天了, 需要加上一天的毫秒数
  308. if (date2 < date1) {
  309. intervalMs += 24 * 60 * 60 * 1000;
  310. }
  311. // 返回间隔秒数
  312. return Math.floor(intervalMs / 1000);
  313. }
  314. function pickerConfirm(t1: number, event: any) {
  315. if (btnDisable) {
  316. return
  317. }
  318. global.scenario = 'FAST'
  319. setBtnDisable(true)
  320. var date = new Date(t1)
  321. var setDate = new Date(global.set_time);
  322. date.setMilliseconds(setDate.getMilliseconds());
  323. date.setSeconds(setDate.getSeconds());
  324. t1 = date.getTime();
  325. if (operateType == 'startFast') {
  326. startFast(t1, fastData.target_duration, event ? event : logEvent).then(res => {
  327. setBtnDisable(false)
  328. setShowTimePicker(false)
  329. }).catch(e => {
  330. setBtnDisable(false)
  331. })
  332. }
  333. else {
  334. endFast(t1, event ? event : logEvent).then(res => {
  335. setBtnDisable(false)
  336. setShowTimePicker(false)
  337. }).catch(e => {
  338. setBtnDisable(false)
  339. })
  340. }
  341. }
  342. if (!fastData)
  343. return <View />
  344. return <View style={{ alignItems: 'center', display: 'flex', flexDirection: 'column', width: rpxToPx(634 / 3), flexShrink: 0 }} onClick={() => props.onClick()}>
  345. <View style={{ width: rpxToPx(634 / 3), }} />
  346. <View style={{ position: 'relative', }}>
  347. {
  348. ring()
  349. }
  350. <View className={health.selTab == 1 ? 'window_name window_name_sel' : 'window_name g01'}>{isFastMode ? t('health.fast') : t('health.eat')}</View>
  351. </View>
  352. {
  353. showModal && <Modal dismiss={() => setShowModal(false)}>
  354. <View style={{ width: 100, height: 100, backgroundColor: 'red' }}>{props.count}</View>
  355. </Modal>
  356. }
  357. {
  358. showTimePicker && modalContent()
  359. }
  360. {
  361. showPicker && timeContent()
  362. }
  363. </View>
  364. }