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

handle unlimited requested api call and add fallbackIcon props #29560

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
17 changes: 17 additions & 0 deletions assets/images/receipt-placeholder.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 27 additions & 14 deletions src/components/ImageWithSizeCalculation.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import _ from 'underscore';
import React, {useState, useRef, useEffect} from 'react';
import React, {useState, useEffect, useMemo} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import Log from '../libs/Log';
import styles from '../styles/styles';
import FullscreenLoadingIndicator from './FullscreenLoadingIndicator';
import Image from './Image';
import useNetwork from '../hooks/useNetwork';

// Define constants for load states
const LoadState = {
INITIAL: 'initial',
LOADING: 'loading',
LOADED: 'loaded',
ERRORED: 'errored',
};

const propTypes = {
/** Url for image to display */
Expand Down Expand Up @@ -39,16 +48,18 @@ const defaultProps = {
*
*/
function ImageWithSizeCalculation(props) {
const isLoadedRef = useRef(null);
const [loadState, setLoadState] = useState(LoadState.INITIAL);
const [isImageCached, setIsImageCached] = useState(true);
const [isLoading, setIsLoading] = useState(false);

const source = useMemo(() => ({ uri: props.url }), [props.url]);
const {isOffline} = useNetwork();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can replace this line with useNetwork({onReconnect: () => setLoadState(LoadState.LOADING)});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explain which we need to change because if we change only,
const {isOffline} = useNetwork();

this line then it is okay but I don't understand why we need to replace this line,

const source = useMemo(() => ({ uri: props.url }), [props.url]);

it is being used for displaying image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, my mistake, I meant just replacing this line const {isOffline} = useNetwork(); 😅


const onError = () => {
Log.hmmm('Unable to fetch image to calculate size', {url: props.url});
setLoadState(LoadState.ERRORED);
};

const imageLoadedSuccessfully = (event) => {
isLoadedRef.current = true;
setLoadState(LoadState.LOADED);
props.onMeasure({
width: event.nativeEvent.width,
height: event.nativeEvent.height,
Expand All @@ -57,39 +68,41 @@ function ImageWithSizeCalculation(props) {

/** Delay the loader to detect whether the image is being loaded from the cache or the internet. */
useEffect(() => {
if (isLoadedRef.current || !isLoading) {
if (loadState !== LoadState.LOADING) {
return;
}
const timeout = _.delay(() => {
if (!isLoading || isLoadedRef.current) {
if (loadState !== LoadState.LOADING) {
return;
}
setLoadState(LoadState.ERRORED);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this line, this delay is just to indicate whether the current image is fetched from the internet or cache, it does not mean the loading failed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is done.

setIsImageCached(false);
}, 200);
return () => clearTimeout(timeout);
}, [isLoading]);
}, [loadState]);

return (
<View style={[styles.w100, styles.h100, props.style]}>
<Image
style={[styles.w100, styles.h100]}
source={{uri: props.url}}
source={source}
isAuthTokenRequired={props.isAuthTokenRequired}
resizeMode={Image.resizeMode.cover}
onLoadStart={() => {
if (isLoadedRef.current || isLoading) {
return;
if (loadState === LoadState.LOADED || isOffline) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need to execute setLoadState(LoadState.INITIAL), just need to return early if loadState === LoadState.LOADED || loadState === LoadState.ERRORED, which will allow us to be compatible with Not Found error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is done.

setLoadState(LoadState.INITIAL);
} else {
setLoadState(LoadState.LOADING);
}
setIsLoading(true);
}}
onLoadEnd={() => {
setIsLoading(false);
setLoadState(LoadState.LOADED);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we just need to execute this in the imageLoadedSuccessfully;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is done.

setIsImageCached(true);
}}
onError={onError}
onLoad={imageLoadedSuccessfully}
/>
{isLoading && !isImageCached && <FullscreenLoadingIndicator style={[styles.opacity1, styles.bgTransparent]} />}
{loadState === LoadState.LOADING && !isImageCached && <FullscreenLoadingIndicator style={[styles.opacity1, styles.bgTransparent]} />}
</View>
);
}
Expand Down
21 changes: 16 additions & 5 deletions src/components/ThumbnailImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import styles from '../styles/styles';
import * as StyleUtils from '../styles/StyleUtils';
import * as DeviceCapabilities from '../libs/DeviceCapabilities';
import useWindowDimensions from '../hooks/useWindowDimensions';
import useNetwork from '../hooks/useNetwork';
import ReceiptPlaceholderImage from '../../assets/images/receipt-placeholder.svg';

const propTypes = {
/** Source URL for the preview image */
Expand Down Expand Up @@ -91,15 +93,24 @@ function ThumbnailImage(props) {
);

const sizeStyles = props.shouldDynamicallyResize ? [StyleUtils.getWidthAndHeightStyle(imageWidth, imageHeight)] : [styles.w100, styles.h100];
const {isOffline} = useNetwork();

return (
<View style={[props.style, styles.overflowHidden]}>
<View style={[...sizeStyles, styles.alignItemsCenter, styles.justifyContentCenter]}>
<ImageWithSizeCalculation
url={props.previewSourceURL}
onMeasure={updateImageSize}
isAuthTokenRequired={props.isAuthTokenRequired}
/>
{isOffline ? (
<ReceiptPlaceholderImage
pointerEvents={'none'}
width={100}
style={styles.receiptPlaceholder}
/>
) : (
<ImageWithSizeCalculation
url={props.previewSourceURL}
onMeasure={updateImageSize}
isAuthTokenRequired={props.isAuthTokenRequired}
/>
)}
</View>
</View>
);
Expand Down
6 changes: 6 additions & 0 deletions src/styles/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -3899,6 +3899,12 @@ const styles = (theme) => ({
checkboxWithLabelCheckboxStyle: {
marginLeft: -2,
},

receiptPlaceholder: {
position: 'absolute',
alignItems:'center',
justifyContent:'center'
},
});

// For now we need to export the styles function that takes the theme as an argument
Expand Down
Loading