MainSleepActiveCard.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. import { IconSwitch1, IconSwitch2 } from "@/components/basic/Icons";
  15. export default function MainSleepActiveCard(props: { count: any, typeChanged: Function }) {
  16. const [isSleepMode, setIsSleepMode] = useState(true)
  17. const startScheduleTime = '19:00'
  18. const endScheduleTime = '06:00'
  19. const user = useSelector((state: any) => state.user);
  20. const health = useSelector((state: any) => state.health);
  21. const dispatch = useDispatch()
  22. useEffect(() => {
  23. const { sleep } = health.windows.sleep_active
  24. if (isCurrentTimeInRange(sleep.period.start_time, sleep.period.end_time)) {
  25. setIsSleepMode(true)
  26. }
  27. else {
  28. setIsSleepMode(false)
  29. }
  30. }, [health.windows])
  31. useEffect(() => {
  32. }, [user.isLogin])
  33. useEffect(() => {
  34. if (health.selTab == 2) {
  35. dispatch(setMode(isSleepMode ? 'SLEEP' : 'ACTIVE'))
  36. }
  37. }, [health.selTab, isSleepMode])
  38. function formatTime(format: string, timestamp?: number) {
  39. return dayjs().format(format)
  40. }
  41. function switchMode() {
  42. var mode = !isSleepMode;
  43. setIsSleepMode(mode)
  44. props.typeChanged(mode ? WindowType.sleep : WindowType.active)
  45. }
  46. const common: RingCommon = {
  47. useCase: 'ChooseScenario',
  48. radius: 115,
  49. lineWidth: 26,
  50. isFast: true,
  51. status: 'WAIT_FOR_START'
  52. }
  53. const bgRing: BgRing = {
  54. color: '#EAE9E9'
  55. }
  56. function getArc() {
  57. const { sleep, active } = health.windows.sleep_active
  58. if (isSleepMode) {
  59. return startArc(sleep.target.start_timestamp)
  60. }
  61. return startArc(active.target.start_timestamp)
  62. }
  63. function getTargetArc() {
  64. const { sleep, active } = health.windows.sleep_active
  65. if (isSleepMode) {
  66. return durationArc(sleep.target.start_timestamp, sleep.target.end_timestamp)
  67. }
  68. return durationArc(active.target.start_timestamp, active.target.end_timestamp)
  69. }
  70. function getRealArc() {
  71. const { sleep, active } = health.windows.sleep_active
  72. if (isSleepMode) {
  73. return durationArc(sleep.target.start_timestamp, new Date().getTime())
  74. }
  75. return durationArc(active.target.start_timestamp, new Date().getTime())
  76. }
  77. function targetRing() {
  78. const color = isSleepMode ? '#D0C0FB' : '#FF498366'
  79. const startArc = getArc()
  80. const durationArc = getTargetArc()
  81. return {
  82. color,
  83. startArc,
  84. durationArc
  85. }
  86. }
  87. function realRing() {
  88. const color = isSleepMode ? MainColorType.sleep : MainColorType.active
  89. const { sleep, active } = health.windows.sleep_active
  90. if (isSleepMode) {
  91. if (!isCurrentTimeInRange(sleep.period.start_time, sleep.period.end_time)) {
  92. return null
  93. }
  94. }
  95. else {
  96. if (!isCurrentTimeInRange(active.period.start_time, active.period.end_time)) {
  97. return null
  98. }
  99. }
  100. return {
  101. color: color,
  102. startArc: getArc(),//-Math.PI / 2,
  103. durationArc: getRealArc()
  104. }
  105. // if (isSleepMode) {
  106. // if (isCurrentTimeInRange(startScheduleTime, endScheduleTime)) {
  107. // var starts: any = startScheduleTime.split(':')
  108. // const startSeconds = parseInt(starts[0] + '') * 60 + parseInt(starts[1] + '')
  109. // const color = MainColorType.sleep
  110. // const startArc = startSeconds / 1440 * 2 * Math.PI - Math.PI / 2
  111. // var endSeconds = new Date().getHours() * 60 + new Date().getMinutes()
  112. // const fastCount = endSeconds - startSeconds > 0 ? endSeconds - startSeconds : endSeconds - startSeconds + 1440
  113. // const durationArc = fastCount / 1440 * 2 * Math.PI
  114. // return {
  115. // color,
  116. // startArc,
  117. // durationArc
  118. // }
  119. // }
  120. // }
  121. // else {
  122. // if (isCurrentTimeInRange(endScheduleTime, startScheduleTime)) {
  123. // var starts: any = endScheduleTime.split(':')
  124. // const startSeconds = parseInt(starts[0] + '') * 60 + parseInt(starts[1] + '')
  125. // const color = MainColorType.active
  126. // const startArc = startSeconds / 1440 * 2 * Math.PI - Math.PI / 2
  127. // var endSeconds = new Date().getHours() * 60 + new Date().getMinutes()
  128. // const fastCount = endSeconds - startSeconds > 0 ? endSeconds - startSeconds : endSeconds - startSeconds + 1440
  129. // const durationArc = fastCount / 1440 * 2 * Math.PI
  130. // return {
  131. // color,
  132. // startArc,
  133. // durationArc
  134. // }
  135. // }
  136. // }
  137. }
  138. function ring() {
  139. var offset = 0
  140. var hour = new Date().getHours()
  141. var minute = new Date().getMinutes()
  142. if (hour != new Date().getHours() || minute != new Date().getMinutes()) {
  143. offset = hour * 60 + minute - new Date().getHours() * 60 - new Date().getMinutes()
  144. }
  145. const currentDot: CurrentDot = {
  146. color: isSleepMode ? MainColorType.sleep : MainColorType.active,
  147. lineWidth: 10,
  148. borderColor: '#fff',
  149. offset: offset,
  150. whiteIcon: true
  151. }
  152. return <Rings common={common} bgRing={bgRing} targetRing={targetRing()} realRing={realRing()} currentDot={currentDot} canvasId={'smal11lee'} />
  153. }
  154. function switchIcon() {
  155. const { sleep } = health.windows.sleep_active
  156. if (isSleepMode) {
  157. if (isCurrentTimeInRange(sleep.period.start_time, sleep.period.end_time)) {
  158. return <IconSwitch1 color="#000" width={15} />
  159. }
  160. return <IconSwitch2 color="#fff" width={15} />
  161. }
  162. if (isCurrentTimeInRange(sleep.period.start_time, sleep.period.end_time)) {
  163. return <IconSwitch2 color="#fff" width={15} />
  164. }
  165. return <IconSwitch1 color="#000" width={15} />
  166. }
  167. function switchBtn() {
  168. var bgColor = ''
  169. const { sleep } = health.windows.sleep_active
  170. if (isSleepMode) {
  171. if (isCurrentTimeInRange(sleep.period.start_time, sleep.period.end_time)) {
  172. bgColor = '#fff'
  173. }
  174. else {
  175. bgColor = MainColorType.active
  176. }
  177. }
  178. else {
  179. if (isCurrentTimeInRange(sleep.period.start_time, sleep.period.end_time)) {
  180. bgColor = MainColorType.sleep
  181. }
  182. else {
  183. bgColor = '#fff'
  184. }
  185. }
  186. return <View className="switch_btn" style={{ backgroundColor: bgColor }} onClick={switchMode}>
  187. {
  188. switchIcon()
  189. }
  190. </View>
  191. }
  192. return <View style={{ width: rpxToPx(750), display: 'flex', flexShrink: 0, flexDirection: 'column', alignItems: 'center' }}>
  193. <View style={{ position: 'relative', }}>
  194. {
  195. ring()
  196. }
  197. <View className="ring_center">
  198. <Text className="time1">{formatTime('HH:mm:ss')}</Text>
  199. <Text className="date1">{global.language == 'en' ? formatTime('dddd, MMM D') : formatTime('MMMD日 dddd')}</Text>
  200. </View>
  201. </View>
  202. {
  203. switchBtn()
  204. }
  205. </View>
  206. }