Console.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import { recordCheck } from "@/services/trackTimeDuration";
  2. import { View, Text, PickerView } from "@tarojs/components";
  3. import trackTimeService, { machine } from "@/store/trackTimeMachine"
  4. import { useEffect, 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 { AtFloatLayout } from "taro-ui";
  10. import LimitPickers from '@/components/LimitPickers';
  11. import { endFast, endSleep, startFast, startSleep } from "../actions/TrackTimeActions";
  12. import { durationDatas, durationIndex, pickerDurations } from "../hooks/Console";
  13. import PickerViews from "@/components/PickerViews";
  14. export default function Component() {
  15. const [key, setKey] = useState('');
  16. const [value, setValue] = useState('');
  17. const user = useSelector((state: any) => state.user);
  18. const common = useSelector((state: any) => state.common);
  19. const [isFast, setIsFast] = useState(true);
  20. const [fastValues, setFastValues] = useState<number[]>([0, 0]);
  21. const [sleepValues, setSleepValues] = useState<number[]>([0, 0]);
  22. const [fastDuration, setFastDuration] = useState<number>(0);
  23. const [sleepDuration, setSleepDuration] = useState<number>(0);
  24. const [fastStr, setFastStr] = useState('00:00');
  25. const [sleepStr, setSleepStr] = useState('00:00');
  26. const [isOpen, setIsOpen] = useState(false);
  27. const [showModal, setShowModal] = useState(false);
  28. const [fastPickerValue, setFastPickerValue] = useState([0,0])
  29. const [sleepPickerValue, setSleepPickerValue] = useState([0,0])
  30. const [isOpenDurationPicker, setIsOpenDurationPicker] = useState(false)
  31. // const [pickerValue, setPickerValue] = useState([0,0])
  32. // const pickerDurations = pickerDurations();
  33. // console.log(pickerDurations())
  34. useEffect(() => {
  35. getStateDetail();
  36. }, [machine.context.checkData]);
  37. useEffect(() => {
  38. getStateDetail();
  39. }, [machine.context.currentStatus])
  40. useEffect(() => {
  41. trackTimeService.onTransition(state => {
  42. if ((state.value as any).FAST_SLEEP) {
  43. setKey('FAST_SLEEP');
  44. setValue((state.value as any).FAST_SLEEP);
  45. }
  46. if ((state.value as any).FAST) {
  47. setKey('FAST');
  48. setValue((state.value as any).FAST);
  49. }
  50. if ((state.value as any).SLEEP) {
  51. setKey('SLEEP');
  52. setValue((state.value as any).SLEEP);
  53. }
  54. });
  55. }, []);
  56. function getStateDetail() {
  57. var state = trackTimeService.getSnapshot().value
  58. if ((state as any).FAST_SLEEP) {
  59. setKey('FAST_SLEEP');
  60. setValue((state as any).FAST_SLEEP);
  61. if ((state as any).FAST_SLEEP == 'WAIT_FOR_START' || (state as any).FAST_SLEEP == 'ONGOING3') {
  62. setIsFast(true);
  63. }
  64. else if ((state as any).FAST_SLEEP == 'ONGOING1') {
  65. setIsFast(false);
  66. }
  67. }
  68. if ((state as any).FAST) {
  69. setKey('FAST');
  70. setValue((state as any).FAST);
  71. setIsFast(true);
  72. }
  73. if ((state as any).SLEEP) {
  74. setKey('SLEEP');
  75. setValue((state as any).SLEEP);
  76. setIsFast(false);
  77. }
  78. var checkData = machine.context.checkData;
  79. if (checkData) {
  80. var current_record = (checkData as any).current_record;
  81. if (current_record.fast) {
  82. var fastTime = TimeFormatter.formateHourMinute(current_record.fast.target_start_time,
  83. current_record.fast.target_end_time);
  84. setFastValues(fastTime.split(':').map(x => parseInt(x)));
  85. setFastStr(fastTime);
  86. var fastCount = current_record.fast.target_end_time - current_record.fast.target_start_time
  87. setFastDuration(fastCount)
  88. setFastPickerValue(durationIndex(current_record.fast.target_start_time, current_record.fast.target_end_time, common))
  89. // setFastPickerValue([fastCount / 60000 / 5 - 12])
  90. }
  91. if (current_record.sleep) {
  92. var sleepTime = TimeFormatter.formateHourMinute(current_record.sleep.target_start_time,
  93. current_record.sleep.target_end_time);
  94. setSleepValues(sleepTime.split(':').map(x => parseInt(x)));
  95. setSleepStr(sleepTime);
  96. var sleepCount = current_record.sleep.target_end_time - current_record.sleep.target_start_time
  97. setSleepDuration(sleepCount)
  98. setSleepPickerValue(durationIndex(current_record.sleep.target_start_time, current_record.sleep.target_end_time, common))
  99. // setSleepPickerValue([sleepCount / 60000 / 5 - 12])
  100. }
  101. }
  102. }
  103. function showPicker() {
  104. setShowModal(true)
  105. setIsOpen(true)
  106. global.set_time = new Date().getTime()
  107. }
  108. function hidePicker() {
  109. setIsOpen(false)
  110. setTimeout(() => {
  111. setShowModal(false)
  112. }, 1000)
  113. }
  114. function layoutContent() {
  115. var limit = new Date().getTime() - 7 * 3600 * 1000 * 24;
  116. var current_record = machine.context.checkData ? (machine.context.checkData as any).current_record : null;
  117. if (current_record && current_record.last_real_check_time)
  118. limit = current_record.last_real_check_time
  119. return <View>
  120. <LimitPickers limit={limit} onCancel={hidePicker} onChange={(e) => {
  121. console.log(new Date(e))
  122. pickerConfirm(e)
  123. hidePicker()
  124. }} />
  125. </View>
  126. }
  127. function pickerConfirm(t: number) {
  128. if (isFast) {
  129. if (value == 'WAIT_FOR_START') {
  130. // const duration = fastValues[0] * 3600 * 1000 + fastValues[1] * 60 * 1000
  131. startFast(t, fastDuration);
  132. }
  133. else {
  134. endFast(t)
  135. }
  136. }
  137. else {
  138. if (value == 'WAIT_FOR_START' || value == 'ONGOING1') {
  139. // const duration = sleepValues[0] * 3600 * 1000 + sleepValues[1] * 60 * 1000
  140. startSleep(t, sleepDuration);
  141. }
  142. else {
  143. endSleep(t)
  144. }
  145. }
  146. }
  147. function mixedBtns() {
  148. return <View>
  149. {
  150. (value == 'WAIT_FOR_START' || value == 'DONE') &&
  151. <Text style={{ color: '#AAFF00' }} onClick={showPicker}>Start Fast</Text>
  152. }
  153. {
  154. (value == 'ONGOING'/* ||value == 'ONGOING1' || value == 'ONGOING2'*/ || value == 'ONGOING3') &&
  155. <Text style={{ color: '#AAFF00' }} onClick={showPicker}>End Fast</Text>
  156. }
  157. {
  158. value == 'ONGOING1' && <Text style={{ color: '#00ffff' }} onClick={showPicker}>Start Sleep</Text>
  159. }
  160. {
  161. value == 'ONGOING2' && <Text style={{ color: '#00ffff' }} onClick={showPicker}>End Sleep</Text>
  162. }
  163. </View>
  164. }
  165. function fastBtns() {
  166. return <View>
  167. <Text style={{ color: '#AAFF00' }} onClick={showPicker}>{value == 'ONGOING' ? 'End Fast' : 'Start Fast'}</Text>
  168. </View>
  169. }
  170. function sleepBtns() {
  171. return <View>
  172. {
  173. value == 'ONGOING' ? <Text style={{ color: '#00ffff' }} onClick={showPicker}>End Sleep</Text> :
  174. <Text style={{ color: '#00ffff' }} onClick={showPicker}>Start Sleep</Text>
  175. }
  176. </View>
  177. }
  178. const handlePickerChange = (e: string) => {
  179. var [hour, minute] = e.split(':').map(x => parseInt(x))
  180. isFast ? setFastValues([hour, minute]) : setSleepValues([hour, minute]);
  181. };
  182. function durationChange(e) {
  183. var count = (e[0]+common.duration.min)*60+e[1]*common.duration.step
  184. isFast ? setFastDuration(count * 60000) : setSleepDuration(count * 60000);
  185. isFast?setFastPickerValue(e):setSleepPickerValue(e)
  186. setIsOpenDurationPicker(false)
  187. }
  188. function login() {
  189. Taro.navigateTo({
  190. url: '/pages/ChooseAuth'
  191. })
  192. }
  193. function durationFormate() {
  194. if (isFast) {
  195. var t = fastDuration / 60000
  196. var hour = Math.floor(t / 60)
  197. var minute = Math.floor(t % 60)
  198. return `${hour > 0 ? hour + '小时' : ''}${minute > 0 ? minute + '分钟' : ''}`
  199. }
  200. else {
  201. var t = sleepDuration / 60000
  202. var hour = Math.floor(t / 60)
  203. var minute = Math.floor(t % 60)
  204. return `${hour > 0 ? hour + '小时' : ''}${minute > 0 ? minute + '分钟' : ''}`
  205. }
  206. }
  207. function showDurationPicker() {
  208. setIsOpenDurationPicker(true)
  209. }
  210. if (!user.isLogin) {
  211. return <View style={{ display: 'flex', flexDirection: 'column', width: '100%', alignItems: 'center' }}>
  212. <Text>16:00</Text>
  213. {/* <PickerViews onChange={() => { }} items={[pickerDurations()]} value={[12 * 15]} /> */}
  214. {/* <TimePickers time={isFast ? fastStr : sleepStr} content="" change={handlePickerChange} isPickerView={true} /> */}
  215. <Text style={{ color: '#AAFF00' }} onClick={login}>Start Fast</Text>
  216. </View>
  217. }
  218. function durationPickerContent() {
  219. return <View style={{ color: '#000' }}>
  220. <PickerViews onChange={durationChange} items={durationDatas(common)} value={isFast?fastPickerValue:sleepPickerValue} height={200} showBtns={true} onCancel={() => { setIsOpenDurationPicker(false) }} />
  221. </View>
  222. }
  223. return (
  224. <View style={{ display: 'flex', flexDirection: 'column', width: '100%', alignItems: 'center' }}>
  225. {
  226. machine.context.checkData && (machine.context.checkData as any).current_record.last_real_check_time &&
  227. <Text style={{ width: '100%', textAlign: 'center', color: 'red' }}>
  228. {TimeFormatter.formateTime((machine.context.checkData as any).current_record.last_real_check_time)}
  229. </Text>
  230. }
  231. {
  232. value == 'ONGOING' && <View>
  233. <Text>{(machine.context.checkData as any).current_record.scenario == 'FAST' ?
  234. TimeFormatter.countdown((machine.context.checkData as any).current_record.fast.target_end_time) :
  235. TimeFormatter.countdown((machine.context.checkData as any).current_record.sleep.target_end_time)}</Text>
  236. </View>
  237. }
  238. {
  239. (value == 'ONGOING1' || value == 'WAIT_FOR_START') &&
  240. <Text style={{marginBottom:10}} onClick={showDurationPicker}>时长:{durationFormate()}</Text>
  241. // <PickerViews onChange={durationChange} items={[pickerDurations()]} value={isFast ? [fastPickerValue] : [sleepPickerValue]} />
  242. // <TimePickers time={isFast ? fastStr : sleepStr} content="" change={handlePickerChange} isPickerView={true} />
  243. }
  244. {
  245. machine.context.checkData && value == 'ONGOING2' && <View>
  246. <Text>{TimeFormatter.countdown((machine.context.checkData as any).current_record.sleep.target_end_time)}</Text>
  247. </View>
  248. }
  249. {
  250. machine.context.checkData && value == 'ONGOING3' && <View>
  251. <Text>{TimeFormatter.countdown((machine.context.checkData as any).current_record.fast.target_end_time)}</Text>
  252. </View>
  253. }
  254. <View>
  255. {
  256. key === 'FAST_SLEEP' && mixedBtns()
  257. }
  258. {
  259. key === 'FAST' && fastBtns()
  260. }
  261. {
  262. key === 'SLEEP' && sleepBtns()
  263. }
  264. </View>
  265. {
  266. showModal && <AtFloatLayout
  267. isOpened={isOpen}
  268. onClose={() => {
  269. setIsOpen(false)
  270. setShowModal(false)
  271. }}
  272. title="">
  273. {
  274. layoutContent()
  275. }
  276. </AtFloatLayout>
  277. }
  278. <AtFloatLayout
  279. isOpened={isOpenDurationPicker}
  280. onClose={() => {
  281. setIsOpenDurationPicker(false)
  282. }}
  283. title="">
  284. {
  285. durationPickerContent()
  286. }
  287. </AtFloatLayout>
  288. </View>
  289. )
  290. }