Skip to content

Commit

Permalink
Preparations for debug menu
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderRedYT committed Feb 4, 2024
1 parent 6d0a23a commit 7561a58
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 2 deletions.
31 changes: 31 additions & 0 deletions src/hooks/useRequireMultiplePresses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useCallback, useRef, useState } from 'react';

const useRequireMultiplePresses = (
onPress: () => void,
requiredPresses = 3,
timeout = 500,
) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [_, setPresses] = useState(0);
const timeoutRef = useRef<NodeJS.Timeout>();

return useCallback(() => {
setPresses(prevPresses => {
if (prevPresses === 0) {
timeoutRef.current = setTimeout(() => {
setPresses(0);
}, timeout);
}

if (prevPresses + 1 === requiredPresses) {
clearTimeout(timeoutRef.current);
onPress();
return 0;
}

return prevPresses + 1;
});
}, [onPress, requiredPresses, timeout]);
};

export default useRequireMultiplePresses;
12 changes: 12 additions & 0 deletions src/hooks/useSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { EqualityFn } from 'react-redux';

import type { SettingsState } from '@/types/settings';

import { useAppSelector } from '@/store';

const useSettings = <T>(
selector: (state: SettingsState) => T,
equalityFn?: EqualityFn<T>,
): T => useAppSelector(state => selector(state.settings), equalityFn);

export default useSettings;
7 changes: 6 additions & 1 deletion src/slices/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type {
UpdateDtuHostnameAction,
UpdateDtuSerialNumberAction,
SetLanguageAction,
EnableAppUpdatesAction,
EnableAppUpdatesAction, DebugEnabledAction,

Check failure on line 21 in src/slices/settings.ts

View workflow job for this annotation

GitHub Actions / Run Tests

Insert `⏎·`
} from '@/types/settings';

const initialState: SettingsState = {
Expand All @@ -28,6 +28,7 @@ const initialState: SettingsState = {
selectedDtuConfig: null,
databaseConfigs: [],
enableAppUpdates: null,
debugEnabled: false,
};

const log = logger.createLogger();
Expand Down Expand Up @@ -209,6 +210,9 @@ const settingsSlice = createSlice({
setEnableAppUpdates: (state, action: EnableAppUpdatesAction) => {
state.enableAppUpdates = action.payload.enable;
},
setDebugEnabled: (state, action: DebugEnabledAction) => {
state.debugEnabled = action.payload.debugEnabled;
},
},
});

Expand All @@ -231,6 +235,7 @@ export const {
updateDatabaseConfig,
updateDTUDatabaseUuid,
setEnableAppUpdates,
setDebugEnabled,
} = settingsSlice.actions;

