Disable system themes on unsupported platforms

This commit is contained in:
Bill Thornton 2021-03-05 16:37:35 -05:00
parent 6bb0a49870
commit 031a4ad370
3 changed files with 51 additions and 14 deletions

View File

@ -3,22 +3,23 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import React, { useContext, useEffect } from 'react';
import { Alert, AsyncStorage, Platform, SectionList, StyleSheet, View } from 'react-native';
import React, { useContext, useEffect } from 'react';
import { Text, ThemeContext } from 'react-native-elements';
import { SafeAreaView } from 'react-native-safe-area-context';
import { useNavigation } from '@react-navigation/native';
import { action } from 'mobx';
import { observer } from 'mobx-react';
import { SafeAreaView } from 'react-native-safe-area-context';
import { useNavigation } from '@react-navigation/native';
import { useTranslation } from 'react-i18next';
import AppInfoFooter from '../components/AppInfoFooter';
import BrowserListItem from '../components/BrowserListItem';
import ButtonListItem from '../components/ButtonListItem';
import ServerListItem from '../components/ServerListItem';
import SwitchListItem from '../components/SwitchListItem';
import { isSystemThemeSupported } from '../utils/Device';
import Links from '../constants/Links';
import Screens from '../constants/Screens';
import ServerListItem from '../components/ServerListItem';
import SwitchListItem from '../components/SwitchListItem';
import { useStores } from '../hooks/useStores';
const SettingsScreen = observer(() => {
@ -140,12 +141,14 @@ const SettingsScreen = observer(() => {
onValueChange: action(value => rootStore.settingStore.isTabLabelsEnabled = value)
});
settingsData.push({
key: 'system-theme-switch',
title: t('settings.systemTheme'),
value: rootStore.settingStore.isSystemThemeEnabled,
onValueChange: action(value => rootStore.settingStore.isSystemThemeEnabled = value)
});
if (isSystemThemeSupported()) {
settingsData.push({
key: 'system-theme-switch',
title: t('settings.systemTheme'),
value: rootStore.settingStore.isSystemThemeEnabled,
onValueChange: action(value => rootStore.settingStore.isSystemThemeEnabled = value)
});
}
// TODO: This should be able to select from a list not just a switch
settingsData.push({

View File

@ -3,9 +3,9 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import { Platform } from 'react-native';
import Constants from 'expo-constants';
import * as Device from 'expo-device';
import Constants from 'expo-constants';
import { Platform } from 'react-native';
import iOS10Profile from './profiles/ios10';
import iOS12Profile from './profiles/ios12';
@ -43,3 +43,9 @@ export function getDeviceProfile() {
// TODO: Add Android support
return {};
}
// Does the platform support system level themes: https://docs.expo.io/versions/latest/sdk/appearance/
export function isSystemThemeSupported() {
return (Platform.OS === 'ios' && parseInt(Platform.Version, 10) > 12) ||
(Platform.OS === 'android' && parseInt(Platform.Version, 10) > 9);
}

View File

@ -6,7 +6,7 @@
import Constants from 'expo-constants';
import { Platform } from 'react-native';
import { getAppName, getDeviceProfile, getSafeDeviceName } from '../Device';
import { getAppName, getDeviceProfile, getSafeDeviceName, isSystemThemeSupported } from '../Device';
import iOS10Profile from '../profiles/ios10';
import iOS12Profile from '../profiles/ios12';
import iOSProfile from '../profiles/ios';
@ -71,4 +71,32 @@ describe('Device', () => {
expect(getDeviceProfile()).toStrictEqual({});
});
});
describe('isSystemThemeSupported()', () => {
it('should return true for iOS 13+', () => {
Platform.Version = '13';
expect(isSystemThemeSupported()).toBe(true);
});
it('should return false for iOS 12 or less', () => {
Platform.Version = '12';
expect(isSystemThemeSupported()).toBe(false);
});
it('should return true for Android 10+', () => {
Platform.OS = 'android';
Platform.Version = '10';
expect(isSystemThemeSupported()).toBe(true);
});
it('should return false for Android 9 or less', () => {
Platform.OS = 'android';
Platform.Version = '9';
expect(isSystemThemeSupported()).toBe(false);
});
it('should return false on unsupported platforms', () => {
Platform.OS = 'web';
expect(isSystemThemeSupported()).toBe(false);
});
});
});