Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: twitter example for android & improve performance #7

Merged
merged 1 commit into from
Mar 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 45 additions & 19 deletions example/src/screens/usage/TwitterProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ import { Avatar, AVATAR_SIZE_MAP } from '../../components';
import { BlurView } from '@react-native-community/blur';
import TwitterVerifiedSvg from '../../../assets/twitter-verified.svg';
import type { TwitterProfileScreenNavigationProps } from '../../navigation';
import { Platform } from 'react-native';

// From reading comments online, the BlurView does not work properly for Android <= 11.
// We will have a boolean to check if we can use the BlurView.
// Note that Android 12 begins at SDK version 31
const canUseBlurView =
Platform.OS === 'ios' || (Platform.OS === 'android' && Number(Platform.Version) >= 31);

const ROOT_HORIZONTAL_PADDING = 12;
const TWITTER_PRIMARY_COLOR = '#1d9bf0';
Expand All @@ -55,20 +62,20 @@ const HeaderComponent: React.FC<ScrollHeaderProps> = ({ showNavBar, scrollY }) =
return { opacity: blurOpacity.value };
});

const paddingBottom = useDerivedValue(() => {
const bannerTranslation = useDerivedValue(() => {
return interpolate(
scrollY.value,
[0, AVATAR_SIZE_VALUE],
[AVATAR_SIZE_VALUE, 0],
[0, -AVATAR_SIZE_VALUE],
Extrapolate.CLAMP
);
}, [scrollY]);

// We use a "paddingBottom" adjustment to increase the banner's size.
// Using height is causing crashes on iOS at the moment...
// https://github.com/software-mansion/react-native-reanimated/issues/2285
const paddingBottomStyle = useAnimatedStyle(() => {
return { paddingBottom: paddingBottom.value };
const bannerTranslationStyle = useAnimatedStyle(() => {
return { transform: [{ translateY: bannerTranslation.value }] };
});

const profileContainerTranslationY = useDerivedValue(() => {
Expand Down Expand Up @@ -120,8 +127,8 @@ const HeaderComponent: React.FC<ScrollHeaderProps> = ({ showNavBar, scrollY }) =
});

return (
<Animated.View style={[styles.smallHeaderContainer, paddingBottomStyle]}>
<Animated.View style={StyleSheet.absoluteFill}>
<View style={styles.smallHeaderContainer}>
<Animated.View style={[StyleSheet.absoluteFill, bannerTranslationStyle]}>
<ScalingView
scrollY={scrollY}
translationDirection="none"
Expand All @@ -131,17 +138,35 @@ const HeaderComponent: React.FC<ScrollHeaderProps> = ({ showNavBar, scrollY }) =
endScale={6}
endRange={height * 0.43}
>
<AnimatedBlurView
style={[StyleSheet.absoluteFill, styles.blurView, blurStyle]}
blurType="dark"
reducedTransparencyFallbackColor="black"
/>

<AnimatedImage
source={require('../../../assets/planets.jpeg')}
resizeMode="cover"
style={[styles.imageStyle, { width }]}
/>
<View style={{ marginBottom: -AVATAR_SIZE_VALUE }}>
{canUseBlurView ? (
<AnimatedBlurView
style={[StyleSheet.absoluteFill, styles.blurView, blurStyle]}
blurType="dark"
reducedTransparencyFallbackColor="white"
blurAmount={5}
// @ts-ignore
// https://github.com/Kureev/react-native-blur/issues/414
// Fixes Android issue where the blur view will take up the whole screen.
overlayColor="transparent"
/>
) : (
<Animated.View
style={[
StyleSheet.absoluteFill,
styles.blurView,
styles.androidBlurViewBg,
blurStyle,
]}
/>
)}

<AnimatedImage
source={require('../../../assets/planets.jpeg')}
resizeMode="cover"
style={[styles.imageStyle, { width }]}
/>
</View>
</ScalingView>
</Animated.View>

Expand Down Expand Up @@ -207,7 +232,7 @@ const HeaderComponent: React.FC<ScrollHeaderProps> = ({ showNavBar, scrollY }) =
</TouchableOpacity>
</Animated.View>
</Animated.View>
</Animated.View>
</View>
);
};

Expand All @@ -230,7 +255,7 @@ const LargeHeaderComponent: React.FC<ScrollLargeHeaderProps> = ({ scrollY }) =>
return interpolate(
scrollY.value,
[0, AVATAR_SIZE_VALUE],
[0, AVATAR_SIZE_VALUE / 2 + 24],
[AVATAR_SIZE_VALUE, AVATAR_SIZE_VALUE / 2 + 24],
Extrapolate.CLAMP
);
}, [scrollY]);
Expand Down Expand Up @@ -449,4 +474,5 @@ const styles = StyleSheet.create({
},
locationAndWebContainer: { flexDirection: 'row', gap: 12, alignItems: 'center' },
dataRow: { flexDirection: 'row', gap: 4, alignItems: 'center' },
androidBlurViewBg: { backgroundColor: 'rgba(0,0,0,0.4)' },
});