Console.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. global.limit = limit
  164. var current_record = machine.context.checkData ? (machine.context.checkData as any).current_record : null;
  165. if (current_record && current_record.last_real_check_time){
  166. limit = current_record.last_real_check_time
  167. global.limit = limit
  168. //当set_time秒数<=latest_record_time秒数时,最小限制时间戳需+1分钟
  169. if (new Date(global.set_time).getSeconds()<=new Date(current_record.last_real_check_time).getSeconds()){
  170. limit = limit + 60 * 1000
  171. }
  172. }
  173. return <LimitPickers ref={limitPickerRef} limit={limit} limitDay={8} onCancel={hidePicker} onChange={(e) => {
  174. console.log(new Date(e))
  175. pickerConfirm(e)
  176. hidePicker()
  177. }} />
  178. }
  179. function pickerConfirm(t: number) {
  180. console.log(t)
  181. var date = new Date(t)
  182. var setDate = new Date(global.set_time);
  183. date.setMilliseconds(setDate.getMilliseconds());
  184. date.setSeconds(setDate.getSeconds());
  185. t = date.getTime();
  186. console.log(t);
  187. if (isFast) {
  188. if (value == 'WAIT_FOR_START') {
  189. // const duration = fastValues[0] * 3600 * 1000 + fastValues[1] * 60 * 1000
  190. startFast(t, fastDuration);
  191. }
  192. else {
  193. endFast(t)
  194. }
  195. }
  196. else {
  197. if (value == 'WAIT_FOR_START' || value == 'ONGOING1') {
  198. // const duration = sleepValues[0] * 3600 * 1000 + sleepValues[1] * 60 * 1000
  199. startSleep(t, sleepDuration);
  200. }
  201. else {
  202. endSleep(t)
  203. }
  204. }
  205. }
  206. function mixedBtns() {
  207. return <View>
  208. {
  209. (value == 'WAIT_FOR_START' || value == 'DONE') &&
  210. <Text style={{ color: '#AAFF00' }} onClick={showPicker}>Start Fast</Text>
  211. }
  212. {
  213. (value == 'ONGOING'/* ||value == 'ONGOING1' || value == 'ONGOING2'*/ || value == 'ONGOING3') &&
  214. <Text style={{ color: '#AAFF00' }} onClick={showPicker}>End Fast</Text>
  215. }
  216. {
  217. value == 'ONGOING1' && <Text style={{ color: '#00ffff' }} onClick={showPicker}>Start Sleep</Text>
  218. }
  219. {
  220. value == 'ONGOING2' && <Text style={{ color: '#00ffff' }} onClick={showPicker}>End Sleep</Text>
  221. }
  222. </View>
  223. }
  224. function fastBtns() {
  225. return <View>
  226. <Text style={{ color: '#AAFF00' }} onClick={showPicker}>{value == 'ONGOING' ? 'End Fast' : 'Start Fast'}</Text>
  227. </View>
  228. }
  229. function sleepBtns() {
  230. return <View>
  231. {
  232. value == 'ONGOING' ? <Text style={{ color: '#00ffff' }} onClick={showPicker}>End Sleep</Text> :
  233. <Text style={{ color: '#00ffff' }} onClick={showPicker}>Start Sleep</Text>
  234. }
  235. </View>
  236. }
  237. const handlePickerChange = (e: string) => {
  238. var [hour, minute] = e.split(':').map(x => parseInt(x))
  239. isFast ? setFastValues([hour, minute]) : setSleepValues([hour, minute]);
  240. };
  241. function durationChange(e) {
  242. var count = (e[0] + common.duration.min) * 60 + e[1] * common.duration.step
  243. isFast ? setFastDuration(count * 60000) : setSleepDuration(count * 60000);
  244. isFast ? setFastPickerValue(e) : setSleepPickerValue(e)
  245. setIsOpenDurationPicker(false)
  246. }
  247. function login() {
  248. Taro.navigateTo({
  249. url: '/pages/ChooseAuth'
  250. })
  251. }
  252. function durationFormate() {
  253. if (isFast) {
  254. var t = fastDuration / 60000
  255. var hour = Math.floor(t / 60)
  256. var minute = Math.floor(t % 60)
  257. return `${hour > 0 ? hour + '小时' : ''}${minute > 0 ? minute + '分钟' : ''}`
  258. }
  259. else {
  260. var t = sleepDuration / 60000
  261. var hour = Math.floor(t / 60)
  262. var minute = Math.floor(t % 60)
  263. return `${hour > 0 ? hour + '小时' : ''}${minute > 0 ? minute + '分钟' : ''}`
  264. }
  265. }
  266. function showDurationPicker() {
  267. setIsOpenDurationPicker(true)
  268. }
  269. if (!user.isLogin) {
  270. return <View style={{ display: 'flex', flexDirection: 'column', width: '100%', alignItems: 'center' }}>
  271. <Text>16:00</Text>
  272. {/* <PickerViews onChange={() => { }} items={[pickerDurations()]} value={[12 * 15]} /> */}
  273. {/* <TimePickers time={isFast ? fastStr : sleepStr} content="" change={handlePickerChange} isPickerView={true} /> */}
  274. <Text style={{ color: '#AAFF00' }} onClick={login}>Start Fast</Text>
  275. </View>
  276. }
  277. function durationPickerContent() {
  278. return <View style={{ color: '#fff', backgroundColor: 'transparent' }}>
  279. <PickerViews ref={durationPickerRef} onChange={durationChange} items={durationDatas(common)} value={isFast ? fastPickerValue : sleepPickerValue} showBtns={true} onCancel={() => { setIsOpenDurationPicker(false) }} />
  280. </View>
  281. }
  282. function minus() {
  283. if (isFast) {
  284. var count = fastDuration - 5 * 60 * 1000
  285. setFastDuration(count)
  286. var hour = count / 60000 / 60
  287. var minute = count / 60000 % 60
  288. setFastPickerValue(durationIndex('00:00', `${hour > 10 ? hour : '0' + hour}:${minute > 10 ? minute : '0' + minute}`, common))
  289. }
  290. else {
  291. var count = sleepDuration - 5 * 60 * 1000
  292. setSleepDuration(count)
  293. var hour = count / 60000 / 60
  294. var minute = count / 60000 % 60
  295. setSleepPickerValue(durationIndex('00:00', `${hour > 10 ? hour : '0' + hour}:${minute > 10 ? minute : '0' + minute}`, common))
  296. }
  297. }
  298. function plus() {
  299. if (isFast) {
  300. var count = fastDuration + 5 * 60 * 1000
  301. setFastDuration(count)
  302. var hour = count / 60000 / 60
  303. var minute = count / 60000 % 60
  304. setFastPickerValue(durationIndex('00:00', `${hour > 10 ? hour : '0' + hour}:${minute > 10 ? minute : '0' + minute}`, common))
  305. }
  306. else {
  307. var count = sleepDuration + 5 * 60 * 1000
  308. setSleepDuration(count)
  309. var hour = count / 60000 / 60
  310. var minute = count / 60000 % 60
  311. setSleepPickerValue(durationIndex('00:00', `${hour > 10 ? hour : '0' + hour}:${minute > 10 ? minute : '0' + minute}`, common))
  312. }
  313. }
  314. function disableMinus() {
  315. if (isFast) {
  316. if (fastDuration <= 60 * 60 * 1000) {
  317. return true;
  318. }
  319. }
  320. else {
  321. if (sleepDuration <= 60 * 60 * 1000) {
  322. return true;
  323. }
  324. }
  325. return false;
  326. }
  327. function disablePlus() {
  328. if (isFast) {
  329. if (fastDuration >= 23 * 60 * 60 * 1000) {
  330. return true;
  331. }
  332. }
  333. else {
  334. if (sleepDuration >= 23 * 60 * 60 * 1000) {
  335. return true;
  336. }
  337. }
  338. return false;
  339. }
  340. return (
  341. <View style={{ display: 'flex', flexDirection: 'column', width: '100%', alignItems: 'center' }}>
  342. {
  343. machine.context.checkData && (machine.context.checkData as any).current_record.last_real_check_time &&
  344. <Text style={{ width: '100%', textAlign: 'center', color: 'red' }}>
  345. {TimeFormatter.formateTime((machine.context.checkData as any).current_record.last_real_check_time)}
  346. </Text>
  347. }
  348. {
  349. value == 'ONGOING' && <View>
  350. <Text>{(machine.context.checkData as any).current_record.scenario == 'FAST' ?
  351. TimeFormatter.countdown((machine.context.checkData as any).current_record.fast.target_end_time) :
  352. TimeFormatter.countdown((machine.context.checkData as any).current_record.sleep.target_end_time)}</Text>
  353. </View>
  354. }
  355. {
  356. (value == 'ONGOING1' || value == 'WAIT_FOR_START') && <Stepper style={{ marginBottom: 10 }} child={
  357. <Text style={{ width: 150, textAlign: 'center' }} onClick={showDurationPicker}>{durationFormate()}</Text>
  358. } minus={minus} plus={plus} disableMinus={disableMinus()} disablePlus={disablePlus()} />
  359. // <Text style={{marginBottom:10}} onClick={showDurationPicker}>时长:{durationFormate()}</Text>
  360. }
  361. {
  362. machine.context.checkData && value == 'ONGOING2' && <View>
  363. <Text>{TimeFormatter.countdown((machine.context.checkData as any).current_record.sleep.target_end_time)}</Text>
  364. </View>
  365. }
  366. {
  367. machine.context.checkData && value == 'ONGOING3' && <View>
  368. <Text>{TimeFormatter.countdown((machine.context.checkData as any).current_record.fast.target_end_time)}</Text>
  369. </View>
  370. }
  371. <View>
  372. {
  373. key === 'FAST_SLEEP' && mixedBtns()
  374. }
  375. {
  376. key === 'FAST' && fastBtns()
  377. }
  378. {
  379. key === 'SLEEP' && sleepBtns()
  380. }
  381. </View>
  382. {
  383. showModal && isOpen && <Modal testInfo={testLayout()} children={layoutContent()} dismiss={() => setIsOpen(false)} confirm={() => {
  384. var picker = limitPickerRef.current;
  385. pickerConfirm((picker as any).getConfirmData());
  386. setIsOpen(false);
  387. }} />
  388. }
  389. {
  390. isOpenDurationPicker && <Modal children={durationPickerContent()} dismiss={() => setIsOpenDurationPicker(false)} confirm={() => {
  391. var picker = durationPickerRef.current;
  392. durationChange((picker as any).getConfirmData());
  393. setIsOpenDurationPicker(false);
  394. }} />
  395. }
  396. {/* {
  397. showModal && <AtFloatLayout
  398. isOpened={isOpen}
  399. onClose={() => {
  400. setIsOpen(false)
  401. setShowModal(false)
  402. }}
  403. title="">
  404. {
  405. layoutContent()
  406. }
  407. </AtFloatLayout>
  408. }
  409. <AtFloatLayout
  410. isOpened={isOpenDurationPicker}
  411. onClose={() => {
  412. setIsOpenDurationPicker(false)
  413. }}
  414. title="">
  415. {
  416. durationPickerContent()
  417. }
  418. </AtFloatLayout> */}
  419. </View>
  420. )
  421. }