Skip to content

[$1000] Zoom view in the attachment preview is lost with 4 or more attachments #18614

Closed

Description

@kavimuru

If you haven’t already, check out our contributing guidelines for onboarding and email contributors@expensify.com to request to join our Slack channel!


Action Performed:

  1. Open the app
  2. Open report with 4 or more image attachments
  3. Open first image and zoom in
  4. Navigate back 4 images and again navigate back to first image and observe that image is zoomed out
  5. Repeat step 3 and 4 with 3 images in step 4 and observe that image is zoomed in

Expected Result:

App should remember zoomed in position throughout the attachment navigations as it does when we only navigate back 3 images

Actual Result:

Image is zoomed out when we navigate back 4 images in attachment preview

Workaround:

Can the user still use Expensify without this being fixed? Have you informed them of the workaround?

Platforms:

Which of our officially supported platforms is this issue occurring on?

  • Android / native
  • Android / Chrome
  • iOS / native
  • iOS / Safari
  • MacOS / Chrome / Safari
  • MacOS / Desktop

Version Number: 1.3.11.2
Reproducible in staging?: y
Reproducible in production?: y
If this was caught during regression testing, add the test name, ID and link from TestRail:
Email or phone of affected tester (no customers):
Logs: https://stackoverflow.com/c/expensify/questions/4856
Notes/Photos/Videos: Any additional supporting documentation

zoom.in.position.is.forgotten.when.navigated.back.4.images.mp4
Recording.530.mp4

Expensify/Expensify Issue URL:
Issue reported by: @dhanashree-sawant
Slack conversation: https://expensify.slack.com/archives/C049HHMV9SM/p1683535771247579

View all open jobs on GitHub

Upwork Automation - Do Not Edit
  • Upwork Job URL: https://www.upwork.com/jobs/~0185edcdfdafdda6aa
  • Upwork Job ID: 1657031802775678976
  • Last Price Increase: 2023-05-26

