MainSleepActiveCard.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import { View, Text } from "@tarojs/components";
  2. import './MainCard.scss'
  3. import { useEffect, useState } from "react";
  4. import { rpxToPx } from "@/utils/tools";
  5. import Rings, { RingCommon, BgRing, TargetRing, CurrentDot } from "@/features/trackTimeDuration/components/Rings";
  6. import dayjs from "dayjs";
  7. import moment from 'moment-timezone'
  8. import { MainColorType } from "@/context/themes/color";
  9. import { useDispatch, useSelector } from "react-redux";
  10. import { sleepWindow } from "@/services/trackTimeDuration";
  11. import { WindowType } from "@/utils/types";
  12. import { durationArc, isCurrentTimeInRange, startArc } from "./util";
  13. import { setMode } from "@/store/health";
  14. export default function MainSleepActiveCard(props: { count: any, typeChanged: Function }) {
  15. const [isSleepMode, setIsSleepMode] = useState(true)
  16. const startScheduleTime = '19:00'
  17. const endScheduleTime = '06:00'
  18. const user = useSelector((state: any) => state.user);
  19. const health = useSelector((state: any) => state.health);
  20. const dispatch = useDispatch()
  21. useEffect(() => {
  22. const { sleep } = health.windows.sleep_active
  23. if (isCurrentTimeInRange(sleep.schedule.start_time, sleep.schedule.end_time)) {
  24. setIsSleepMode(true)
  25. }
  26. else {
  27. setIsSleepMode(false)
  28. }
  29. }, [health.windows])
  30. useEffect(() => {
  31. }, [user.isLogin])
  32. useEffect(() => {
  33. if (health.selTab == 2) {
  34. dispatch(setMode(isSleepMode ? 'SLEEP' : 'ACTIVE'))
  35. }
  36. }, [health.selTab,isSleepMode])
  37. function formatTime(format: string, timestamp?: number) {
  38. return dayjs().format(format)
  39. }
  40. function switchMode() {
  41. var mode = !isSleepMode;
  42. setIsSleepMode(mode)
  43. props.typeChanged(mode ? WindowType.sleep : WindowType.active)
  44. }
  45. const common: RingCommon = {
  46. useCase: 'ChooseScenario',
  47. radius: 115,
  48. lineWidth: 26,
  49. isFast: true,
  50. status: 'WAIT_FOR_START'
  51. }
  52. const bgRing: BgRing = {
  53. color: '#EAE9E9'
  54. }
  55. function getArc() {
  56. const { sleep, active } = health.windows.sleep_active
  57. if (isSleepMode) {
  58. return startArc(sleep.target.start_timestamp)
  59. }
  60. return startArc(active.target.start_timestamp)
  61. }
  62. function getTargetArc() {
  63. const { sleep, active } = health.windows.sleep_active
  64. if (isSleepMode) {
  65. return durationArc(sleep.target.start_timestamp, sleep.target.end_timestamp)
  66. }
  67. return durationArc(active.target.start_timestamp, active.target.end_timestamp)
  68. }
  69. function getRealArc() {
  70. const { sleep, active } = health.windows.sleep_active
  71. if (isSleepMode) {
  72. return durationArc(sleep.target.start_timestamp, new Date().getTime())
  73. }
  74. return durationArc(active.target.start_timestamp, new Date().getTime())
  75. }
  76. function targetRing() {
  77. const color = isSleepMode ? '#D0C0FB' : '#FF498366'
  78. const startArc = getArc()
  79. const durationArc = getTargetArc()
  80. return {
  81. color,
  82. startArc,
  83. durationArc
  84. }
  85. }
  86. function realRing() {
  87. const color = isSleepMode?MainColorType.sleep:MainColorType.active
  88. const { sleep,active } = health.windows.sleep_active
  89. if (isSleepMode){
  90. if (!isCurrentTimeInRange(sleep.schedule.start_time, sleep.schedule.end_time)){
  91. return null
  92. }
  93. }
  94. else {
  95. if (!isCurrentTimeInRange(active.schedule.start_time, active.schedule.end_time)){
  96. return null
  97. }
  98. }
  99. return {
  100. color: color,
  101. startArc: getArc(),//-Math.PI / 2,
  102. durationArc: getRealArc()
  103. }
  104. // if (isSleepMode) {
  105. // if (isCurrentTimeInRange(startScheduleTime, endScheduleTime)) {
  106. // var starts: any = startScheduleTime.split(':')
  107. // const startSeconds = parseInt(starts[0] + '') * 60 + parseInt(starts[1] + '')
  108. // const color = MainColorType.sleep
  109. // const startArc = startSeconds / 1440 * 2 * Math.PI - Math.PI / 2
  110. // var endSeconds = new Date().getHours() * 60 + new Date().getMinutes()
  111. // const fastCount = endSeconds - startSeconds > 0 ? endSeconds - startSeconds : endSeconds - startSeconds + 1440
  112. // const durationArc = fastCount / 1440 * 2 * Math.PI
  113. // return {
  114. // color,
  115. // startArc,
  116. // durationArc
  117. // }
  118. // }
  119. // }
  120. // else {
  121. // if (isCurrentTimeInRange(endScheduleTime, startScheduleTime)) {
  122. // var starts: any = endScheduleTime.split(':')
  123. // const startSeconds = parseInt(starts[0] + '') * 60 + parseInt(starts[1] + '')
  124. // const color = MainColorType.active
  125. // const startArc = startSeconds / 1440 * 2 * Math.PI - Math.PI / 2
  126. // var endSeconds = new Date().getHours() * 60 + new Date().getMinutes()
  127. // const fastCount = endSeconds - startSeconds > 0 ? endSeconds - startSeconds : endSeconds - startSeconds + 1440
  128. // const durationArc = fastCount / 1440 * 2 * Math.PI
  129. // return {
  130. // color,
  131. // startArc,
  132. // durationArc
  133. // }
  134. // }
  135. // }
  136. }
  137. function ring() {
  138. var offset = 0
  139. var hour = new Date().getHours()
  140. var minute = new Date().getMinutes()
  141. if (hour != new Date().getHours() || minute != new Date().getMinutes()) {
  142. offset = hour * 60 + minute - new Date().getHours() * 60 - new Date().getMinutes()
  143. }
  144. const currentDot: CurrentDot = {
  145. color: isSleepMode ? MainColorType.sleep : MainColorType.active,
  146. lineWidth: 10,
  147. borderColor: '#fff',
  148. offset: offset,
  149. whiteIcon: true
  150. }
  151. return <Rings common={common} bgRing={bgRing} targetRing={targetRing()} realRing={realRing()} currentDot={currentDot} canvasId={'smal11lee'} />
  152. }
  153. return <View style={{ width: rpxToPx(750), display: 'flex', flexShrink: 0, flexDirection: 'column', alignItems: 'center' }}>
  154. <View style={{ position: 'relative', }}>
  155. {
  156. ring()
  157. }
  158. <View className="ring_center">
  159. <Text className="time1">{formatTime('HH:mm:ss')}</Text>
  160. <Text className="date1">{global.language == 'en' ? formatTime('dddd, MMM D') : formatTime('MMMD日 dddd')}</Text>
  161. </View>
  162. </View>
  163. <Text onClick={switchMode}>Switch</Text>
  164. </View>
  165. }