Skip to content

Commit

Permalink
Add addChangeListener to react to change in device's system theme
Browse files Browse the repository at this point in the history
Set initial theme to current device theme
Pick this fix facebook/react-native#28823
  • Loading branch information
rocknegi committed Feb 1, 2021
1 parent 1e0042f commit f81f020
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
10 changes: 6 additions & 4 deletions App.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import SplashScreen from './src/screens/SplashScreen';
import { createDrawerNavigator } from 'react-navigation-drawer';
import SelectedCategory from './src/screens/SelectedCategory';
import VideoPlayer from './src/components/VideoPlayer';
import { Appearance } from 'react-native';

MaterialCommunityIcons.loadFont();
Feather.loadFont();
Expand Down Expand Up @@ -57,7 +58,6 @@ const AppNavigator = createSwitchNavigator(
const AppContainer = createAppContainer(AppNavigator);

export default App = () => {

useEffect(() => {
const rewarded = RewardedAd.createForAdRequest(REWARDS, {
requestNonPersonalizedAdsOnly: false,
Expand All @@ -73,11 +73,13 @@ export default App = () => {
});

setTimeout(() => {
rewarded.load();
}, 5000)
// rewarded.load();
}, 5000);


}, []);

const themeHook = useState('light');
const themeHook = useState(Appearance.getColorScheme());
return (
<ThemeContext.Provider value={themeHook}>
<AppContainer />
Expand Down
20 changes: 13 additions & 7 deletions android/app/src/main/java/com/hksj/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ public class MainActivity extends ReactActivity {
protected String getMainComponentName() {
return "hksj";
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Intent intent = new Intent("onConfigurationChanged");
intent.putExtra("newConfig", newConfig);
this.sendBroadcast(intent);
}
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
getReactInstanceManager().onConfigurationChanged(this, newConfig);
}

// @Override
// public void onConfigurationChanged(Configuration newConfig) {
// super.onConfigurationChanged(newConfig);
// Intent intent = new Intent("onConfigurationChanged");
// intent.putExtra("newConfig", newConfig);
// this.sendBroadcast(intent);
// }

@Override
protected ReactActivityDelegate createReactActivityDelegate() {
Expand Down
18 changes: 15 additions & 3 deletions src/screens/CustomDrawerScreen.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useContext } from 'react';
import React, { useContext, useEffect } from 'react';
import { View, Text, FlatList, Switch, StyleSheet, Linking } from 'react-native';
import { Appearance } from 'react-native';

import ThemeContext from '../theme';
import { lightTheme } from '../theme/light-theme';
Expand All @@ -10,14 +11,25 @@ import { TouchableOpacity } from 'react-native-gesture-handler';

export default function CustomDrawerScreen({ navigation }) {
const [themeMode, setThemeMode] = useContext(ThemeContext);

useEffect(() => {
Appearance.addChangeListener(onThemeChange);

return () => Appearance.removeChangeListener(onThemeChange);
}, [])

const onThemeChange = () => {
setThemeMode(Appearance.getColorScheme());
}

const getTheme = (prop) => {
switch (prop) {
case 'icon':
return themeMode === 'light' ? '#000' : '#fff';
case 'text':
return themeMode === 'light' ? lightTheme.text : darkTheme.text;
case 'bgTint':
return themeMode === 'light' ? '#eee' : '#01CBC6';
return themeMode === 'light' ? '#eee' : '#424242';
default:
return null;
}
Expand Down Expand Up @@ -116,7 +128,7 @@ export default function CustomDrawerScreen({ navigation }) {
<Text style={themeMode === 'light' ? lightTheme.text : darkTheme.text}>
Black AF Theme
</Text>
<Switch thumbColor="#1a8a98" trackColor={{ false: '#e4e4e4', true: '#358c96' }} value={themeMode === 'dark'} onValueChange={toggleTheme} />
<Switch thumbColor="#00E676" trackColor={{ false: '#e4e4e4', true: '#424242' }} value={themeMode === 'dark'} onValueChange={toggleTheme} />
</View>
</CommonLayout>
);
Expand Down

0 comments on commit f81f020

Please sign in to comment.