Skip to content

Commit

Permalink
Home: handle background breakage - fix #972 (#974)
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Jan Modras authored and chrmod committed Apr 22, 2020
1 parent 61debe9 commit 67f8e30
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"images" : [
{
"filename" : "cliqz-bg0.jpg",
"idiom" : "universal"
},
{
"filename" : "cliqz-bg0_mobile.jpg",
"idiom" : "universal",
"width-class" : "compact"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 16 additions & 7 deletions ReactNative/js/screens/Home/components/Background.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import FastImage from 'react-native-fast-image';

const DAY_OF_MONTH = new Date().getDate();
const BACKGROUND_URL = `https://cdn.cliqz.com/serp/configs/config_${DAY_OF_MONTH}.json`;

const styles = {
mask: [
StyleSheet.absoluteFill,
Expand All @@ -13,22 +12,25 @@ const styles = {
},
],
};
const fallbackImageSource = { uri: 'home-background' };
const maskSource = { uri: 'mask' };

const useBackgroundImage = () => {
const [url, setUrl] = useState(Settings.get('backgroundUrl'));
useEffect(() => {
const fetchBackground = async () => {
const responseData = await fetch(BACKGROUND_URL);
const responseData = await fetch(BACKGROUND_URL, { cache: 'no-cache' });
const responseJSON = await responseData.json();
const backgrounds = Platform.isPad
? responseJSON.backgrounds
: responseJSON.backgrounds_mobile;
const backgroundIndex = Math.floor(Math.random() * backgrounds.length);
const background = backgrounds[backgroundIndex];
if (background && background.url !== url) {
setUrl(background.url);
const newUrl = background.url;
setUrl(newUrl);
Settings.set({
backgroundUrl: background.url,
backgroundUrl: newUrl,
backgroundTimestamp: Date.now(),
});
}
Expand Down Expand Up @@ -61,11 +63,18 @@ export default ({ height, children }) => {
}),
[backgroundUrl],
);
const maskSource = useMemo(() => ({ uri: 'mask' }), []);

const [hasError, setError] = useState(false);
return (
<View style={style} accessibilityIgnoresInvertColors>
<FastImage style={StyleSheet.absoluteFill} source={backgroundSource} />
{hasError ? (
<Image style={StyleSheet.absoluteFill} source={fallbackImageSource} />
) : (
<FastImage
style={StyleSheet.absoluteFill}
source={backgroundSource}
onError={setError}
/>
)}
<Image style={styles.mask} source={maskSource} />
{children}
</View>
Expand Down

0 comments on commit 67f8e30

Please sign in to comment.