|
|
@@ -1,32 +1,27 @@
|
|
|
import { recordCheck } from "@/services/trackTimeDuration";
|
|
|
-import { View, Text, PickerView, PickerViewColumn, Picker } from "@tarojs/components";
|
|
|
+import { View, Text } from "@tarojs/components";
|
|
|
import trackTimeService, { machine } from "@/store/trackTimeMachine"
|
|
|
import { useEffect, useState } from "react";
|
|
|
import { TimeFormatter } from "@/utils/time_format";
|
|
|
import TimePickers from '@/components/TimePickers'
|
|
|
import { useSelector } from "react-redux";
|
|
|
import Taro from "@tarojs/taro";
|
|
|
+import { AtFloatLayout } from "taro-ui";
|
|
|
+import LimitPickers from '@/components/LimitPickers';
|
|
|
+import { endFast, endSleep, startFast, startSleep } from "../actions/TrackTimeActions";
|
|
|
|
|
|
export default function Component() {
|
|
|
- // const [current,send] = useService(machine)
|
|
|
- // const currentState = trackTimeService.getSnapshot();
|
|
|
- // console.log(currentState.value);
|
|
|
- // debugger
|
|
|
const [key, setKey] = useState('');
|
|
|
const [value, setValue] = useState('');
|
|
|
- const [counter, setCounter] = useState(0)
|
|
|
- const [timerId, setTimerId] = useState(null)
|
|
|
const user = useSelector((state: any) => state.user);
|
|
|
|
|
|
const [isFast, setIsFast] = useState(true);
|
|
|
const [fastValues, setFastValues] = useState<number[]>([0, 0]);
|
|
|
const [sleepValues, setSleepValues] = useState<number[]>([0, 0]);
|
|
|
- const [fastStr,setFastStr] = useState('00:00');
|
|
|
- const [sleepStr,setSleepStr] = useState('00:00');
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+ const [fastStr, setFastStr] = useState('00:00');
|
|
|
+ const [sleepStr, setSleepStr] = useState('00:00');
|
|
|
+ const [isOpen, setIsOpen] = useState(false);
|
|
|
+ const [resetPicker, setResetPicker] = useState(false);
|
|
|
|
|
|
useEffect(() => {
|
|
|
getStateDetail();
|
|
|
@@ -34,9 +29,6 @@ export default function Component() {
|
|
|
|
|
|
useEffect(() => {
|
|
|
trackTimeService.onTransition(state => {
|
|
|
- // console.log(state.value);
|
|
|
- var value = trackTimeService.getSnapshot().value
|
|
|
- // console.log('aaa:'+JSON.parse(JSON.stringify(value)));
|
|
|
if ((state.value as any).FAST_SLEEP) {
|
|
|
setKey('FAST_SLEEP');
|
|
|
setValue((state.value as any).FAST_SLEEP);
|
|
|
@@ -52,35 +44,12 @@ export default function Component() {
|
|
|
});
|
|
|
}, []);
|
|
|
|
|
|
- useEffect(() => {
|
|
|
- startTimer();
|
|
|
- return () => {
|
|
|
- // 在组件卸载时清除定时器
|
|
|
- if (timerId) {
|
|
|
- clearInterval(timerId);
|
|
|
- }
|
|
|
- };
|
|
|
- }, [timerId]);
|
|
|
-
|
|
|
- const startTimer = () => {
|
|
|
- // 避免重复启动定时器
|
|
|
- if (timerId) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- const id = setInterval(() => {
|
|
|
- setCounter((prevCounter) => prevCounter + 1);
|
|
|
- }, 1000);
|
|
|
-
|
|
|
- setTimerId(id as any);
|
|
|
- };
|
|
|
-
|
|
|
function getStateDetail() {
|
|
|
var state = trackTimeService.getSnapshot().value
|
|
|
if ((state as any).FAST_SLEEP) {
|
|
|
setKey('FAST_SLEEP');
|
|
|
setValue((state as any).FAST_SLEEP);
|
|
|
- if ((state as any).FAST_SLEEP == 'WAIT_FOR_START') {
|
|
|
+ if ((state as any).FAST_SLEEP == 'WAIT_FOR_START' || (state as any).FAST_SLEEP == 'ONGOING3') {
|
|
|
setIsFast(true);
|
|
|
}
|
|
|
else if ((state as any).FAST_SLEEP == 'ONGOING1') {
|
|
|
@@ -99,272 +68,121 @@ export default function Component() {
|
|
|
}
|
|
|
|
|
|
var checkData = machine.context.checkData;
|
|
|
- if (checkData){
|
|
|
+ if (checkData) {
|
|
|
var fastTime = TimeFormatter.formateHourMinute((checkData as any).current_record.fast.target_start_time,
|
|
|
- (checkData as any).current_record.fast.target_end_time);
|
|
|
+ (checkData as any).current_record.fast.target_end_time);
|
|
|
var sleepTime = TimeFormatter.formateHourMinute((checkData as any).current_record.sleep.target_start_time,
|
|
|
- (checkData as any).current_record.sleep.target_end_time);
|
|
|
+ (checkData as any).current_record.sleep.target_end_time);
|
|
|
|
|
|
setFastValues(fastTime.split(':').map(x => parseInt(x)));
|
|
|
setSleepValues(sleepTime.split(':').map(x => parseInt(x)));
|
|
|
setFastStr(fastTime);
|
|
|
setSleepStr(sleepTime);
|
|
|
- console.log(fastTime);
|
|
|
- console.log(sleepTime)
|
|
|
- console.log(fastValues)
|
|
|
- console.log(sleepValues)
|
|
|
- console.log('--------------')
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
|
|
|
- function isMap(obj: any): obj is Map<any, any> {
|
|
|
- return obj instanceof Map;
|
|
|
- }
|
|
|
|
|
|
- function startFast(start_time: number) {
|
|
|
- const duration = fastValues[0] * 3600 * 1000 + fastValues[1] * 5 * 60 * 1000//8 * 3600 * 1000;
|
|
|
- const extra = {
|
|
|
- set_time: start_time - 20 * 1000,
|
|
|
- confirm_time: start_time + 50 * 1000,
|
|
|
- }
|
|
|
- recordCheck({
|
|
|
- action: 'FAST_START',
|
|
|
- real_check_time: start_time,
|
|
|
- target_duration: duration,
|
|
|
- extra: extra
|
|
|
- }).then(res => {
|
|
|
- console.log(res);
|
|
|
- trackTimeService.send({ type: 'START_FAST' });
|
|
|
- });
|
|
|
- // dispatch(startFast());
|
|
|
- }
|
|
|
- function startSleep(start_time: number) {
|
|
|
- const duration = sleepValues[0] * 3600 * 1000 + sleepValues[1] * 5 * 60 * 1000
|
|
|
- const extra = {
|
|
|
- set_time: start_time - 20 * 1000,
|
|
|
- confirm_time: start_time + 50 * 1000,
|
|
|
- }
|
|
|
- recordCheck({
|
|
|
- action: 'SLEEP_START',
|
|
|
- real_check_time: start_time,
|
|
|
- target_duration: duration,
|
|
|
- extra: extra
|
|
|
- }).then(res => {
|
|
|
- console.log(res);
|
|
|
- trackTimeService.send({ type: 'START_SLEEP' });
|
|
|
- });
|
|
|
- // dispatch(startSleep());
|
|
|
+ function showPicker() {
|
|
|
+ setIsOpen(true)
|
|
|
}
|
|
|
- function endSleep(start_time: number) {
|
|
|
- // const duration = 8 * 3600 * 1000;
|
|
|
- const extra = {
|
|
|
- set_time: start_time - 20 * 1000,
|
|
|
- confirm_time: start_time + 50 * 1000,
|
|
|
- }
|
|
|
- recordCheck({
|
|
|
- action: 'SLEEP_END',
|
|
|
- real_check_time: start_time,
|
|
|
- extra: extra
|
|
|
- }).then(res => {
|
|
|
- console.log(res);
|
|
|
- trackTimeService.send({ type: 'END_SLEEP' });
|
|
|
- });
|
|
|
- }
|
|
|
- function endFast(start_time: number) {
|
|
|
- const extra = {
|
|
|
- set_time: start_time - 20 * 1000,
|
|
|
- confirm_time: start_time + 50 * 1000,
|
|
|
- }
|
|
|
- recordCheck({
|
|
|
- action: 'FAST_END',
|
|
|
- real_check_time: start_time,
|
|
|
- extra: extra
|
|
|
- }).then(res => {
|
|
|
- console.log(res);
|
|
|
- trackTimeService.send({ type: 'END_FAST' });
|
|
|
- trackTimeService.send({ type: 'RESET' });
|
|
|
- trackTimeService.send({ type: global.scenario });
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- function minTime() {
|
|
|
- if (value == 'WAIT_FOR_START' || !(machine.context.checkData as any).current_record.fast.real_start_time) {
|
|
|
- return '00:00'
|
|
|
- }
|
|
|
- var time = new Date((machine.context.checkData as any).current_record.fast.real_start_time);
|
|
|
- const formatter = new Intl.DateTimeFormat('en-US', {
|
|
|
- hour: '2-digit',
|
|
|
- minute: '2-digit',
|
|
|
- });
|
|
|
|
|
|
- return formatter.format(time);
|
|
|
+ function hidePicker() {
|
|
|
+ setIsOpen(false)
|
|
|
+ setTimeout(() => {
|
|
|
+ setResetPicker(true)
|
|
|
+ }, 500)
|
|
|
+ setTimeout(() => {
|
|
|
+ setResetPicker(false)
|
|
|
+ }, 1000)
|
|
|
}
|
|
|
|
|
|
- function sleepEndMinTime(){
|
|
|
- if (!(machine.context.checkData as any).current_record.sleep.real_start_time){
|
|
|
- return '00:00'
|
|
|
+ function layoutContent() {
|
|
|
+ var limit = new Date().getTime() - 7 * 3600 * 1000 * 24;
|
|
|
+ var current_record = machine.context.checkData ? (machine.context.checkData as any).current_record : null;
|
|
|
+ if (current_record && current_record.last_real_check_time)
|
|
|
+ limit = current_record.last_real_check_time
|
|
|
+ if (resetPicker) {
|
|
|
+ return <View />
|
|
|
}
|
|
|
- var time = new Date((machine.context.checkData as any).current_record.sleep.real_start_time);
|
|
|
-
|
|
|
- const formatter = new Intl.DateTimeFormat('en-US', {
|
|
|
- hour: '2-digit',
|
|
|
- minute: '2-digit',
|
|
|
- });
|
|
|
- debugger
|
|
|
- return formatter.format(time);
|
|
|
+ return <View>
|
|
|
+ <LimitPickers limit={limit} onCancel={() => { setIsOpen(false) }} onChange={(e) => {
|
|
|
+ console.log(new Date(e))
|
|
|
+ pickerConfirm(e)
|
|
|
+ hidePicker()
|
|
|
+ }} />
|
|
|
+ </View>
|
|
|
}
|
|
|
|
|
|
- function fastEndMinTime(){
|
|
|
- if (value == 'ONGOING'||!(machine.context.checkData as any).current_record.sleep.real_end_time){
|
|
|
- return '00:00'
|
|
|
+ function pickerConfirm(t: number) {
|
|
|
+ if (isFast) {
|
|
|
+ if (value == 'WAIT_FOR_START') {
|
|
|
+ const duration = fastValues[0] * 3600 * 1000 + fastValues[1] * 60 * 1000
|
|
|
+ startFast(t, duration);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ endFast(t)
|
|
|
+ }
|
|
|
}
|
|
|
- var time = new Date((machine.context.checkData as any).current_record.sleep.real_end_time);
|
|
|
- if (!time){
|
|
|
- return '00:00'
|
|
|
+ else {
|
|
|
+ if (value == 'WAIT_FOR_START' || value == 'ONGOING1') {
|
|
|
+ const duration = sleepValues[0] * 3600 * 1000 + sleepValues[1] * 60 * 1000
|
|
|
+ startSleep(t, duration);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ endSleep(t)
|
|
|
+ }
|
|
|
}
|
|
|
- const formatter = new Intl.DateTimeFormat('en-US', {
|
|
|
- hour: '2-digit',
|
|
|
- minute: '2-digit',
|
|
|
- });
|
|
|
-
|
|
|
- return formatter.format(time);
|
|
|
- }
|
|
|
-
|
|
|
- function currentTime() {
|
|
|
- var time = new Date();
|
|
|
- const formatter = new Intl.DateTimeFormat('en-US', {
|
|
|
- hour: '2-digit',
|
|
|
- minute: '2-digit',
|
|
|
- });
|
|
|
-
|
|
|
- return formatter.format(time);
|
|
|
}
|
|
|
|
|
|
function mixedBtns() {
|
|
|
return <View>
|
|
|
{
|
|
|
(value == 'WAIT_FOR_START' || value == 'DONE') &&
|
|
|
- <Picker mode="time" onChange={timePickerChange} value={currentTime()} end={currentTime()}><Text style={{ color: '#AAFF00' }}>Start Fast</Text></Picker>
|
|
|
+ <Text style={{ color: '#AAFF00' }} onClick={showPicker}>Start Fast</Text>
|
|
|
}
|
|
|
{
|
|
|
(value == 'ONGOING'/* ||value == 'ONGOING1' || value == 'ONGOING2'*/ || value == 'ONGOING3') &&
|
|
|
- <Picker mode="time" onChange={timePickerChange} value={currentTime()} start={fastEndMinTime()} end={currentTime()}>
|
|
|
- <Text>End Fast</Text>
|
|
|
- </Picker>
|
|
|
+ <Text style={{ color: '#AAFF00' }} onClick={showPicker}>End Fast</Text>
|
|
|
}
|
|
|
{
|
|
|
- value == 'ONGOING1' && <Picker mode="time" onChange={timePickerChange} value={currentTime()} start={minTime()} end={currentTime()}>
|
|
|
- <Text style={{ color: '#00ffff' }}>Start Sleep</Text>
|
|
|
- </Picker>
|
|
|
+ value == 'ONGOING1' && <Text style={{ color: '#00ffff' }} onClick={showPicker}>Start Sleep</Text>
|
|
|
}
|
|
|
{
|
|
|
- value == 'ONGOING2' && <Picker mode="time" onChange={timePickerChange} value={currentTime()} start={sleepEndMinTime()} end={currentTime()}>
|
|
|
- <Text>End Sleep</Text>
|
|
|
- </Picker>
|
|
|
+ value == 'ONGOING2' && <Text style={{ color: '#00ffff' }} onClick={showPicker}>End Sleep</Text>
|
|
|
+
|
|
|
}
|
|
|
</View>
|
|
|
}
|
|
|
|
|
|
function fastBtns() {
|
|
|
return <View>
|
|
|
- {
|
|
|
- value == 'ONGOING' ?
|
|
|
- <Picker mode="time" onChange={timePickerChange} value={currentTime()} end={currentTime()}>
|
|
|
- <Text>End Fast</Text>
|
|
|
- </Picker> :
|
|
|
- <Picker mode="time" onChange={timePickerChange} value={currentTime()} end={currentTime()}>
|
|
|
- <Text style={{ color: '#AAFF00' }}>Start Fast</Text>
|
|
|
- </Picker>
|
|
|
- }
|
|
|
+ <Text style={{ color: '#AAFF00' }} onClick={showPicker}>{value == 'ONGOING' ? 'End Fast' : 'Start Fast'}</Text>
|
|
|
</View>
|
|
|
}
|
|
|
|
|
|
function sleepBtns() {
|
|
|
return <View>
|
|
|
{
|
|
|
- value == 'ONGOING' ?
|
|
|
- <Picker mode="time" onChange={timePickerChange} value={currentTime()} end={currentTime()}>
|
|
|
- <Text >End Sleep</Text>
|
|
|
- </Picker> :
|
|
|
- <Picker mode="time" onChange={timePickerChange} value={currentTime()} end={currentTime()}>
|
|
|
- <Text style={{ color: '#00ffff' }}>Start Sleep</Text></Picker>
|
|
|
+ value == 'ONGOING' ? <Text style={{ color: '#00ffff' }} onClick={showPicker}>End Sleep</Text> :
|
|
|
+ <Text style={{ color: '#00ffff' }} onClick={showPicker}>Start Sleep</Text>
|
|
|
}
|
|
|
</View>
|
|
|
}
|
|
|
|
|
|
- function timePickerChange(e) {
|
|
|
- var time = e.detail.value;
|
|
|
- var hour = time.split(':')[0];
|
|
|
- var minute = time.split(':')[1];
|
|
|
- var dt = new Date();
|
|
|
- dt.setHours(hour);
|
|
|
- dt.setMinutes(minute);
|
|
|
- if (value == 'ONGOING2') {
|
|
|
- endSleep(dt.getTime());
|
|
|
- return;
|
|
|
- }
|
|
|
- else if (value == 'ONGOING3') {
|
|
|
- endFast(dt.getTime());
|
|
|
- return;
|
|
|
- }
|
|
|
- if (isFast) {
|
|
|
- if (value == 'ONGOING') {
|
|
|
- endFast(dt.getTime());
|
|
|
- }
|
|
|
- else {
|
|
|
- startFast(dt.getTime())
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- else {
|
|
|
- if (value == 'ONGOING') {
|
|
|
- endSleep(dt.getTime());
|
|
|
- }
|
|
|
- else {
|
|
|
- startSleep(dt.getTime())
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
const handlePickerChange = (e: string) => {
|
|
|
var [hour, minute] = e.split(':').map(x => parseInt(x))
|
|
|
isFast ? setFastValues([hour, minute]) : setSleepValues([hour, minute]);
|
|
|
- debugger
|
|
|
};
|
|
|
|
|
|
- function login(){
|
|
|
+ function login() {
|
|
|
Taro.navigateTo({
|
|
|
url: '/pages/ChooseAuth'
|
|
|
- })
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
if (!user.isLogin) {
|
|
|
return <View style={{ display: 'flex', flexDirection: 'column', width: '100%', alignItems: 'center' }}>
|
|
|
- <TimePickers time={isFast ? fastStr : sleepStr} content="" change={handlePickerChange} isPickerView={true}/>
|
|
|
- {/* <PickerView
|
|
|
-
|
|
|
- value={isFast ? fastValues : sleepValues}
|
|
|
- onChange={handlePickerChange}
|
|
|
- indicatorStyle='height: 50px;' style='width: 100%; height: 100px;'>
|
|
|
- <PickerViewColumn>
|
|
|
- {hours.map(item => {
|
|
|
- return (
|
|
|
- <View style={{ display: 'flex', alignItems: 'center', justifyContent: 'center' }}>{item}小时</View>
|
|
|
- );
|
|
|
- })}
|
|
|
- </PickerViewColumn>
|
|
|
- <PickerViewColumn>
|
|
|
- {minutes.map(item => {
|
|
|
- return (
|
|
|
- <View style={{ display: 'flex', alignItems: 'center', justifyContent: 'center' }}>{item}分钟</View>
|
|
|
- );
|
|
|
- })}
|
|
|
- </PickerViewColumn>
|
|
|
- </PickerView> */}
|
|
|
+ <TimePickers time={isFast ? fastStr : sleepStr} content="" change={handlePickerChange} isPickerView={true} />
|
|
|
<Text style={{ color: '#AAFF00' }} onClick={login}>Start Fast</Text>
|
|
|
</View>
|
|
|
}
|
|
|
@@ -372,34 +190,14 @@ export default function Component() {
|
|
|
<View style={{ display: 'flex', flexDirection: 'column', width: '100%', alignItems: 'center' }}>
|
|
|
{
|
|
|
value == 'ONGOING' && <View>
|
|
|
- <Text>{(machine.context.checkData as any).current_record.scenario=='FAST'?
|
|
|
- TimeFormatter.countdown((machine.context.checkData as any).current_record.fast.target_end_time):
|
|
|
- TimeFormatter.countdown((machine.context.checkData as any).current_record.sleep.target_end_time)}</Text>
|
|
|
+ <Text>{(machine.context.checkData as any).current_record.scenario == 'FAST' ?
|
|
|
+ TimeFormatter.countdown((machine.context.checkData as any).current_record.fast.target_end_time) :
|
|
|
+ TimeFormatter.countdown((machine.context.checkData as any).current_record.sleep.target_end_time)}</Text>
|
|
|
</View>
|
|
|
}
|
|
|
{
|
|
|
(value == 'ONGOING1' || value == 'WAIT_FOR_START') &&
|
|
|
- <TimePickers time={isFast ? fastStr : sleepStr} content="" change={handlePickerChange} isPickerView={true}/>
|
|
|
- // <PickerView
|
|
|
-
|
|
|
- // value={isFast ? fastValues : sleepValues}
|
|
|
- // onChange={handlePickerChange}
|
|
|
- // indicatorStyle='height: 50px;' style='width: 100%; height: 100px;'>
|
|
|
- // <PickerViewColumn>
|
|
|
- // {hours.map(item => {
|
|
|
- // return (
|
|
|
- // <View style={{ display: 'flex', alignItems: 'center', justifyContent: 'center' }}>{item}小时</View>
|
|
|
- // );
|
|
|
- // })}
|
|
|
- // </PickerViewColumn>
|
|
|
- // <PickerViewColumn>
|
|
|
- // {minutes.map(item => {
|
|
|
- // return (
|
|
|
- // <View style={{ display: 'flex', alignItems: 'center', justifyContent: 'center' }}>{item}分钟</View>
|
|
|
- // );
|
|
|
- // })}
|
|
|
- // </PickerViewColumn>
|
|
|
- // </PickerView>
|
|
|
+ <TimePickers time={isFast ? fastStr : sleepStr} content="" change={handlePickerChange} isPickerView={true} />
|
|
|
}
|
|
|
{
|
|
|
machine.context.checkData && value == 'ONGOING2' && <View>
|
|
|
@@ -424,7 +222,23 @@ export default function Component() {
|
|
|
key === 'SLEEP' && sleepBtns()
|
|
|
}
|
|
|
</View>
|
|
|
- {/* <TimePickers time="00:10"/> */}
|
|
|
+
|
|
|
+ {
|
|
|
+ machine.context.checkData && (machine.context.checkData as any).current_record.last_real_check_time &&
|
|
|
+ <Text style={{ width: '100%', textAlign: 'center' }}>
|
|
|
+ {TimeFormatter.formateTime((machine.context.checkData as any).current_record.last_real_check_time)}
|
|
|
+ </Text>
|
|
|
+ }
|
|
|
+ <AtFloatLayout
|
|
|
+ isOpened={isOpen}
|
|
|
+ onClose={() => {
|
|
|
+ setIsOpen(false)
|
|
|
+ }}
|
|
|
+ title="">
|
|
|
+ {
|
|
|
+ layoutContent()
|
|
|
+ }
|
|
|
+ </AtFloatLayout>
|
|
|
</View>
|
|
|
)
|
|
|
}
|