1- import React , { useState , useRef } from 'react' ;
1+ import _ from 'underscore' ;
2+ import React , { useState , useRef , useEffect } from 'react' ;
23import { View } from 'react-native' ;
34import PropTypes from 'prop-types' ;
45import Log from '../libs/Log' ;
@@ -38,8 +39,9 @@ const defaultProps = {
3839 *
3940 */
4041function ImageWithSizeCalculation ( props ) {
41- const [ isLoading , setIsLoading ] = useState ( false ) ;
4242 const isLoadedRef = useRef ( null ) ;
43+ const [ isImageCached , setIsImageCached ] = useState ( true ) ;
44+ const [ isLoading , setIsLoading ] = useState ( false ) ;
4345
4446 const onError = ( ) => {
4547 Log . hmmm ( 'Unable to fetch image to calculate size' , { url : props . url } ) ;
@@ -53,6 +55,20 @@ function ImageWithSizeCalculation(props) {
5355 } ) ;
5456 } ;
5557
58+ /** Delay the loader to detect whether the image is being loaded from the cache or the internet. */
59+ useEffect ( ( ) => {
60+ if ( isLoadedRef . current || ! isLoading ) {
61+ return ;
62+ }
63+ const timeout = _ . delay ( ( ) => {
64+ if ( ! isLoading || isLoadedRef . current ) {
65+ return ;
66+ }
67+ setIsImageCached ( false ) ;
68+ } , 200 ) ;
69+ return ( ) => clearTimeout ( timeout ) ;
70+ } , [ isLoading ] ) ;
71+
5672 return (
5773 < View style = { [ styles . w100 , styles . h100 , props . style ] } >
5874 < Image
@@ -66,11 +82,14 @@ function ImageWithSizeCalculation(props) {
6682 }
6783 setIsLoading ( true ) ;
6884 } }
69- onLoadEnd = { ( ) => setIsLoading ( false ) }
85+ onLoadEnd = { ( ) => {
86+ setIsLoading ( false ) ;
87+ setIsImageCached ( true ) ;
88+ } }
7089 onError = { onError }
7190 onLoad = { imageLoadedSuccessfully }
7291 />
73- { isLoading && < FullscreenLoadingIndicator style = { [ styles . opacity1 , styles . bgTransparent ] } /> }
92+ { isLoading && ! isImageCached && < FullscreenLoadingIndicator style = { [ styles . opacity1 , styles . bgTransparent ] } /> }
7493 </ View >
7594 ) ;
7695}
0 commit comments