Suggest.tsx 17 KB

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