MainFastEatCard.tsx 13 KB

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