MainDayNightCard.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import { View, Text, ScrollView } 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, RealRing } from "@/features/trackTimeDuration/components/Rings";
  6. import dayjs from "dayjs";
  7. import 'dayjs/locale/zh-cn';
  8. import 'dayjs/locale/en';
  9. import momentT from 'moment';
  10. // import moment from 'moment-timezone'
  11. import { MainColorType } from "@/context/themes/color";
  12. import { useDispatch, useSelector } from "react-redux";
  13. import Calendar from "@/features/health/calendar";
  14. import { WindowStatusType, WindowType } from "@/utils/types";
  15. import { durationArc, isCurrentTimeInRange, startArc } from "./util";
  16. import { setMode } from "@/store/health";
  17. import { getScenario, getWindowStatus } from "./hooks/health_hooks";
  18. import { useTranslation } from "react-i18next";
  19. import { useRouter } from "@tarojs/taro";
  20. let useActionSheet;
  21. let useRoute;
  22. let useNavigation;
  23. if (process.env.TARO_ENV == 'rn') {
  24. useRoute = require("@react-navigation/native").useRoute
  25. useNavigation = require("@react-navigation/native").useNavigation
  26. useActionSheet = require('@expo/react-native-action-sheet').useActionSheet
  27. }
  28. export default function MainDayNightCard(props: {
  29. count: number,
  30. typeChanged: Function,
  31. id: number,
  32. onClick: Function,
  33. scale: number
  34. }) {
  35. const health = useSelector((state: any) => state.health);
  36. let router
  37. let navigation;
  38. if (useNavigation) {
  39. navigation = useNavigation()
  40. }
  41. if (process.env.TARO_ENV == 'rn') {
  42. router = useRoute()
  43. }
  44. else {
  45. router = useRouter()
  46. }
  47. const { day } = health.windows.night_day
  48. const isTempDay = () => {
  49. if (router.params.window == 'DAY') {
  50. return true
  51. }
  52. if (router.params.window == 'NIGHT') {
  53. return false
  54. }
  55. if (isCurrentTimeInRange(day.period.start_time, day.period.end_time)) {
  56. return true
  57. }
  58. return false
  59. }
  60. const [isDayMode, setIsDayMode] = useState(isTempDay)
  61. const user = useSelector((state: any) => state.user);
  62. const dispatch = useDispatch();
  63. const {t} = useTranslation()
  64. useEffect(() => {
  65. const { day } = health.windows.night_day
  66. if (isCurrentTimeInRange(day.period.start_time, day.period.end_time)) {
  67. setIsDayMode(true)
  68. }
  69. else {
  70. setIsDayMode(false)
  71. }
  72. }, [])
  73. useEffect(() => {
  74. if (health.selTab == 0) {
  75. dispatch(setMode(isDayMode ? 'DAY' : 'NIGHT'))
  76. }
  77. }, [health.selTab, isDayMode])
  78. useEffect(() => {
  79. if (health.mode == 'DAY') {
  80. debugger
  81. setIsDayMode(true)
  82. }
  83. else if (health.mode == 'NIGHT') {
  84. setIsDayMode(false)
  85. }
  86. }, [health.mode])
  87. useEffect(() => {
  88. dayjs.locale(global.language == 'en' ? 'en' : 'zh-cn');
  89. require('moment/locale/en-gb')
  90. require('moment/locale/zh-cn')
  91. momentT.locale(global.language == 'en' ? 'en-gb' : 'zh-cn')
  92. // moment.defineLocale(global.language == 'en' ? 'en-gb' : 'zh-cn', (momentT.localeData() as any)._config)
  93. // moment.locale(global.language == 'en' ? 'en-gb' : 'zh-cn')
  94. }, [props.count])
  95. const common: RingCommon = {
  96. useCase: 'ChooseScenario',
  97. radius: 27,
  98. lineWidth: 12,
  99. isFast: true,
  100. status: 'WAIT_FOR_START'
  101. }
  102. const bgRing: BgRing = {
  103. color: MainColorType.ringBg
  104. }
  105. function getTargetArc() {
  106. const { day, night } = health.windows.night_day
  107. if (isDayMode) {
  108. return durationArc(day.target.start_timestamp, day.target.end_timestamp)
  109. }
  110. return durationArc(night.target.start_timestamp, night.target.end_timestamp)
  111. }
  112. function getStartRealArc() {
  113. const { day, night } = health.windows.night_day
  114. const timestamp = new Date().getTime()
  115. if (isDayMode) {
  116. if (timestamp >= day.target.start_timestamp && timestamp <= day.target.end_timestamp)
  117. return startArc(day.target.start_timestamp)
  118. }
  119. else {
  120. if (timestamp >= night.target.start_timestamp && timestamp <= night.target.end_timestamp)
  121. return startArc(night.target.start_timestamp)
  122. }
  123. return startArc(timestamp)
  124. }
  125. function getRealArc() {
  126. const status = getWindowStatus(health.windows, isDayMode ? 'DAY' : 'NIGHT')
  127. if (status == WindowStatusType.upcoming) {
  128. const scenario = getScenario(health.windows, isDayMode ? 'DAY' : 'NIGHT')
  129. return durationArc(new Date().getTime(), scenario.target.start_timestamp)
  130. }
  131. const { day, night } = health.windows.night_day
  132. if (isDayMode) {
  133. return durationArc(day.target.start_timestamp, new Date().getTime())
  134. }
  135. return durationArc(night.target.start_timestamp, new Date().getTime())
  136. }
  137. function getArc() {
  138. const { day, night } = health.windows.night_day
  139. if (isDayMode) {
  140. return startArc(day.target.start_timestamp)
  141. }
  142. return startArc(night.target.start_timestamp)
  143. }
  144. function currentDot() {
  145. if (health.mode == 'DAY' || health.mode == 'NIGHT') {
  146. const status = getWindowStatus(health.windows, isDayMode ? 'DAY' : 'NIGHT')
  147. return {
  148. color: status == WindowStatusType.upcoming?'#B2B2B2':isDayMode ? MainColorType.day : MainColorType.night,
  149. lineWidth: 4,
  150. borderColor: '#F5F5F5',
  151. offset: 0
  152. }
  153. }
  154. return null;
  155. }
  156. function ring() {
  157. var offset = 0
  158. var hour = new Date().getHours()
  159. var minute = new Date().getMinutes()
  160. if (hour != new Date().getHours() || minute != new Date().getMinutes()) {
  161. offset = hour * 60 + minute - new Date().getHours() * 60 - new Date().getMinutes()
  162. }
  163. var targetColor: string = isDayMode ? MainColorType.dayLight : MainColorType.nightLight
  164. var realColor: string = isDayMode ? MainColorType.day : MainColorType.night
  165. const status = getWindowStatus(health.windows, isDayMode ? 'DAY' : 'NIGHT')
  166. if (status == WindowStatusType.upcoming) {
  167. realColor = '#CCCCCC'
  168. }
  169. const targetRing: TargetRing = {
  170. color: targetColor,
  171. startArc: getArc(),//-Math.PI / 2,
  172. durationArc: getTargetArc()
  173. }
  174. let realRing: RealRing = {
  175. color: realColor,
  176. startArc: getStartRealArc(),//-Math.PI / 2,
  177. durationArc: getRealArc()
  178. }
  179. if (status == WindowStatusType.upcoming){
  180. realRing = null
  181. }
  182. return <Rings
  183. common={common}
  184. bgRing={bgRing}
  185. targetRing={targetRing}
  186. realRing={realRing}
  187. canvasId={'smal1w1l' + props.id}
  188. currentDot={currentDot()}
  189. scale={props.scale??1.0}
  190. showCurrentDotAnimation={false}
  191. />
  192. }
  193. return <View onClick={() => props.onClick()} style={{ width: rpxToPx(634 / 3), display: 'flex', flexShrink: 0, flexDirection: 'column', alignItems: 'center', position: 'relative' }}>
  194. <View style={{ position: 'relative' }}>
  195. {
  196. ring()
  197. }
  198. <View className={health.selTab == 0 ? 'window_name window_name_sel' : 'window_name g01'}>{isDayMode ? t('health.window_day') : t('health.night')}</View>
  199. </View>
  200. </View>
  201. }