| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- import { View, Text } from "@tarojs/components";
- import './MainCard.scss'
- import { useEffect, useState } from "react";
- 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 { useDispatch, useSelector } from "react-redux";
- import { sleepWindow } from "@/services/trackTimeDuration";
- import { WindowType } from "@/utils/types";
- import { durationArc, isCurrentTimeInRange, startArc } from "./util";
- import { setMode } from "@/store/health";
- export default function MainSleepActiveCard(props: { count: any, typeChanged: Function }) {
- const [isSleepMode, setIsSleepMode] = useState(true)
- const startScheduleTime = '19:00'
- const endScheduleTime = '06:00'
- const user = useSelector((state: any) => state.user);
- const health = useSelector((state: any) => state.health);
- const dispatch = useDispatch()
- useEffect(() => {
- const { sleep } = health.windows.sleep_active
- if (isCurrentTimeInRange(sleep.schedule.start_time, sleep.schedule.end_time)) {
- setIsSleepMode(true)
- }
- else {
- setIsSleepMode(false)
- }
- }, [health.windows])
- useEffect(() => {
- }, [user.isLogin])
- useEffect(() => {
- if (health.selTab == 2) {
- dispatch(setMode(isSleepMode ? 'SLEEP' : 'ACTIVE'))
- }
- }, [health.selTab,isSleepMode])
- function formatTime(format: string, timestamp?: number) {
- return dayjs().format(format)
- }
- function switchMode() {
- var mode = !isSleepMode;
- setIsSleepMode(mode)
- props.typeChanged(mode ? WindowType.sleep : WindowType.active)
- }
- const common: RingCommon = {
- useCase: 'ChooseScenario',
- radius: 115,
- lineWidth: 26,
- isFast: true,
- status: 'WAIT_FOR_START'
- }
- const bgRing: BgRing = {
- color: '#EAE9E9'
- }
- function getArc() {
- const { sleep, active } = health.windows.sleep_active
- if (isSleepMode) {
- return startArc(sleep.target.start_timestamp)
- }
- return startArc(active.target.start_timestamp)
- }
- function getTargetArc() {
- const { sleep, active } = health.windows.sleep_active
- if (isSleepMode) {
- return durationArc(sleep.target.start_timestamp, sleep.target.end_timestamp)
- }
- return durationArc(active.target.start_timestamp, active.target.end_timestamp)
- }
- function getRealArc() {
- const { sleep, active } = health.windows.sleep_active
- if (isSleepMode) {
- return durationArc(sleep.target.start_timestamp, new Date().getTime())
- }
- return durationArc(active.target.start_timestamp, new Date().getTime())
- }
- function targetRing() {
- const color = isSleepMode ? '#D0C0FB' : '#FF498366'
- const startArc = getArc()
- const durationArc = getTargetArc()
- return {
- color,
- startArc,
- durationArc
- }
- }
- function realRing() {
- const color = isSleepMode?MainColorType.sleep:MainColorType.active
- const { sleep,active } = health.windows.sleep_active
- if (isSleepMode){
- if (!isCurrentTimeInRange(sleep.schedule.start_time, sleep.schedule.end_time)){
- return null
- }
- }
- else {
- if (!isCurrentTimeInRange(active.schedule.start_time, active.schedule.end_time)){
- return null
- }
- }
- return {
- color: color,
- startArc: getArc(),//-Math.PI / 2,
- durationArc: getRealArc()
- }
- // if (isSleepMode) {
- // if (isCurrentTimeInRange(startScheduleTime, endScheduleTime)) {
- // var starts: any = startScheduleTime.split(':')
- // const startSeconds = parseInt(starts[0] + '') * 60 + parseInt(starts[1] + '')
- // const color = MainColorType.sleep
- // const startArc = startSeconds / 1440 * 2 * Math.PI - Math.PI / 2
- // var endSeconds = new Date().getHours() * 60 + new Date().getMinutes()
- // const fastCount = endSeconds - startSeconds > 0 ? endSeconds - startSeconds : endSeconds - startSeconds + 1440
- // const durationArc = fastCount / 1440 * 2 * Math.PI
- // return {
- // color,
- // startArc,
- // durationArc
- // }
- // }
- // }
- // else {
- // if (isCurrentTimeInRange(endScheduleTime, startScheduleTime)) {
- // var starts: any = endScheduleTime.split(':')
- // const startSeconds = parseInt(starts[0] + '') * 60 + parseInt(starts[1] + '')
- // const color = MainColorType.active
- // const startArc = startSeconds / 1440 * 2 * Math.PI - Math.PI / 2
- // var endSeconds = new Date().getHours() * 60 + new Date().getMinutes()
- // const fastCount = endSeconds - startSeconds > 0 ? endSeconds - startSeconds : endSeconds - startSeconds + 1440
- // const durationArc = fastCount / 1440 * 2 * Math.PI
- // return {
- // color,
- // startArc,
- // durationArc
- // }
- // }
- // }
- }
- 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: isSleepMode ? MainColorType.sleep : MainColorType.active,
- lineWidth: 10,
- borderColor: '#fff',
- offset: offset,
- whiteIcon: true
- }
- return <Rings common={common} bgRing={bgRing} targetRing={targetRing()} realRing={realRing()} currentDot={currentDot} canvasId={'smal11lee'} />
- }
- return <View style={{ width: rpxToPx(750), display: 'flex', flexShrink: 0, flexDirection: 'column', alignItems: 'center' }}>
- <View style={{ position: 'relative', }}>
- {
- ring()
- }
- <View className="ring_center">
- <Text className="time1">{formatTime('HH:mm:ss')}</Text>
- <Text className="date1">{global.language == 'en' ? formatTime('dddd, MMM D') : formatTime('MMMD日 dddd')}</Text>
- </View>
- </View>
- <Text onClick={switchMode}>Switch</Text>
- </View>
- }
|