Skip to content

Commit

Permalink
feat: lock app button
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarkhanzadian committed Oct 24, 2024
1 parent 7d78f9f commit 396124e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
4 changes: 3 additions & 1 deletion apps/mobile/src/app/(home)/settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useSafeAreaInsets } from 'react-native-safe-area-context';

import { Divider } from '@/components/divider';
import { NotifyUserSheet } from '@/components/sheets/notify-user-sheet.layout';
import { useAuthContext } from '@/components/splash-screen-guard/use-auth-context';
import { AppRoutes } from '@/routes';
import { TestId } from '@/shared/test-id';
import { t } from '@lingui/macro';
Expand Down Expand Up @@ -35,6 +36,7 @@ export default function SettingsScreen() {
const feesSheetRef = useRef<SheetRef>(null);
const theme = useTheme<Theme>();
const router = useRouter();
const { lockApp } = useAuthContext();

return (
<>
Expand Down Expand Up @@ -204,7 +206,7 @@ export default function SettingsScreen() {
</Text>
</Box>
<Button
onPress={() => {}}
onPress={lockApp}
buttonState="outline"
title={t({
id: 'settings.lock_button',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Box, Button, Theme, useOnMount } from '@leather.io/ui/native';

import { LeatherLockedSplash } from '../animations/leather-locked-splash';
import { LeatherSplash } from '../animations/leather-splash';
import { AuthContext } from './use-auth-context';
import { useAuthState } from './use-auth-state';

const DEFAULT_ANIMATION_FINISHED = false;
Expand All @@ -33,7 +34,7 @@ export function SplashScreenGuard({ children }: HasChildren) {
lockedSplashRef.current?.play();
}, []);

const { tryAuthentication, authState } = useAuthState({
const { tryAuthentication, authState, lockApp } = useAuthState({
playSplash,
setAnimationFinished,
});
Expand All @@ -58,7 +59,7 @@ export function SplashScreenGuard({ children }: HasChildren) {
}

if (animationFinished) {
return children; // to their parents
return <AuthContext.Provider value={{ lockApp }}>{children}</AuthContext.Provider>;
}

const splash =
Expand Down
11 changes: 11 additions & 0 deletions apps/mobile/src/components/splash-screen-guard/use-auth-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { createContext, useContext } from 'react';

interface AuthContextArgs {
lockApp(): void;
}

export const AuthContext = createContext<AuthContextArgs>({
lockApp: () => {},
});

export const useAuthContext = () => useContext(AuthContext);
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,20 @@ export function useAuthState({
if (securityLevelPreference === 'secure') {
setAnimationFinished(false);
setAuthState('started');
userLeavesApp(+new Date());

// add latest active timestamp only if the app was actually unlocked
const appUnlocked = authState === 'passed-on-first' || authState === 'passed-afterwards';
if (appUnlocked) {
userLeavesApp(+new Date());
}
}
}, [securityLevelPreference, userLeavesApp, setAnimationFinished]);
}, [securityLevelPreference, userLeavesApp, setAnimationFinished, authState]);

const lockApp = useCallback(() => {
setAnimationFinished(false);
setAuthState('failed');
userLeavesApp(null);
}, [setAnimationFinished, setAuthState, userLeavesApp]);

useAppState({
onAppForeground,
Expand All @@ -72,5 +83,6 @@ export function useAuthState({
return {
tryAuthentication,
authState,
lockApp,
};
}

0 comments on commit 396124e

Please sign in to comment.