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

fix: check canUseTouchScreen on each render #24356

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions src/components/Attachments/AttachmentCarousel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, {useRef, useCallback, useState, useEffect} from 'react';
import {View, FlatList, PixelRatio, Keyboard} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import * as DeviceCapabilities from '../../../libs/DeviceCapabilities';
import styles from '../../../styles/styles';
import CarouselActions from './CarouselActions';
import AttachmentView from '../AttachmentView';
Expand All @@ -19,8 +18,8 @@ import Navigation from '../../../libs/Navigation/Navigation';
import BlockingView from '../../BlockingViews/BlockingView';
import * as Illustrations from '../../Icon/Illustrations';
import variables from '../../../styles/variables';
import * as DeviceCapabilities from '../../../libs/DeviceCapabilities';

const canUseTouchScreen = DeviceCapabilities.canUseTouchScreen();
const viewabilityConfig = {
// To facilitate paging through the attachments, we want to consider an item "viewable" when it is
// more than 95% visible. When that happens we update the page index in the state.
Expand All @@ -31,6 +30,7 @@ function AttachmentCarousel({report, reportActions, source, onNavigate, setDownl
const scrollRef = useRef(null);

const {windowWidth, isSmallScreenWidth} = useWindowDimensions();
const canUseTouchScreen = DeviceCapabilities.canUseTouchScreen();
Copy link
Contributor

Choose a reason for hiding this comment

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

One minor concern is that we currently can't trigger a re-render when the value from DeviceCapabilities.canUseTouchScreen() changes. This means we're dependent on external factors to trigger a re-render of AttachmentCarousel before it acknowledges any change in the canUseTouchScreen() value.

However, given the context of this specific use-case, this behavior seems acceptable.


const [containerWidth, setContainerWidth] = useState(0);
const [page, setPage] = useState(0);
Expand Down Expand Up @@ -99,7 +99,7 @@ function AttachmentCarousel({report, reportActions, source, onNavigate, setDownl

scrollRef.current.scrollToIndex({index: nextIndex, animated: canUseTouchScreen});
},
[attachments, page],
[attachments, canUseTouchScreen, page],
);

/**
Expand Down Expand Up @@ -160,7 +160,7 @@ function AttachmentCarousel({report, reportActions, source, onNavigate, setDownl
isUsedInCarousel
/>
),
[activeSource, setShouldShowArrows, shouldShowArrows],
[activeSource, canUseTouchScreen, setShouldShowArrows, shouldShowArrows],
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import {useCallback, useEffect, useRef, useState} from 'react';
import CONST from '../../../CONST';
import * as DeviceCapabilities from '../../../libs/DeviceCapabilities';

const canUseTouchScreen = DeviceCapabilities.canUseTouchScreen();

function useCarouselArrows() {
const canUseTouchScreen = DeviceCapabilities.canUseTouchScreen();
const [shouldShowArrows, setShouldShowArrowsInternal] = useState(canUseTouchScreen);
const autoHideArrowTimeout = useRef(null);

Expand All @@ -25,7 +24,7 @@ function useCarouselArrows() {
autoHideArrowTimeout.current = setTimeout(() => {
setShouldShowArrowsInternal(false);
}, CONST.ARROW_HIDE_DELAY);
}, [cancelAutoHideArrows]);
}, [canUseTouchScreen, cancelAutoHideArrows]);

const setShouldShowArrows = useCallback(
(show = true) => {
Expand Down