export const { reducer: SettingsReducer } = settingsSlice;
Expand Down
5 changes: 5 additions & 0 deletions src/types/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface SettingsState {
appTheme: 'light' | 'dark' | 'system';
language: 'en' | 'de';
enableAppUpdates: boolean | null;
debugEnabled: boolean;

// opendtu
dtuConfigs: OpenDTUConfig[];
Expand Down Expand Up @@ -105,3 +106,7 @@ export type UpdateDTUDatabaseUuidAction = PayloadAction<{
export type EnableAppUpdatesAction = PayloadAction<{
enable: boolean;
}>;

export type DebugEnabledAction = PayloadAction<{
debugEnabled: boolean;
}>;
6 changes: 6 additions & 0 deletions src/views/navigation/NavigationStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import SelectDatabaseScreen from '@/views/navigation/screens/SelectDatabaseScree
import SetupAddOpenDTUScreen from '@/views/navigation/screens/SetupAddOpenDTUScreen';
import SetupAuthenticateOpenDTUInstanceScreen from '@/views/navigation/screens/SetupAuthenticateOpenDTUInstanceScreen';
import SetupOpenDTUCompleteScreen from '@/views/navigation/screens/SetupOpenDTUCompleteScreen';

Check failure on line 25 in src/views/navigation/NavigationStack.tsx

View workflow job for this annotation

GitHub Actions / Run Tests

Delete `Screen';⏎import·DebugScreen·from·'@/views/navigation/screens/Debug`
import DebugScreen from '@/views/navigation/screens/DebugScreen';

export type PropsWithNavigation = {
navigation: NavigationProp<ParamListBase>;
Expand Down Expand Up @@ -119,6 +120,11 @@ const NavigationStack: FC = () => {
component={LicensesScreen}
options={{ headerBackVisible: true }}
/>
<Stack.Screen
name="DebugScreen"
component={DebugScreen}
options={{ headerBackVisible: true }}
/>
</Stack.Navigator>
);
};
Expand Down
16 changes: 16 additions & 0 deletions src/views/navigation/screens/DebugScreen.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { FC } from 'react';
import { Appbar } from 'react-native-paper';

import type { PropsWithNavigation } from '@/views/navigation/NavigationStack';

const DebugScreen: FC<PropsWithNavigation> = () => {
return (
<>
<Appbar.Header>
<Appbar.Content title="Debug" />
</Appbar.Header>
</>
);
};

export default DebugScreen;
30 changes: 29 additions & 1 deletion src/views/navigation/tabs/MainSettingsTab.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
import type { NavigationProp, ParamListBase } from '@react-navigation/native';
import { useNavigation } from '@react-navigation/native';
import packageJson from '@root/package.json';

import type { FC } from 'react';
import { useMemo, useCallback, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { ScrollView } from 'react-native';
import { Box } from 'react-native-flex-layout';
import { Badge, List, useTheme } from 'react-native-paper';
import { Badge, List, Text, useTheme } from 'react-native-paper';
import { useDispatch } from 'react-redux';

import { setDebugEnabled } from '@/slices/settings';

import ChangeLanguageModal from '@/components/modals/ChangeLanguageModal';
import ChangeThemeModal from '@/components/modals/ChangeThemeModal';

import useDtuState from '@/hooks/useDtuState';
import useHasNewAppVersion from '@/hooks/useHasNewAppVersion';
import useIsConnected from '@/hooks/useIsConnected';
import useRequireMultiplePresses from '@/hooks/useRequireMultiplePresses';
import useSettings from '@/hooks/useSettings';

import { StyledSafeAreaView } from '@/style';

const MainSettingsTab: FC = () => {
const navigation = useNavigation() as NavigationProp<ParamListBase>;
const { t } = useTranslation();
const dispatch = useDispatch();

const theme = useTheme();

Expand All @@ -40,6 +47,8 @@ const MainSettingsTab: FC = () => {
usedForIndicatorOnly: true,
});

const showDebugScreen = useSettings(state => state?.debugEnabled);

const hasSystemInformation = !!useDtuState(state => !!state?.systemStatus);
const hasNetworkInformation = !!useDtuState(state => !!state?.networkStatus);
const hasNtpInformation = !!useDtuState(state => !!state?.ntpStatus);
Expand Down Expand Up @@ -86,6 +95,15 @@ const MainSettingsTab: FC = () => {
navigation.navigate('MqttInformationScreen');
}, [navigation]);

const handleDebugScreen = useCallback(() => {
navigation.navigate('DebugScreen');
}, [navigation]);

const enableDebugMode = useCallback(() => {
dispatch(setDebugEnabled({ debugEnabled: true }));
}, [dispatch]);
const handleUnlockDebug = useRequireMultiplePresses(enableDebugMode);

return (
<StyledSafeAreaView theme={theme}>
<Box style={{ width: '100%', flex: 1 }}>
Expand Down Expand Up @@ -158,7 +176,17 @@ const MainSettingsTab: FC = () => {
left={props => <List.Icon {...props} icon="file-document" />}
onPress={handleLicenses}
/>
{showDebugScreen ? (
<List.Item
title={t('settings.debug')}
left={props => <List.Icon {...props} icon="bug" />}
onPress={handleDebugScreen}
/>
) : null}
</List.Section>
<Text style={{ textAlign: 'center' }} onPress={handleUnlockDebug}>
Version {packageJson.version}
</Text>
</ScrollView>
</Box>
<ChangeThemeModal
Expand Down

0 comments on commit 7561a58

Please sign in to comment.