From df478cdd9b3a312d3dfbfe20a13caf9008610233 Mon Sep 17 00:00:00 2001 From: moist-webDev Date: Fri, 26 May 2023 00:12:54 -0400 Subject: [PATCH] theme and title functionality --- App.tsx | 78 +++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 67 insertions(+), 11 deletions(-) diff --git a/App.tsx b/App.tsx index ded288d..09ae86e 100644 --- a/App.tsx +++ b/App.tsx @@ -1,5 +1,5 @@ import { StatusBar } from 'expo-status-bar'; -import { StyleSheet, View} from 'react-native'; +import { StyleSheet, View, Appearance} from 'react-native'; import { NativeRouter, Route, Routes } from "react-router-native"; import Navbar from './components/Navbar'; import Home from './routes/Home'; @@ -7,31 +7,87 @@ import Movies from './routes/Movies'; import Shows from './routes/Shows'; import Other from './routes/Other'; import Settings from './routes/Settings'; -import {darkTheme, lightTheme} from './lib/theme'; +import {dark, light} from './lib/theme'; import { useEffect, useState } from 'react'; +import AsyncStorage from '@react-native-async-storage/async-storage'; +import TitleBar from './components/TitleBar'; +const findTheme = async () => { + const theme = await AsyncStorage.getItem('theme'); + if(theme === 'dark') { + return dark; + } else if(theme === 'light') { + return light; + } else { + const system = Appearance.getColorScheme(); + if (system === 'dark') { + return dark; + } else { + return light; + } + } +} + +const findThemeMode = async () => { + const theme = await AsyncStorage.getItem('theme'); + if (theme){ + return theme; + } else { + return 'auto'; + } +} export default function App() { - let [theme, setTheme] = useState(darkTheme); + let [theme, setTheme] = useState(light); + let [themeMode, setThemeMode] = useState('auto'); + let [title, setTitle] = useState('Home'); - const changeTheme = () => { - if (theme === darkTheme){ - setTheme(lightTheme); - } else { - setTheme(darkTheme); + useEffect(() => { + const fetchData = async () => { + const theme = await findTheme(); + const themeMode = await findThemeMode(); + setTheme(theme); + setThemeMode(themeMode); + } + fetchData() + }, []); + + const changeTheme = async (mode:string ) => { + switch(mode) { + case 'dark': + setThemeMode('dark'); + setTheme(dark); + await AsyncStorage.setItem('theme', 'dark'); + break; + case 'light': + setThemeMode('light'); + setTheme(light); + await AsyncStorage.setItem('theme', 'light'); + break; + default: + setThemeMode('auto'); + const system = Appearance.getColorScheme(); + if (system === 'dark') { + setTheme(dark); + } else { + setTheme(light); + } + await AsyncStorage.setItem('theme', "auto"); + break; } } return ( + }/> } /> } /> } /> - } /> + } /> - + @@ -43,7 +99,7 @@ export default function App() { const styles = StyleSheet.create({ mainContainer: { - paddingTop: 40, + paddingTop: 0, flex: 1, flexDirection: 'column', alignItems: 'baseline',