IndexItem.tsx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. import { View, Text, Image, PageContainer } from "@tarojs/components";
  2. import { bigRingRadius, dotIsOuterRange, getBgRing, getCommon, getDot, getReal, getSchedule, getTarget, ringWidth, smallRingRadius, thirdRingRadius, timeTotimestamp } from "../hooks/RingData";
  3. import { RealRing, CurrentDot } from "@/features/trackTimeDuration/components/Rings";
  4. import Rings from "./Rings";
  5. import './IndexItem.scss'
  6. import { useTranslation } from "react-i18next";
  7. import { ColorType } from "@/context/themes/color";
  8. import { jumpPage } from "@/features/trackTimeDuration/hooks/Common";
  9. import { TimeFormatter } from "@/utils/time_format";
  10. import { useSelector } from "react-redux";
  11. import { useEffect, useState } from "react";
  12. import Taro from "@tarojs/taro";
  13. import Modal from "@/components/layout/Modal.weapp";
  14. import { ModalType } from "@/utils/types";
  15. import TimelineStage from "./TimelineStage";
  16. import CircadianDetailPopup from "@/features/trackTimeDuration/components/CircadianDetailPopup";
  17. import { rpxToPx } from "@/utils/tools";
  18. let useNavigation;
  19. if (process.env.TARO_ENV == 'rn') {
  20. useNavigation = require("@react-navigation/native").useNavigation
  21. }
  22. export default function Component(props: { type: string, data: any, time: any, showStage?: boolean }) {
  23. const { t } = useTranslation()
  24. const [record, setRecord] = useState(props.data.current_record);
  25. const user = useSelector((state: any) => state.user);
  26. const dayNight = useSelector((state: any) => state.dayNight);
  27. const [stageList, setStageList] = useState([true, false, false])
  28. const [isStageMode, setIsStageMode] = useState(false)
  29. const [showDetailModal, setShowDetailModal] = useState(false)
  30. const [count, setCount] = useState(0)
  31. let navigation;
  32. if (useNavigation) {
  33. navigation = useNavigation()
  34. }
  35. useEffect(() => {
  36. setRecord(props.data.current_record)
  37. }, [props.data])
  38. useEffect(() => {
  39. setCount((pre) => pre + 1)
  40. }, [dayNight.nightRingSunrise, dayNight.nightRingSunset])
  41. useEffect(() => {
  42. if (props.type == 'FAST_SLEEP') {
  43. global.updateMixItem = (data) => {
  44. setStageList(data)
  45. }
  46. global.changeMixIndex = (index) => {
  47. setIsStageMode(index == 1)
  48. }
  49. }
  50. }, [])
  51. function durationArc(start_time: number, end_time: number) {
  52. var duration = (end_time - start_time) / 1000;
  53. return duration / (24 * 3600) * 2 * Math.PI;
  54. }
  55. const startArc = (time: number) => {
  56. var date = new Date(time);
  57. var hour = date.getHours();
  58. var minute = date.getMinutes();
  59. var second = date.getSeconds();
  60. return (hour * 3600 + minute * 60 + second) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
  61. }
  62. function bigRing() {
  63. var common = getCommon(null, true)
  64. common.radius = bigRingRadius;
  65. common.lineWidth = ringWidth;
  66. var bgRing = getBgRing()
  67. var currentDot1 = getDot(record, true)
  68. var targetBigRing1 = getTarget(record, true)
  69. if (record.status == 'ONGOING') {
  70. var realRing1 = getReal(record, true, false)
  71. return <Rings common={common} bgRing={bgRing} currentDot={currentDot1} realRing={realRing1} targetRing={targetBigRing1} canvasId={props.type + props.time + 'big'} />
  72. }
  73. if (record.status == 'WAIT_FOR_START') {
  74. var realRing1 = getSchedule(props.data.scenario, props.data.scenario.name != 'SLEEP', true, true)//getSchedule(record, props.type != 'SLEEP', true)
  75. var list: any = []
  76. if (props.data.scenario.name == 'FAST_SLEEP') {
  77. realRing1.color = ColorType.fast + '66'
  78. if (dotIsOuterRange(true, record.fast)) {
  79. currentDot1.color = ColorType.ring
  80. }
  81. var detail = timeTotimestamp(props.data.scenario)
  82. if (stageList[0]) {
  83. const realRingBig: RealRing = {
  84. color: global.fastColor ? global.fastColor : ColorType.fast,
  85. startArc: startArc(detail.fast.target_start_time),
  86. durationArc: durationArc(detail.fast.target_start_time, detail.sleep.target_start_time)
  87. }
  88. list.push(realRingBig)
  89. }
  90. if (stageList[1]) {
  91. const realRingBig: RealRing = {
  92. color: global.fastColor ? global.fastColor : ColorType.fast,
  93. startArc: startArc(detail.sleep.target_start_time),
  94. durationArc: durationArc(detail.sleep.target_start_time, detail.sleep.target_end_time)
  95. }
  96. list.push(realRingBig)
  97. }
  98. if (stageList[2]) {
  99. const realRingBig: RealRing = {
  100. color: global.fastColor ? global.fastColor : ColorType.fast,
  101. startArc: startArc(detail.sleep.target_end_time),
  102. durationArc: durationArc(detail.sleep.target_end_time, detail.fast.target_end_time)
  103. }
  104. list.push(realRingBig)
  105. }
  106. }
  107. else if (props.data.scenario.name == 'SLEEP') {
  108. realRing1.color = ColorType.sleep + '66'
  109. }
  110. else {
  111. realRing1.color = ColorType.fast + '66'
  112. }
  113. if (user.isLogin) {
  114. list = []
  115. }
  116. return <Rings common={common} bgRing={bgRing} currentDot={isStageMode ? null : currentDot1} stageList={list} realRing={realRing1} canvasId={props.type + props.time + 'big'} />
  117. }
  118. var realRing1 = getReal(record, true, false)
  119. return <Rings common={common} bgRing={bgRing} realRing={realRing1} currentDot={currentDot1} targetRing={targetBigRing1} canvasId={props.type + props.time + 'big'} />
  120. }
  121. function smallRing() {
  122. var common = getCommon(null, false)
  123. common.radius = smallRingRadius;
  124. common.lineWidth = ringWidth;
  125. var bgRing = getBgRing()
  126. var realRing = getReal(record, false, false)
  127. if (props.type == 'SLEEP' || props.data.scenario.name == 'FAST_SLEEP') {
  128. if (!record.sleep) {
  129. return null
  130. }
  131. if (record.sleep.status == 'WAIT_FOR_END') {
  132. var targetBigRing1 = getTarget(record, false)
  133. targetBigRing1.color = ColorType.sleep + '66'
  134. var currentDot = getDot(record, false)
  135. realRing.durationArc = durationArc(record.sleep.target_start_time, (new Date()).getTime())
  136. return <Rings common={common} bgRing={bgRing} currentDot={currentDot} canvasId={props.type + props.time + 'small'} targetRing={targetBigRing1} realRing={realRing} />
  137. }
  138. else if (record.status == 'WAIT_FOR_START' || record.status == 'ONGOING1') {
  139. realRing = getSchedule(props.data.scenario, false, true, true)//getSchedule(record, false, true)
  140. realRing.color = ColorType.sleep + '66'
  141. var currentDot = getDot(record, false)
  142. if (dotIsOuterRange(true, record.sleep)) {
  143. currentDot.color = ColorType.ring
  144. }
  145. return <Rings common={common} bgRing={bgRing} currentDot={isStageMode ? null : currentDot} realRing={realRing} canvasId={props.type + props.time + 'small'} />
  146. }
  147. else if (record.sleep.status == 'NOT_COMPLETED') {
  148. realRing.durationArc = 0.01
  149. var currentDot = getDot(record, false)
  150. return <Rings common={common} bgRing={bgRing} currentDot={currentDot} canvasId={props.type + props.time + 'small'} realRing={realRing} />
  151. }
  152. else if (record.sleep.status == 'COMPLETED') {
  153. realRing = getReal(record, false, true)
  154. var currentDot = getDot(record, false)
  155. return <Rings common={common} bgRing={bgRing} currentDot={currentDot} canvasId={props.type + props.time + 'small'} realRing={realRing} />
  156. }
  157. else if (record.sleep.status == 'ONGOING2') {
  158. var currentDot = getDot(record, false)
  159. return <Rings common={common} bgRing={bgRing} currentDot={currentDot} canvasId={props.type + props.time + 'small'} />
  160. }
  161. return <Rings common={common} bgRing={bgRing} canvasId={props.type + props.time + 'small'} />
  162. }
  163. else {
  164. var currentDot = getDot(record, false)
  165. var targetRing = getTarget(record, false)
  166. if (record.status == 'ONGOING2') {
  167. var realRing = getReal(record, false, false)
  168. return <Rings common={common} bgRing={bgRing} realRing={realRing} currentDot={currentDot} targetRing={targetRing} canvasId={props.type + props.time + 'small'} />
  169. }
  170. if (record.status == 'ONGOING3') {
  171. currentDot.color = 'rgba(0, 255, 255, 0.5)'
  172. }
  173. return <Rings common={common} bgRing={bgRing} currentDot={currentDot} canvasId={props.type + props.time + 'small'} />
  174. }
  175. }
  176. function dayRing() {
  177. var common = getCommon(null, true)
  178. common.radius = thirdRingRadius;
  179. common.lineWidth = ringWidth;
  180. var bgRing = getBgRing()
  181. const realRingBig: RealRing = {
  182. color: ColorType.night + '66',
  183. startArc: 0,
  184. durationArc: 2
  185. }
  186. var sunRise = 24 * 60 + 6 * 60
  187. var sunSet = 18 * 60
  188. // if (dayNight.gpsInfo && user.test_user) {
  189. var sunRiseObj = dayNight.nightRingSunrise
  190. var sunSetObj = dayNight.nightRingSunset
  191. sunRise = 24 * 60 + parseInt(sunRiseObj.split(':')[0]) * 60 + parseInt(sunRiseObj.split(':')[1])
  192. sunSet = parseInt(sunSetObj.split(':')[0]) * 60 + parseInt(sunSetObj.split(':')[1])
  193. if (sunSetObj.indexOf('PM') != -1) {
  194. sunSet += 12 * 60
  195. }
  196. // }
  197. var duration = sunRise - sunSet
  198. realRingBig.startArc = (sunSet * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
  199. realRingBig.durationArc = (duration * 60) / (24 * 3600) * 2 * Math.PI;
  200. var currentDot = getDot(record, false)
  201. currentDot.color = ColorType.night
  202. var now = new Date()
  203. var t = now.getHours() * 60 + now.getMinutes()
  204. var duration2 = t - sunSet
  205. if (duration2 < 0) {
  206. duration2 += 24 * 60
  207. }
  208. const realRing: RealRing = {
  209. color: ColorType.night,
  210. startArc: (sunSet * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0,
  211. durationArc: (duration2 * 60) / (24 * 3600) * 2 * Math.PI
  212. }
  213. if (new Date().getDate()){
  214. }
  215. return <Rings common={common} bgRing={bgRing} targetRing={realRingBig} realRing={duration2 > duration ? null : realRing} currentDot={currentDot} canvasId={props.type + props.time + 'day'} />
  216. }
  217. function rings() {
  218. return <View style={{
  219. position: 'relative', zIndex: 1,
  220. marginLeft: !props.showStage && global.showNightRing === true ? -6 : 0,
  221. }}>
  222. {
  223. bigRing()
  224. }
  225. {
  226. props.data.scenario.name == 'FAST_SLEEP' && <View style={{ display: 'flex', position: 'absolute', left: 0, right: 0, top: 0, bottom: 0, alignItems: 'center', justifyContent: 'center' }}>
  227. {
  228. smallRing()
  229. }
  230. </View>
  231. }
  232. {
  233. !props.showStage && global.showNightRing === true && <View style={{ display: 'flex', position: 'absolute', left: -14, top: -14, right: -14, bottom: -14 }}>
  234. {
  235. dayRing()
  236. }
  237. </View>
  238. }
  239. </View>
  240. }
  241. function getDuration(obj) {
  242. if (!obj) {
  243. }
  244. if (obj.status == 'NOT_STARTED' || obj.status == 'NOT_COMPLETED') {
  245. return ''
  246. }
  247. var start = obj.real_start_time
  248. var end = obj.real_end_time
  249. if (!end) {
  250. end = (new Date()).getTime()
  251. }
  252. if (obj.status == 'WAIT_FOR_START') {
  253. start = obj.target_start_time
  254. end = obj.target_end_time
  255. }
  256. return TimeFormatter.durationFormate(start, end)
  257. // return TimeFormatter.calculateTimeDifference(start, end)
  258. }
  259. function fastDuration() {
  260. if (!record.fast) {
  261. return ''
  262. }
  263. if (record.fast.status == 'WAIT_FOR_END') {
  264. return TimeFormatter.countdown(record.fast.real_start_time)
  265. // return TimeFormatter.formateTimeDifference(record.fast.real_start_time, new Date().getTime(), false)
  266. }
  267. if (isStageMode && record.scenario == 'FAST_SLEEP') {
  268. if (stageList[0]) {
  269. return TimeFormatter.durationFormate(record.fast.target_start_time, record.sleep.target_start_time)
  270. }
  271. if (stageList[1]) {
  272. return TimeFormatter.durationFormate(record.sleep.target_start_time, record.sleep.target_end_time)
  273. }
  274. if (stageList[2]) {
  275. return TimeFormatter.durationFormate(record.sleep.target_end_time, record.fast.target_end_time)
  276. }
  277. }
  278. return getDuration(record.fast)
  279. }
  280. function sleepDuration() {
  281. if (!record.sleep) {
  282. return ''
  283. }
  284. if (record.sleep.status == 'WAIT_FOR_END') {
  285. return TimeFormatter.formateTimeDifference(record.sleep.real_start_time, new Date().getTime(), false)
  286. }
  287. return getDuration(record.sleep)
  288. }
  289. function goClock() {
  290. if (!user.isLogin) {
  291. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  292. return
  293. }
  294. if (props.showStage) {
  295. return
  296. }
  297. global.showSegmentPop()
  298. }
  299. function nightDuration() {
  300. var sunRiseObj = dayNight.nightRingSunrise
  301. var sunSetObj = dayNight.nightRingSunset
  302. var sunRise = 24 * 60 + parseInt(sunRiseObj.split(':')[0]) * 60 + parseInt(sunRiseObj.split(':')[1])
  303. var sunSet = parseInt(sunSetObj.split(':')[0]) * 60 + parseInt(sunSetObj.split(':')[1])
  304. if (sunSetObj.indexOf('PM') != -1) {
  305. sunSet += 12 * 60
  306. }
  307. var duration = (sunRise - sunSet) * 60 * 1000
  308. return TimeFormatter.calculateTimeDifference(new Date().getTime(), new Date().getTime() + duration);
  309. }
  310. function fastPicker() {
  311. if (!user.isLogin) {
  312. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  313. return
  314. }
  315. global.showFastPicker()
  316. }
  317. function sleepPicker(e) {
  318. if (!user.isLogin) {
  319. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  320. return
  321. }
  322. if (record.status == 'ONGOING3') {
  323. return;
  324. }
  325. global.showSleepPicker()
  326. }
  327. function goDetail(e) {
  328. if (process.env.TARO_ENV == 'weapp') {
  329. e.stopPropagation()
  330. }
  331. if (!user.isLogin) {
  332. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  333. return
  334. }
  335. setShowDetailModal(true)
  336. }
  337. function modalContent() {
  338. if (process.env.TARO_ENV == 'weapp') {
  339. return <Modal
  340. testInfo={null}
  341. dismiss={() => {
  342. setShowDetailModal(false)
  343. }}
  344. confirm={() => { }}>
  345. {
  346. <CircadianDetailPopup
  347. record={record}
  348. schedule={props.data.scenario.schedule}
  349. onClose={() => { setShowDetailModal(false); }} />
  350. }
  351. </Modal>
  352. }
  353. else if (process.env.TARO_ENV == 'rn') {
  354. return <PageContainer style={{ backgroundColor: '#1c1c1c' }}
  355. // overlayStyle='background-color:rgba(0,0,0,0.9)'
  356. // custom-style='background-color:#1c1c1c'
  357. overlayStyle={{ backgroundColor: 'rgba(0,0,0,0.9)' }}
  358. customStyle={{ backgroundColor: '#1c1c1c' }}
  359. closeOnSlideDown={false}
  360. onBeforeEnter={() => {
  361. }}
  362. onBeforeLeave={() => {
  363. }}
  364. onClick={() => { alert('b') }}
  365. onClickOverlay={() => { alert('a') }}
  366. onAfterLeave={() => { setShowDetailModal(false) }}
  367. show={showDetailModal} round={true} overlay={true} position='bottom'
  368. >
  369. {
  370. <CircadianDetailPopup
  371. record={record}
  372. schedule={props.data.scenario.schedule}
  373. onClose={() => { setShowDetailModal(false) }} />
  374. }
  375. </PageContainer>
  376. }
  377. }
  378. return <View style={{
  379. marginLeft: rpxToPx(-40),
  380. marginRight: rpxToPx(-40),
  381. marginTop: rpxToPx(-40),
  382. padding: rpxToPx(48),
  383. // backgroundColor:'pink'
  384. }}>
  385. <View className="time_operate_item1" onClick={goDetail}>
  386. <View className="fast_sleep_item" style={{
  387. marginTop: !props.showStage && global.showNightRing === true ? -6 : 0,
  388. marginBottom: !props.showStage && global.showNightRing === true ? -6 : 0,
  389. padding: !props.showStage && global.showNightRing === true ? 14 : 0,
  390. paddingRight: 0
  391. }}>
  392. {
  393. rings()
  394. }
  395. <View className="duration_bg2" style={{ marginLeft: !props.showStage && global.showNightRing === true ? rpxToPx(68) : rpxToPx(52), height: bigRingRadius * 2, overflow: 'visible' }}>
  396. {
  397. !props.showStage && global.showNightRing === true &&
  398. <Text className="duration_title2">{t('feature.common.overnight')}</Text>
  399. }
  400. {
  401. !props.showStage && global.showNightRing === true &&
  402. <Text className="duration_value2" style={{ color: ColorType.night }}>{nightDuration()}</Text>
  403. }
  404. {
  405. (props.data.scenario.name == 'FAST' || props.data.scenario.name == 'FAST_SLEEP') && <Text className="duration_title2">{t('feature.track_time_duration.record_fast_sleep.item.fast')}</Text>
  406. }
  407. {
  408. (props.data.scenario.name == 'FAST' || props.data.scenario.name == 'FAST_SLEEP') && <Text className="duration_value2" style={{ color: global.fastColor ? global.fastColor : ColorType.fast }}>{fastDuration()}</Text>
  409. }
  410. {
  411. (props.data.scenario.name == 'SLEEP' || props.data.scenario.name == 'FAST_SLEEP') && <Text className="duration_title2" >{t('feature.track_time_duration.record_fast_sleep.item.sleep')}</Text>
  412. }
  413. {
  414. (props.data.scenario.name == 'SLEEP' || props.data.scenario.name == 'FAST_SLEEP') && <Text className="duration_value2" style={{ color: global.sleepColor ? global.sleepColor : ColorType.sleep, marginBottom: 0 }}>{sleepDuration()}</Text>
  415. }
  416. </View>
  417. <Image className="arrow2" src={require('@/assets/images/arrow3.png')} />
  418. {/* {
  419. !props.showStage && <View className="record_arrow_bg" style={{ backgroundColor: global.isDebug ? 'red' : 'transparent' }}>
  420. <View style={{ flex: 1 }} />
  421. </View>} */}
  422. </View>
  423. {
  424. showDetailModal && modalContent()
  425. }
  426. </View>
  427. </View>
  428. }