| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559 |
- import { View, Text, Image, Switch } from "@tarojs/components";
- import { bigRingRadius, dotIsOuterRange, getBgRing, getCommon, getDot, getReal, getSchedule, getTarget, ringWidth, smallRingRadius, thirdRingRadius } from "@/features/trackTimeDuration/hooks/RingData";
- import { RealRing, CurrentDot } from "@/features/trackTimeDuration/components/Rings";
- import { ColorType } from "@/context/themes/color";
- import { useDispatch, useSelector } from "react-redux";
- import Rings from "@/features/trackTimeDuration/components/Rings";
- import { jumpPage } from "@/features/trackTimeDuration/hooks/Common";
- import { rpxToPx } from "@/utils/tools";
- import { useEffect, useState } from "react";
- import Taro, { useDidHide, useDidShow } from "@tarojs/taro";
- import './Discovery.scss'
- import { useTranslation } from "react-i18next";
- import { TimeFormatter } from "@/utils/time_format";
- import { getPerm, uploadPerm } from "@/services/user";
- import Box from "@/components/layout/Box";
- import { getPlans, setPlan } from "@/services/trackTimeDuration";
- import DayNightCard from "@/features/trackTimeDuration/components/DayNightCard";
- import { clearDayStore } from "@/store/day";
- import { AtActivityIndicator } from "taro-ui";
- import NoData from "@/components/view/NoData";
- // import AtActivityIndicator from "taro-ui/types/activity-indicator";
- let useNavigation;
- if (process.env.TARO_ENV == 'rn') {
- useNavigation = require("@react-navigation/native").useNavigation
- }
- let pauseTimer = false
- const defaultValue = `{"scenarios":[{"name":"FAST","count":0,"schedule":{"fast":{"start_time":"15:00","end_time":"08:00"}}},{"name":"SLEEP","count":0,"schedule":{"sleep":{"start_time":"22:00","end_time":"07:00"}}},{"name":"FAST_SLEEP","count":0,"schedule":{"fast":{"start_time":"15:00","end_time":"08:00"},"sleep":{"start_time":"22:00","end_time":"07:00"}}}],"select_count":0,"theme_color":{"fast":"#00FFFF","sleep":"#8183FF"}}`
- export default function Discovery() {
- const user = useSelector((state: any) => state.user);
- const day = useSelector((state: any) => state.day);
- const ring = useSelector((state: any) => state.ring);
- const [schedule, setSchedule] = useState(null)
- const [count, setCount] = useState(0)
- const [loaded, setLoaded] = useState(false)
- const [showErrorPage, setErrorPage] = useState(false)
- const dispatch = useDispatch();
- const { t } = useTranslation()
- let navigation;
- let timeStamp = new Date().getTime()
- if (useNavigation) {
- navigation = useNavigation()
- }
- useEffect(() => {
- setInterval(() => {
- if (global.pauseIndexTimer || pauseTimer) {
- return
- }
- setCount((prevCounter) => prevCounter + 1)
- }, 1000)
- }, [])
- useDidShow(() => {
- setCount(pre => pre + 1)
- //resume timer
- pauseTimer = false
- })
- useDidHide(() => {
- //pause timer
- pauseTimer = true
- })
- useEffect(() => {
- if (!user.isLogin) {
- dispatch(clearDayStore());
- }
- }, [user.isLogin])
- // useEffect(() => {
- // setSchedule(global.homeData.scenarios)
- // }, [global.homeData])
- useEffect(() => {
- getPlanData()
- setCount(pre => pre + 1)
- }, [user.isLogin])
- function getPlanData() {
- getPlans().then(res => {
- setSchedule((res as any).scenarios)
- setLoaded(true)
- setErrorPage(false)
- }).catch(e => {
- setSchedule(JSON.parse(defaultValue))
- setLoaded(true)
- setErrorPage(true)
- })
- }
- function getFoodTime() {
- if (ring.schedule) {
- return [ring.schedule.fast.end_time, ring.schedule.fast.start_time]
- }
- var obj;
- (schedule as any).map(item => {
- if (item.name == 'FAST') {
- obj = item
- }
- })
- if (!obj) {
- console.log('报错', schedule)
- return ['16:00', '08:00'];
- }
- return [obj.schedule.fast.end_time, obj.schedule.fast.start_time]
- }
- function getActivityTime() {
- if (ring.schedule) {
- return [ring.schedule.sleep.end_time, ring.schedule.sleep.start_time]
- }
- var obj;
- (schedule as any).map(item => {
- if (item.name == 'SLEEP') {
- obj = item
- }
- })
- return [obj.schedule.sleep.end_time, obj.schedule.sleep.start_time]
- }
- function bigRing() {
- var common = getCommon(null, true)
- common.radius = bigRingRadius;
- common.lineWidth = ringWidth;
- var bgRing = getBgRing()
- const targetRing: RealRing = {
- color: (ring.current_record && ring.current_record.scenario == 'SLEEP') ? ColorType.activity + '66' : ColorType.food + '66',
- startArc: 0,
- durationArc: 2
- }
- var list = getFoodTime()
- var start;
- var end;
- if (ring.current_record && ring.current_record.scenario == 'SLEEP') {
- var list2 = getActivityTime()
- start = parseInt(list2[0].split(':')[0]) * 60 + parseInt(list2[0].split(':')[1])
- end = parseInt(list2[1].split(':')[0]) * 60 + parseInt(list2[1].split(':')[1])
- if (ring.current_record.status != 'WAIT_FOR_START') {
- var startDate = new Date(ring.current_record.sleep.target_start_time)
- var endDate = new Date(ring.current_record.sleep.target_end_time)
- end = startDate.getHours() * 60 + startDate.getMinutes()
- start = endDate.getHours() * 60 + endDate.getMinutes()
- }
- }
- else {
- start = parseInt(list[0].split(':')[0]) * 60 + parseInt(list[0].split(':')[1])
- end = parseInt(list[1].split(':')[0]) * 60 + parseInt(list[1].split(':')[1])
- if (ring.current_record && ring.current_record.status != 'WAIT_FOR_START') {
- var startDate = new Date(ring.current_record.fast.target_start_time)
- var endDate = new Date(ring.current_record.fast.target_end_time)
- end = startDate.getHours() * 60 + startDate.getMinutes()
- start = endDate.getHours() * 60 + endDate.getMinutes()
- }
- }
- if (end < start) {
- end += 24 * 60
- }
- var duration = end - start
- targetRing.startArc = (start * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
- targetRing.durationArc = (duration * 60) / (24 * 3600) * 2 * Math.PI;
- var currentDot = getDot(null, false)
- var date = new Date()
- var minutes = date.getHours() * 60 + date.getMinutes()
- if (minutes < start) {
- minutes += 1440
- }
- currentDot.color = ColorType.food
- var now = new Date()
- var t = now.getHours() * 60 + now.getMinutes()
- var duration2 = t - start
- if (duration2 < 0) {
- duration2 += 24 * 60
- }
- let realRing: RealRing = {
- color: ColorType.food,
- startArc: (start * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0,
- durationArc: (duration2 * 60) / (24 * 3600) * 2 * Math.PI
- }
- if (ring.current_record && ring.current_record.scenario == 'SLEEP') {
- realRing.color = ColorType.activity
- currentDot.color = ColorType.activity
- }
- if (!user.isLogin) {
- currentDot = null
- realRing = null
- }
- if (ring.current_record.status == 'ONGOING1') {
- realRing = null
- }
- return <Rings common={common} bgRing={bgRing} targetRing={targetRing} realRing={duration2 <= duration ? realRing : null} currentDot={currentDot} canvasId={'day_big'} />
- }
- function smallRing() {
- var common = getCommon(null, false)
- common.radius = smallRingRadius;
- common.lineWidth = ringWidth;
- var bgRing = getBgRing()
- const realRingBig: RealRing = {
- color: ColorType.activity + '66',
- startArc: 0,
- durationArc: 2
- }
- var list = getActivityTime()
- var start = parseInt(list[0].split(':')[0]) * 60 + parseInt(list[0].split(':')[1])
- var end = parseInt(list[1].split(':')[0]) * 60 + parseInt(list[1].split(':')[1])
- if (ring.current_record) {
- if (ring.current_record.scenario == 'SLEEP') {
- if (ring.current_record.status == 'ONGOING') {
- var startDate = new Date(ring.current_record.sleep.target_start_time)
- var endDate = new Date(ring.current_record.sleep.target_end_time)
- end = startDate.getHours() * 60 + startDate.getMinutes()
- start = endDate.getHours() * 60 + endDate.getMinutes()
- }
- }
- else {
- if (ring.current_record.status == 'ONGOING2') {
- var startDate = new Date(ring.current_record.sleep.target_start_time)
- var endDate = new Date(ring.current_record.sleep.target_end_time)
- end = startDate.getHours() * 60 + startDate.getMinutes()
- start = endDate.getHours() * 60 + endDate.getMinutes()
- }
- else if (ring.current_record.status == 'ONGOING3') {
- //睡眠已完成时,睡眠小于24小时,使用真实的real_end_time-real_start_time
- //大于24小时,使用real_end_time+sleep_duration
- if (ring.current_record.sleep.real_end_time - ring.current_record.sleep.real_start_time < 24 * 3600 * 1000) {
- var startDate = new Date(ring.current_record.sleep.real_start_time)
- var endDate = new Date(ring.current_record.sleep.real_end_time)
- end = startDate.getHours() * 60 + startDate.getMinutes()
- start = endDate.getHours() * 60 + endDate.getMinutes()
- }
- else {
- var startDate = new Date(ring.current_record.sleep.real_end_time)
- var durationTemp = end - start//ring.current_record.sleep.target_end_time-ring.current_record.sleep.target_start_time
- if (durationTemp < 0) {
- durationTemp += 24 * 60
- }
- var endDate = new Date(startDate.getTime() + durationTemp * 60 * 1000)
- start = startDate.getHours() * 60 + startDate.getMinutes()
- end = endDate.getHours() * 60 + endDate.getMinutes()
- }
- }
- }
- }
- if (end < start) {
- end += 24 * 60
- }
- var duration = end - start
- realRingBig.startArc = (start * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
- realRingBig.durationArc = (duration * 60) / (24 * 3600) * 2 * Math.PI;
- var currentDot = getDot(null, false)
- var date = new Date()
- var minutes = date.getHours() * 60 + date.getMinutes()
- if (minutes < start) {
- minutes += 1440
- }
- currentDot.color = ColorType.activity
- var now = new Date()
- var t = now.getHours() * 60 + now.getMinutes()
- var duration2 = t - start
- if (duration2 < 0) {
- duration2 += 24 * 60
- }
- let realRing: RealRing = {
- color: ColorType.activity,
- startArc: (start * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0,
- durationArc: (duration2 * 60) / (24 * 3600) * 2 * Math.PI
- }
- if (!user.isLogin) {
- currentDot = null
- realRing = null
- }
- if (ring.current_record.status == 'ONGOING2') {
- realRing = null
- }
- return <Rings common={common} bgRing={bgRing} targetRing={realRingBig} realRing={duration2 <= duration ? realRing : null} currentDot={currentDot} canvasId={'day_small'} />
- }
- function dayRing() {
- var common = getCommon(null, true)
- common.radius = thirdRingRadius;
- common.lineWidth = ringWidth;
- var bgRing = getBgRing()
- const realRingBig: RealRing = {
- color: ColorType.day + '66',
- startArc: 0,
- durationArc: 2
- }
- var sunRise = 6 * 60
- var sunSet = 18 * 60
- var sunRiseObj = day.dayRingSunrise
- var sunSetObj = day.dayRingSunset
- sunRise = parseInt(sunRiseObj.split(':')[0]) * 60 + parseInt(sunRiseObj.split(':')[1])
- sunSet = parseInt(sunSetObj.split(':')[0]) * 60 + parseInt(sunSetObj.split(':')[1])
- var duration = sunSet - sunRise
- realRingBig.startArc = (sunRise * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
- realRingBig.durationArc = (duration * 60) / (24 * 3600) * 2 * Math.PI;
- var currentDot = getDot(null, false)
- var date = new Date()
- var minutes = date.getHours() * 60 + date.getMinutes()
- if (minutes < sunRise) {
- minutes += 1440
- }
- currentDot.color = ColorType.day
- var now = new Date()
- var t = now.getHours() * 60 + now.getMinutes()
- var duration2 = t - sunRise
- if (duration2 < 0) {
- duration2 += 24 * 60
- }
- var realRing: RealRing = {
- color: ColorType.day,
- startArc: (sunRise * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0,
- durationArc: (duration2 * 60) / (24 * 3600) * 2 * Math.PI
- }
- if (day.dayRingDate) {
- if (new Date(day.dayRingDate).getDate() != new Date().getDate()) {
- realRing = null;
- }
- }
- if (!user.isLogin) {
- currentDot = null
- realRing = null
- }
- return <Rings common={common} bgRing={bgRing} targetRing={realRingBig} realRing={duration2 <= duration ? realRing : null} currentDot={currentDot} canvasId={'day_day'} />
- }
- function goAcitivity() {
- if (user.isLogin) {
- jumpPage('/pages/workout/Workout', 'Workout', navigation)
- return
- }
- jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
- }
- function goFood() {
- if (user.isLogin) {
- jumpPage('/pages/food/Food', 'Food', navigation)
- return
- }
- jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
- }
- function dayDuration() {
- var sunRiseObj = day.dayRingSunrise
- var sunSetObj = day.dayRingSunset
- var sunRise = parseInt(sunRiseObj.split(':')[0]) * 60 + parseInt(sunRiseObj.split(':')[1])
- var sunSet = parseInt(sunSetObj.split(':')[0]) * 60 + parseInt(sunSetObj.split(':')[1])
- if (sunSetObj.indexOf('PM') != -1) {
- sunSet += 12 * 60
- }
- var duration = (sunSet - sunRise) * 60 * 1000
- return TimeFormatter.calculateTimeDifference(new Date().getTime(), new Date().getTime() + duration);
- }
- function eatDuration() {
- var list = getFoodTime()
- var start = parseInt(list[0].split(':')[0]) * 60 + parseInt(list[0].split(':')[1])
- var end = parseInt(list[1].split(':')[0]) * 60 + parseInt(list[1].split(':')[1])
- if (ring.current_record && ring.current_record.status != 'WAIT_FOR_START' && ring.current_record.scenario != 'SLEEP') {
- var startDate = new Date(ring.current_record.fast.target_start_time)
- var endDate = new Date(ring.current_record.fast.target_end_time)
- end = startDate.getHours() * 60 + startDate.getMinutes()
- start = endDate.getHours() * 60 + endDate.getMinutes()
- }
- if (end < start) {
- end += 24 * 60
- }
- var duration = (end - start) * 60 * 1000
- return TimeFormatter.calculateTimeDifference(new Date().getTime(), new Date().getTime() + duration);
- }
- function activityDuration() {
- var list = getActivityTime()
- var start = parseInt(list[0].split(':')[0]) * 3600 + parseInt(list[0].split(':')[1]) * 60
- var end = parseInt(list[1].split(':')[0]) * 3600 + parseInt(list[1].split(':')[1]) * 60
- if (ring.current_record) {
- if (ring.current_record.scenario == 'SLEEP') {
- if (ring.current_record.status == 'ONGOING') {
- var startDate = new Date(ring.current_record.sleep.target_start_time)
- var endDate = new Date(ring.current_record.sleep.target_end_time)
- end = startDate.getHours() * 3600 + startDate.getMinutes() * 60 + startDate.getSeconds()
- start = endDate.getHours() * 3600 + endDate.getMinutes() * 60 + endDate.getSeconds()
- }
- }
- else {
- if (ring.current_record.status == 'ONGOING2') {
- var startDate = new Date(ring.current_record.sleep.target_start_time)
- var endDate = new Date(ring.current_record.sleep.target_end_time)
- end = startDate.getHours() * 3600 + startDate.getMinutes() * 60 + startDate.getSeconds()
- start = endDate.getHours() * 3600 + endDate.getMinutes() * 60 + endDate.getSeconds()
- }
- else if (ring.current_record.status == 'ONGOING3') {
- //睡眠已完成时,睡眠小于24小时,使用真实的real_end_time-real_start_time
- //大于24小时,使用real_end_time+sleep_duration
- if (ring.current_record.sleep.real_end_time - ring.current_record.sleep.real_start_time < 24 * 3600 * 1000) {
- var startDate = new Date(ring.current_record.sleep.real_start_time)
- var endDate = new Date(ring.current_record.sleep.real_end_time)
- end = startDate.getHours() * 3600 + startDate.getMinutes() * 60 + startDate.getSeconds()
- start = endDate.getHours() * 3600 + endDate.getMinutes() * 60 + endDate.getSeconds()
- }
- else {
- var startDate = new Date(ring.current_record.sleep.real_end_time)
- var durationTemp = start - end//ring.current_record.sleep.target_end_time-ring.current_record.sleep.target_start_time
- if (durationTemp < 0) {
- durationTemp += 24 * 3600
- }
- var endDate = new Date(startDate.getTime() + durationTemp * 1000)
- end = startDate.getHours() * 3600 + startDate.getMinutes() * 60 + startDate.getSeconds()
- start = endDate.getHours() * 3600 + endDate.getMinutes() * 60 + endDate.getSeconds()
- debugger
- console.log('active', end, start, durationTemp)
- }
- }
- }
- }
- if (end < start) {
- end += 24 * 3600
- }
- var duration = (end - start) * 1000
- return TimeFormatter.calculateTimeDifference(new Date().getTime(), new Date().getTime() + duration);
- }
- function rings() {
- return <View style={{
- position: 'relative', zIndex: 1,
- marginLeft: day.showDayRing && false ? -6 : 0,
- }}>
- {
- bigRing()
- }
- {
- ring.current_record && ring.current_record.scenario == 'FAST_SLEEP' && <View style={{ display: 'flex', position: 'absolute', left: 0, right: 0, top: 0, bottom: 0, alignItems: 'center', justifyContent: 'center' }}>
- {
- smallRing()
- }
- </View>
- }
- {
- day.showDayRing && false && <View style={{ display: 'flex', position: 'absolute', left: -14, top: -14, right: -14, bottom: -14 }}>
- {
- dayRing()
- }
- </View>
- }
- </View>
- }
- if (!loaded) {
- return <View style={{ width: rpxToPx(750), height: rpxToPx(900), display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
- <AtActivityIndicator size={40} color="#fff" />
- </View>
- }
- return <View style={{ display: 'flex', flexDirection: 'column' }}>
- <Box>
- <View>
- <View style={{
- marginLeft: rpxToPx(-40),
- marginRight: rpxToPx(-40),
- marginTop: rpxToPx(-40),
- padding: rpxToPx(48)
- }}>
- <View style={{
- display: 'flex',
- flexDirection: 'row',
- marginTop: day.showDayRing && false ? -6 : 0,
- marginBottom: day.showDayRing && false ? -6 : 0,
- padding: day.showDayRing && false ? 14 : 0,
- alignItems: 'center'
- }}>
- {
- schedule && rings()
- }
- {schedule && <View className="duration_bg3"
- style={{ marginLeft: day.showDayRing && false ? rpxToPx(68) : rpxToPx(52), height: bigRingRadius * 2, overflow: 'visible' }}>
- {
- day.showDayRing && false && <Text className="duration_title2">{t('feature.common.day')}</Text>
- }
- {
- day.showDayRing && false &&
- <Text className="duration_value2" style={{ color: ColorType.day }}>{dayDuration()}</Text>
- }
- {
- ring.current_record && ring.current_record.scenario != 'SLEEP' && <Text className="duration_title2">进食</Text>
- }
- {
- ring.current_record && ring.current_record.scenario != 'SLEEP' && <Text className="duration_value2" style={{ color: ColorType.food }}>{eatDuration()}</Text>
- }
- {
- ring.current_record && ring.current_record.scenario != 'FAST' && <Text className="duration_title2">活动</Text>
- }
- {
- ring.current_record && ring.current_record.scenario != 'FAST' && <Text className="duration_value2" style={{ color: ColorType.activity, marginBottom: 0 }}>{activityDuration()}</Text>
- }
- </View>
- }
- </View>
- </View>
- <View className="food_btn1" style={{ backgroundColor: ColorType.food }} onClick={goFood}>
- <Text style={{ fontWeight: 'bold' }}>{t('page.food.title')}</Text>
- </View>
- <View className="food_btn1" style={{ backgroundColor: ColorType.activity, marginBottom: 0 }} onClick={goAcitivity}>
- <Text style={{ fontWeight: 'bold' }}>{t('page.workout.title')}</Text>
- </View>
- </View>
- </Box>
- <DayNightCard isNight={false} count={count} />
- {
- showErrorPage && <NoData refresh={() => { getPlanData() }} />
- }
- </View>
- }
|