Console.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. import { recordCheck } from "@/services/trackTimeDuration";
  2. import { View, Text, PickerView } from "@tarojs/components";
  3. import trackTimeService, { machine } from "@/store/trackTimeMachine"
  4. import { useEffect, useRef, useState } from "react";
  5. import { TimeFormatter } from "@/utils/time_format";
  6. import TimePickers from '@/components/TimePickers'
  7. import { useSelector } from "react-redux";
  8. import Taro from "@tarojs/taro";
  9. import LimitPickers from '@/components/LimitPickers';
  10. import { endFast, endSleep, startFast, startSleep } from "../actions/TrackTimeActions";
  11. import { durationDatas, durationIndex, pickerDurations } from "../hooks/Console";
  12. import PickerViews from "@/components/PickerViews";
  13. import Modal from "@/components/Modal";
  14. import Stepper from "@/components/Stepper";
  15. export default function Component() {
  16. const scenario = useSelector((state: any) => state.scenario);
  17. const [key, setKey] = useState('');
  18. const [value, setValue] = useState('');
  19. const user = useSelector((state: any) => state.user);
  20. const common = useSelector((state: any) => state.common);
  21. const [isFast, setIsFast] = useState(true);
  22. const [fastValues, setFastValues] = useState<number[]>([0, 0]);
  23. const [sleepValues, setSleepValues] = useState<number[]>([0, 0]);
  24. const [fastDuration, setFastDuration] = useState<number>(0);
  25. const [sleepDuration, setSleepDuration] = useState<number>(0);
  26. const [fastStr, setFastStr] = useState('00:00');
  27. const [sleepStr, setSleepStr] = useState('00:00');
  28. const [isOpen, setIsOpen] = useState(false);
  29. const [showModal, setShowModal] = useState(false);
  30. const [fastPickerValue, setFastPickerValue] = useState([0, 0])
  31. const [sleepPickerValue, setSleepPickerValue] = useState([0, 0])
  32. const [isOpenDurationPicker, setIsOpenDurationPicker] = useState(false)
  33. const limitPickerRef = useRef(null)
  34. const durationPickerRef = useRef(null)
  35. // const [pickerValue, setPickerValue] = useState([0,0])
  36. // const pickerDurations = pickerDurations();
  37. // console.log(pickerDurations())
  38. useEffect(() => {
  39. getStateDetail();
  40. }, [machine.context.checkData]);
  41. useEffect(() => {
  42. getStateDetail();
  43. }, [machine.context.currentStatus])
  44. useEffect(() => {
  45. trackTimeService.onTransition(state => {
  46. if ((state.value as any).FAST_SLEEP) {
  47. setKey('FAST_SLEEP');
  48. setValue((state.value as any).FAST_SLEEP);
  49. }
  50. if ((state.value as any).FAST) {
  51. setKey('FAST');
  52. setValue((state.value as any).FAST);
  53. }
  54. if ((state.value as any).SLEEP) {
  55. setKey('SLEEP');
  56. setValue((state.value as any).SLEEP);
  57. }
  58. });
  59. }, []);
  60. function getStateDetail() {
  61. var state = trackTimeService.getSnapshot().value
  62. if ((state as any).FAST_SLEEP) {
  63. setKey('FAST_SLEEP');
  64. setValue((state as any).FAST_SLEEP);
  65. if ((state as any).FAST_SLEEP == 'WAIT_FOR_START' || (state as any).FAST_SLEEP == 'ONGOING3') {
  66. setIsFast(true);
  67. }
  68. else if ((state as any).FAST_SLEEP == 'ONGOING1') {
  69. setIsFast(false);
  70. }
  71. }
  72. if ((state as any).FAST) {
  73. setKey('FAST');
  74. setValue((state as any).FAST);
  75. setIsFast(true);
  76. }
  77. if ((state as any).SLEEP) {
  78. setKey('SLEEP');
  79. setValue((state as any).SLEEP);
  80. setIsFast(false);
  81. }
  82. var checkData = machine.context.checkData;
  83. if (checkData) {
  84. var current_record = (checkData as any).current_record;
  85. if (current_record.fast) {
  86. var fastTime = TimeFormatter.formateHourMinute(current_record.fast.target_start_time,
  87. current_record.fast.target_end_time);
  88. setFastValues(fastTime.split(':').map(x => parseInt(x)));
  89. setFastStr(fastTime);
  90. var fastCount = current_record.fast.target_end_time - current_record.fast.target_start_time
  91. setFastDuration(fastCount)
  92. setFastPickerValue(durationIndex(current_record.fast.target_start_time, current_record.fast.target_end_time, common))
  93. // setFastPickerValue([fastCount / 60000 / 5 - 12])
  94. }
  95. if (current_record.sleep) {
  96. var sleepTime = TimeFormatter.formateHourMinute(current_record.sleep.target_start_time,
  97. current_record.sleep.target_end_time);
  98. setSleepValues(sleepTime.split(':').map(x => parseInt(x)));
  99. setSleepStr(sleepTime);
  100. var sleepCount = current_record.sleep.target_end_time - current_record.sleep.target_start_time
  101. setSleepDuration(sleepCount)
  102. setSleepPickerValue(durationIndex(current_record.sleep.target_start_time, current_record.sleep.target_end_time, common))
  103. // setSleepPickerValue([sleepCount / 60000 / 5 - 12])
  104. }
  105. }
  106. }
  107. function showPicker() {
  108. setShowModal(true)
  109. setIsOpen(true)
  110. global.set_time = new Date().getTime()
  111. console.log(global.set_time)
  112. }
  113. function hidePicker() {
  114. setIsOpen(false)
  115. setTimeout(() => {
  116. setShowModal(false)
  117. }, 1000)
  118. }
  119. function testLayout() {
  120. var current_record = machine.context.checkData ? (machine.context.checkData as any).current_record : null;
  121. if (current_record == null) {
  122. return <View />
  123. }
  124. var isStart = false;
  125. if (value == 'WAIT_FOR_START' || value == 'ONGOING1') {
  126. isStart = true
  127. }
  128. var isFast = false;
  129. switch (value) {
  130. case 'WAIT_FOR_START':
  131. case 'ONGOING':
  132. {
  133. isFast = (scenario.name == 'FAST'||scenario.name == 'FAST_SLEEP')
  134. }
  135. break;
  136. case 'ONGOING1':
  137. case 'ONGOING2':
  138. {
  139. isFast = false;
  140. }
  141. break
  142. case 'ONGOING3':
  143. {
  144. isFast = true;
  145. }
  146. break;
  147. }
  148. return <View style={{ color: '#fff', paddingTop: 30, paddingLeft: 30, display: 'flex', flexDirection: 'column' }}>
  149. <Text>check scenario:{isFast ? 'fast' : 'sleep'}</Text>
  150. <Text>check type:{isStart ? 'start' : 'end'}</Text>
  151. <Text style={{ marginTop: 30 }}>picker restriction</Text>
  152. <Text style={{ marginLeft: 50 }}>min:{TimeFormatter.formatTimestamp(global.limit)}</Text>
  153. <Text style={{ marginLeft: 50 }}>pick:{TimeFormatter.formatTimestamp(global.picker_time)}</Text>
  154. <Text style={{ marginLeft: 50 }}>max{TimeFormatter.formatTimestamp(global.set_time)}</Text>
  155. <Text style={{ marginTop: 30 }}>now:{TimeFormatter.formatTimestamp(new Date().getTime())}</Text>
  156. <Text>elapsed:{TimeFormatter.calculateTimeDifference(global.picker_time, new Date().getTime())}</Text>
  157. <Text style={{ marginTop: 30 }}>real start:{isStart ? '-' : TimeFormatter.formatTimestamp(isFast ? current_record.fast.real_start_time : current_record.sleep.real_start_time)}</Text>
  158. <Text>real duration:{isStart ? '-' : TimeFormatter.calculateTimeDifference(isFast ? current_record.fast.real_start_time : current_record.sleep.real_start_time, new Date().getTime())}</Text>
  159. </View>
  160. }
  161. function layoutContent() {
  162. var limit = global.set_time - 7 * 3600 * 1000 * 24;
  163. var current_record = machine.context.checkData ? (machine.context.checkData as any).current_record : null;
  164. if (current_record && current_record.last_real_check_time){
  165. limit = current_record.last_real_check_time
  166. //当set_time秒数<=latest_record_time秒数时,最小限制时间戳需+1分钟
  167. if (new Date(global.set_time).getSeconds()<=new Date(current_record.last_real_check_time).getSeconds()){
  168. limit = limit + 60 * 1000
  169. }
  170. }
  171. global.limit = limit
  172. return <LimitPickers ref={limitPickerRef} limit={limit} limitDay={8} onCancel={hidePicker} onChange={(e) => {
  173. console.log(new Date(e))
  174. pickerConfirm(e)
  175. hidePicker()
  176. }} />
  177. }
  178. function pickerConfirm(t: number) {
  179. console.log(t)
  180. var date = new Date(t)
  181. var setDate = new Date(global.set_time);
  182. date.setMilliseconds(setDate.getMilliseconds());
  183. date.setSeconds(setDate.getSeconds());
  184. t = date.getTime();
  185. console.log(t);
  186. if (isFast) {
  187. if (value == 'WAIT_FOR_START') {
  188. // const duration = fastValues[0] * 3600 * 1000 + fastValues[1] * 60 * 1000
  189. startFast(t, fastDuration);
  190. }
  191. else {
  192. endFast(t)
  193. }
  194. }
  195. else {
  196. if (value == 'WAIT_FOR_START' || value == 'ONGOING1') {
  197. // const duration = sleepValues[0] * 3600 * 1000 + sleepValues[1] * 60 * 1000
  198. startSleep(t, sleepDuration);
  199. }
  200. else {
  201. endSleep(t)
  202. }
  203. }
  204. }
  205. function mixedBtns() {
  206. return <View>
  207. {
  208. (value == 'WAIT_FOR_START' || value == 'DONE') &&
  209. <Text style={{ color: '#AAFF00' }} onClick={showPicker}>Start Fast</Text>
  210. }
  211. {
  212. (value == 'ONGOING'/* ||value == 'ONGOING1' || value == 'ONGOING2'*/ || value == 'ONGOING3') &&
  213. <Text style={{ color: '#AAFF00' }} onClick={showPicker}>End Fast</Text>
  214. }
  215. {
  216. value == 'ONGOING1' && <Text style={{ color: '#00ffff' }} onClick={showPicker}>Start Sleep</Text>
  217. }
  218. {
  219. value == 'ONGOING2' && <Text style={{ color: '#00ffff' }} onClick={showPicker}>End Sleep</Text>
  220. }
  221. </View>
  222. }
  223. function fastBtns() {
  224. return <View>
  225. <Text style={{ color: '#AAFF00' }} onClick={showPicker}>{value == 'ONGOING' ? 'End Fast' : 'Start Fast'}</Text>
  226. </View>
  227. }
  228. function sleepBtns() {
  229. return <View>
  230. {
  231. value == 'ONGOING' ? <Text style={{ color: '#00ffff' }} onClick={showPicker}>End Sleep</Text> :
  232. <Text style={{ color: '#00ffff' }} onClick={showPicker}>Start Sleep</Text>
  233. }
  234. </View>
  235. }
  236. const handlePickerChange = (e: string) => {
  237. var [hour, minute] = e.split(':').map(x => parseInt(x))
  238. isFast ? setFastValues([hour, minute]) : setSleepValues([hour, minute]);
  239. };
  240. function durationChange(e) {
  241. var count = (e[0] + common.duration.min) * 60 + e[1] * common.duration.step
  242. isFast ? setFastDuration(count * 60000) : setSleepDuration(count * 60000);
  243. isFast ? setFastPickerValue(e) : setSleepPickerValue(e)
  244. setIsOpenDurationPicker(false)
  245. }
  246. function login() {
  247. Taro.navigateTo({
  248. url: '/pages/ChooseAuth'
  249. })
  250. }
  251. function durationFormate() {
  252. if (isFast) {
  253. var t = fastDuration / 60000
  254. var hour = Math.floor(t / 60)
  255. var minute = Math.floor(t % 60)
  256. return `${hour > 0 ? hour + '小时' : ''}${minute > 0 ? minute + '分钟' : ''}`
  257. }
  258. else {
  259. var t = sleepDuration / 60000
  260. var hour = Math.floor(t / 60)
  261. var minute = Math.floor(t % 60)
  262. return `${hour > 0 ? hour + '小时' : ''}${minute > 0 ? minute + '分钟' : ''}`
  263. }
  264. }
  265. function showDurationPicker() {
  266. setIsOpenDurationPicker(true)
  267. }
  268. if (!user.isLogin) {
  269. return <View style={{ display: 'flex', flexDirection: 'column', width: '100%', alignItems: 'center' }}>
  270. <Text>16:00</Text>
  271. {/* <PickerViews onChange={() => { }} items={[pickerDurations()]} value={[12 * 15]} /> */}
  272. {/* <TimePickers time={isFast ? fastStr : sleepStr} content="" change={handlePickerChange} isPickerView={true} /> */}
  273. <Text style={{ color: '#AAFF00' }} onClick={login}>Start Fast</Text>
  274. </View>
  275. }
  276. function durationPickerContent() {
  277. return <View style={{ color: '#fff', backgroundColor: 'transparent' }}>
  278. <PickerViews ref={durationPickerRef} onChange={durationChange} items={durationDatas(common)} value={isFast ? fastPickerValue : sleepPickerValue} showBtns={true} onCancel={() => { setIsOpenDurationPicker(false) }} />
  279. </View>
  280. }
  281. function minus() {
  282. if (isFast) {
  283. var count = fastDuration - 5 * 60 * 1000
  284. setFastDuration(count)
  285. var hour = count / 60000 / 60
  286. var minute = count / 60000 % 60
  287. setFastPickerValue(durationIndex('00:00', `${hour > 10 ? hour : '0' + hour}:${minute > 10 ? minute : '0' + minute}`, common))
  288. }
  289. else {
  290. var count = sleepDuration - 5 * 60 * 1000
  291. setSleepDuration(count)
  292. var hour = count / 60000 / 60
  293. var minute = count / 60000 % 60
  294. setSleepPickerValue(durationIndex('00:00', `${hour > 10 ? hour : '0' + hour}:${minute > 10 ? minute : '0' + minute}`, common))
  295. }
  296. }
  297. function plus() {
  298. if (isFast) {
  299. var count = fastDuration + 5 * 60 * 1000
  300. setFastDuration(count)
  301. var hour = count / 60000 / 60
  302. var minute = count / 60000 % 60
  303. setFastPickerValue(durationIndex('00:00', `${hour > 10 ? hour : '0' + hour}:${minute > 10 ? minute : '0' + minute}`, common))
  304. }
  305. else {
  306. var count = sleepDuration + 5 * 60 * 1000
  307. setSleepDuration(count)
  308. var hour = count / 60000 / 60
  309. var minute = count / 60000 % 60
  310. setSleepPickerValue(durationIndex('00:00', `${hour > 10 ? hour : '0' + hour}:${minute > 10 ? minute : '0' + minute}`, common))
  311. }
  312. }
  313. function disableMinus() {
  314. if (isFast) {
  315. if (fastDuration <= 60 * 60 * 1000) {
  316. return true;
  317. }
  318. }
  319. else {
  320. if (sleepDuration <= 60 * 60 * 1000) {
  321. return true;
  322. }
  323. }
  324. return false;
  325. }
  326. function disablePlus() {
  327. if (isFast) {
  328. if (fastDuration >= 23 * 60 * 60 * 1000) {
  329. return true;
  330. }
  331. }
  332. else {
  333. if (sleepDuration >= 23 * 60 * 60 * 1000) {
  334. return true;
  335. }
  336. }
  337. return false;
  338. }
  339. return (
  340. <View style={{ display: 'flex', flexDirection: 'column', width: '100%', alignItems: 'center' }}>
  341. {
  342. machine.context.checkData && (machine.context.checkData as any).current_record.last_real_check_time &&
  343. <Text style={{ width: '100%', textAlign: 'center', color: 'red' }}>
  344. {TimeFormatter.formateTime((machine.context.checkData as any).current_record.last_real_check_time)}
  345. </Text>
  346. }
  347. {
  348. value == 'ONGOING' && <View>
  349. <Text>{(machine.context.checkData as any).current_record.scenario == 'FAST' ?
  350. TimeFormatter.countdown((machine.context.checkData as any).current_record.fast.target_end_time) :
  351. TimeFormatter.countdown((machine.context.checkData as any).current_record.sleep.target_end_time)}</Text>
  352. </View>
  353. }
  354. {
  355. (value == 'ONGOING1' || value == 'WAIT_FOR_START') && <Stepper style={{ marginBottom: 10 }} child={
  356. <Text style={{ width: 150, textAlign: 'center' }} onClick={showDurationPicker}>{durationFormate()}</Text>
  357. } minus={minus} plus={plus} disableMinus={disableMinus()} disablePlus={disablePlus()} />
  358. // <Text style={{marginBottom:10}} onClick={showDurationPicker}>时长:{durationFormate()}</Text>
  359. }
  360. {
  361. machine.context.checkData && value == 'ONGOING2' && <View>
  362. <Text>{TimeFormatter.countdown((machine.context.checkData as any).current_record.sleep.target_end_time)}</Text>
  363. </View>
  364. }
  365. {
  366. machine.context.checkData && value == 'ONGOING3' && <View>
  367. <Text>{TimeFormatter.countdown((machine.context.checkData as any).current_record.fast.target_end_time)}</Text>
  368. </View>
  369. }
  370. <View>
  371. {
  372. key === 'FAST_SLEEP' && mixedBtns()
  373. }
  374. {
  375. key === 'FAST' && fastBtns()
  376. }
  377. {
  378. key === 'SLEEP' && sleepBtns()
  379. }
  380. </View>
  381. {
  382. showModal && isOpen && <Modal testInfo={testLayout()} children={layoutContent()} dismiss={() => setIsOpen(false)} confirm={() => {
  383. var picker = limitPickerRef.current;
  384. pickerConfirm((picker as any).getConfirmData());
  385. setIsOpen(false);
  386. }} />
  387. }
  388. {
  389. isOpenDurationPicker && <Modal children={durationPickerContent()} dismiss={() => setIsOpenDurationPicker(false)} confirm={() => {
  390. var picker = durationPickerRef.current;
  391. durationChange((picker as any).getConfirmData());
  392. setIsOpenDurationPicker(false);
  393. }} />
  394. }
  395. {/* {
  396. showModal && <AtFloatLayout
  397. isOpened={isOpen}
  398. onClose={() => {
  399. setIsOpen(false)
  400. setShowModal(false)
  401. }}
  402. title="">
  403. {
  404. layoutContent()
  405. }
  406. </AtFloatLayout>
  407. }
  408. <AtFloatLayout
  409. isOpened={isOpenDurationPicker}
  410. onClose={() => {
  411. setIsOpenDurationPicker(false)
  412. }}
  413. title="">
  414. {
  415. durationPickerContent()
  416. }
  417. </AtFloatLayout> */}
  418. </View>
  419. )
  420. }