IndexItem.tsx 23 KB

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