Skip to content

Commit

Permalink
chore: code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
richardo2016x committed Aug 6, 2024
1 parent 32e6d69 commit 1d1ff6b
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 181 deletions.
2 changes: 1 addition & 1 deletion apps/mobile/src/AppNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import {
import usePrevious from 'ahooks/lib/usePrevious';
import AutoLockView from './components/AutoLockView';
import { GlobalSecurityTipStubModal } from './components/Security/SecurityTipStubModal';
import { FloatViewAutoLockCount } from './screens/Settings/components/LockAbout';
import { FloatViewAutoLockCount } from './screens/Settings/components/FloatView';

const RootStack = createNativeStackNavigator<RootStackParamsList>();

Expand Down
171 changes: 168 additions & 3 deletions apps/mobile/src/screens/Settings/components/FloatView.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,174 @@
import React, { useMemo } from 'react';
import { PanResponder, Text, View } from 'react-native';
import React from 'react';
import { Dimensions, Text, View } from 'react-native';
import Animated, {
useAnimatedStyle,
useSharedValue,
} from 'react-native-reanimated';

import { createGetStyles } from '@/utils/styles';
import Animated from 'react-native-reanimated';
import { useThemeStyles } from '@/hooks/theme';
import { useAutoLockCountDown } from './LockAbout';
import { colord } from 'colord';
import { NEED_DEVSETTINGBLOCKS } from '@/constant/env';
import { useFloatingView } from '@/hooks/appSettings';
import {
Gesture,
GestureDetector,
GestureHandlerRootView,
TouchableWithoutFeedback,
} from 'react-native-gesture-handler';
import { RcIconLogo } from '@/assets/icons/common';

function clamp(val: number, min: number, max: number) {
return Math.min(Math.max(val, min), max);
}
const VIEW_W = 240;
const DRAGGER_SIZE = 60;
const INIT_RIGHT = VIEW_W - DRAGGER_SIZE;
const INIT_LAYOUT = {
top: 100,
};
const screenLayout = Dimensions.get('screen');
export function FloatViewAutoLockCount() {
const { styles } = useThemeStyles(getFloatViewAutoLockCountStyles);
const { countDownText, textColor } = useAutoLockCountDown();
const { collapsed, toggleCollapsed, shouldShow } = useFloatingView();

const [translationX, translationY, prevTranslationX, prevTranslationY] = [
useSharedValue(0),
useSharedValue(0),
useSharedValue(0),
useSharedValue(0),
];

const animatedStyles = useAnimatedStyle(() => {
return {
transform: [{ translateY: translationY.value }],
};
});

const composedGesture = React.useMemo(() => {
const tap = Gesture.Tap()
.runOnJS(true)
.onEnd(() => {
toggleCollapsed();
});

const pan = Gesture.Pan()
.minDistance(5)
.enabled(true)
.runOnJS(true)
.onStart(() => {
prevTranslationX.value = translationX.value;
prevTranslationY.value = translationY.value;
})
.onUpdate(event => {
try {
const maxTranslateX = screenLayout.width / 2 - 50;
const maxTranslateY = screenLayout.height / 2 - 50;

translationX.value = clamp(
prevTranslationX.value + event.translationX,
-maxTranslateX,
maxTranslateX,
);
translationY.value = clamp(
prevTranslationY.value + event.translationY,
-maxTranslateY,
maxTranslateY,
);
} catch (err: any) {
console.error('onUpdate error', err?.message);
}
});

const composed = Gesture.Race(...[pan, tap]);
return composed;
}, [
toggleCollapsed,
translationX,
translationY,
prevTranslationX,
prevTranslationY,
]);

if (!NEED_DEVSETTINGBLOCKS) return null;
if (!shouldShow) return null;

return (
<GestureHandlerRootView
style={[styles.gestureContainer, !collapsed && styles.containerExpanded]}>
<Animated.View
style={[
styles.container,
animatedStyles,
// {
// top: dragPosRef.current.getLayout().top
// },
]}>
<GestureDetector gesture={composedGesture}>
{/* dragger */}
<TouchableWithoutFeedback style={styles.dragger}>
<RcIconLogo width={48} height={48} />
</TouchableWithoutFeedback>
</GestureDetector>
<View pointerEvents="none" style={[styles.animatedView]}>
{countDownText && <Text style={styles.label}>Auto Lock after </Text>}
<Text style={{ color: textColor, fontWeight: '600' }}>
{countDownText || 'Time Reached'}
</Text>
</View>
</Animated.View>
</GestureHandlerRootView>
);
}

const getFloatViewAutoLockCountStyles = createGetStyles(colors => {
return {
gestureContainer: {
flex: 1,
position: 'absolute',
// ...makeDebugBorder(),
zIndex: 999,
width: VIEW_W,
top: INIT_LAYOUT.top,
height: 60,
right: -INIT_RIGHT,
},
container: {
flex: 1,
position: 'absolute',
borderRadius: 6,
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
},
dragger: {
borderTopLeftRadius: 60,
borderBottomLeftRadius: 60,
height: 60,
width: 60,
flexShrink: 0,
opacity: 0.9,
backgroundColor: colors['blue-default'],
// ...makeDebugBorder('yellow'),
justifyContent: 'center',
alignItems: 'center',
},
containerExpanded: {
right: 0,
},
animatedView: {
backgroundColor: colord('#000000').alpha(0.5).toRgbString(),
flexShrink: 1,
width: '100%',
height: '100%',
justifyContent: 'center',
alignItems: 'center',
flexDirection: 'row',
},
label: {
color: colord('#ffffff').alpha(0.8).toRgbString(),
},
};
});
178 changes: 1 addition & 177 deletions apps/mobile/src/screens/Settings/components/LockAbout.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,18 @@
import React, { useRef } from 'react';
import {
Animated as RNAnimated,
Dimensions,
PanResponder,
Text,
View,
} from 'react-native';
import {
Gesture,
GestureDetector,
GestureHandlerRootView,
TouchableOpacity,
TouchableWithoutFeedback,
} from 'react-native-gesture-handler';
import Animated, {
useAnimatedStyle,
useSharedValue,
} from 'react-native-reanimated';
import { Text, View } from 'react-native';

import { useAutoLockTime, useLastUnlockTime } from '@/hooks/appTimeout';
import { useThemeColors, useThemeStyles } from '@/hooks/theme';
import useInterval from 'react-use/lib/useInterval';
import { NEED_DEVSETTINGBLOCKS } from '@/constant/env';
import { getTimeSpan, getTimeSpanByMs } from '@/utils/time';
import { usePasswordStatus } from '@/hooks/useLock';
import { colord } from 'colord';
import { createGetStyles, makeDebugBorder } from '@/utils/styles';
import {
useAutoLockTimeMinites,
useFloatingView,
useToggleShowAutoLockCountdown,
} from '@/hooks/appSettings';
import { TIME_SETTINGS } from '@/constant/autoLock';
import TouchableView from '@/components/Touchable/TouchableView';
import { RcIconLogo } from '@/assets/icons/common';

export function useAutoLockCountDown() {
const { autoLockTime } = useAutoLockTime();
Expand Down Expand Up @@ -142,161 +121,6 @@ export function AutoLockSettingLabel() {
);
}

function clamp(val: number, min: number, max: number) {
return Math.min(Math.max(val, min), max);
}
const VIEW_W = 240;
const DRAGGER_SIZE = 60;
const INIT_RIGHT = VIEW_W - DRAGGER_SIZE;
const INIT_LAYOUT = {
top: 100,
// left: Dimensions.get('window').width - INIT_RIGHT,
};
const screenLayout = Dimensions.get('screen');
export function FloatViewAutoLockCount() {
const { styles } = useThemeStyles(getFloatViewAutoLockCountStyles);
const { countDownText, textColor } = useAutoLockCountDown();
const { collapsed, toggleCollapsed, shouldShow } = useFloatingView();

const [translationX, translationY, prevTranslationX, prevTranslationY] = [
useSharedValue(0),
useSharedValue(0),
useSharedValue(0),
useSharedValue(0),
];

const animatedStyles = useAnimatedStyle(() => {
return {
transform: [{ translateY: translationY.value }],
};
});

const composedGesture = React.useMemo(() => {
const tap = Gesture.Tap()
.runOnJS(true)
.onEnd(() => {
toggleCollapsed();
});

const pan = Gesture.Pan()
.minDistance(5)
.enabled(true)
.runOnJS(true)
.onStart(() => {
prevTranslationX.value = translationX.value;
prevTranslationY.value = translationY.value;
})
.onUpdate(event => {
try {
const maxTranslateX = screenLayout.width / 2 - 50;
const maxTranslateY = screenLayout.height / 2 - 50;

translationX.value = clamp(
prevTranslationX.value + event.translationX,
-maxTranslateX,
maxTranslateX,
);
translationY.value = clamp(
prevTranslationY.value + event.translationY,
-maxTranslateY,
maxTranslateY,
);
} catch (err: any) {
console.error('onUpdate error', err?.message);
}
});

const composed = Gesture.Race(...[pan, tap]);
return composed;
}, [
toggleCollapsed,
translationX,
translationY,
prevTranslationX,
prevTranslationY,
]);

if (!NEED_DEVSETTINGBLOCKS) return null;
if (!shouldShow) return null;

return (
<GestureHandlerRootView
style={[styles.gestureContainer, !collapsed && styles.containerExpanded]}>
<Animated.View
style={[
styles.container,
animatedStyles,
// {
// top: dragPosRef.current.getLayout().top
// },
]}>
<GestureDetector gesture={composedGesture}>
{/* dragger */}
<TouchableOpacity style={styles.dragger}>
<RcIconLogo width={48} height={48} />
</TouchableOpacity>
</GestureDetector>
<View pointerEvents="none" style={[styles.animatedView]}>
{countDownText && <Text style={styles.label}>Auto Lock after </Text>}
<Text style={{ color: textColor, fontWeight: '600' }}>
{countDownText || 'Time Reached'}
</Text>
</View>
</Animated.View>
</GestureHandlerRootView>
);
}

const getFloatViewAutoLockCountStyles = createGetStyles(colors => {
return {
gestureContainer: {
flex: 1,
position: 'absolute',
// ...makeDebugBorder(),
zIndex: 999,
width: VIEW_W,
top: INIT_LAYOUT.top,
height: 60,
right: -INIT_RIGHT,
},
container: {
flex: 1,
position: 'absolute',
borderRadius: 6,
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
},
dragger: {
borderTopLeftRadius: 60,
borderBottomLeftRadius: 60,
height: 60,
width: 60,
flexShrink: 0,
opacity: 0.5,
backgroundColor: colors['blue-default'],
// ...makeDebugBorder('yellow'),
justifyContent: 'center',
alignItems: 'center',
},
containerExpanded: {
right: 0,
},
animatedView: {
backgroundColor: colord('#000000').alpha(0.5).toRgbString(),
flexShrink: 1,
width: '100%',
height: '100%',
justifyContent: 'center',
alignItems: 'center',
flexDirection: 'row',
},
label: {
color: colord('#ffffff').alpha(0.8).toRgbString(),
},
};
});

export function LastUnlockTimeLabel() {
const { unlockTime } = useLastUnlockTime();

Expand Down

0 comments on commit 1d1ff6b

Please sign in to comment.