RecordFastSleep.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import Box from "@/components/layout/Box";
  2. import Header from "@/components/layout/Header";
  3. import Modal from "@/components/layout/Modal";
  4. import RecordItem from "@/features/common/RecordItem";
  5. import { delRecord } from "@/services/trackTimeDuration";
  6. import { ModalType } from "@/utils/types";
  7. import { View, Text, Image } from "@tarojs/components";
  8. import Taro from "@tarojs/taro";
  9. import { useState } from "react";
  10. import TimelineFastSleep from "./TimelineFastSleep";
  11. import { TimeFormatter } from "@/utils/time_format";
  12. import './RecordFastSleep.scss'
  13. import { getBgRing, getCommon, getDot, getReal, getTarget } from "../hooks/RingData";
  14. import Rings from "./Rings";
  15. import Segment from "@/components/navigation/Segment";
  16. import Stage from "./Stage";
  17. import CenterContentTitleModal from "@/features/common/CenterContentTitleModal";
  18. import { useTranslation } from "react-i18next";
  19. import { ColorType } from "@/context/themes/color";
  20. import TimelineStage from "./TimelineStage";
  21. import { jumpPage } from "../hooks/Common";
  22. export default function RecordFastSleep(props: { data: any, type: string, delSuccess?: Function }) {
  23. const [showDetailModal, setShowDetailModal] = useState(false)
  24. const [segmentIndex, setSegmentIndex] = useState(0)
  25. const { t } = useTranslation()
  26. const canvasId = props.data.id
  27. const record = props.data;
  28. function header() {
  29. if (props.type == 'latest') {
  30. return <Header title={t('feature.track_time_duration.record_fast_sleep.header.latest_record')} action={() => {
  31. jumpPage('/pages/common/RecordsHistory?type=time&title=time')
  32. }} />
  33. }
  34. return null
  35. }
  36. function del() {
  37. var id = props.data.id
  38. delRecord(id
  39. ).then(res => {
  40. global.refreshTime()
  41. Taro.showToast({
  42. title: t('page.records_history.del_success')
  43. })
  44. props.delSuccess && props.delSuccess(props.data)
  45. // Taro.navigateBack()
  46. })
  47. }
  48. function schedules() {
  49. debugger
  50. return <View style={{ display: 'flex', flexDirection: 'column' }}>
  51. {/*
  52. <Text className="modalTitle1" style={{
  53. marginBottom: 10,
  54. color: '#fff', fontWeight: 'bold',
  55. }}>{TimeFormatter.getDateAndWeek(props.data.first_real_check_time)}</Text> */}
  56. <TimelineStage data={props.data} title={t('feature.track_time_duration.record_fast_sleep.pop_title')}
  57. subTitle={TimeFormatter.getDateAndWeek(props.data.first_real_check_time)} first_real_check_time={props.data.first_real_check_time}/>
  58. {/* {
  59. props.data.scenario == 'FAST_SLEEP' &&
  60. <View style={{ marginBottom: 0 }}>
  61. <Segment titles={['断食睡眠时间', '断食阶段']} changed={(e) => {
  62. setSegmentIndex(e);
  63. global.segmentIndex = e
  64. }} />
  65. </View>
  66. }
  67. <View className="segment_detail" style={{
  68. marginTop: 10,
  69. display: 'flex', alignItems: 'center',
  70. flexDirection: 'column', justifyContent: 'center'
  71. }}>
  72. <View style={{ position: 'relative',width:'100%',display: 'flex', alignItems: 'center',
  73. flexDirection: 'column', justifyContent: 'center' }}>
  74. <View style={{ opacity: segmentIndex == 0 ? 1 : 0 }}>
  75. <TimelineFastSleep data={props.data} />
  76. </View>
  77. {
  78. props.data.scenario == 'FAST_SLEEP' && <View style={{
  79. position: 'absolute', left: 0, top: 0, right: 0, bottom: 0,
  80. opacity: segmentIndex == 1 ? 1 : 0
  81. }}>
  82. <Stage data={props.data} />
  83. </View>
  84. }
  85. </View>
  86. </View> */}
  87. </View>
  88. }
  89. function showDetail(e) {
  90. if (props.type == 'latest') {
  91. setSegmentIndex(0)
  92. global.segmentIndex = 0
  93. setShowDetailModal(true)
  94. return;
  95. }
  96. setShowDetailModal(true)
  97. // var node = (<Modal children={schedules()}
  98. // modalType={ModalType.center}
  99. // dismiss={() => {
  100. // global.showModal(false, null)
  101. // }}
  102. // confirm={() => { }} />);
  103. // global.showModal(true, node);
  104. }
  105. function getDuration(obj) {
  106. if (!obj) {
  107. }
  108. if (obj.status == 'NOT_STARTED' || obj.status == 'NOT_COMPLETED') {
  109. return ''
  110. }
  111. var start = obj.real_start_time
  112. var end = obj.real_end_time
  113. if (!end) {
  114. end = (new Date()).getTime()
  115. }
  116. return TimeFormatter.durationFormate(start,end)
  117. // return TimeFormatter.calculateTimeDifference(start, end)
  118. }
  119. function durationArc(start_time: number, end_time: number) {
  120. var duration = (end_time - start_time) / 1000;
  121. return duration / (24 * 3600) * 2 * Math.PI;
  122. }
  123. function bigRing() {
  124. var common = getCommon(null, true)
  125. common.radius = 42;
  126. common.lineWidth = 9;
  127. var bgRing = getBgRing()
  128. if (props.type == 'record' || props.type == 'latest') {
  129. var realRing = getReal(record, true, true)
  130. if (props.data.status == 'ONGOING3') {
  131. realRing.color = 'rgba(0,0,0,0)'
  132. // bgRing.color = 'rgba(0,0,0,0)'
  133. }
  134. return <Rings common={common} bgRing={bgRing} canvasId={canvasId} realRing={realRing} />
  135. }
  136. else {
  137. var currentDot1 = getDot(record, true)
  138. var targetBigRing1 = getTarget(record, true)
  139. if (record.status == 'ONGOING') {
  140. var realRing1 = getReal(record, true, false)
  141. return <Rings common={common} bgRing={bgRing} currentDot={currentDot1} realRing={realRing1} targetRing={targetBigRing1} canvasId={canvasId} />
  142. }
  143. if (record.status == 'WAIT_FOR_START') {
  144. return <Rings common={common} bgRing={bgRing} currentDot={currentDot1} canvasId={canvasId} />
  145. }
  146. var realRing1 = getReal(record, true, false)
  147. return <Rings common={common} bgRing={bgRing} realRing={realRing1} currentDot={currentDot1} targetRing={targetBigRing1} canvasId={canvasId} />
  148. }
  149. }
  150. function smallRing() {
  151. if (record.scenario == 'FAST_SLEEP') {
  152. var common = getCommon(null, false)
  153. common.radius = 28;
  154. common.lineWidth = 9;
  155. var bgRing = getBgRing()
  156. var realRing = getReal(record, false, false)
  157. if (props.type == 'record' || props.type == 'latest') {
  158. if (record.sleep.status == 'WAIT_FOR_END') {
  159. realRing.durationArc = durationArc(record.sleep.target_start_time, (new Date()).getTime())
  160. return <Rings common={common} bgRing={bgRing} canvasId={canvasId + 'small'} realRing={realRing} />
  161. }
  162. else if (record.sleep.status == 'NOT_COMPLETED') {
  163. realRing.durationArc = 0.01
  164. return <Rings common={common} bgRing={bgRing} canvasId={canvasId + 'small'} realRing={realRing} />
  165. }
  166. else if (record.sleep.status == 'COMPLETED') {
  167. realRing = getReal(record, false, true)
  168. return <Rings common={common} bgRing={bgRing} canvasId={canvasId + 'small'} realRing={realRing} />
  169. }
  170. return <Rings common={common} bgRing={bgRing} canvasId={canvasId + 'small'} />
  171. }
  172. else {
  173. var currentDot = getDot(record, false)
  174. var targetRing = getTarget(record, false)
  175. if (record.status == 'ONGOING2') {
  176. var realRing = getReal(record, false, false)
  177. return <Rings common={common} bgRing={bgRing} realRing={realRing} currentDot={currentDot} targetRing={targetRing} canvasId={canvasId + 'small'} />
  178. }
  179. if (record.status == 'ONGOING3') {
  180. currentDot.color = 'rgba(0, 255, 255, 0.5)'
  181. }
  182. return <Rings common={common} bgRing={bgRing} currentDot={currentDot} canvasId={canvasId + 'small'} />
  183. }
  184. }
  185. return null
  186. }
  187. function rings() {
  188. return <View style={{ position: 'relative', zIndex: 1 }}>
  189. {
  190. bigRing()
  191. }
  192. <View style={{ display: 'flex', position: 'absolute', left: 0, right: 0, top: 0, bottom: 0, alignItems: 'center', justifyContent: 'center' }}>
  193. {
  194. smallRing()
  195. }
  196. </View>
  197. </View>
  198. }
  199. function recordDetail() {
  200. var fastDuration = ''
  201. var sleepDuration = ''
  202. var showFast = false;
  203. var showSleep = false;
  204. if (record.scenario == 'FAST_SLEEP') {
  205. fastDuration = getDuration(record.fast)
  206. sleepDuration = getDuration(record.sleep)
  207. if (record.status == 'ONGOING3') {
  208. fastDuration = t('feature.common.wait_for_end')
  209. }
  210. if (record.sleep.status == "NOT_STARTED") {
  211. sleepDuration = t('feature.common.not_started')
  212. }
  213. else if (record.sleep.status == 'NOT_COMPLETED') {
  214. sleepDuration = t('feature.common.not_completed')
  215. }
  216. showFast = true
  217. showSleep = true
  218. }
  219. else if (record.scenario == 'FAST') {
  220. fastDuration = getDuration(record.fast)
  221. showFast = true
  222. }
  223. else {
  224. sleepDuration = getDuration(record.sleep)
  225. showSleep = true
  226. }
  227. return <View className="fast_sleep_item">
  228. {
  229. rings()
  230. }
  231. <View className="duration_bg">
  232. {
  233. showFast && <Text className="duration_title">{t('feature.track_time_duration.record_fast_sleep.item.fast')}</Text>
  234. }
  235. {
  236. showFast && <Text className="duration_value" style={{ color: global.fastColor ? global.fastColor : ColorType.fast }}>{fastDuration}</Text>
  237. }
  238. {
  239. showSleep && <Text className="duration_title">{t('feature.track_time_duration.record_fast_sleep.item.sleep')}</Text>
  240. }
  241. {
  242. showSleep && <Text className="duration_value" style={{ color: global.sleepColor ? global.sleepColor : ColorType.sleep }}>{sleepDuration}</Text>
  243. }
  244. </View>
  245. {/* <Image className="arrow1" src={require('@/assets/images/arrow.png')} /> */}
  246. <View className="record_arrow_bg" style={{ backgroundColor: global.isDebug ? 'red' : 'transparent' }}>
  247. <View style={{flex:1}}/>
  248. <Text className='recordTime'>{TimeFormatter.dateDescription(props.data.first_real_check_time, true)}</Text>
  249. <Image className="arrow2" src={require('@/assets/images/arrow3.png')} />
  250. </View>
  251. </View>
  252. }
  253. return <View>
  254. {/* {header()} */}
  255. <View className="time_operate_item">
  256. <RecordItem canDel={record.status == 'COMPLETED'} delete={del}
  257. onClick={showDetail}
  258. >{recordDetail()}
  259. </RecordItem>
  260. {
  261. showDetailModal && <Modal children={schedules()}
  262. modalType={ModalType.center}
  263. dismiss={() => setShowDetailModal(false)}
  264. confirm={() => { }} />
  265. }
  266. </View>
  267. </View>
  268. }