IndexItem.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. import { View, Text, Image } from "@tarojs/components";
  2. import { dotIsOuterRange, getBgRing, getCommon, getDot, getReal, getSchedule, getTarget, 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. let useNavigation;
  17. if (process.env.TARO_ENV == 'rn') {
  18. useNavigation = require("@react-navigation/native").useNavigation
  19. }
  20. export default function Component(props: { type: string, data: any, time: any, showStage?: boolean }) {
  21. const { t } = useTranslation()
  22. const [record, setRecord] = useState(props.data.current_record);
  23. const user = useSelector((state: any) => state.user);
  24. const dayNight = useSelector((state: any) => state.dayNight);
  25. const [stageList, setStageList] = useState([true, false, false])
  26. const [isStageMode, setIsStageMode] = useState(false)
  27. let navigation;
  28. if (useNavigation) {
  29. navigation = useNavigation()
  30. }
  31. useEffect(() => {
  32. setRecord(props.data.current_record)
  33. }, [props.data])
  34. useEffect(() => {
  35. if (props.type == 'FAST_SLEEP') {
  36. global.updateMixItem = (data) => {
  37. setStageList(data)
  38. }
  39. global.changeMixIndex = (index) => {
  40. setIsStageMode(index == 1)
  41. }
  42. }
  43. }, [])
  44. function durationArc(start_time: number, end_time: number) {
  45. var duration = (end_time - start_time) / 1000;
  46. return duration / (24 * 3600) * 2 * Math.PI;
  47. }
  48. const startArc = (time: number) => {
  49. var date = new Date(time);
  50. var hour = date.getHours();
  51. var minute = date.getMinutes();
  52. var second = date.getSeconds();
  53. return (hour * 3600 + minute * 60 + second) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
  54. }
  55. function bigRing() {
  56. var common = getCommon(null, true)
  57. common.radius = 42;
  58. common.lineWidth = 9;
  59. var bgRing = getBgRing()
  60. var currentDot1 = getDot(record, true)
  61. var targetBigRing1 = getTarget(record, true)
  62. if (record.status == 'ONGOING') {
  63. var realRing1 = getReal(record, true, false)
  64. return <Rings common={common} bgRing={bgRing} currentDot={currentDot1} realRing={realRing1} targetRing={targetBigRing1} canvasId={props.type + props.time + 'big'} />
  65. }
  66. if (record.status == 'WAIT_FOR_START') {
  67. var realRing1 = getSchedule(props.data.scenario, props.type != 'SLEEP', true, true)//getSchedule(record, props.type != 'SLEEP', true)
  68. var list: any = []
  69. if (props.type == 'FAST_SLEEP') {
  70. realRing1.color = ColorType.fast + '66'
  71. if (dotIsOuterRange(true, record.fast)) {
  72. currentDot1.color = ColorType.ring
  73. }
  74. var detail = timeTotimestamp(props.data.scenario)
  75. if (stageList[0]) {
  76. const realRingBig: RealRing = {
  77. color: global.fastColor ? global.fastColor : ColorType.fast,
  78. startArc: startArc(detail.fast.target_start_time),
  79. durationArc: durationArc(detail.fast.target_start_time, detail.sleep.target_start_time)
  80. }
  81. list.push(realRingBig)
  82. }
  83. if (stageList[1]) {
  84. const realRingBig: RealRing = {
  85. color: global.fastColor ? global.fastColor : ColorType.fast,
  86. startArc: startArc(detail.sleep.target_start_time),
  87. durationArc: durationArc(detail.sleep.target_start_time, detail.sleep.target_end_time)
  88. }
  89. list.push(realRingBig)
  90. }
  91. if (stageList[2]) {
  92. const realRingBig: RealRing = {
  93. color: global.fastColor ? global.fastColor : ColorType.fast,
  94. startArc: startArc(detail.sleep.target_end_time),
  95. durationArc: durationArc(detail.sleep.target_end_time, detail.fast.target_end_time)
  96. }
  97. list.push(realRingBig)
  98. }
  99. }
  100. if (!isStageMode) {
  101. list = []
  102. }
  103. return <Rings common={common} bgRing={bgRing} currentDot={isStageMode ? null : currentDot1} stageList={list} realRing={realRing1} canvasId={props.type + props.time + 'big'} />
  104. }
  105. var realRing1 = getReal(record, true, false)
  106. return <Rings common={common} bgRing={bgRing} realRing={realRing1} currentDot={currentDot1} targetRing={targetBigRing1} canvasId={props.type + props.time + 'big'} />
  107. }
  108. function smallRing() {
  109. var common = getCommon(null, false)
  110. common.radius = 28;
  111. common.lineWidth = 9;
  112. var bgRing = getBgRing()
  113. var realRing = getReal(record, false, false)
  114. if (props.type == 'SLEEP' || props.type == 'FAST_SLEEP') {
  115. if (record.sleep.status == 'WAIT_FOR_END') {
  116. var targetBigRing1 = getTarget(record, false)
  117. targetBigRing1.color = ColorType.sleep + '66'
  118. var currentDot = getDot(record, false)
  119. realRing.durationArc = durationArc(record.sleep.target_start_time, (new Date()).getTime())
  120. return <Rings common={common} bgRing={bgRing} currentDot={currentDot} canvasId={props.type + props.time + 'small'} targetRing={targetBigRing1} realRing={realRing} />
  121. }
  122. else if (record.status == 'WAIT_FOR_START' || record.status == 'ONGOING1') {
  123. realRing = getSchedule(props.data.scenario, false, true, true)//getSchedule(record, false, true)
  124. var currentDot = getDot(record, false)
  125. if (dotIsOuterRange(true, record.sleep)) {
  126. currentDot.color = ColorType.ring
  127. }
  128. return <Rings common={common} bgRing={bgRing} currentDot={isStageMode ? null : currentDot} realRing={realRing} canvasId={props.type + props.time + 'small'} />
  129. }
  130. else if (record.sleep.status == 'NOT_COMPLETED') {
  131. realRing.durationArc = 0.01
  132. var currentDot = getDot(record, false)
  133. return <Rings common={common} bgRing={bgRing} currentDot={currentDot} canvasId={props.type + props.time + 'small'} realRing={realRing} />
  134. }
  135. else if (record.sleep.status == 'COMPLETED') {
  136. realRing = getReal(record, false, true)
  137. return <Rings common={common} bgRing={bgRing} canvasId={props.type + props.time + 'small'} realRing={realRing} />
  138. }
  139. else if (record.sleep.status == 'ONGOING2') {
  140. var currentDot = getDot(record, false)
  141. return <Rings common={common} bgRing={bgRing} currentDot={currentDot} canvasId={props.type + props.time + 'small'} />
  142. }
  143. return <Rings common={common} bgRing={bgRing} canvasId={props.type + props.time + 'small'} />
  144. }
  145. else {
  146. var currentDot = getDot(record, false)
  147. var targetRing = getTarget(record, false)
  148. if (record.status == 'ONGOING2') {
  149. var realRing = getReal(record, false, false)
  150. return <Rings common={common} bgRing={bgRing} realRing={realRing} currentDot={currentDot} targetRing={targetRing} canvasId={props.type + props.time + 'small'} />
  151. }
  152. if (record.status == 'ONGOING3') {
  153. currentDot.color = 'rgba(0, 255, 255, 0.5)'
  154. }
  155. return <Rings common={common} bgRing={bgRing} currentDot={currentDot} canvasId={props.type + props.time + 'small'} />
  156. }
  157. }
  158. function dayRing() {
  159. var common = getCommon(null, true)
  160. common.radius = 56;
  161. common.lineWidth = 9;
  162. var bgRing = getBgRing()
  163. const realRingBig: RealRing = {
  164. color: ColorType.night + '66',
  165. startArc: 0,
  166. durationArc: 2
  167. }
  168. var sunRise = 24 * 60 + 6 * 60
  169. var sunSet = 18 * 60
  170. if (dayNight.gpsInfo && user.test_user) {
  171. var sunRiseObj = dayNight.gpsInfo.daylights[0].sunrise
  172. var sunSetObj = dayNight.gpsInfo.daylights[0].sunset
  173. sunRise = 24 * 60 + parseInt(sunRiseObj.split(':')[0]) * 60 + parseInt(sunRiseObj.split(':')[1])
  174. sunSet = parseInt(sunSetObj.split(':')[0]) * 60 + parseInt(sunSetObj.split(':')[1])
  175. if (sunSetObj.indexOf('PM') != -1) {
  176. sunSet += 12 * 60
  177. }
  178. }
  179. var duration = sunRise - sunSet
  180. realRingBig.startArc = (sunSet * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
  181. realRingBig.durationArc = (duration * 60) / (24 * 3600) * 2 * Math.PI;
  182. var currentDot = getDot(record, false)
  183. if (dotIsOuterRange(false, null, { sunrise: dayNight.sunRise, sunset: dayNight.sunSet })) {
  184. currentDot.color = ColorType.ring
  185. }
  186. else {
  187. currentDot.color = ColorType.night + '66'
  188. }
  189. return <Rings common={common} bgRing={bgRing} realRing={realRingBig} currentDot={currentDot} canvasId={props.type + props.time + 'day'} />
  190. }
  191. function rings() {
  192. return <View style={{ position: 'relative', zIndex: 1 }}>
  193. {
  194. bigRing()
  195. }
  196. {
  197. props.type == 'FAST_SLEEP' && <View style={{ display: 'flex', position: 'absolute', left: 0, right: 0, top: 0, bottom: 0, alignItems: 'center', justifyContent: 'center' }}>
  198. {
  199. smallRing()
  200. }
  201. </View>
  202. }
  203. {
  204. props.type == 'FAST_SLEEP' && !props.showStage && user.isLogin && global.showNightRing === true && <View style={{ display: 'flex', position: 'absolute', left: -14, top: -14, right: -14, bottom: -14 }}>
  205. {
  206. dayRing()
  207. }
  208. </View>
  209. }
  210. </View>
  211. }
  212. function getDuration(obj) {
  213. if (!obj) {
  214. }
  215. if (obj.status == 'NOT_STARTED' || obj.status == 'NOT_COMPLETED') {
  216. return ''
  217. }
  218. var start = obj.real_start_time
  219. var end = obj.real_end_time
  220. if (!end) {
  221. end = (new Date()).getTime()
  222. }
  223. if (obj.status == 'WAIT_FOR_START') {
  224. start = obj.target_start_time
  225. end = obj.target_end_time
  226. }
  227. return TimeFormatter.durationFormate(start, end)
  228. // return TimeFormatter.calculateTimeDifference(start, end)
  229. }
  230. function fastDuration() {
  231. if (record.fast.status == 'WAIT_FOR_END') {
  232. return TimeFormatter.formateTimeDifference(record.fast.real_start_time, new Date().getTime(), false)
  233. }
  234. if (isStageMode && record.scenario == 'FAST_SLEEP') {
  235. if (stageList[0]) {
  236. return TimeFormatter.durationFormate(record.fast.target_start_time, record.sleep.target_start_time)
  237. }
  238. if (stageList[1]) {
  239. return TimeFormatter.durationFormate(record.sleep.target_start_time, record.sleep.target_end_time)
  240. }
  241. if (stageList[2]) {
  242. return TimeFormatter.durationFormate(record.sleep.target_end_time, record.fast.target_end_time)
  243. }
  244. }
  245. return getDuration(record.fast)
  246. }
  247. function sleepDuration() {
  248. if (record.sleep.status == 'WAIT_FOR_END') {
  249. return TimeFormatter.formateTimeDifference(record.sleep.real_start_time, new Date().getTime(), false)
  250. }
  251. return getDuration(record.sleep)
  252. }
  253. function goClock() {
  254. if (!user.isLogin) {
  255. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  256. return
  257. }
  258. if (props.showStage) {
  259. return
  260. }
  261. global.showSegmentPop()
  262. }
  263. function nightDuration() {
  264. if (dayNight.gpsInfo && dayNight.isMember) {
  265. var sunRiseObj = dayNight.gpsInfo.daylights[0].sunrise
  266. var sunSetObj = dayNight.gpsInfo.daylights[0].sunset
  267. var sunRise = 24 * 60 + parseInt(sunRiseObj.split(':')[0]) * 60 + parseInt(sunRiseObj.split(':')[1])
  268. var sunSet = parseInt(sunSetObj.split(':')[0]) * 60 + parseInt(sunSetObj.split(':')[1])
  269. if (sunSetObj.indexOf('PM') != -1) {
  270. sunSet += 12 * 60
  271. }
  272. var duration = (sunRise - sunSet) * 60 * 1000
  273. return TimeFormatter.calculateTimeDifference(new Date().getTime(), new Date().getTime() + duration);
  274. } else {
  275. return '12小时'
  276. }
  277. }
  278. function fastPicker() {
  279. if (!user.isLogin) {
  280. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  281. return
  282. }
  283. global.showFastPicker()
  284. }
  285. function sleepPicker(e) {
  286. if (!user.isLogin) {
  287. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  288. return
  289. }
  290. if (record.status == 'ONGOING3') {
  291. return;
  292. }
  293. global.showSleepPicker()
  294. }
  295. return <View className="time_operate_item">
  296. <View className="fast_sleep_item">
  297. {
  298. rings()
  299. }
  300. <View className="duration_bg">
  301. {
  302. props.type == 'FAST_SLEEP' && !props.showStage && user.isLogin && global.showNightRing === true &&
  303. <Text className="duration_title">{t('feature.common.overnight')}</Text>
  304. }
  305. {
  306. props.type == 'FAST_SLEEP' && !props.showStage && user.isLogin && global.showNightRing === true &&
  307. <Text className="duration_value" style={{ color: ColorType.night }}>{nightDuration()}</Text>
  308. }
  309. {
  310. (props.type == 'FAST' || props.type == 'FAST_SLEEP') && <Text className="duration_title" onClick={fastPicker}>{t('feature.track_time_duration.record_fast_sleep.item.fast')}</Text>
  311. }
  312. {
  313. (props.type == 'FAST' || props.type == 'FAST_SLEEP') && <Text className="duration_value" onClick={fastPicker} style={{ color: global.fastColor ? global.fastColor : ColorType.fast }}>{fastDuration()}</Text>
  314. }
  315. {
  316. (props.type == 'SLEEP' || props.type == 'FAST_SLEEP') && <Text className="duration_title" onClick={sleepPicker}>{t('feature.track_time_duration.record_fast_sleep.item.sleep')}</Text>
  317. }
  318. {
  319. (props.type == 'SLEEP' || props.type == 'FAST_SLEEP') && <Text className="duration_value" onClick={sleepPicker} style={{ color: global.sleepColor ? global.sleepColor : ColorType.sleep }}>{sleepDuration()}</Text>
  320. }
  321. </View>
  322. {
  323. !props.showStage && <View className="record_arrow_bg" style={{ backgroundColor: global.isDebug ? 'red' : 'transparent' }} onClick={goClock}>
  324. <View style={{ flex: 1 }} />
  325. <Image className="arrow2" src={require('@/assets/images/arrow3.png')} />
  326. </View>}
  327. </View>
  328. </View>
  329. }