Skip to content

Fix iOS Track distance screen blink on swipe back#97020

Draft
MobileMage wants to merge 3 commits into
Expensify:mainfrom
MobileMage:MobileMage/fix-94904-ios-discard-blink
Draft

Fix iOS Track distance screen blink on swipe back#97020
MobileMage wants to merge 3 commits into
Expensify:mainfrom
MobileMage:MobileMage/fix-94904-ios-discard-blink

Conversation

@MobileMage

@MobileMage MobileMage commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Explanation of Change

On iOS, edge-swiping back on an IOU step screen (Track distance, amount, hours, manual distance) flashes the screen before it dismisses. The shared discard-changes hook armed react-navigation's removal guard with a hard-coded usePreventRemove(true, ...), which iOS maps to preventNativeDismiss = true. On a clean screen with nothing to discard, the native swipe still gets cancelled and snaps back before JS re-dispatches the pop, and that snap-back is the blink.

This PR ties the guard to whether the screen has unsaved changes. The native hook now drives usePreventRemove from a shouldPreventRemove state, recomputed from the screen's dirtiness in a callback and effect rather than by reading refs during render, so the hook still compiles under React Compiler. The recompute runs after every commit and is deferred one tick past it, so ref-backed inputs (which update their child's state in the same event) are read after they settle. A clean screen dismisses in one native animation with no blink; a dirty screen still blocks the swipe and shows the discard modal, and the in-callback check still lets an intentional save pass through.

Screens whose dirtiness transitions come with a re-render are covered by the every-commit recompute. Screens that go dirty without re-rendering at all call the hook's new recheckUnsavedChanges from their change handler: IOURequestStepAmount (via MoneyRequestAmountForm forwarding onAmountChange, including the +/- sign flip, which bypassed the input's change handler), IOURequestStepHours, IOURequestStepDistanceManual, and the distance step's Manual tab in IOURequestStepDistance. The web hook keeps its behavior (it reads dirtiness in useBeforeRemove at navigation time) and returns a stable no-op recheck for the shared type. Android's back handling reads dirtiness on its own rather than through preventRemove, and header-back taps flow through the same re-armed guard.

Reviewer note: the accepted proposal's literal usePreventRemove(hasUnsavedChanges()) reads a ref during render, which fails the repo's React Compiler compliance gate (main compiles this hook today, so that would be a regression). This PR implements the same behavior through a shouldPreventRemove state recomputed in a callback/effect instead.

Fixed Issues

$ #94904
PROPOSAL: #94904 (comment)

Tests

  1. Open the Expensify app.
  2. Tap the FAB button > Track distance.
  3. Tap Odometer.
  4. Swipe back.
  5. Verify the Inbox or previous screen is displayed, and the "Track distance" screen does not appear or blink.
  6. Repeat, but enter a value on the step before swiping back, and verify the discard-changes modal appears.
  7. On the Odometer tab, type a single digit into a reading and immediately swipe back. Verify the discard modal appears.
  8. Edit a map-distance expense, open the Manual tab, type a distance, swipe back. Verify the discard modal appears.
  9. On the amount step of a policy that allows negative amounts, tap only the +/- flip button, then swipe back. Verify the discard modal appears.
  • Verify that no errors appear in the JS console

Offline tests

No offline-specific behavior. The change is client-side navigation only.

  • Verify that no errors appear in the JS console

QA Steps

Same as Tests.

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

Screenshots/Videos

Videos will be added in a few hours.

Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari

usePreventRemove was hard-coded to true, so iOS set preventNativeDismiss
even with nothing to discard: the edge-swipe was cancelled and snapped
back before JS popped the screen. Drive the guard from a shouldPreventRemove
state that only reflects real unsaved changes, recomputed in a callback/effect
(never reading refs during render, so React Compiler still compiles the hook).
Ref-based input screens call recheckUnsavedChanges after each keystroke so
the guard re-arms. Fixes Expensify#94904.
@codecov

codecov Bot commented Jul 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Looks like you've decreased code coverage for some files. Please write tests to increase, or at least maintain, the existing level of code coverage. See our documentation here for how to interpret this table.

Files with missing lines Coverage Δ
...ooks/useDiscardChangesConfirmation/index.native.ts 100.00% <100.00%> (ø)
...rc/pages/iou/request/step/IOURequestStepAmount.tsx 89.83% <100.00%> (+1.69%) ⬆️
.../pages/iou/request/step/IOURequestStepDistance.tsx 77.82% <100.00%> (+2.60%) ⬆️
src/pages/iou/request/step/IOURequestStepHours.tsx 83.78% <100.00%> (+2.70%) ⬆️
src/hooks/useDiscardChangesConfirmation/index.ts 93.47% <50.00%> (-2.08%) ⬇️
src/pages/iou/MoneyRequestAmountForm.tsx 85.22% <50.00%> (+1.31%) ⬆️
.../iou/request/step/IOURequestStepDistanceManual.tsx 0.00% <0.00%> (ø)
... and 47 files with indirect coverage changes

The preventRemove state could read one input event behind (or never arm)
for screens whose typed value lives in a child input ref:

- Defer the recompute past the commit so it reads the child's settled
  state instead of the previous render's value (single digit or paste
  then back would have dismissed with no prompt).
- Re-evaluate after every commit instead of only when the memoized
  dirtiness callback changes identity, which covers screens like the
  odometer step whose refs move together with state the compiler
  does not track through the callback.
- Wire recheckUnsavedChanges into the distance step's manual tab, which
  never re-renders on typing.
- Notify the parent from the amount sign flip, which bypassed the
  input's change handler entirely.
- Replace the web hook's counter reducer with a stable no-op so amount,
  hours, and manual-distance screens stop re-rendering per keystroke.
- Pin the recheck arming behavior in the native hook tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant