Skip to content

Commit 4ccc6d6

Browse files
authored
fix: twitter example for android & improve performance (#7)
1 parent 3c3a264 commit 4ccc6d6

File tree

1 file changed

+45
-19
lines changed

1 file changed

+45
-19
lines changed

example/src/screens/usage/TwitterProfile.tsx

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ import { Avatar, AVATAR_SIZE_MAP } from '../../components';
3131
import { BlurView } from '@react-native-community/blur';
3232
import TwitterVerifiedSvg from '../../../assets/twitter-verified.svg';
3333
import type { TwitterProfileScreenNavigationProps } from '../../navigation';
34+
import { Platform } from 'react-native';
35+
36+
// From reading comments online, the BlurView does not work properly for Android <= 11.
37+
// We will have a boolean to check if we can use the BlurView.
38+
// Note that Android 12 begins at SDK version 31
39+
const canUseBlurView =
40+
Platform.OS === 'ios' || (Platform.OS === 'android' && Number(Platform.Version) >= 31);
3441

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

58-
const paddingBottom = useDerivedValue(() => {
65+
const bannerTranslation = useDerivedValue(() => {
5966
return interpolate(
6067
scrollY.value,
6168
[0, AVATAR_SIZE_VALUE],
62-
[AVATAR_SIZE_VALUE, 0],
69+
[0, -AVATAR_SIZE_VALUE],
6370
Extrapolate.CLAMP
6471
);
6572
}, [scrollY]);
6673

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

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

122129
return (
123-
<Animated.View style={[styles.smallHeaderContainer, paddingBottomStyle]}>
124-
<Animated.View style={StyleSheet.absoluteFill}>
130+
<View style={styles.smallHeaderContainer}>
131+
<Animated.View style={[StyleSheet.absoluteFill, bannerTranslationStyle]}>
125132
<ScalingView
126133
scrollY={scrollY}
127134
translationDirection="none"
@@ -131,17 +138,35 @@ const HeaderComponent: React.FC<ScrollHeaderProps> = ({ showNavBar, scrollY }) =
131138
endScale={6}
132139
endRange={height * 0.43}
133140
>
134-
<AnimatedBlurView
135-
style={[StyleSheet.absoluteFill, styles.blurView, blurStyle]}
136-
blurType="dark"
137-
reducedTransparencyFallbackColor="black"
138-
/>
139-
140-
<AnimatedImage
141-
source={require('../../../assets/planets.jpeg')}
142-
resizeMode="cover"
143-
style={[styles.imageStyle, { width }]}
144-
/>
141+
<View style={{ marginBottom: -AVATAR_SIZE_VALUE }}>
142+
{canUseBlurView ? (
143+
<AnimatedBlurView
144+
style={[StyleSheet.absoluteFill, styles.blurView, blurStyle]}
145+
blurType="dark"
146+
reducedTransparencyFallbackColor="white"
147+
blurAmount={5}
148+
// @ts-ignore
149+
// https://github.com/Kureev/react-native-blur/issues/414
150+
// Fixes Android issue where the blur view will take up the whole screen.
151+
overlayColor="transparent"
152+
/>
153+
) : (
154+
<Animated.View
155+
style={[
156+
StyleSheet.absoluteFill,
157+
styles.blurView,
158+
styles.androidBlurViewBg,
159+
blurStyle,
160+
]}
161+
/>
162+
)}
163+
164+
<AnimatedImage
165+
source={require('../../../assets/planets.jpeg')}
166+
resizeMode="cover"
167+
style={[styles.imageStyle, { width }]}
168+
/>
169+
</View>
145170
</ScalingView>
146171
</Animated.View>
147172

@@ -207,7 +232,7 @@ const HeaderComponent: React.FC<ScrollHeaderProps> = ({ showNavBar, scrollY }) =
207232
</TouchableOpacity>
208233
</Animated.View>
209234
</Animated.View>
210-
</Animated.View>
235+
</View>
211236
);
212237
};
213238

@@ -230,7 +255,7 @@ const LargeHeaderComponent: React.FC<ScrollLargeHeaderProps> = ({ scrollY }) =>
230255
return interpolate(
231256
scrollY.value,
232257
[0, AVATAR_SIZE_VALUE],
233-
[0, AVATAR_SIZE_VALUE / 2 + 24],
258+
[AVATAR_SIZE_VALUE, AVATAR_SIZE_VALUE / 2 + 24],
234259
Extrapolate.CLAMP
235260
);
236261
}, [scrollY]);
@@ -449,4 +474,5 @@ const styles = StyleSheet.create({
449474
},
450475
locationAndWebContainer: { flexDirection: 'row', gap: 12, alignItems: 'center' },
451476
dataRow: { flexDirection: 'row', gap: 4, alignItems: 'center' },
477+
androidBlurViewBg: { backgroundColor: 'rgba(0,0,0,0.4)' },
452478
});

0 commit comments

Comments
 (0)