From 47e6ca5e59363f674ce1eacf15f33709e72c29e2 Mon Sep 17 00:00:00 2001 From: Zhiyuan Zheng Date: Wed, 15 Dec 2021 23:08:51 +0100 Subject: [PATCH] Push not available scenario --- src/i18n/en/screens/tabs.json | 1 + src/i18n/zh-Hans/screens/tabs.json | 1 + src/screens/Tabs/Me/Push.tsx | 205 +++++++++++++++++------------ 3 files changed, 125 insertions(+), 82 deletions(-) diff --git a/src/i18n/en/screens/tabs.json b/src/i18n/en/screens/tabs.json index ddb1cbfb..d19afc80 100644 --- a/src/i18n/en/screens/tabs.json +++ b/src/i18n/en/screens/tabs.json @@ -144,6 +144,7 @@ } }, "push": { + "notAvailable": "Your phone does not support tooot's push notification", "enable": { "direct": "Enable push notification", "settings": "Enable in settings" diff --git a/src/i18n/zh-Hans/screens/tabs.json b/src/i18n/zh-Hans/screens/tabs.json index 0671f3d4..95e2e6a1 100644 --- a/src/i18n/zh-Hans/screens/tabs.json +++ b/src/i18n/zh-Hans/screens/tabs.json @@ -144,6 +144,7 @@ } }, "push": { + "notAvailable": "您的机型暂不支持 tooot 推送通知", "enable": { "direct": "启用推送通知", "settings": "在系统设置中启用" diff --git a/src/screens/Tabs/Me/Push.tsx b/src/screens/Tabs/Me/Push.tsx index 75e6e26f..810c3366 100644 --- a/src/screens/Tabs/Me/Push.tsx +++ b/src/screens/Tabs/Me/Push.tsx @@ -1,5 +1,6 @@ import analytics from '@components/analytics' import Button from '@components/Button' +import Icon from '@components/Icon' import { MenuContainer, MenuRow } from '@components/Menu' import { updateInstancePush } from '@utils/slices/instances/updatePush' import { updateInstancePushAlert } from '@utils/slices/instances/updatePushAlert' @@ -12,14 +13,16 @@ import { } from '@utils/slices/instancesSlice' import { StyleConstants } from '@utils/styles/constants' import layoutAnimation from '@utils/styles/layoutAnimation' +import { useTheme } from '@utils/styles/ThemeManager' import * as Notifications from 'expo-notifications' import * as WebBrowser from 'expo-web-browser' import React, { useState, useEffect, useMemo } from 'react' import { useTranslation } from 'react-i18next' -import { AppState, Linking, ScrollView } from 'react-native' +import { AppState, Linking, ScrollView, Text, View } from 'react-native' import { useDispatch, useSelector } from 'react-redux' const TabMePush: React.FC = () => { + const { theme } = useTheme() const { t } = useTranslation('screenTabs') const instanceAccount = useSelector( getInstanceAccount, @@ -30,6 +33,9 @@ const TabMePush: React.FC = () => { const dispatch = useDispatch() const instancePush = useSelector(getInstancePush) + const [pushAvailable, setPushAvailable] = useState( + undefined + ) const [pushEnabled, setPushEnabled] = useState() const [pushCanAskAgain, setPushCanAskAgain] = useState() const checkPush = async () => { @@ -39,10 +45,15 @@ const TabMePush: React.FC = () => { setPushCanAskAgain(settings.canAskAgain) } useEffect(() => { + Notifications.getExpoPushTokenAsync({ + experienceId: '@xmflsct/tooot' + }) + .then(data => setPushAvailable(!!data)) + .catch(() => setPushAvailable(false)) checkPush() - AppState.addEventListener('change', checkPush) + const subscription = AppState.addEventListener('change', checkPush) return () => { - AppState.removeEventListener('change', checkPush) + subscription.remove() } }, []) @@ -54,13 +65,15 @@ const TabMePush: React.FC = () => { const alerts = useMemo(() => { return instancePush?.alerts - ? (['follow', 'favourite', 'reblog', 'mention', 'poll'] as [ - 'follow', - 'favourite', - 'reblog', - 'mention', - 'poll' - ]).map(alert => ( + ? ( + ['follow', 'favourite', 'reblog', 'mention', 'poll'] as [ + 'follow', + 'favourite', + 'reblog', + 'mention', + 'poll' + ] + ).map(alert => ( { return ( - {pushEnabled === false ? ( - -