AllRings.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. import Box from '@/components/layout/Box'
  2. import './AllRings.scss'
  3. import { PageContainer, View, Text } from '@tarojs/components'
  4. import { useState } from 'react'
  5. import Modal from '@/components/layout/Modal.weapp'
  6. import { bigRingRadius, getBgRing, getCommon, getDot, getReal, getSchedule, getTarget, ringWidth, smallRingRadius, thirdRingRadius, timeTotimestamp } from '../hooks/RingData'
  7. import { rpxToPx } from '@/utils/tools'
  8. import { ColorType } from '@/context/themes/color'
  9. import { useTranslation } from 'react-i18next'
  10. import Rings from "./Rings";
  11. import { TimeFormatter } from '@/utils/time_format'
  12. import { useSelector } from 'react-redux'
  13. import { RealRing } from '@/features/trackTimeDuration/components/Rings'
  14. export default function AllRings(props: { data?: any, time?: any }) {
  15. const [showRing, setShowRing] = useState(false)
  16. const [record, setRecord] = useState(props.data.current_record);
  17. const user = useSelector((state: any) => state.user);
  18. const dayNight = useSelector((state: any) => state.night);
  19. const { t } = useTranslation()
  20. function tapShow() {
  21. setShowRing(true)
  22. }
  23. function durationArc(start_time: number, end_time: number) {
  24. var duration = (end_time - start_time) / 1000;
  25. return duration / (24 * 3600) * 2 * Math.PI;
  26. }
  27. const startArc = (time: number) => {
  28. var date = new Date(time);
  29. var hour = date.getHours();
  30. var minute = date.getMinutes();
  31. var second = date.getSeconds();
  32. return (hour * 3600 + minute * 60 + second) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
  33. }
  34. function bigRing() {
  35. var common = getCommon(null, true)
  36. common.radius = bigRingRadius;
  37. common.lineWidth = ringWidth;
  38. var bgRing = getBgRing()
  39. var currentDot1 = getDot(record, true)
  40. var targetBigRing1 = getTarget(record, true)
  41. targetBigRing1.color = record.scenario == 'SLEEP' ? ColorType.sleep + '66' : ColorType.fast + '66'
  42. if (record.status == 'ONGOING') {
  43. var realRing1 = getReal(record, true, false)
  44. // debugger
  45. return <Rings common={common} bgRing={bgRing} currentDot={currentDot1} realRing={realRing1} targetRing={targetBigRing1} canvasId={props.type + 'index_big'} />
  46. }
  47. if (record.status == 'WAIT_FOR_START') {
  48. var scenario = JSON.parse(JSON.stringify(props.data.scenario))
  49. if (scenario.name == 'SLEEP') {
  50. var countduration = (props.data.current_record.sleep.target_end_time - props.data.current_record.sleep.target_start_time) / 60000
  51. var min1 = TimeFormatter.timestringToSeconds(scenario.schedule.sleep.start_time) / 60
  52. var min2 = TimeFormatter.timestringToSeconds(scenario.schedule.sleep.end_time) / 60
  53. var leftMinutes = min1 < min2 ? min2 - min1 : min2 + 24 * 60 - min1
  54. if (leftMinutes != countduration) {
  55. min2 = min1 + countduration
  56. if (min2 > 1440) {
  57. min2 -= 1440
  58. }
  59. var hour = Math.floor(min2 / 60)
  60. var minute = min2 % 60
  61. scenario.schedule.sleep.end_time = `${hour}:${minute}`
  62. }
  63. }
  64. else {
  65. var countduration = (props.data.current_record.fast.target_end_time - props.data.current_record.fast.target_start_time) / 60000
  66. var min1 = TimeFormatter.timestringToSeconds(scenario.schedule.fast.start_time) / 60
  67. var min2 = TimeFormatter.timestringToSeconds(scenario.schedule.fast.end_time) / 60
  68. var leftMinutes = min1 < min2 ? min2 - min1 : min2 + 24 * 60 - min1
  69. if (leftMinutes != countduration) {
  70. min2 = min1 + countduration
  71. if (min2 > 1440) {
  72. min2 -= 1440
  73. }
  74. var hour = Math.floor(min2 / 60)
  75. var minute = min2 % 60
  76. scenario.schedule.fast.end_time = `${hour}:${minute}`
  77. }
  78. }
  79. var realRing1 = getSchedule(scenario, scenario.name != 'SLEEP', true, true)//getSchedule(record, props.type != 'SLEEP', true)
  80. var list: any = []
  81. if (scenario.name == 'FAST_SLEEP') {
  82. realRing1.color = ColorType.fast + '66'
  83. }
  84. else if (scenario.name == 'SLEEP') {
  85. realRing1.color = ColorType.sleep + '66'
  86. }
  87. else {
  88. realRing1.color = ColorType.fast + '66'
  89. }
  90. if (user.isLogin) {
  91. list = []
  92. }
  93. if (!user.isLogin) {
  94. currentDot1 = null
  95. // realRing1 = null
  96. }
  97. return <Rings common={common} bgRing={bgRing} currentDot={currentDot1} stageList={list} realRing={realRing1} canvasId={'full_index_big'} />
  98. }
  99. var realRing1 = getReal(record, true, false)
  100. return <Rings common={common} bgRing={bgRing} realRing={realRing1} currentDot={currentDot1} targetRing={targetBigRing1} canvasId={'full_index_big'} />
  101. }
  102. function smallRing() {
  103. var common = getCommon(null, false)
  104. common.radius = smallRingRadius;
  105. common.lineWidth = ringWidth;
  106. var bgRing = getBgRing()
  107. var realRing = getReal(record, false, false)
  108. if (props.data.scenario.name == 'FAST_SLEEP') {
  109. if (!record.sleep) {
  110. return null
  111. }
  112. if (record.sleep.status == 'WAIT_FOR_END') {
  113. var targetBigRing1 = getTarget(record, false)
  114. targetBigRing1.color = ColorType.sleep + '66'
  115. var currentDot = getDot(record, false)
  116. realRing.durationArc = durationArc(record.sleep.target_start_time, (new Date()).getTime())
  117. return <Rings common={common} bgRing={bgRing} currentDot={currentDot} canvasId={'full_index_small'} targetRing={targetBigRing1} realRing={realRing} />
  118. }
  119. else if (record.status == 'WAIT_FOR_START' || record.status == 'ONGOING1') {
  120. var scenario = JSON.parse(JSON.stringify(props.data.scenario))
  121. if (record.status == 'WAIT_FOR_START') {
  122. realRing = getSchedule(scenario, false, true, record.status == 'WAIT_FOR_START')//getSchedule(record, false, true)
  123. }
  124. else {
  125. realRing.startArc = startArc(record.sleep.target_start_time)
  126. realRing.durationArc = durationArc(record.sleep.target_start_time, record.sleep.target_end_time)
  127. }
  128. realRing.color = ColorType.sleep + '66'
  129. var currentDot = getDot(record, false)
  130. if (!user.isLogin) {
  131. currentDot = null
  132. }
  133. return <Rings common={common} bgRing={bgRing} currentDot={currentDot} realRing={realRing} canvasId={'full_index_small'} />
  134. }
  135. else if (record.sleep.status == 'NOT_COMPLETED') {
  136. realRing.durationArc = 0.01
  137. var currentDot = getDot(record, false)
  138. return <Rings common={common} bgRing={bgRing} currentDot={currentDot} canvasId={'full_index_small'} realRing={realRing} />
  139. }
  140. else if (record.sleep.status == 'COMPLETED') {
  141. realRing = getReal(record, false, true)
  142. var currentDot = getDot(record, false)
  143. if (record.status == 'ONGOING3') {
  144. currentDot = null
  145. // currentDot.color = '#ffffffff'
  146. }
  147. return <Rings common={common} bgRing={bgRing} currentDot={currentDot} canvasId={'full_index_small'} realRing={realRing} />
  148. }
  149. else if (record.sleep.status == 'ONGOING2') {
  150. var currentDot = getDot(record, false)
  151. return <Rings common={common} bgRing={bgRing} currentDot={currentDot} canvasId={'full_index_small'} />
  152. }
  153. return <Rings common={common} bgRing={bgRing} canvasId={'full_index_small'} />
  154. }
  155. else {
  156. var currentDot = getDot(record, false)
  157. var targetRing = getTarget(record, false)
  158. if (record.status == 'ONGOING2') {
  159. var realRing = getReal(record, false, false)
  160. return <Rings common={common} bgRing={bgRing} realRing={realRing} currentDot={currentDot} targetRing={targetRing} canvasId={'full_index_small'} />
  161. }
  162. if (record.status == 'ONGOING3') {
  163. currentDot.color = 'rgba(0, 255, 255, 0.5)'
  164. }
  165. return <Rings common={common} bgRing={bgRing} currentDot={currentDot} canvasId={'full_index_small'} />
  166. }
  167. }
  168. function dayRing() {
  169. var common = getCommon(null, true)
  170. common.radius = thirdRingRadius;
  171. common.lineWidth = ringWidth;
  172. var bgRing = getBgRing()
  173. let realRingBig: RealRing = {
  174. color: ColorType.night + '66',
  175. startArc: 0,
  176. durationArc: 2
  177. }
  178. var sunRise = 24 * 60 + 6 * 60
  179. var sunSet = 18 * 60
  180. var sunRiseObj = dayNight.nightRingSunrise
  181. var sunSetObj = dayNight.nightRingSunset
  182. sunRise = 24 * 60 + parseInt(sunRiseObj.split(':')[0]) * 60 + parseInt(sunRiseObj.split(':')[1])
  183. sunSet = parseInt(sunSetObj.split(':')[0]) * 60 + parseInt(sunSetObj.split(':')[1])
  184. if (sunSetObj.indexOf('PM') != -1) {
  185. sunSet += 12 * 60
  186. }
  187. // }
  188. var duration = sunRise - sunSet
  189. realRingBig.startArc = (sunSet * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
  190. realRingBig.durationArc = (duration * 60) / (24 * 3600) * 2 * Math.PI;
  191. var currentDot = getDot(record, false)
  192. currentDot.color = ColorType.night
  193. var now = new Date()
  194. var t = now.getHours() * 60 + now.getMinutes()
  195. var duration2 = t - sunSet
  196. if (duration2 < 0) {
  197. duration2 += 24 * 60
  198. }
  199. let realRing: RealRing = {
  200. color: ColorType.night,
  201. startArc: (sunSet * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0,
  202. durationArc: (duration2 * 60) / (24 * 3600) * 2 * Math.PI
  203. }
  204. if (dayNight.nightRingDate) {
  205. if (new Date(dayNight.nightRingDate).getDate() == new Date().getDate() && new Date().getHours() < 12) {
  206. realRing = null;
  207. }
  208. }
  209. if (!user.isLogin) {
  210. currentDot = null;
  211. realRing = null;
  212. }
  213. return <Rings common={common} bgRing={bgRing} targetRing={realRingBig} realRing={duration2 > duration ? null : realRing} currentDot={currentDot} canvasId={'full_index_day'} />
  214. }
  215. function rings() {
  216. return <View style={{
  217. position: 'relative', zIndex: 1,
  218. marginLeft: -6,
  219. }}>
  220. {
  221. bigRing()
  222. }
  223. {
  224. props.data.scenario.name == 'FAST_SLEEP' && <View style={{ display: 'flex', position: 'absolute', left: 0, right: 0, top: 0, bottom: 0, alignItems: 'center', justifyContent: 'center' }}>
  225. {
  226. smallRing()
  227. }
  228. </View>
  229. }
  230. {
  231. <View style={{ display: 'flex', position: 'absolute', left: -14, top: -14, right: -14, bottom: -14 }}>
  232. {
  233. dayRing()
  234. }
  235. </View>
  236. }
  237. </View>
  238. }
  239. function getDuration(obj) {
  240. if (!obj) {
  241. }
  242. if (obj.status == 'NOT_STARTED' || obj.status == 'NOT_COMPLETED') {
  243. return ''
  244. }
  245. var start = obj.real_start_time
  246. var end = obj.real_end_time
  247. if (!end) {
  248. end = (new Date()).getTime()
  249. }
  250. if (obj.status == 'WAIT_FOR_START') {
  251. start = obj.target_start_time
  252. end = obj.target_end_time
  253. }
  254. return TimeFormatter.durationFormate(start, end)
  255. // return TimeFormatter.calculateTimeDifference(start, end)
  256. }
  257. function fastDuration() {
  258. if (!record.fast) {
  259. return ''
  260. }
  261. if (record.fast.status == 'WAIT_FOR_END') {
  262. return TimeFormatter.countdown(record.fast.real_start_time)
  263. // return TimeFormatter.formateTimeDifference(record.fast.real_start_time, new Date().getTime(), false)
  264. }
  265. return getDuration(record.fast)
  266. }
  267. function sleepDuration() {
  268. if (!record.sleep) {
  269. return ''
  270. }
  271. if (record.sleep.status == 'WAIT_FOR_END') {
  272. return TimeFormatter.formateTimeDifference(record.sleep.real_start_time, new Date().getTime(), false)
  273. }
  274. return getDuration(record.sleep)
  275. }
  276. function nightDuration() {
  277. var sunRiseObj = dayNight.nightRingSunrise
  278. var sunSetObj = dayNight.nightRingSunset
  279. var sunRise = 24 * 60 + parseInt(sunRiseObj.split(':')[0]) * 60 + parseInt(sunRiseObj.split(':')[1])
  280. var sunSet = parseInt(sunSetObj.split(':')[0]) * 60 + parseInt(sunSetObj.split(':')[1])
  281. if (sunSetObj.indexOf('PM') != -1) {
  282. sunSet += 12 * 60
  283. }
  284. var duration = (sunRise - sunSet) * 60 * 1000
  285. return TimeFormatter.calculateTimeDifference(new Date().getTime(), new Date().getTime() + duration);
  286. }
  287. function popDetail() {
  288. return <Box>
  289. <View className="ring_full_container">
  290. <View className="time_operate_item1">
  291. <View className='fast_sleep_item three_ring_card_detail'>
  292. {
  293. rings()
  294. }
  295. <View className="duration_bg2" style={{ marginLeft: rpxToPx(68), height: bigRingRadius * 2, overflow: 'visible' }}>
  296. <Text className="duration_title2">{t('feature.common.overnight')}</Text>
  297. <Text className="duration_value2" style={{ color: ColorType.night }}>{nightDuration()}</Text>
  298. {
  299. (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>
  300. }
  301. {
  302. (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>
  303. }
  304. {
  305. (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>
  306. }
  307. {
  308. (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>
  309. }
  310. </View>
  311. </View>
  312. </View>
  313. </View>
  314. </Box>
  315. }
  316. function modalContent() {
  317. if (process.env.TARO_ENV == 'weapp') {
  318. return <Modal
  319. testInfo={null}
  320. dismiss={() => {
  321. setShowRing(false)
  322. }}
  323. confirm={() => { }}>
  324. {popDetail()}
  325. </Modal>
  326. }
  327. else if (process.env.TARO_ENV == 'rn') {
  328. return <PageContainer style={{ backgroundColor: '#1c1c1c' }}
  329. // overlayStyle='background-color:rgba(0,0,0,0.9)'
  330. // custom-style='background-color:#1c1c1c'
  331. overlayStyle={{ backgroundColor: 'rgba(0,0,0,0.9)' }}
  332. customStyle={{ backgroundColor: '#1c1c1c' }}
  333. closeOnSlideDown={false}
  334. onBeforeEnter={() => {
  335. }}
  336. onBeforeLeave={() => {
  337. }}
  338. onClick={() => { alert('b') }}
  339. onClickOverlay={() => { alert('a') }}
  340. onAfterLeave={() => { setShowRing(false) }}
  341. show={showRing} round={true} overlay={true} position='bottom'
  342. >
  343. {popDetail()}
  344. </PageContainer>
  345. }
  346. }
  347. return <Box onClick={tapShow}>
  348. <View style={{ color: '#fff' }}>显示同步生物钟</View>
  349. {
  350. showRing && modalContent()
  351. }
  352. </Box>
  353. }