Activity

  1. added
    Daily
    Bug
    on May 8, 2023
  2. melvin-bot commented on May 8, 2023

    @melvin-bot

    Triggered auto assignment to @michaelhaxhiu (Bug), see https://stackoverflow.com/c/expensify/questions/14418 for more details.

  3. melvin-bot commented on May 8, 2023

    @melvin-bot

    Bug0 Triage Checklist (Main S/O)

    • This "bug" occurs on a supported platform (ensure Platforms in OP are ✅)
    • This bug is not a duplicate report (check E/App issues and #expensify-bugs)
      • If it is, comment with a link to the original report, close the issue and add any novel details to the original issue instead
    • This bug is reproducible using the reproduction steps in the OP. S/O
      • If the reproduction steps are clear and you're unable to reproduce the bug, check with the reporter and QA first, then close the issue.
      • If the reproduction steps aren't clear and you determine the correct steps, please update the OP.
    • This issue is filled out as thoroughly and clearly as possible
      • Pay special attention to the title, results, platforms where the bug occurs, and if the bug happens on staging/production.
    • I have reviewed and subscribed to the linked Slack conversation to ensure Slack/Github stay in sync
  4. therealsujitk commented on May 9, 2023

    @therealsujitk
    Contributor

    Proposal

    Please re-state the problem that we are trying to solve in this issue.

    Zoom gets reset after navigating 3 attachments before or after.

    What is the root cause of that problem?

    This isn't a bug, it's intended behaviour. To prevent devices from lagging/crashing it is limited to load three attachments at the same time, after which the furthest attachment gets unloaded to clear up cache. This is done using the maxToRenderPerBatch attribute in line 375 in the code shown below.

    <FlatList
    listKey="AttachmentCarousel"
    horizontal
    // Inverting the list for touchscreen devices that can swipe or have an animation when scrolling
    // promotes the natural feeling of swiping left/right to go to the next/previous image
    // We don't want to invert the list for desktop/web because this interferes with mouse
    // wheel or trackpad scrolling (in cases like document preview where you can scroll vertically)
    inverted={this.canUseTouchScreen}
    decelerationRate="fast"
    showsHorizontalScrollIndicator={false}
    bounces={false}
    // Scroll only one image at a time no matter how fast the user swipes
    disableIntervalMomentum
    pagingEnabled
    snapToAlignment="start"
    snapToInterval={this.state.containerWidth}
    // Enable scrolling by swiping on mobile (touch) devices only
    // disable scroll for desktop/browsers because they add their scrollbars
    // Enable scrolling FlatList only when PDF is not in a zoomed state
    scrollEnabled={this.canUseTouchScreen && !this.state.isZoomed}
    ref={this.scrollRef}
    initialScrollIndex={this.state.page}
    initialNumToRender={3}
    windowSize={5}
    maxToRenderPerBatch={3}
    data={this.state.attachments}
    CellRendererComponent={this.renderCell}
    renderItem={this.renderItem}
    getItemLayout={this.getItemLayout}
    keyExtractor={item => item.source}
    viewabilityConfig={this.viewabilityConfig}
    onViewableItemsChanged={this.updatePage}
    />

    What changes do you think we should make in order to solve the problem?

    To keep behaviour consistent, we could store data related to the state of the attachment (such as position, scroll, etc.) in an array. This way we can restore it's state when it's reloaded. For this an attribute (ex: initialState) can be created in the AttachmentView component to specify what the view should look like when initially loaded.

    In our makeInitialState() function where we create an array of attachments, we also create a local variable to store attachment states.

    this.attachmentStates = new Array(attachments.length).fill({});

    Note: We don't need to store this in the react state because we only need to use it initially when the AttachmentView component mounts.

    We then create a function to update this state whenever required.

    /**
     * Updates the attachment state to restore it later
     * @param {Object} state current attachment state
     * @param {Number} index index of the attachment to update
     */
    updateAttachmentState(state, index) {
        this.attachmentStates[index] = state;
    }

    Our AttachmentView component will then look like this. These props will be then passed down to ImageView and PDFView.

    <AttachmentView
        source={authSource}
        file={item.file}
        initialState={this.attachmentStates[index]}
        onStateUpdate={(state) => this.updateAttachmentState(state, index)}
    />

    Finally in the ImageView and PDFView we read the initial state in the constructor.

    this.state = {
        isLoading: true,
        containerHeight: 0,
        ...
        ...this.props.initialState,
    };

    And in the componentWillUnmount we use the this.props.onStateUpdate() function since it will only be required if the component is unmounted.

    What alternative solutions did you explore? (Optional)

    • One solution would be to disable navigation while an attachment is zoomed in like WhatsApp does. For this we simply pass an onScaleChanged function prop to the AttachmentView component and further to the ImageView to call the updateZoomState() function in AttachmentCarousel which hides the arrows if zoom > 1. This is already done for touch devices only for PDF files.
    • Another solution would be to reset zoom if navigated to keep behaviour consistent. There are two ways to do this,
      • The first method involves creating a resetZoom() function in the ImageView component.

        /**
         * Reset the image zoom, used by the AttachmentCarousel component.
         */
        resetZoom() {
            this.setState({isZoomed: false, isDragging: false, isMouseDown: false});
        }

        We then pass down a ref from AttachmentCarousel -> AttachmentView -> ImageView.

        this.currentAttachment = React.createRef();
        // AttachmentCarousel
        <AttachmentView
            source={authSource}
            file={item.file}
            attachmentRef={index === this.state.page ? this.currentAttachment : undefined}
        />
        
        // AttachmentView
        <ImageView
            ref={props.attachmentRef}
            url={props.source}
            isAuthTokenRequired={isImage && props.isAuthTokenRequired}
        />

        After this we can access the resetZoom() function using this ref with the line below. This can be added to the updatePage() function in the setState() callback.

        this.currentAttachment.current?.resetZoom();
      • The second method involves bring the zoom state from ImageView to AttachmentCarousel which I DO NOT recommend as it's just very messy.

      • A third method (I wound't really call it a method) in theory, would be to set maxToRenderPerBatch={1} 😅.

  5. melvin-bot commented on May 9, 2023

    @melvin-bot

    Looks like something related to react-navigation may have been mentioned in this issue discussion.

    As a reminder, please make sure that all proposals are not workarounds and that any and all attempt to fix the issue holistically have been made before proceeding with a solution. Proposals to change our DeprecatedCustomActions.js files should not be accepted.

    Feel free to drop a note in #expensify-open-source with any questions.

  6. removed their assignment
    on May 9, 2023
  7. added
    Bug
    and removed
    Bug
    on May 9, 2023
  8. melvin-bot commented on May 9, 2023

    @melvin-bot

    Triggered auto assignment to @maddylewis (Bug), see https://stackoverflow.com/c/expensify/questions/14418 for more details.

  9. melvin-bot commented on May 9, 2023

    @melvin-bot
  10. michaelhaxhiu commented on May 9, 2023

    @michaelhaxhiu
    Contributor

    small note: re-assigning as I'm OOO till next week

  11. maddylewis commented on May 9, 2023

    @maddylewis
    Contributor

    i am OOO starting tomorrow but will try to repro this / push this along

  12. 43 remaining items

  13. kidroca commented on May 24, 2023

    @kidroca
    Contributor
    1. Will there be a case where the FlatList component ignores the windowSize prop due to lack of system resources, I feel there's a possibility of this so the application wouldn't crash.

    While it's a valid consideration, it's important to note that the FlatList component isn't designed to manage system resources. Instead, it will simply attempt to perform the task we've specified. If we start to notice any performance degradation, it will be our responsibility to revisit and refine our solution accordingly.

    2. This approach could slow down the system as more and more attachments are added especially if they are heavy. And again there may be a possibility that FlatList destroys attachments to prevent crashes.

    Indeed, an excessive number of image attachments could pose a performance challenge. However, this would only be an issue if the user scrolls past hundreds of images. We can manage this effectively by setting an upper limit.

    const desiredSize = Math.max(5, this.renderedItems.size);
    const windowSize = Math.min(100, desiredSize);

    This boundary ensures that the window doesn't grow beyond a manageable size. While technically the issue could still occur, we have to ask: would a user realistically zoom into an attachment, scroll 100 attachments away, and then scroll back?

    A potential complementary or standalone solution to the virtual window size could involve using react-freeze. This package preserves the component state while replacing off-screen items with a low-cost placeholder. It's a method we're already employing in a different part of the app.
    We could render items outside the immediate view area as 'frozen' with the following:

    renderItem({ item, index }) {
      const diff = Math.abs(index - this.state.page);
      if (diff > 2) // return FreezedItem
      else // return RegularItem
    }

    This approach would ensure that off-screen items don't overly tax system resources while maintaining user experience continuity.

  14. fedirjh commented on May 26, 2023

    @fedirjh
    Contributor

    Hey @kidroca Thank you for clarifying the issue with the windowSize and for providing an alternative approach. Your suggestion of maintaining a Set of rendered items and dynamically adjusting the windowSize based on the number of rendered items is an interesting solution. The only concern is that we may have more memory consumption. I think we can limit the max windowSize to just 20 or maybe 10. It doesn’t looks realistic for a user to scroll 10 attachments away, and then scroll back and expects to get the maintained zoom state.

  15. fedirjh commented on May 26, 2023

    @fedirjh
    Contributor

    Thank you for the engaging and insightful discussion, @kidroca and @therealsujitk! 🙌🏼 . Taking into account the concerns raised, it prompts me to question whether addressing this issue is truly necessary. Upon evaluating the current behavior, it seems that the actual result is quite satisfactory. Users are experiencing a good and reasonable interaction, with the attachment state (including zoom and scroll position) being successfully preserved for the last 3 visited attachments.

    I would appreciate hearing @MonilBhavsar's thoughts on this issue. Your input would be valuable in determining whether further action is required.

  16. melvin-bot commented on May 26, 2023

    @melvin-bot

    📣 It's been a week! Do we have any satisfactory proposals yet? Do we need to adjust the bounty for this issue? 💸

  17. MonilBhavsar commented on May 29, 2023

    @MonilBhavsar
    Contributor

    The only concern is that we may have more memory consumption. I think we can limit the max windowSize to just 20 or maybe 10. It doesn’t looks realistic for a user to scroll 10 attachments away, and then scroll back and expects to get the maintained zoom state.

    I agree with this 👍

  18. MonilBhavsar commented on May 29, 2023

    @MonilBhavsar
    Contributor

    Users are experiencing a good and reasonable interaction, with the attachment state (including zoom and scroll position) being successfully preserved for the last 3 visited attachments.

    Well, I personally think it is fair to maintain zoomed state for last 3 visited attachments. cc Bug-zero team - @maddylewis @miljakljajic @michaelhaxhiu
    If we agree, we can :donothing: and consider this as "a feature and not a bug"

  19. melvin-bot commented on May 29, 2023

    @melvin-bot

    @MonilBhavsar @fedirjh @maddylewis this issue is now 3 weeks old. There is one more week left before this issue breaks WAQ and will need to go internal. What needs to happen to get a PR in review this week? Please create a thread in #expensify-open-source to discuss. Thanks!

  20. maddylewis commented on May 31, 2023

    @maddylewis
    Contributor

    @MonilBhavsar - i think we can consider this a feature and not a bug!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugDailyExternalHelp Wanted

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions