import { View, ScrollView, Text, Swiper, SwiperItem } from "@tarojs/components"; import './WeekCalendar.scss' import { useEffect, useState, useMemo, memo, useRef } from "react"; import { ColorType } from "@/context/themes/color"; import WeekCalendarItem from "./WeekCalendarItem"; import { rpxToPx } from "@/utils/tools"; import { TimeFormatter } from "@/utils/time_format"; import { clockSummaryStats, eatWakes } from "@/services/trackTimeDuration"; import { useTranslation } from "react-i18next"; import { IconBigArrow } from "@/components/basic/Icons"; import dayjs from "dayjs"; let pageIndex = 0 let lastFastValue = 0; let lastSleepValue = 0; let lastOffset; let LinearGradient; if (process.env.TARO_ENV == 'rn') { LinearGradient = require('react-native-linear-gradient').default } const WeekCalendar = memo((props: { isFastSleep: boolean }) => { const [calendars, setCalendars] = useState([]) const [current, setCurrent] = useState(0) const [summary, setSummary] = useState(null) const [calendarData, setCalendarData] = useState(null) const [minTime, setMinTime] = useState(0) const [left, setLeft] = useState(0) const [isLoading, setIsLoading] = useState(false) const { t } = useTranslation() const ref = useRef(null) const pageSize = 40 useEffect(() => { // pageIndex = -1 getRecords() if (props.isFastSleep) { global.refrehWeekly = () => { console.log('正在刷新weekly', isLoading) // getRecords() if (isLoading) return pageIndex = 0; setIsLoading(true) var timestamp = TimeFormatter.getMondayTimestamp() var now = new Date() if (now.getDay() == 0 && now.getHours() >= 0 && now.getHours() < 12) { timestamp -= 24 * 3600 * 1000 * 7 } var list: any = [] var offset = 12 * 3600 * 1000 + pageIndex * pageSize * 24 * 3600 * 1000 if (!props.isFastSleep) { offset = pageIndex * pageSize * 24 * 3600 * 1000 } for (var i = 0; i < pageSize; i++) { list.push(`${i + pageIndex * pageSize},${timestamp - 7 * 24 * 3600 * 1000 * i - offset},${timestamp - 7 * 24 * 3600 * 1000 * i + 7 * 24 * 3600 * 1000 - offset}`) } if (props.isFastSleep) { clockSummaryStats({ times: list.join(';') }).then(res => { var list = (res as any).data.reverse() if (pageIndex == 0) { setCalendars(list) setSummary(list[current].summary_stats ? list[current].summary_stats : null) setCalendarData(list[current]) setIsLoading(false) } }) } else { eatWakes({ times: list.join(';') }).then(res => { var list = (res as any).data.reverse() if (pageIndex == 0) { setCalendars(list) setSummary(list[current].summary_stats ? list[current].summary_stats : null) setCalendarData(list[current]) setIsLoading(false) } }) } } } }, []) function getRecords() { if (isLoading) return pageIndex = 0; setIsLoading(true) var timestamp = TimeFormatter.getMondayTimestamp() var now = new Date() if (now.getDay() == 0 && now.getHours() >= 0 && now.getHours() < 12) { timestamp -= 24 * 3600 * 1000 * 7 } var list: any = [] var offset = 12 * 3600 * 1000 + pageIndex * pageSize * 24 * 3600 * 1000 if (!props.isFastSleep) { timestamp = TimeFormatter.getSundayTimestamp() offset = pageIndex * pageSize * 24 * 3600 * 1000 } for (var i = 0; i < pageSize; i++) { list.push(`${i + pageIndex * pageSize},${timestamp - 7 * 24 * 3600 * 1000 * i - offset},${timestamp - 7 * 24 * 3600 * 1000 * i + 7 * 24 * 3600 * 1000 - offset}`) } if (props.isFastSleep) { clockSummaryStats({ times: list.join(';') }).then(res => { var list = (res as any).data.reverse() if (list.length > 1) { lastFastValue = list[list.length - 2].summary_stats.fast.avg lastSleepValue = list[list.length - 2].summary_stats.sleep.avg } if (pageIndex == 0) { setCalendars(list) setCurrent(list.length - 1) setSummary(list[list.length - 1].summary_stats ? list[list.length - 1].summary_stats : null) setCalendarData(list[list.length - 1]) setMinTime((res as any).extra.real_start_time_min) setLeft((list.length - 1) * parseInt(rpxToPx(658) + '')) setIsLoading(false); if (ref.current) { (ref.current as any).scrollToOffset((list.length - 1) * parseInt(rpxToPx(658) + ''), false); setTimeout(() => { (ref.current as any).scrollToOffset((list.length - 1) * parseInt(rpxToPx(658) + ''), false); }, 500) } } else { } }) } else { eatWakes({ times: list.join(';') }).then(res => { var list = (res as any).data.reverse() if (list.length > 1) { lastFastValue = list[list.length - 2].summary_stats.eat.avg lastSleepValue = list[list.length - 2].summary_stats.wake.avg } if (pageIndex == 0) { setCalendars(list) setCurrent(list.length - 1) setSummary(list[list.length - 1].summary_stats ? list[list.length - 1].summary_stats : null) setCalendarData(list[list.length - 1]) if ((res as any).extra) { setMinTime((res as any).extra.real_start_time_min) } setLeft((list.length - 1) * parseInt(rpxToPx(658) + '')) setIsLoading(false); if (ref.current) { (ref.current as any).scrollToOffset((list.length - 1) * parseInt(rpxToPx(658) + ''), false); setTimeout(() => { (ref.current as any).scrollToOffset((list.length - 1) * parseInt(rpxToPx(658) + ''), false); }, 500) } } else { } }) } // clockSummaryStats({start:new Date().getTime()-7*24*3600*1000,end:new Date().getTime()}).then(res => { }) } function dragStart(e) { // fingerDrag = true } function dragEnd(e) { // fingerDrag = false } function onScroll(e) { // if (!fingerDrag && process.env.TARO_ENV == 'weapp') return if (lastOffset && Math.abs(lastOffset - e.detail.scrollLeft) > 300) { setLeft(lastOffset) return } // isScrolling = true var x = e.detail.scrollLeft + 5 lastOffset = e.detail.scrollLeft // var page = parseInt(x / parseInt(rpxToPx(658) + '') + '') var page = Math.round(x / parseInt(rpxToPx(658) + '')) console.log(page, x) if (current != page && page < calendars.length && page >= 0) { lastFastValue = calendars[current].summary_stats.fast.avg lastSleepValue = calendars[current].summary_stats.sleep.avg setCurrent(page) setSummary(calendars[page].summary_stats ? calendars[page].summary_stats : null) setCalendarData(calendars[page]) // if (page <= 0) { // getRecords(); // } } // if (scrollTimer) // clearTimeout(scrollTimer); // scrollTimer = setTimeout(() => { // isScrolling = false // fingerDrag = false; // }, 300) as any } function onChange(e) { var page = e.detail.current lastFastValue = props.isFastSleep ? calendars[current].summary_stats.fast.avg : calendars[current].summary_stats.eat.avg lastSleepValue = props.isFastSleep ? calendars[current].summary_stats.sleep.avg : calendars[current].summary_stats.wake.avg setCurrent(page) setSummary(calendars[page].summary_stats ? calendars[page].summary_stats : null) setCalendarData(calendars[page]) } function getDate(timestamp) { var date = new Date(timestamp) if (global.language == 'en') { return dayjs(timestamp).format('MMM D') } return `${date.getMonth() + 1}月${date.getDate()}日` } function getWeekDuration() { if (global.language == 'en') { if (!props.isFastSleep) { return `${t('feature.common.day_of_week_full.sun')}, ${getDate(calendarData.start)} - ${t('feature.common.day_of_week_full.sat')}, ${getDate(calendarData.end - 1)}` } return `${t('feature.common.day_of_week_full.sun')} ${t('feature.common.time_desc.afternoon')}, ${getDate(calendarData.start)} - ${t('feature.common.day_of_week_full.sun')} ${t('feature.common.time_desc.morning')}, ${getDate(calendarData.end)}` } if (!props.isFastSleep) { return `${getDate(calendarData.start)} ${t('feature.common.day_of_week_full.sun')} - ${getDate(calendarData.end - 1)} ${t('feature.common.day_of_week_full.sat')}` } return `${getDate(calendarData.start)} ${t('feature.common.day_of_week_full.sun')}${t('feature.common.time_desc.afternoon')} - ${getDate(calendarData.end)} ${t('feature.common.day_of_week_full.sun')}${t('feature.common.time_desc.morning')}` } function getWeekIndex() { // style={{ opacity: current == calendars.length - 1 ? 1 : 0 }} if (current == calendars.length - 1) { return t('feature.common.week_desc.current_week') } else if (current == calendars.length - 2) { return t('feature.common.week_desc.last_week') } else { return t('feature.common.week_desc.weeks_ago', { index: calendars.length - current - 1 }) } } function showWeeklyItem(index) { if (process.env.TARO_ENV == 'weapp') { if (index >= current - 1 && index <= current + 1) { return true } return false } if (index >= current - 2 && index <= current + 2) { return true } return false } if (!calendarData) return return { !summary || (props.isFastSleep ? summary.fast.avg == 0 : summary.eat.avg == 0) ? : summary.fast.avg : lastFastValue > summary.eat.avg) ? 'calendar-arrow-bg-down' : 'calendar-arrow-bg'} style={{ backgroundColor: props.isFastSleep ? ColorType.fast : ColorType.food }}> } {props.isFastSleep ? t('feature.track_time_duration.weekly.fast_average') : t('feature.track_time_duration.weekly.eat_average')} {!summary || (props.isFastSleep ? summary.fast.avg == 0 : summary.eat.avg == 0) ? t('feature.track_time_duration.record_fast_sleep.none') : TimeFormatter.calculateTimeDifference(new Date().getTime(), new Date().getTime() + (props.isFastSleep ? summary.fast.avg : summary.eat.avg))} { !summary || (props.isFastSleep ? summary.sleep.avg == 0 : summary.wake.avg == 0) ? : (props.isFastSleep ? summary.sleep.avg : summary.wake.avg) ? 'calendar-arrow-bg-down' : 'calendar-arrow-bg'} style={{ backgroundColor: props.isFastSleep ? ColorType.sleep : ColorType.activity }}> } {props.isFastSleep ? t('feature.track_time_duration.weekly.sleep_average') : t('feature.track_time_duration.weekly.wake_average')} {!summary || (props.isFastSleep ? summary.sleep.avg == 0 : summary.wake.avg == 0) ? t('feature.track_time_duration.record_fast_sleep.none') : TimeFormatter.calculateTimeDifference(new Date().getTime(), new Date().getTime() + (props.isFastSleep ? summary.sleep.avg : summary.wake.avg))} {getWeekIndex()} {getWeekDuration()} { process.env.TARO_ENV == 'weapp' ? : } { calendars.map((item, index) => { return { showWeeklyItem(index) ? : } }) } { props.isFastSleep ? 12:00 18:00 24:00 06:00 12:00 : 00:00 06:00 12:00 18:00 24:00 } }) export default WeekCalendar;