From bd1dcd680946cc303e79d66ac8a13eda19e15192 Mon Sep 17 00:00:00 2001 From: Nikolaos Karaolidis Date: Sat, 15 Jul 2023 13:10:16 +0300 Subject: [PATCH] Refactor state Signed-off-by: Nikolaos Karaolidis --- .../terminallyonline/MainActivity.java | 8 +++++ index.js | 5 ++- src/navigation.tsx | 32 +++++++++---------- src/screens/home.tsx | 2 +- src/screens/tags.tsx | 2 +- src/state/settings.ts | 4 ++- src/types/index.ts | 1 + src/types/route.ts | 9 ++++++ 8 files changed, 42 insertions(+), 21 deletions(-) create mode 100644 src/types/route.ts diff --git a/android/app/src/main/java/com/karaolidis/terminallyonline/MainActivity.java b/android/app/src/main/java/com/karaolidis/terminallyonline/MainActivity.java index 3e00a12..bf777b6 100644 --- a/android/app/src/main/java/com/karaolidis/terminallyonline/MainActivity.java +++ b/android/app/src/main/java/com/karaolidis/terminallyonline/MainActivity.java @@ -5,6 +5,8 @@ import com.facebook.react.ReactActivityDelegate; import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint; import com.facebook.react.defaults.DefaultReactActivityDelegate; +import android.os.Bundle; + public class MainActivity extends ReactActivity { /** @@ -16,6 +18,12 @@ public class MainActivity extends ReactActivity { return "Terminally Online"; } + // react-native-screens override + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(null); + } + /** * Returns the instance of the {@link ReactActivityDelegate}. Here we use a util class {@link * DefaultReactActivityDelegate} which allows you to easily enable Fabric and Concurrent React diff --git a/index.js b/index.js index 0116efe..df06f51 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,9 @@ import { AppRegistry } from 'react-native'; +import 'react-native-get-random-values'; +import { enableFreeze } from 'react-native-screens'; import App from './src/app'; import { name as appName } from './app.json'; -import 'react-native-get-random-values'; + +enableFreeze(true); AppRegistry.registerComponent(appName, () => App); diff --git a/src/navigation.tsx b/src/navigation.tsx index dd0ae22..63dc544 100644 --- a/src/navigation.tsx +++ b/src/navigation.tsx @@ -10,10 +10,11 @@ import { BottomNavigation, useTheme } from 'react-native-paper'; import { Home, Tags, Settings, AddMeme, AddTag } from './screens'; import { darkNavigationTheme, lightNavigationTheme } from './theme'; import { FloatingActionButton } from './components'; +import { ROUTE } from './types'; const TabNavigator = () => { - const [showFab, setShowFab] = React.useState(true); const TabNavigatorBase = createBottomTabNavigator(); + const [showFab, setShowFab] = React.useState(true); return ( <> @@ -39,7 +40,7 @@ const TabNavigator = () => { target: state.key, }); } - setShowFab(route.name !== 'Settings'); + setShowFab((route.name as ROUTE) !== ROUTE.SETTINGS); }} renderIcon={({ route, focused, color }) => { const { options } = descriptors[route.key]; @@ -58,7 +59,7 @@ const TabNavigator = () => { /> )}> ( @@ -67,7 +68,7 @@ const TabNavigator = () => { }} /> ( @@ -76,7 +77,7 @@ const TabNavigator = () => { }} /> ( @@ -97,18 +98,15 @@ const NavigationContainer = () => { return ( - - - - + + + + ); diff --git a/src/screens/home.tsx b/src/screens/home.tsx index 93e52e6..09507bb 100644 --- a/src/screens/home.tsx +++ b/src/screens/home.tsx @@ -71,7 +71,7 @@ const Home = () => { }; const [search, setSearch] = useState(''); - const memes = useQuery('Meme'); + const memes = useQuery(Meme.schema.name); return ( diff --git a/src/screens/tags.tsx b/src/screens/tags.tsx index 4141017..3239fbc 100644 --- a/src/screens/tags.tsx +++ b/src/screens/tags.tsx @@ -60,7 +60,7 @@ const Tags = () => { }; const [search, setSearch] = useState(''); - const tags = useQuery('Tag') + const tags = useQuery(Tag.schema.name) .filtered(`name CONTAINS[c] "${search}"`) .sorted(tagSortQuery(sort), sortDirection === SORT_DIRECTION.ASCENDING); diff --git a/src/state/settings.ts b/src/state/settings.ts index f5e5deb..f5afae1 100644 --- a/src/state/settings.ts +++ b/src/state/settings.ts @@ -44,6 +44,8 @@ const updateStorageUri = createAsyncThunk( const updateNoMedia = createAsyncThunk( 'settings/updateNoMedia', async (newNoMedia: boolean, { dispatch, getState }) => { + dispatch(setNoMedia(newNoMedia)); + const state = getState() as RootState; const { storageUri } = state.settings; if (!storageUri) return; @@ -51,12 +53,12 @@ const updateNoMedia = createAsyncThunk( const noMediaExists = await FileSystem.exists( AndroidScoped.appendPath(storageUri, '.nomedia'), ); + if (newNoMedia && !noMediaExists) { await createFile(storageUri, '.nomedia', 'text/x-unknown'); } else if (!newNoMedia && noMediaExists) { await deleteFile(AndroidScoped.appendPath(storageUri, '.nomedia')); } - dispatch(setNoMedia(newNoMedia)); }, ); diff --git a/src/types/index.ts b/src/types/index.ts index c27df43..a1a8b18 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,3 +1,4 @@ +export { ROUTE } from './route'; export { HOME_SORT, homeSortQuery, diff --git a/src/types/route.ts b/src/types/route.ts new file mode 100644 index 0000000..92ca206 --- /dev/null +++ b/src/types/route.ts @@ -0,0 +1,9 @@ +enum ROUTE { + MAIN = 'Main', + HOME = 'Home', + TAGS = 'Tags', + SETTINGS = 'Settings', + ADD_MEME = 'Add Meme', + ADD_TAG = 'Add Tag', +} +export { ROUTE };