setting.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import { View, Text, Switch, ScrollView } from "@tarojs/components";
  2. import Metric from "@/features/trackSomething/components/Metric";
  3. import { useDidShow, usePullDownRefresh, useShareAppMessage } from "@tarojs/taro";
  4. import { useTranslation } from "react-i18next";
  5. import Segment from '@/components/navigation/Segment'
  6. import { useEffect, useState } from "react";
  7. import { kIsAndroid, rpxToPx } from "@/utils/tools";
  8. import './setting.scss'
  9. import { getNotifySettings, postNotifySettings } from "@/services/notifications";
  10. import { ColorType } from "@/context/themes/color";
  11. import { getLocalPush } from "@/features/trackTimeDuration/actions/TrackTimeActions";
  12. import { useSelector } from "react-redux";
  13. import ProductList from "../store/product_list";
  14. import { jumpPage } from "@/features/trackTimeDuration/hooks/Common";
  15. let useNavigation;
  16. let AppState;
  17. if (process.env.TARO_ENV == 'rn') {
  18. AppState = require("react-native").AppState
  19. useNavigation = require("@react-navigation/native").useNavigation
  20. }
  21. export default function Page() {
  22. const { t } = useTranslation()
  23. const [segmentIndex, setSegmentIndex] = useState(0)
  24. const [notification, setNotification] = useState<any>(null)
  25. const [isExtra, setIsExtra] = useState(false)
  26. const [isSunrise, setIsSunrise] = useState(false)
  27. const [isSunset, setIsSunset] = useState(false)
  28. const [isSolarNoon, setIsSolarNoon] = useState(false)
  29. const [loaded, setLoaded] = useState(false)
  30. const [systemFast, setSystemFast] = useState(true)
  31. const [systemExtra, setSystemExtra] = useState(true)
  32. const [systemSun, setSystemSun] = useState(true)
  33. const user = useSelector((state: any) => state.user);
  34. let navigation;
  35. if (useNavigation) {
  36. navigation = useNavigation()
  37. }
  38. // useEffect(() => {
  39. // }, [])
  40. useEffect(()=>{
  41. checkSetting()
  42. getSettings()
  43. if (process.env.TARO_ENV == 'rn') {
  44. AppState.addEventListener('change', handleAppStateChange);
  45. // AppState.addEventListener('change', handleAppStateChange);
  46. // navigation.addListener('focus', () => {
  47. // checkSetting()
  48. // });
  49. // // 当页面即将消失时执行
  50. // navigation.addListener('blur', () => {
  51. // console.log('notfication setting blur')
  52. // });
  53. }
  54. },[user.isLogin])
  55. const handleAppStateChange = (nextAppState) => {
  56. console.log(nextAppState)
  57. if (nextAppState != 'active') {
  58. return
  59. }
  60. if (nextAppState == 'active') {
  61. checkSetting()
  62. }
  63. };
  64. useDidShow(() => {
  65. console.log('notfication setting user did show')
  66. })
  67. function getSettings() {
  68. getNotifySettings().then(res => {
  69. var dt = (res as any).notification
  70. setNotification(dt)
  71. setIsExtra(dt.fast_sleep.extra_reminders.in_app == 'ON')
  72. setIsSunrise(dt.follow_sun.sunrise.in_app == 'ON')
  73. setIsSunset(dt.follow_sun.sunset.in_app == 'ON')
  74. setIsSolarNoon(dt.follow_sun.solar_noon.in_app == 'ON')
  75. setLoaded(true)
  76. })
  77. }
  78. function checkSetting() {
  79. console.log('notification setting begin')
  80. if (process.env.TARO_ENV == 'rn' && kIsAndroid) {
  81. var Jto = require('react-native').NativeModules.NativeBridge;
  82. Jto.getChannelStatus().then(result => {
  83. var data = JSON.parse(result);
  84. console.log('notification setting', data)
  85. if (data.all) {
  86. }
  87. else {
  88. var REMINDER_FS_STATUS = ''
  89. var REMINDER_SUN_STATUS = ''
  90. var REMINDER_FS_EXTRA_STATUS = ''
  91. if ('REMINDER_FS' in data) {
  92. REMINDER_FS_STATUS = data.REMINDER_FS ? 'ON' : 'OFF'
  93. setSystemFast(data.REMINDER_FS)
  94. }
  95. else {
  96. REMINDER_FS_STATUS = 'NA'
  97. }
  98. if ('REMINDER_SUN' in data) {
  99. REMINDER_SUN_STATUS = data.REMINDER_SUN ? 'ON' : 'OFF'
  100. setSystemSun(data.REMINDER_SUN)
  101. }
  102. else {
  103. REMINDER_SUN_STATUS = 'NA'
  104. }
  105. if ('REMINDER_FS_EXTRA' in data) {
  106. REMINDER_FS_EXTRA_STATUS = data.REMINDER_FS_EXTRA ? 'ON' : 'OFF'
  107. setSystemExtra(data.REMINDER_FS_EXTRA)
  108. }
  109. else {
  110. REMINDER_FS_EXTRA_STATUS = 'NA'
  111. }
  112. postNotifySettings({
  113. channels: {
  114. REMINDER_FS: {
  115. system: REMINDER_FS_STATUS
  116. },
  117. REMINDER_SUN: {
  118. system: REMINDER_SUN_STATUS
  119. },
  120. REMINDER_FS_EXTRA: {
  121. system: REMINDER_FS_EXTRA_STATUS
  122. }
  123. }
  124. }).then(res => {
  125. })
  126. }
  127. })
  128. }
  129. }
  130. function goSetting() {
  131. if (process.env.TARO_ENV == 'rn' && kIsAndroid) {
  132. var Jto = require('react-native').NativeModules.NativeBridge;
  133. Jto.openNotificationSettings()
  134. }
  135. }
  136. function free() {
  137. return <View className="setting_container">
  138. <View className="setting_section">
  139. <Text className="setting_section_title">Fasting</Text>
  140. </View>
  141. <Text className="setting_header">Reminders</Text>
  142. <View className="setting_cell">
  143. <Text className="setting_cell_title" style={{ flex: 1 }}>At your scheduled time</Text>
  144. <Text className="setting_cell_value1">{notification.fast_sleep.reminders.in_app}</Text>
  145. </View>
  146. <Text className="setting_footer">A timely reminder so you never miss your scheduled time for fasting.</Text>
  147. </View>
  148. }
  149. function iap(){
  150. jumpPage('','ProductList',navigation)
  151. }
  152. function pro() {
  153. return <ScrollView style={{ flex: 1 }}>
  154. {/* <ProductList /> */}
  155. {/* <Text style={{fontSize:30,color:'#fff'}} onClick={iap}>iap test</Text> */}
  156. <View className="setting_container">
  157. <View className="setting_section">
  158. <Text className="setting_section_title">Fasting with/or Sleep</Text>
  159. </View>
  160. <Text className="setting_header">Reminders</Text>
  161. <View className="setting_cell">
  162. <Text className="setting_cell_title" style={{ flex: 1 }}>At my scheduled time</Text>
  163. <Text className="setting_cell_value1">{systemFast ? 'Always' : 'Off'}</Text>
  164. </View>
  165. <Text className="setting_footer">A timely reminder so you never miss your scheduled time for fasting or sleep.</Text>
  166. <Text className="setting_header">Extra Reminders</Text>
  167. <View className="setting_cell">
  168. <Text className="setting_cell_title" style={{ flex: 1 }}>If I missed previous action</Text>
  169. <Switch className="myswitch" checked={isExtra} color={ColorType.fast} onChange={(e) => {
  170. // setIsMulti(e.detail.value)
  171. postNotifySettings({
  172. notification: {
  173. fast_sleep: {
  174. extra_reminders: {
  175. in_app: e.detail.value ? 'ON' : 'OFF'
  176. }
  177. }
  178. }
  179. }).then(res => {
  180. setIsExtra(e.detail.value)
  181. getLocalPush()
  182. })
  183. }} />
  184. </View>
  185. <Text className="setting_footer">In cases when you missed a previous action, you receive another reminder to log it together with the current action. This gives you extra protection against any streak loss.</Text>
  186. <View className="setting_section">
  187. <Text className="setting_section_title">Follow the Sun (PRO)</Text>
  188. </View>
  189. <Text className="setting_header">Reminders for Your Daily Local Sun Schedule</Text>
  190. <View className="setting_cell_group">
  191. <View className="setting_cell_group_item">
  192. <Text className="setting_cell_title" style={{ flex: 1 }}>Sunrise</Text>
  193. <Switch className="myswitch" checked={isSunrise} color={ColorType.fast} onChange={(e) => {
  194. // setIsMulti(e.detail.value)
  195. postNotifySettings({
  196. notification: {
  197. follow_sun: {
  198. sunrise: {
  199. in_app: e.detail.value ? 'ON' : 'OFF'
  200. }
  201. }
  202. }
  203. }).then(res => {
  204. setIsSunrise(e.detail.value)
  205. global.swiperDayNightRefresh()
  206. })
  207. }} />
  208. </View>
  209. <View className="setting_cell_group_item">
  210. <Text className="setting_cell_title" style={{ flex: 1 }}>Sunset</Text>
  211. <Switch className="myswitch" checked={isSunset} color={ColorType.fast} onChange={(e) => {
  212. // setIsMulti(e.detail.value)
  213. postNotifySettings({
  214. notification: {
  215. follow_sun: {
  216. sunset: {
  217. in_app: e.detail.value ? 'ON' : 'OFF'
  218. }
  219. }
  220. }
  221. }).then(res => {
  222. setIsSunset(e.detail.value)
  223. global.swiperDayNightRefresh()
  224. })
  225. }} />
  226. </View>
  227. <View className="setting_cell_group_item">
  228. <Text className="setting_cell_title" style={{ flex: 1 }}>Solar Noon</Text>
  229. <Switch className="myswitch" checked={isSolarNoon} color={ColorType.fast} onChange={(e) => {
  230. // setIsMulti(e.detail.value)
  231. postNotifySettings({
  232. notification: {
  233. follow_sun: {
  234. solar_noon: {
  235. in_app: e.detail.value ? 'ON' : 'OFF'
  236. }
  237. }
  238. }
  239. }).then(res => {
  240. setIsSolarNoon(e.detail.value)
  241. global.swiperDayNightRefresh()
  242. })
  243. }} />
  244. </View>
  245. </View>
  246. <Text className="setting_footer">Note if live in polar region, during time of Polar Day (Midnight Sun) when the Sun is up all day and during time of Polar Night when the Sun is down all day, the only reminder available is for daily Solar Noon.</Text>
  247. </View>
  248. </ScrollView>
  249. }
  250. function checkSystemChannel() {
  251. if (!systemFast) {
  252. return true;
  253. }
  254. if (!systemExtra && isExtra) {
  255. return true;
  256. }
  257. if (!systemSun && (isSolarNoon || isSunrise || isSunset)) {
  258. return true;
  259. }
  260. return false;
  261. }
  262. return <View className="container" style={{ flex: 1 }}>
  263. {/* <View className='setting_segment'>
  264. <Segment titles={[
  265. `Free`,
  266. `Pro`,
  267. ]}
  268. changed={(e) => {
  269. setSegmentIndex(e);
  270. // global.segmentIndex = e
  271. // console.log('segment item click', e)
  272. }} />
  273. </View> */}
  274. {
  275. loaded && pro()
  276. }
  277. {
  278. process.env.TARO_ENV == 'rn' && kIsAndroid && checkSystemChannel() && <View className="setting_tip" onClick={goSetting}>
  279. <Text className="setting_tip_text">Jump to App's Notifications settings{'>>'}</Text>
  280. </View>
  281. }
  282. </View>
  283. }