Suggest.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. import { ColorType } from "@/context/themes/color";
  2. import { ChooseScenarioBtn } from "@/features/common/SpecBtns";
  3. import { jumpPage } from "@/features/trackTimeDuration/hooks/Common";
  4. import { View, Text, Picker, ScrollView } from "@tarojs/components";
  5. import { useDidShow, useRouter } from "@tarojs/taro";
  6. import { useEffect, useState } from "react";
  7. import './ChooseScenario.scss';
  8. import PickerViews from "@/components/input/PickerViews";
  9. import { useDispatch, useSelector } from "react-redux";
  10. import { rpxToPx } from "@/utils/tools";
  11. import { updateFast, updateSleep, updateStep } from "@/store/set_target";
  12. import { TimeFormatter } from "@/utils/time_format";
  13. import Footer from "@/components/layout/Footer";
  14. import { IconCheck } from "@/components/basic/Icons";
  15. export default function Suggest() {
  16. let router: any = null
  17. var navigation: any = null
  18. if (process.env.TARO_ENV === 'rn') {
  19. var useNavigation = require("@react-navigation/native").useNavigation
  20. navigation = useNavigation()
  21. const useRoute = require("@react-navigation/native").useRoute
  22. router = useRoute()
  23. }
  24. else {
  25. router = useRouter()
  26. }
  27. const common = useSelector((state: any) => state.common);
  28. const target = useSelector((state: any) => state.target);
  29. const [pageStep] = useState(target.step);
  30. const dispatch = useDispatch();
  31. const [suggestIndex, setSuggestIndex] = useState(-1)
  32. const [suggestItems, setSuggestItem] = useState<any>([])
  33. const [scrollTop, setScrollTop] = useState(0)
  34. const [fastRecommendIndex, setFastRecommendIndex] = useState(-1)
  35. const [fastLastIndex, setFastLastIndex] = useState(-1)
  36. useEffect(() => {
  37. if (pageStep == 1) {
  38. var obj = target.fast.schedule.fast
  39. var array: any = []
  40. var currentIndex = -1;
  41. var recommendIndex = -1;
  42. var lastIndex = -1;
  43. for (var i = 0; i < obj.duration.options.length; i++) {
  44. if (obj.duration.options[i] == obj.duration.last_value) {
  45. lastIndex = i
  46. }
  47. else if (obj.duration.options[i] == obj.duration.default_value) {
  48. recommendIndex = i
  49. }
  50. var fast = JSON.parse(JSON.stringify(target.fast))
  51. var startCount = fast.schedule.fast.end_time.split(':')[0] * 60 + fast.schedule.fast.end_time.split(':')[1] * 1 - obj.duration.options[i] * 60
  52. if (startCount < 0) {
  53. startCount += 24 * 60
  54. }
  55. var start = TimeFormatter.padZero(Math.floor(startCount / 60)) + ':' + TimeFormatter.padZero(startCount % 60)
  56. array.push({
  57. hours: obj.duration.options[i],
  58. left: 24 - obj.duration.options[i],
  59. // minutes: obj.duration.minutes[i],
  60. // number: obj.duration.numbers[i],
  61. time: start
  62. })
  63. if (obj.duration.options[i] == obj.duration.init_value) {
  64. currentIndex = i
  65. }
  66. }
  67. // 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)
  68. // if (duration <= 0) {
  69. // duration += 24 * 60
  70. // }
  71. // var durationHours = Math.floor(duration / 60)
  72. // var durationMinutes = duration % 60
  73. // var currentIndex = -1;
  74. // var array: any = []
  75. // for (var i = 12; i <= 23; i++) {
  76. // if (i != 23 || durationMinutes == 0) {
  77. // array.push({
  78. // hours: i,
  79. // minutes: durationMinutes
  80. // });
  81. // }
  82. // }
  83. // for (var i = 0; i < array.length; i++) {
  84. // if (array[i].hours == durationHours && array[i].minutes == durationMinutes) {
  85. // currentIndex = i
  86. // break
  87. // }
  88. // }
  89. setFastRecommendIndex(recommendIndex)
  90. setFastLastIndex(lastIndex)
  91. setSuggestItem(array)
  92. setSuggestIndex(currentIndex)
  93. setScrollTop(currentIndex * rpxToPx(160))
  94. }
  95. else if (pageStep == 3) {
  96. var obj = target.sleep.schedule.sleep
  97. var sleep_cycle = obj.cycle
  98. var sleep_latency = obj.latency.init_value
  99. var array: any = []
  100. var index: number = 0;
  101. for (var i = 0; i < sleep_cycle.num.options.length; i++) {
  102. var count = sleep_cycle.num.options[i] * sleep_cycle.duration.init_value + sleep_latency
  103. //sleep.schedule.sleep.cycle.num.init_value = sleep.schedule.sleep.cycle.num.options[suggestIndex]
  104. if (sleep_cycle.num.options[i] == sleep_cycle.num.init_value) {
  105. index = i
  106. }
  107. var sleep = JSON.parse(JSON.stringify(target.sleep))
  108. var startCount = sleep.schedule.sleep.end_time.split(':')[0] * 60 + sleep.schedule.sleep.end_time.split(':')[1] * 1 - count
  109. if (startCount < 0) {
  110. startCount += 24 * 60
  111. }
  112. var start = TimeFormatter.padZero(Math.floor(startCount / 60)) + ':' + TimeFormatter.padZero(startCount % 60)
  113. array.push({
  114. hours: Math.floor(count / 60),
  115. minutes: count % 60,
  116. number: sleep_cycle.num.options[i],
  117. time: start
  118. })
  119. }
  120. setSuggestItem(array)
  121. setSuggestIndex(index)
  122. }
  123. }, [])
  124. useDidShow(() => {
  125. dispatch(updateStep({ step: pageStep }))
  126. })
  127. function next() {
  128. if (pageStep == 1) {
  129. var item = suggestItems[suggestIndex]
  130. var fast = JSON.parse(JSON.stringify(target.fast))
  131. // var startCount = fast.schedule.fast.end_time.split(':')[0] * 60 + fast.schedule.fast.end_time.split(':')[1] * 1 - (item.hours * 60 + item.minutes)
  132. // if (startCount < 0) {
  133. // startCount += 24 * 60
  134. // }
  135. // var start = TimeFormatter.padZero(Math.floor(startCount / 60)) + ':' + TimeFormatter.padZero(startCount % 60)
  136. fast.schedule.fast.start_time = item.time
  137. fast.schedule.fast.duration.init_value = fast.schedule.fast.duration.options[suggestIndex]
  138. dispatch(updateFast({ fast: fast }))
  139. }
  140. else if (pageStep == 3) {
  141. var item = suggestItems[suggestIndex]
  142. var sleep = JSON.parse(JSON.stringify(target.sleep))
  143. var startCount = sleep.schedule.sleep.end_time.split(':')[0] * 60 + sleep.schedule.sleep.end_time.split(':')[1] * 1 - (item.hours * 60 + item.minutes)
  144. if (startCount < 0) {
  145. startCount += 24 * 60
  146. }
  147. var start = TimeFormatter.padZero(Math.floor(startCount / 60)) + ':' + TimeFormatter.padZero(startCount % 60)
  148. sleep.schedule.sleep.start_time = start
  149. sleep.schedule.sleep.cycle.num.init_value = sleep.schedule.sleep.cycle.num.options[suggestIndex]
  150. dispatch(updateSleep({ sleep: sleep }))
  151. }
  152. var step: number = target.step + 1
  153. dispatch(updateStep({ step: step }))
  154. var url = '/pages/clock/Suggest'
  155. var pageName = 'Suggest'
  156. if (!target.isMixed) {
  157. if (step == 2) {
  158. url = '/pages/clock/SetGoal'
  159. pageName = 'SetGoal'
  160. }
  161. }
  162. else {
  163. if (step == 4) {
  164. url = '/pages/clock/SetGoal'
  165. pageName = 'SetGoal'
  166. }
  167. }
  168. jumpPage(url, pageName, navigation, {})
  169. }
  170. function myself() {
  171. jumpPage(`/pages/clock/SetGoal?isSelf=1`, 'SetGoal', navigation, {
  172. isSelf: 1
  173. })
  174. }
  175. function suggestTitle() {
  176. switch (pageStep) {
  177. case 0:
  178. return 'Set End Time of Fasting'
  179. case 1:
  180. return 'Suggested Fasting Schedule'
  181. case 2:
  182. return 'Set Wake Time'
  183. case 3:
  184. return 'Suggested Sleep Schedule'
  185. default:
  186. return ''
  187. }
  188. }
  189. function suggestDesc() {
  190. switch (pageStep) {
  191. case 0:
  192. return 'What time do you want to break your fast and start your breakfast?'
  193. case 1:
  194. return 'To benefit from overnight fasting and enjoy breakfast at ' + target.fast.schedule.fast.end_time + ', start fasting at suggested time.'
  195. case 2:
  196. return 'What time do you want to wake up?'
  197. case 3:
  198. return 'To wake up refreshed at ' + target.sleep.schedule.sleep.end_time + ', go to bed at suggested time.'
  199. default:
  200. return ''
  201. }
  202. }
  203. function durationDatas() {
  204. var min: number = 0
  205. var max: number = 23
  206. var step: number = 5
  207. var minutes: string[] = []
  208. for (let i = 0; i < 60; i += step) {
  209. minutes.push(TimeFormatter.padZero(i))
  210. }
  211. var hours: string[] = []
  212. for (let i = min; i <= max; i++) {
  213. hours.push(TimeFormatter.padZero(i))
  214. }
  215. return [hours, minutes]
  216. }
  217. function durationValues() {
  218. var eatTime = target.fast.schedule.fast.end_time
  219. if (pageStep == 2) {
  220. eatTime = target.sleep.schedule.sleep.end_time
  221. }
  222. var eatHour = eatTime.split(':')[0]
  223. var eatMin = eatTime.split(':')[1]
  224. var list = durationDatas()
  225. var hourIndex = 0
  226. var minIndex = 0
  227. for (let i = 0; i < list[0].length; i++) {
  228. if (list[0][i] == eatHour) {
  229. hourIndex = i
  230. break
  231. }
  232. }
  233. for (let i = 0; i < list[1].length; i++) {
  234. if (list[1][i] == eatMin) {
  235. minIndex = i
  236. break
  237. }
  238. }
  239. return [hourIndex, minIndex]
  240. }
  241. function sleepLatency() {
  242. var obj = target.sleep.schedule.sleep.latency
  243. var min: number = obj.min
  244. var max: number = obj.max
  245. var step: number = obj.step
  246. var minutes: string[] = []
  247. for (let i = min; i <= max; i += step) {
  248. minutes.push(i + '')
  249. }
  250. return [minutes]
  251. }
  252. function sleepLatencyValue() {
  253. var obj = target.sleep.schedule.sleep.latency
  254. var value: number = obj.init_value
  255. var min: number = obj.min
  256. var step: number = obj.step
  257. var index = (value - min) / step
  258. return [index]
  259. }
  260. function timePickerChanged(e: any) {
  261. var obj = target.fast.schedule.fast
  262. if (pageStep == 2) {
  263. obj = target.sleep.schedule.sleep
  264. }
  265. 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)
  266. if (duration <= 0) {
  267. duration += 24 * 60
  268. }
  269. var endCount = duration - (e[0] * 60 + e[1] * 5)
  270. if (endCount < 0) {
  271. endCount += 24 * 60
  272. }
  273. var time = TimeFormatter.padZero(e[0]) + ':' + TimeFormatter.padZero(e[1] * 5)
  274. var start = TimeFormatter.padZero(Math.floor(endCount / 60)) + ':' + TimeFormatter.padZero(endCount % 60)
  275. if (pageStep == 0) {
  276. var fast = JSON.parse(JSON.stringify(target.fast))
  277. fast.schedule.fast.end_time = time
  278. fast.schedule.fast.start_time = start
  279. dispatch(updateFast({ fast: fast }))
  280. }
  281. else {
  282. var sleep = JSON.parse(JSON.stringify(target.sleep))
  283. sleep.schedule.sleep.end_time = time
  284. sleep.schedule.sleep.start_time = start
  285. dispatch(updateSleep({ sleep: sleep }))
  286. }
  287. }
  288. function timePicker() {
  289. return <View>
  290. <View style={{ marginBottom: rpxToPx(40), marginLeft: rpxToPx(46), marginRight: rpxToPx(46), borderRadius: rpxToPx(20), overflow: 'hidden' }}>
  291. <PickerViews ref={null}
  292. onChange={timePickerChanged}
  293. items={durationDatas()}
  294. value={durationValues()} height={200}
  295. title=''
  296. themeColor={target.isMixed ? global.fastColor ? global.fastColor : ColorType.fast : global.sleepColor ? global.sleepColor : ColorType.sleep}
  297. showBtns={false}
  298. hideTitle={true}
  299. onCancel={() => { }} />
  300. </View>
  301. {
  302. pageStep == 1 && <Text className="suggest_footer_note">If you plan to skip breakfast, pick a time for your first meal of the day.</Text>
  303. }
  304. </View>
  305. }
  306. function fallSleepChanged(e: any) {
  307. var value = target.sleep.schedule.sleep.latency
  308. var time = e[0] * value.step + value.min
  309. var sleep = JSON.parse(JSON.stringify(target.sleep))
  310. sleep.schedule.sleep.latency.init_value = time
  311. dispatch(updateSleep({ sleep: sleep }))
  312. }
  313. function fallSleepPicker() {
  314. return <View>
  315. <View className="target_desc">How long does it take you to fall asleep?</View>
  316. <View style={{ marginLeft: rpxToPx(46), marginRight: rpxToPx(46), borderRadius: rpxToPx(20), overflow: 'hidden' }}>
  317. <PickerViews ref={null}
  318. onChange={fallSleepChanged}
  319. items={sleepLatency()}
  320. value={sleepLatencyValue()} height={200}
  321. title=''
  322. themeColor={target.isMixed ? global.fastColor ? global.fastColor : ColorType.fast : global.sleepColor ? global.sleepColor : ColorType.sleep}
  323. showBtns={false}
  324. hideTitle={true}
  325. onCancel={() => { }} />
  326. </View>
  327. <Text className="suggest_footer_note">It takes 10 to 30 minutes for most people to fall asleep once they're in bed.</Text>
  328. </View>
  329. }
  330. function suggestList() {
  331. if (pageStep == 1) {
  332. return <ScrollView scrollY style={{ height: 400 }} scrollTop={scrollTop}>
  333. <View style={{ flexDirection: 'column', display: 'flex' }}>
  334. {
  335. suggestItems.map((item: any, index: number) => {
  336. return <View onClick={() => {
  337. setSuggestIndex(index)
  338. }} key={index} className={index == suggestIndex ? 'item_sel1' : 'item1'}>
  339. <View style={{ display: 'flex', flexDirection: 'row', alignItems: 'center' }}>
  340. <View className="suggest_item_title">{item.hours} hours</View>
  341. <View style={{ flex: 1 }} />
  342. {
  343. index == fastRecommendIndex &&
  344. <View style={{color:'#fff'}}>Recommended</View>
  345. }
  346. {
  347. index == fastLastIndex &&
  348. <View style={{color:'#fff',marginLeft:5}}>Last</View>
  349. }
  350. </View>
  351. <View className="suggest_item_desc">{item.left} hrs eating</View>
  352. {
  353. index == suggestIndex &&
  354. <View className="suggest_choose">
  355. <View style={{ color: ColorType.fast }}>Start fasting at {item.time}</View>
  356. <IconCheck color={ColorType.fast} width={38} height={26} />
  357. </View>
  358. }
  359. </View>
  360. })
  361. }
  362. </View>
  363. </ScrollView>
  364. }
  365. return <View style={{ flex: 1, overflow: 'scroll' }}>
  366. {
  367. suggestItems.map((item: any, index: number) => {
  368. return <View onClick={() => {
  369. setSuggestIndex(index)
  370. }} key={index} className={index == suggestIndex ? 'item_sel2' : 'item1'}>
  371. <View className="suggest_item_title">{item.hours} hrs {item.minutes} mins</View>
  372. <View className="suggest_item_desc">{item.number} sleep cycles</View>
  373. {
  374. index == suggestIndex && <View className="suggest_choose">
  375. <View style={{ color: ColorType.sleep }}>Go to bed at {item.time}</View>
  376. <IconCheck color={ColorType.sleep} width={38} height={26} />
  377. </View>
  378. }
  379. </View>
  380. })
  381. }
  382. </View>
  383. }
  384. function content() {
  385. return <View style={{ flex: 1, display: 'flex', flexDirection: 'column' }}>
  386. {
  387. (pageStep == 0 || pageStep == 2) && timePicker()
  388. }
  389. {
  390. (pageStep == 1 || pageStep == 3) && suggestList()
  391. }
  392. {
  393. pageStep == 2 && fallSleepPicker()
  394. }
  395. </View>
  396. }
  397. return <View style={{ display: 'flex', flexDirection: 'column' }}>
  398. <Text className="target_title">{suggestTitle()}</Text>
  399. <Text className="target_desc">{suggestDesc()}</Text>
  400. {
  401. content()
  402. }
  403. <Footer>
  404. <View style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center' }}>
  405. <ChooseScenarioBtn
  406. onClick={next}
  407. title={pageStep == 0 || pageStep == 2 ? 'Suggest Plan' : 'Next'}
  408. background={pageStep < 2 ? ColorType.fast : ColorType.sleep}
  409. />
  410. <Text className="suggest_footer_btn" onClick={myself}>Set Schedule Myself</Text>
  411. </View>
  412. </Footer>
  413. </View>
  414. }