Skip to content

Commit

Permalink
fix: image loader.
Browse files Browse the repository at this point in the history
  • Loading branch information
Krishna2323 committed Jul 31, 2023
1 parent eb02006 commit af9e1fc
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/components/ImageWithSizeCalculation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, {useState, useRef} from 'react';
import _ from 'underscore';
import React, {useState, useRef, useEffect} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import Log from '../libs/Log';
Expand Down Expand Up @@ -40,6 +41,7 @@ const defaultProps = {
function ImageWithSizeCalculation(props) {
const [isLoading, setIsLoading] = useState(false);
const isLoadedRef = useRef(null);
const [isImageCached, setIsImageCached] = useState(true);

const onError = () => {
Log.hmmm('Unable to fetch image to calculate size', {url: props.url});
Expand All @@ -53,6 +55,19 @@ function ImageWithSizeCalculation(props) {
});
};

/** Delay the loader to detect whether the image is being loaded from the cache or the internet. */
useEffect(() => {
let timeout;

if (isLoading) {
timeout = _.delay(() => {
setIsImageCached(false);
}, 175);
}

return () => clearTimeout(timeout);
}, [isLoading]);

return (
<View style={[styles.w100, styles.h100, props.style]}>
<Image
Expand All @@ -66,11 +81,14 @@ function ImageWithSizeCalculation(props) {
}
setIsLoading(true);
}}
onLoadEnd={() => setIsLoading(false)}
onLoadEnd={() => {
setIsLoading(false);
setIsImageCached(true);
}}
onError={onError}
onLoad={imageLoadedSuccessfully}
/>
{isLoading && <FullscreenLoadingIndicator style={[styles.opacity1, styles.bgTransparent]} />}
{isLoading && !isImageCached && <FullscreenLoadingIndicator style={[styles.opacity1, styles.bgTransparent]} />}
</View>
);
}
Expand Down

0 comments on commit af9e1fc

Please sign in to comment.