import { ColorType } from "@/context/themes/color"; import { ChooseScenarioBtn } from "@/features/common/SpecBtns"; import { jumpPage } from "@/features/trackTimeDuration/hooks/Common"; import { View, Text, Picker, ScrollView } from "@tarojs/components"; import { useDidShow, useRouter } from "@tarojs/taro"; import { useEffect, useRef, useState } from "react"; import './ChooseScenario.scss'; import PickerViews from "@/components/input/PickerViews"; import { useDispatch, useSelector } from "react-redux"; import { rpxToPx } from "@/utils/tools"; import { updateFast, updateSleep, updateStep } from "@/store/set_target"; import { TimeFormatter } from "@/utils/time_format"; import Footer from "@/components/layout/Footer"; import { IconCheck } from "@/components/basic/Icons"; import Rings2, { BgRing, RingCommon, TargetRing } from '@/features/trackTimeDuration/components/Rings'; import { useTranslation } from "react-i18next"; import Taro from "@tarojs/taro"; let ScrollViewRN if (process.env.TARO_ENV === 'rn') { ScrollViewRN = require('react-native').ScrollView } export default function Suggest() { let router: any = null var navigation: any = null if (process.env.TARO_ENV === 'rn') { var useNavigation = require("@react-navigation/native").useNavigation navigation = useNavigation() const useRoute = require("@react-navigation/native").useRoute router = useRoute() } else { router = useRouter() } const common = useSelector((state: any) => state.common); const target = useSelector((state: any) => state.target); const [pageStep] = useState(target.step); const dispatch = useDispatch(); const [suggestIndex, setSuggestIndex] = useState(-1) const [suggestItems, setSuggestItem] = useState([]) const [scrollTop, setScrollTop] = useState(0) const [fastRecommendIndex, setFastRecommendIndex] = useState(-1) const [fastLastIndex, setFastLastIndex] = useState(-1) const [scrollHeight, setScrollHeight] = useState(0) const { t } = useTranslation() const scrollViewRef = useRef(null); useEffect(() => { if (navigation) { navigation.setOptions({ headerTitle: '', }); } if (pageStep == 1) { var obj = target.fast.schedule.fast var array: any = [] var currentIndex = -1; var recommendIndex = -1; var lastIndex = -1; for (var i = 0; i < obj.duration.options.length; i++) { if (obj.duration.options[i] == obj.duration.last_value) { lastIndex = i } if (obj.duration.options[i] == obj.duration.default_value) { recommendIndex = i } var fast = JSON.parse(JSON.stringify(target.fast)) var startCount = fast.schedule.fast.end_time.split(':')[0] * 60 + fast.schedule.fast.end_time.split(':')[1] * 1 - obj.duration.options[i] if (startCount < 0) { startCount += 24 * 60 } var start = TimeFormatter.padZero(Math.floor(startCount / 60)) + ':' + TimeFormatter.padZero(startCount % 60) array.push({ hours: obj.duration.options[i] / 60, left: 24 - obj.duration.options[i] / 60, // minutes: obj.duration.minutes[i], // number: obj.duration.numbers[i], time: start }) if (obj.duration.options[i] == obj.duration.init_value) { currentIndex = i } } // var duration = obj.end_time.split(':')[0] * 60 + obj.end_time.split(':')[1] * 1 - (obj.start_time.split(':')[0] * 60 + obj.start_time.split(':')[1] * 1) // if (duration <= 0) { // duration += 24 * 60 // } // var durationHours = Math.floor(duration / 60) // var durationMinutes = duration % 60 // var currentIndex = -1; // var array: any = [] // for (var i = 12; i <= 23; i++) { // if (i != 23 || durationMinutes == 0) { // array.push({ // hours: i, // minutes: durationMinutes // }); // } // } // for (var i = 0; i < array.length; i++) { // if (array[i].hours == durationHours && array[i].minutes == durationMinutes) { // currentIndex = i // break // } // } setFastRecommendIndex(recommendIndex) setFastLastIndex(lastIndex) setSuggestItem(array) setSuggestIndex(currentIndex) setScrollTop(currentIndex * (rpxToPx(92 + 28) + 72) - 30) if (process.env.TARO_ENV == 'rn') { setTimeout(() => { var screenHeight = Taro.getSystemInfoSync().screenHeight var safeBottom = Taro.getSystemInfoSync().safeArea?.bottom setScrollHeight(Taro.getSystemInfoSync().windowHeight - 50 - rpxToPx(88) - rpxToPx(420) - (screenHeight - safeBottom) - rpxToPx(46)); }, 100) setTimeout(() => { (scrollViewRef.current as any).scrollTo({ x: 0, y: currentIndex * (rpxToPx(92 + 28) + 72) - 30, animated: true }) }, 500) } else { setTimeout(() => { Taro.createSelectorQuery().select('#myScrollView').boundingClientRect((rect) => { var screenHeight = Taro.getSystemInfoSync().screenHeight var safeBottom = Taro.getSystemInfoSync().safeArea?.bottom setScrollHeight(Taro.getSystemInfoSync().windowHeight - (rect as any).top - rpxToPx(88) - 50 - (screenHeight - safeBottom) - rpxToPx(46)); }).exec() }, 100) } } else if (pageStep == 3) { var obj = target.sleep.schedule.sleep var sleep_cycle = obj.cycle var sleep_latency = obj.latency.init_value var array: any = [] var index: number = 0; for (var i = 0; i < sleep_cycle.num.options.length; i++) { var count = sleep_cycle.num.options[i] * sleep_cycle.duration.init_value + sleep_latency //sleep.schedule.sleep.cycle.num.init_value = sleep.schedule.sleep.cycle.num.options[suggestIndex] if (sleep_cycle.num.options[i] == sleep_cycle.num.init_value) { index = i } var sleep = JSON.parse(JSON.stringify(target.sleep)) var startCount = sleep.schedule.sleep.end_time.split(':')[0] * 60 + sleep.schedule.sleep.end_time.split(':')[1] * 1 - count if (startCount < 0) { startCount += 24 * 60 } var start = TimeFormatter.padZero(Math.floor(startCount / 60)) + ':' + TimeFormatter.padZero(startCount % 60) array.push({ hours: Math.floor(count / 60), minutes: count % 60, number: sleep_cycle.num.options[i], time: start }) } setSuggestItem(array) setSuggestIndex(index) } }, []) useDidShow(() => { dispatch(updateStep({ step: pageStep })) }) function next() { if (pageStep == 1) { var item = suggestItems[suggestIndex] var fast = JSON.parse(JSON.stringify(target.fast)) // var startCount = fast.schedule.fast.end_time.split(':')[0] * 60 + fast.schedule.fast.end_time.split(':')[1] * 1 - (item.hours * 60 + item.minutes) // if (startCount < 0) { // startCount += 24 * 60 // } // var start = TimeFormatter.padZero(Math.floor(startCount / 60)) + ':' + TimeFormatter.padZero(startCount % 60) fast.schedule.fast.start_time = item.time fast.schedule.fast.duration.init_value = fast.schedule.fast.duration.options[suggestIndex] dispatch(updateFast({ fast: fast })) } else if (pageStep == 3) { var item = suggestItems[suggestIndex] var sleep = JSON.parse(JSON.stringify(target.sleep)) var startCount = sleep.schedule.sleep.end_time.split(':')[0] * 60 + sleep.schedule.sleep.end_time.split(':')[1] * 1 - (item.hours * 60 + item.minutes) if (startCount < 0) { startCount += 24 * 60 } var start = TimeFormatter.padZero(Math.floor(startCount / 60)) + ':' + TimeFormatter.padZero(startCount % 60) sleep.schedule.sleep.start_time = start sleep.schedule.sleep.cycle.num.init_value = sleep.schedule.sleep.cycle.num.options[suggestIndex] dispatch(updateSleep({ sleep: sleep })) } var step: number = target.step + 1 dispatch(updateStep({ step: step })) var url = '/pages/clock/Suggest?trigger_event=' + router.params.trigger_event var pageName = 'Suggest' if (!target.isMixed) { if (step == 2) { url = '/pages/clock/SetGoal?trigger_event=' + router.params.trigger_event pageName = 'SetGoal' } } else { if (step == 4) { url = '/pages/clock/SetGoal?trigger_event=' + router.params.trigger_event pageName = 'SetGoal' } } jumpPage(url, pageName, navigation, { trigger_event: router.params.trigger_event }) } function myself() { jumpPage(`/pages/clock/SetGoal?isSelf=1&trigger_event=` + router.params.trigger_event, 'SetGoal', navigation, { isSelf: 1, trigger_event: router.params.trigger_event }) } function suggestTitle() { switch (pageStep) { case 0: return t('feature.suggest.step_0_title') case 1: return t('feature.suggest.step_1_title') case 2: return t('feature.suggest.step_2_title') case 3: return t('feature.suggest.step_3_title') default: return '' } } function suggestDesc() { switch (pageStep) { case 0: return t('feature.suggest.end_fast_picker_header') case 1: return t('feature.suggest.fast_suggest_header', { time: target.fast.schedule.fast.end_time }) case 2: return t('feature.suggest.end_sleep_picker_header') case 3: return t('feature.suggest.sleep_suggest_header', { time: target.sleep.schedule.sleep.end_time }) default: return '' } } function durationDatas() { var min: number = 0 var max: number = 23 var step: number = 5 var minutes: string[] = [] for (let i = 0; i < 60; i += step) { minutes.push(TimeFormatter.padZero(i)) } var hours: string[] = [] for (let i = min; i <= max; i++) { hours.push(TimeFormatter.padZero(i)) } return [hours, minutes] } function durationValues() { var eatTime = target.fast.schedule.fast.end_time if (pageStep == 2) { eatTime = target.sleep.schedule.sleep.end_time } var eatHour = eatTime.split(':')[0] var eatMin = eatTime.split(':')[1] var list = durationDatas() var hourIndex = 0 var minIndex = 0 for (let i = 0; i < list[0].length; i++) { if (list[0][i] == eatHour) { hourIndex = i break } } for (let i = 0; i < list[1].length; i++) { if (list[1][i] == eatMin) { minIndex = i break } } return [hourIndex, minIndex] } function sleepLatency() { var obj = target.sleep.schedule.sleep.latency var min: number = obj.min var max: number = obj.max var step: number = obj.step var minutes: string[] = [] for (let i = min; i <= max; i += step) { minutes.push(i + ' ' + t('feature.suggest.mins')) } return [minutes] } function sleepLatencyValue() { var obj = target.sleep.schedule.sleep.latency var value: number = obj.init_value var min: number = obj.min var step: number = obj.step var index = (value - min) / step return [index] } function timePickerChanged(e: any) { var obj = target.fast.schedule.fast if (pageStep == 2) { obj = target.sleep.schedule.sleep } var duration = obj.end_time.split(':')[0] * 60 + obj.end_time.split(':')[1] * 1 - (obj.start_time.split(':')[0] * 60 + obj.start_time.split(':')[1] * 1) if (duration <= 0) { duration += 24 * 60 } var endCount = duration - (e[0] * 60 + e[1] * 5) if (endCount < 0) { endCount += 24 * 60 } var time = TimeFormatter.padZero(e[0]) + ':' + TimeFormatter.padZero(e[1] * 5) var start = TimeFormatter.padZero(Math.floor(endCount / 60)) + ':' + TimeFormatter.padZero(endCount % 60) if (pageStep == 0) { var fast = JSON.parse(JSON.stringify(target.fast)) fast.schedule.fast.end_time = time fast.schedule.fast.start_time = start dispatch(updateFast({ fast: fast })) } else { var sleep = JSON.parse(JSON.stringify(target.sleep)) sleep.schedule.sleep.end_time = time sleep.schedule.sleep.start_time = start dispatch(updateSleep({ sleep: sleep })) } } function picker1() { return { }} /> } function timePicker() { return { process.env.TARO_ENV == 'weapp' ? {picker1()} : {picker1()} } { pageStep == 0 && {t('feature.suggest.end_fast_picker_footer')} } { pageStep == 1 && If you plan to skip breakfast, pick a time for your first meal of the day. } } function fallSleepChanged(e: any) { var value = target.sleep.schedule.sleep.latency var time = e[0] * value.step + value.min var sleep = JSON.parse(JSON.stringify(target.sleep)) sleep.schedule.sleep.latency.init_value = time dispatch(updateSleep({ sleep: sleep })) } function picker2() { return { }} /> } function fallSleepPicker() { return {t('feature.suggest.fall_sleep_picker_header')} { process.env.TARO_ENV == 'weapp' ? { picker2() } : { picker2() } } {t('feature.suggest.fall_sleep_picker_footer')} } // const common: RingCommon = { // useCase: 'ChooseScenario', // radius: 33, // lineWidth: 8, // isFast: true, // status: 'WAIT_FOR_START' // } const bgRing: BgRing = { color: '#262626' } // const targetRing: TargetRing = { // color: ColorType.fast, // startArc: timeToArc(ring.schedule.fast.start_time), // durationArc: durationToArc(ring.schedule.fast.start_time, ring.schedule.fast.end_time) // } function ringCommon() { return { useCase: 'ChooseScenario', radius: 30, lineWidth: 12, isFast: true, status: 'WAIT_FOR_START' } } function targetRing(start, end, isFast, isHighLight) { var color: any = isFast ? ColorType.fast + '66' : ColorType.sleep + '66' if (!isHighLight) { color = isFast ? ColorType.fast + '0D' : ColorType.sleep + '0D' } return { color: color, startArc: timeToArc(start), durationArc: durationToArc(start, end) } } function point(isFast: boolean, isShow: boolean) { if (!isShow) return null; var now = new Date() var t = suggestItems[suggestIndex].time now.setHours(parseInt(t.split(':')[0])) now.setMinutes(parseInt(t.split(':')[1])) return [{ color: isFast ? ColorType.fast : ColorType.sleep, borderColor: 'transparent', timestamp: now.getTime() }] } function timeToArc(time: string) { var count: number = parseInt(time.split(':')[1]) + parseInt(time.split(':')[0]) * 60 // count = count>720?count-720:count return (count as any) / 1440 * 2 * Math.PI - Math.PI / 2 } function durationToArc(startTime: string, endTime: string) { var start: number = parseInt(startTime.split(':')[1]) + parseInt(startTime.split(':')[0]) * 60 var end: number = parseInt(endTime.split(':')[1]) + parseInt(endTime.split(':')[0]) * 60 if (end <= start) { end += 1440 } return (end - start) / 1440 * 2 * Math.PI } function scrollContent() { return { suggestItems.map((item: any, index: number) => { return setSuggestIndex(index)}> {item.hours}{t('feature.suggest.hour')} {index == suggestIndex ? t('feature.suggest.fast_desc', { start_time: item.hours, end_time: item.left }) : t('feature.suggest.intermittent_fast', { data: `${item.hours}/${item.left}` }) } { index == suggestIndex && {t('feature.suggest.start_fast_time', { time: item.time })} } { index == suggestIndex && } { index == fastRecommendIndex && {t('feature.suggest.popular')} } { index == fastLastIndex && {t('feature.suggest.recent')} } // return { // setSuggestIndex(index) // }} key={index} className={index == suggestIndex ? 'item_choose_selected' : 'item_choose'}> // // {item.hours} hours // // { // index == fastRecommendIndex && // Popular // } // { // index == fastLastIndex && // Recent // } // // {index == suggestIndex ? `${item.hours} hours fasting / ${item.left} hours eating` : `${item.hours}/${item.left} Intermittent Fasting`} // { // index == suggestIndex && // // Start fasting at {item.time} // // // } // }) } } function suggestList() { if (pageStep == 1 && process.env.TARO_ENV === 'weapp') { return { scrollContent() } } if (pageStep == 1 && process.env.TARO_ENV === 'rn') { return { scrollContent() } } return { suggestItems.map((item: any, index: number) => { return setSuggestIndex(index)}> {item.hours}{t('feature.suggest.hours2')}{item.minutes > 0 && item.minutes + t('feature.suggest.minute')} {t('feature.suggest.cycles', { times: item.number })} { index == suggestIndex && {t('feature.suggest.go_to_bed_at', { time: item.time })} } { index == 0 && {t('feature.suggest.popular')} } { index == 1 && {t('feature.suggest.restorative')} } { index == suggestIndex && } }) } {t('feature.suggest.sleep_suggest_footer')} } function content() { return { (pageStep == 0 || pageStep == 2) && timePicker() } { (pageStep == 1 || pageStep == 3) && suggestList() } {/* { pageStep == 3 && {t('feature.suggest.sleep_suggest_footer')} } */} { pageStep == 2 && fallSleepPicker() } { pageStep == 2 && process.env.TARO_ENV=='weapp' && } } function btnText() { // if (pageStep == 3){ // return t('feature.suggest.done') // } // if (pageStep == 1 && !target.isMixed){ // return t('feature.suggest.done') // } return t('feature.suggest.btn') } return {suggestTitle()} {suggestDesc()} { content() } { process.env.TARO_ENV == 'rn' && }
{t('feature.suggest.set_myself')}
{ process.env.TARO_ENV == 'rn' && } }