| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710 |
- import { View, Text, Image } from "@tarojs/components";
- import './MainCard.scss'
- import { useEffect, useRef, useState } from "react";
- import Modal from "@/components/layout/Modal.weapp";
- import { rpxToPx } from "@/utils/tools";
- import Rings, { RingCommon, BgRing, TargetRing, CurrentDot } from "@/features/trackTimeDuration/components/Rings";
- import dayjs from "dayjs";
- import moment from 'moment-timezone'
- import { MainColorType } from "@/context/themes/color";
- import { fastWindow, setSchedule, updateRecord } from "@/services/trackTimeDuration";
- import { useDispatch, useSelector } from "react-redux";
- import { jumpPage } from "../trackTimeDuration/hooks/Common";
- import ConsolePicker from "../trackTimeDuration/components/ConsolePicker";
- import { endFast, startFast } from "../trackTimeDuration/actions/TrackTimeActions";
- import formatMilliseconds from "@/utils/format_time";
- import TimePicker from "@/features/common/TimePicker";
- import showAlert from "@/components/basic/Alert";
- import showActionSheet from "@/components/basic/ActionSheet";
- import { records } from "@/services/health";
- import MainHistory from "./MainHistory";
- import { WindowStatusType, WindowType } from "@/utils/types";
- import { durationArc, isCurrentTimeInRange, startArc } from "./util";
- import { setMode } from "@/store/health";
- import { IconSwitch1, IconSwitch2 } from "@/components/basic/Icons";
- import { getScenario, getWindowStatus } from "./hooks/health_hooks";
- let useNavigation;
- let Linking, PushNotification;
- let checkNotification;
- let min = 0
- let max = 0
- let defaultTimestamp = 0
- let useActionSheet;
- if (process.env.TARO_ENV == 'rn') {
- useNavigation = require("@react-navigation/native").useNavigation
- Linking = require('react-native').Linking;
- // JPush = require('jpush-react-native').default;
- PushNotification = require('react-native-push-notification')
- checkNotification = require('@/utils/native_permission_check').checkNotification;
- useActionSheet = require('@expo/react-native-action-sheet').useActionSheet
- }
- export default function MainFastEatCard(props: { count: any, typeChanged: Function, id: number, onClick: Function }) {
- const [isFastMode, setIsFastMode] = useState(true)
- const [showModal, setShowModal] = useState(false)
- const [showTimePicker, setShowTimePicker] = useState(false);
- const limitPickerRef = useRef(null)
- const [operateType, setOperateType] = useState('startFast')
- const [btnDisable, setBtnDisable] = useState(false)
- const [logEvent, setLogEvent] = useState('LOG_ONCE');
- const [eatData, setEatData] = useState<any>(null)
- const [fastData, setFastData] = useState<any>(null)
- const [startTime, setStartTime] = useState<any>(null)
- const [endTime, setEndTime] = useState<any>(null)
- const [status, setStatus] = useState<any>('upcoming')
- const [showPicker, setShowPicker] = useState(false)
- const [isStart, setIsStart] = useState(true)
- const user = useSelector((state: any) => state.user);
- const health = useSelector((state: any) => state.health);
- const dispatch = useDispatch()
- let navigation, showActionSheetWithOptions;
- if (useNavigation) {
- navigation = useNavigation()
- showActionSheetWithOptions = useActionSheet()
- }
- useEffect(() => {
- if (health.mode == 'FAST') {
- setIsFastMode(true)
- }
- else if (health.mode == 'EAT') {
- setIsFastMode(false)
- }
- }, [health.mode])
- useEffect(() => {
- const { fast, eat } = health.windows.fast_eat
- var now = new Date().getTime()
- 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)) {
- setIsFastMode(true)
- }
- else {
- setIsFastMode(false)
- }
- setEatData(eat)
- setFastData(fast)
- update(fast)
- }, [user.isLogin])
- useEffect(() => {
- if (fastData) {
- update(fastData)
- }
- }, [props.count])
- useEffect(() => {
- if (health.selTab == 1) {
- dispatch(setMode(isFastMode ? 'FAST' : 'EAT'))
- }
- }, [health.selTab, isFastMode])
- function update(fast) {
- var now = new Date().getTime()
- if (fast.status == 'WAIT_FOR_END') {
- setStatus('process')
- }
- else if ((fast.target.start_timestamp <= now && fast.target.end_timestamp >= now) || isCurrentTimeInRange(fast.period.start_time, fast.period.end_time)) {
- setStartTime(fast.period.start_time)
- setStatus('new')
- }
- else {
- setStatus('upcoming')
- }
- }
- const common: RingCommon = {
- useCase: 'ChooseScenario',
- radius: 37,
- lineWidth: 14,
- isFast: true,
- status: 'WAIT_FOR_START'
- }
- const bgRing: BgRing = {
- color: MainColorType.ringBg
- }
- function scheduleRing() {
- const { fast, eat } = health.windows.fast_eat
- var starts: any = fast.period.start_time.split(':')
- var ends: any = fast.period.end_time.split(':')
- const startSeconds: any = parseInt(starts[0] + '') * 60 + parseInt(starts[1] + '')
- const endSeconds: any = parseInt(ends[0] + '') * 60 + parseInt(ends[1] + '')
- const color = isFastMode ? MainColorType.fastLight : MainColorType.eatLight
- const startArc = isFastMode ? startSeconds / 1440 * 2 * Math.PI - Math.PI / 2 : endSeconds / 1440 * 2 * Math.PI - Math.PI / 2
- const fastCount = endSeconds - startSeconds > 0 ? endSeconds - startSeconds : endSeconds - startSeconds + 1440
- const eatCount = 1440 - fastCount
- const durationArc = isFastMode ? fastCount / 1440 * 2 * Math.PI : eatCount / 1440 * 2 * Math.PI
- return {
- color,
- startArc,
- durationArc
- }
- }
- function targetRing() {
- if (status != 'process') {
- return null
- }
- const { fast, eat } = health.windows.fast_eat
- var starts: any = fast.period.start_time.split(':')
- var ends: any = fast.period.end_time.split(':')
- const startSeconds: any = parseInt(starts[0] + '') * 60 + parseInt(starts[1] + '')
- const endSeconds: any = parseInt(ends[0] + '') * 60 + parseInt(ends[1] + '')
- const color = isFastMode ? '#9AE2FE' : '#FE810C66'
- var startArc = isFastMode ? startSeconds / 1440 * 2 * Math.PI - Math.PI / 2 : endSeconds / 1440 * 2 * Math.PI - Math.PI / 2
- const fastCount = endSeconds - startSeconds > 0 ? endSeconds - startSeconds : endSeconds - startSeconds + 1440
- const eatCount = 1440 - fastCount
- var durationArc = isFastMode ? fastCount / 1440 * 2 * Math.PI : eatCount / 1440 * 2 * Math.PI
- if (isFastMode) {
- var dt = new Date(fastData.target.start_time)
- var realSeconds = dt.getHours() * 3600 + dt.getMinutes() * 60 + dt.getSeconds()
- startArc = realSeconds / (1440 * 60) * 2 * Math.PI - Math.PI / 2
- durationArc = ((fastData.target.end_time - fastData.target.start_time) / 1000) / (1440 * 60) * 2 * Math.PI
- }
-
- return {
- color,
- startArc,
- durationArc,
- radius: isFastMode ? 90 : null,
- lineWidth: isFastMode ? 10 : null
- }
- }
- function getRealArc(time) {
- var date = new Date(time);
- var hour = date.getHours();
- var minute = date.getMinutes();
- var second = date.getSeconds();
- return (hour * 3600 + minute * 60 + second) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
- }
- function getRealDurationArc(start, end) {
- var duration = (end - start) / 1000;
- return duration / (24 * 3600) * 2 * Math.PI;
- }
- function realRing() {
- const status = getWindowStatus(health.windows, isFastMode ? 'FAST' : 'EAT')
- const scenario = getScenario(health.windows, isFastMode ? 'FAST' : 'EAT')
- if (status == WindowStatusType.upcoming) {
- return {
- color: '#cccccc',
- startArc: getRealArc(new Date().getTime()),
- durationArc: getRealDurationArc(new Date().getTime(), scenario.target.start_timestamp),
- }
- }
- return {
- color: isFastMode ? MainColorType.fast : MainColorType.eat,
- startArc: getRealArc(scenario.target.start_timestamp),
- durationArc: getRealDurationArc(scenario.target.start_timestamp, new Date().getTime())
- }
- }
- function getTimeToDestination(timeStr, isPasted) {
- // 获取当前时间
- const now = new Date();
- const currentHours = now.getHours();
- const currentMinutes = now.getMinutes();
- const currentSeconds = now.getSeconds();
- // 解析目标时间
- const [targetHours, targetMinutes] = timeStr.split(':').map(Number);
- // 计算时间差
- let hours = targetHours - currentHours;
- let minutes = targetMinutes - currentMinutes;
- let seconds = 60 - currentSeconds;
- if (minutes < 0) {
- minutes += 60;
- hours--;
- }
- if (hours < 0) {
- hours += 24;
- }
- if (seconds === 60) {
- seconds = 0;
- minutes++;
- }
- // 如果是过去的时间,则返回剩余时间
- if (isPasted) {
- hours = 24 - hours;
- minutes = 60 - minutes;
- seconds = 60 - seconds;
- }
- // 格式化输出
- return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
- }
- function ring() {
- var offset = 0
- var hour = new Date().getHours()
- var minute = new Date().getMinutes()
- if (hour != new Date().getHours() || minute != new Date().getMinutes()) {
- offset = hour * 60 + minute - new Date().getHours() * 60 - new Date().getMinutes()
- }
- // const currentDot: CurrentDot = {
- // color: isFastMode ? MainColorType.fast : MainColorType.eat,
- // lineWidth: 10,
- // borderColor: '#fff',
- // offset: offset,
- // whiteIcon: true
- // }
- return <Rings common={common} bgRing={bgRing} scheduleRing={scheduleRing()} targetRing={targetRing()} realRing={realRing()} canvasId={'smal11l' + props.id} />
- }
- function formatTime(format: string, timestamp?: number) {
- // var moment = require('moment-timezone');
- // if (current) {
- // if (timestamp) {
- // return moment(timestamp).tz(current.time.timezone.id).format(format)
- // }
- // return moment().tz(current.time.timezone.id).format(format)
- // }
- return dayjs().format(format)
- }
- function switchMode() {
- const mode = !isFastMode
- setIsFastMode(mode)
- props.typeChanged(mode ? WindowType.fast : WindowType.eat)
- }
- function getFastStatus() {
- switch (status) {
- case 'process':
- return 'Process'
- case 'new':
- return 'New Open'
- case 'upcoming':
- return 'Upcoming'
- }
- return ''
- }
- function getFastTime() {
- var milliSeconds = 0;
- switch (status) {
- case 'process':
- milliSeconds = new Date().getTime() - fastData.real_start_time
- break;
- case 'new':
- return getTimeToDestination(fastData.period.start_time, true)
- case 'upcoming':
- return getTimeToDestination(fastData.period.start_time, false)
- }
- var seconds = Math.floor(milliSeconds / 1000)
- const hours = Math.floor(seconds / 3600);
- const minutes = Math.floor((seconds % 3600) / 60);
- const remainingSeconds = seconds % 60;
- return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
- }
- function getEatTime() {
- return getTimeToDestination(eatData.period.start_time, isCurrentTimeInRange(eatData.period.start_time, eatData.period.end_time))
- }
- function tapFastStart() {
- setIsStart(true)
- setShowPicker(true)
- }
- function tapFastEnd() {
- setIsStart(false)
- setShowPicker(true)
- }
- function tapStartLog() {
- if (status == 'upcoming') {
- return;
- }
- if (!user.isLogin) {
- jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
- return
- }
- defaultTimestamp = new Date().getTime()
- min = defaultTimestamp - 1 * 24 * 3600 * 1000
- max = defaultTimestamp
- setOperateType('startFast')
- setShowTimePicker(true)
- }
- function tapEndLog() {
- if (status != 'process') {
- return
- }
- defaultTimestamp = new Date().getTime()
- // defaultTimestamp = e ? new Date().getTime() : logEventTimestamp
- min = defaultTimestamp - 1 * 24 * 3600 * 1000
- max = defaultTimestamp
- setOperateType('endFast')
- setShowTimePicker(true)
- }
- function modalContent() {
- global.set_time = new Date().getTime()
- return <Modal
- testInfo={null}
- dismiss={() => {
- setShowTimePicker(false)
- }}
- confirm={() => { }}>
- {
- timePickerContent()
- }
- </Modal>
- }
- function timeContent() {
- return <Modal
- testInfo={null}
- dismiss={() => {
- setShowPicker(false)
- }}
- confirm={() => { }}>
- {
- pickerContent()
- }
- </Modal>
- }
- function timePickerContent() {
- var title = operateType == 'endFast' ? '结束断食' : '开始断食'
- var color = MainColorType.fast
- var endTimestamp = 0
- if (operateType == 'endFast') {
- endTimestamp = fastData.target.end_time
- }
- var duration = fastData.target.duration
- return <View className="modal_content">
- <ConsolePicker ref={limitPickerRef}
- themeColor={color}
- title={title}
- onCancel={() => {
- setShowTimePicker(false)
- }}
- min={min}
- max={max}
- current={defaultTimestamp}
- duration={duration}
- endTimestamp={endTimestamp}
- isFast={true}
- isEnd={operateType == 'endFast'}
- isTimeout={false}
- isLoading={btnDisable}
- onChange={(e) => {
- pickerConfirm(e, null)
- global.pauseIndexTimer = false
- }}
- />
- </View>
- }
- function pickerContent() {
- const timestamp = isStart ? fastData.target.start_time : fastData.target.end_time
- const strTime = dayjs(timestamp).format('HH:mm')
- return <TimePicker time={strTime}
- color={MainColorType.fast}
- title={isStart ? '开始断食' : '结束断食'}
- confirm={(e) => {
- confirmPickerTime(e)
- }}
- cancel={() => {
- setShowPicker(false)
- }} />
- }
- function confirmPickerTime(strTime) {
- if (status != 'process') {
- setSchedule({
- code: isStart ? 'FAST_START' : 'FAST_END',
- time: strTime
- }).then(res => {
- setShowPicker(false)
- })
- }
- else {
- var startTime = dayjs(fastData.target.start_time).format('HH:mm:ss')
- var endTime = strTime + ':00'
- console.log(startTime, endTime)
- if (startTime == endTime) {
- showAlert({
- title: '',
- content: '开始时间不能与结束时间相同'
- })
- return;
- }
- updateRecord({
- fast: {
- target_duration: getIntervalSeconds(startTime, endTime) * 1000
- }
- }, fastData.id).then(res => {
- setShowPicker(false)
- })
- }
- }
- function getIntervalSeconds(time1, time2) {
- // 将时间字符串转换为 Date 对象
- const date1 = new Date(`2000-01-01T${time1}Z`);
- const date2 = new Date(`2000-01-01T${time2}Z`);
- // 计算两个 Date 对象之间的时间差
- let intervalMs = date2.getTime() - date1.getTime();
- // 如果 time2 比 time1 小, 说明跨天了, 需要加上一天的毫秒数
- if (date2 < date1) {
- intervalMs += 24 * 60 * 60 * 1000;
- }
- // 返回间隔秒数
- return Math.floor(intervalMs / 1000);
- }
- function pickerConfirm(t1: number, event: any) {
- if (btnDisable) {
- return
- }
- global.scenario = 'FAST'
- setBtnDisable(true)
- var date = new Date(t1)
- var setDate = new Date(global.set_time);
- date.setMilliseconds(setDate.getMilliseconds());
- date.setSeconds(setDate.getSeconds());
- t1 = date.getTime();
- if (operateType == 'startFast') {
- startFast(t1, fastData.target_duration, event ? event : logEvent).then(res => {
- setBtnDisable(false)
- setShowTimePicker(false)
- }).catch(e => {
- setBtnDisable(false)
- })
- }
- else {
- endFast(t1, event ? event : logEvent).then(res => {
- setBtnDisable(false)
- setShowTimePicker(false)
- }).catch(e => {
- setBtnDisable(false)
- })
- }
- }
- function goAddEat(meal, index) {
- if (!enableMeal(meal)) {
- return;
- }
- jumpPage('/pages/clock/AddEat?meal=' + JSON.stringify(eatData.meals[index]))
- }
- function enableMeal(meal) {
- if (meal.real_start_time > 0) {
- return true
- }
- if (meal.target.start_time > new Date().getTime()) {
- return true
- }
- return true
- }
- function more() {
- showActionSheet({
- showActionSheetWithOptions: showActionSheetWithOptions,
- title: 'Oprate Title',
- itemList: [
- 'Add Snack',
- '自定义餐次列表',
- ],
- success: (res) => {
- switch (res) {
- case 0:
- break;
- case 1:
- jumpPage('/_health/pages/edit_schedule')
- break;
- }
- }
- });
- }
- function switchIcon() {
- if (isFastMode) {
- if (status != 'upcoming') {
- return <IconSwitch1 color="#000" width={15} />
- }
- return <IconSwitch2 color="#fff" width={15} />
- }
- if (status != 'upcoming') {
- return <IconSwitch2 color="#fff" width={15} />
- }
- return <IconSwitch1 color="#000" width={15} />
- }
- function switchBtn() {
- var bgColor = ''
- if (isFastMode) {
- if (status != 'upcoming') {
- bgColor = '#fff'
- }
- else {
- bgColor = MainColorType.eat
- }
- }
- else {
- if (status != 'upcoming') {
- bgColor = MainColorType.fast
- }
- else {
- bgColor = '#fff'
- }
- }
- return <View className="switch_btn" style={{ backgroundColor: bgColor }} onClick={switchMode}>
- {
- switchIcon()
- }
- </View>
- }
- if (!fastData)
- return <View />
- return <View style={{ alignItems: 'center', display: 'flex', flexDirection: 'column', width: rpxToPx(750 / 3), flexShrink: 0 }} onClick={() => props.onClick()}>
- <View style={{ width: rpxToPx(750 / 3), }} />
- <View style={{ position: 'relative', }}>
- {
- ring()
- }
- <View className={health.selTab == 1 ? 'window_name window_name_sel' : 'window_name'}>{isFastMode ? 'Fast' : 'Eat'}</View>
- {/* <View className="ring_center">
- {
- isFastMode && <Text>{getFastStatus()}</Text>
- }
- <Text className="time1">{isFastMode ? getFastTime() : getEatTime()}</Text>
- <Text className="date1">{global.language == 'en' ? formatTime('dddd, MMM D') : formatTime('MMMD日 dddd')}</Text>
- </View> */}
- </View>
- {/* <View>{isFastMode ? formatMilliseconds(health.windows.fast_eat.fast.target.duration) : formatMilliseconds(health.windows.fast_eat.eat.target.duration)}</View> */}
- {/* {
- isFastMode && <View>
- <View className="log_row">
- {
- status == 'process' ? <View className="schedule_name">Fast starts</View> : <View className="schedule" onClick={tapFastStart}>
- <Text className="schedule_name">
- Fast starts:
- </Text>
- <Text className="schedule">
- {startScheduleTime}
- </Text>
- </View>
- }
- {
- status == 'process' ? <View className="schedule">{dayjs(fastData.target.start_time).format('HH:mm')}</View> :
- <View onClick={tapStartLog} className={status == 'new' ? "fast_log_btn" : "fast_log_btn fast_log_btn_disable"}>Log{status == 'new' && <View className="badge" />}</View>
- }
- </View>
- <View className="log_row">
- <View className="schedule" onClick={tapFastEnd}>
- <Text className="schedule_name">
- Fast ends:
- </Text>
- <Text className="schedule">
- {status == 'process' ? dayjs(fastData.target.end_time).format('HH:mm') : endScheduleTime}
- </Text>
- </View>
- <View onClick={tapEndLog} className={status == 'process' ? "fast_log_btn" : "fast_log_btn fast_log_btn_disable"}>Log</View>
- </View>
- </View>
- }
- {
- !isFastMode && <View>
- {
- eatData.meals.map((item, index) => {
- return <View className="log_row" style={{ justifyContent: 'flex-start' }} key={index}>
- {
- item.real_start_time && item.media && item.media.length > 0 && item.media[0].url && <Image src={item.media[0].url} style={{ width: 50, height: 50, marginRight: 10 }} />
- }
- <View className="schedule">
- <Text className="schedule_name">{item.name}</Text>
- <Text className="schedule">
- {item.real_start_time ? dayjs(item.real_start_time).format('HH:mm') + ' - ' + dayjs(item.real_end_time).format('HH:mm') : item.period.start_time}
- </Text>
- </View>
- <View style={{ flex: 1 }} />
- {
- item.real_start_time ? <View className="fast_log_btn fast_log_btn_disable">已记录</View> : <View onClick={() => goAddEat(item, index)} className={enableMeal(item) ? "fast_log_btn fast_log_eat_btn" : "fast_log_btn fast_log_btn_disable"}>Add</View>
- }
- </View>
- })
- }
- </View>
- } */}
- {/* {
- switchBtn()
- } */}
- {/* {
- !isFastMode && <Text onClick={more}>更多</Text>
- } */}
- {/* <MainHistory type={isFastMode ? 'FAST' : 'EAT'} /> */}
- {
- showModal && <Modal dismiss={() => setShowModal(false)}>
- <View style={{ width: 100, height: 100, backgroundColor: 'red' }}>{props.count}</View>
- </Modal>
- }
- {
- showTimePicker && modalContent()
- }
- {
- showPicker && timeContent()
- }
- </View>
- }
|