More.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { View, Text } from "@tarojs/components";
  2. import Taro from "@tarojs/taro";
  3. import { forwardRef, useEffect, useRef, useState } from "react";
  4. import { useTranslation } from "react-i18next";
  5. import { useDispatch, useSelector } from "react-redux";
  6. import trackTimeService, { machine } from "@/store/trackTimeMachine"
  7. import { recordCheck } from "@/services/trackTimeDuration";
  8. import LimitPickers from "@/components/input/LimitPickers";
  9. import { endFast } from "../actions/TrackTimeActions";
  10. import Modal from "@/components/layout/Modal";
  11. import { setStep } from "@/store/scenario";
  12. import { TimeFormatter } from "@/utils/time_format";
  13. import { ColorType } from "@/context/themes/color";
  14. const Component = forwardRef((props, ref) => {
  15. const isFastFirst = true;
  16. const { t } = useTranslation()
  17. const scenario = useSelector((state: any) => state.scenario);
  18. const user = useSelector((state: any) => state.user);
  19. const [isOpen, setIsOpen] = useState(false);
  20. const [showModal, setShowModal] = useState(false);
  21. const limitPickerRef = useRef<any>(null);
  22. const dispatch = useDispatch();
  23. useEffect(() => {
  24. var value = trackTimeService.getSnapshot().value
  25. }, [machine.context.currentStatus])
  26. useEffect(() => {
  27. trackTimeService.onTransition(state => {
  28. var value = trackTimeService.getSnapshot().value
  29. });
  30. }, []);
  31. function login() {
  32. Taro.navigateTo({
  33. url: '/pages/account/ChooseAuth'
  34. })
  35. }
  36. function waitActionSheet() {
  37. Taro.showActionSheet({
  38. itemList: [t('feature.track_time_duration.action_sheet.change_schedule'), t('feature.track_time_duration.action_sheet.switch_scenario')]
  39. })
  40. .then(res => {
  41. switch (res.tapIndex) {
  42. case 0:
  43. if (scenario.name == 'FAST_SLEEP') {
  44. dispatch(setStep(isFastFirst ? 'fast' : 'sleep'))
  45. }
  46. else if (scenario.name == 'SLEEP') {
  47. dispatch(setStep('sleep'))
  48. }
  49. else {
  50. dispatch(setStep('fast'))
  51. }
  52. Taro.navigateTo({
  53. url: '/pages/clock/SetSchedule'
  54. })
  55. break;
  56. case 1:
  57. Taro.navigateTo({
  58. url: '/pages/clock/ChooseScenario'
  59. })
  60. break;
  61. }
  62. })
  63. .catch(err => {
  64. console.log(err.errMsg)
  65. })
  66. }
  67. function endFastActionSheet() {
  68. Taro.showActionSheet({
  69. itemList: [t('feature.track_time_duration.action_sheet.end_fast')]
  70. })
  71. .then(res => {
  72. console.log(res.tapIndex)
  73. switch (res.tapIndex) {
  74. case 0:
  75. {
  76. setShowModal(true)
  77. setIsOpen(true)
  78. global.set_time = new Date().getTime()
  79. }
  80. break;
  81. }
  82. })
  83. .catch(err => {
  84. console.log(err.errMsg)
  85. })
  86. }
  87. function checkActionSheetData() {
  88. console.log('checkActionSheetData')
  89. if (!user.isLogin) {
  90. return login()
  91. }
  92. var state = trackTimeService.getSnapshot().value
  93. if ((state as any).FAST_SLEEP === 'WAIT_FOR_START' ||
  94. (state as any).FAST === 'WAIT_FOR_START' ||
  95. (state as any).SLEEP === 'WAIT_FOR_START') {
  96. waitActionSheet()
  97. }
  98. else if ((state as any).FAST_SLEEP === 'ONGOING1' ||
  99. (state as any).FAST_SLEEP === 'ONGOING2') {
  100. endFastActionSheet()
  101. }
  102. else if ((state as any).FAST_SLEEP === 'ONGOING3') {
  103. // Taro.showToast({
  104. // title: '暂无更多操作',
  105. // icon: 'none',
  106. // duration: 2000
  107. // })
  108. }
  109. }
  110. global.checkActionSheetData = checkActionSheetData
  111. function layoutContent() {
  112. var limit = global.set_time - 7 * 3600 * 1000 * 24;
  113. var current_record = machine.context.checkData ? (machine.context.checkData as any).current_record : null;
  114. global.limit = limit
  115. if (current_record && current_record.last_real_check_time) {
  116. limit = current_record.last_real_check_time
  117. global.limit = limit
  118. //当set_time秒数<=latest_record_time秒数时,最小限制时间戳需+1分钟
  119. if (new Date(global.set_time).getSeconds() <= new Date(current_record.last_real_check_time).getSeconds()) {
  120. limit = limit + 60 * 1000
  121. }
  122. }
  123. return <LimitPickers ref={limitPickerRef}
  124. title = '结束断食'
  125. themeColor={global.fastColor?global.fastColor:ColorType.fast}
  126. limit={limit} onCancel={() => { setIsOpen(false); setShowModal(false) }}
  127. onChange={(e) => {
  128. endFast(e)
  129. setIsOpen(false)
  130. }} />
  131. }
  132. function testLayout() {
  133. var current_record = machine.context.checkData ? (machine.context.checkData as any).current_record : null;
  134. if (current_record == null) {
  135. return <View />
  136. }
  137. return <View style={{ color: '#fff', paddingTop: 30, paddingLeft: 30, display: 'flex', flexDirection: 'column' }}>
  138. <Text>check scenario:fast</Text>
  139. <Text>check type:end</Text>
  140. <Text style={{ marginTop: 30 }}>picker restriction</Text>
  141. <Text style={{ marginLeft: 50 }}>min:{TimeFormatter.formatTimestamp(global.limit)}</Text>
  142. <Text style={{ marginLeft: 50 }}>pick:{TimeFormatter.formatTimestamp(global.picker_time)}</Text>
  143. <Text style={{ marginLeft: 50 }}>max{TimeFormatter.formatTimestamp(global.set_time)}</Text>
  144. <Text style={{ marginTop: 30 }}>now:{TimeFormatter.formatTimestamp(new Date().getTime())}</Text>
  145. <Text>elapsed:{TimeFormatter.calculateTimeDifference(global.picker_time, new Date().getTime())}</Text>
  146. <Text style={{ marginTop: 30 }}>real start:{TimeFormatter.formatTimestamp(current_record.fast.real_start_time)}</Text>
  147. <Text>real duration:{TimeFormatter.calculateTimeDifference(current_record.fast.real_start_time, new Date().getTime())}</Text>
  148. </View>
  149. }
  150. return (
  151. <View ref={ref} style={{ width: '100%', marginTop: 20, marginBottom: 20, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
  152. <Text onClick={checkActionSheetData}>More Component</Text>
  153. {
  154. showModal && isOpen && <Modal children={layoutContent()} dismiss={() => setIsOpen(false)}
  155. testInfo={testLayout()}
  156. confirm={() => {
  157. var picker = limitPickerRef.current;
  158. endFast((picker as any).getConfirmData());
  159. setIsOpen(false);
  160. }} />
  161. }
  162. {/* {
  163. showModal && <AtFloatLayout
  164. isOpened={isOpen}
  165. onClose={() => {
  166. setIsOpen(false)
  167. setShowModal(false)
  168. }}
  169. title="">
  170. {
  171. layoutContent()
  172. }
  173. </AtFloatLayout>
  174. } */}
  175. </View>
  176. )
  177. }
  178. )
  179. export default Component;