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: messages content overlap when bottom sheet is shown #42143

Open
wants to merge 81 commits into
base: main
Choose a base branch
from

Conversation

kirillzyusko
Copy link
Contributor

@kirillzyusko kirillzyusko commented May 14, 2024

Details

A successor of #16356

The first step of react-native-keyboard-controller migration plan

Important

This PR relies on react-native-keyboard-controller (useKeyboardHandler) hook and KeyboardAvoidingView component).

How it works 🤔

This PR is iOS only for now. Other platforms are not affected. Also, in this explanation, we are talking about iOS only since on Android, iOS web and Android Web keyboard handling works differently.

Note

We're talking about default platform behavior. If we start to use react-native-keyboard-controller on Android - it'll disable all default keyboard handling by OS and will delegate the keyboard handling to us - so we will have identical behavior across both iOS/Android platforms.

But it may introduce additional issues (because we need to enter edge-to-edge mode - that would be good to do on entire app level, but it may bring some issues with StatusBar paddings, so everything has to be verified very carefully). That's why I think it would be better to handle in separate PR.

When we long press on a message, we want to be able to see it. It's easy when we have the keyboard closed. But gets tricky when the keyboard is open.

But first of all let's focus on the case when the keyboard is closed. User long press on the message, we render the bottom sheet (action sheet) with some options. If the message is higher than the height of bottom sheet - the message is visible. When the message is the last one (first from the bottom), it can be fully or partially covered by the bottom sheet.

Press to see videos
Message above bottom sheet Message and bottom sheet overlap
300481023-9dc8be61-799d-470b-997f-ee5c5995fe19.mp4
300481277-0d0570cd-a85f-4cf5-8a91-6a3d5ad72c58.mp4

Also everything is tricky when we have keyboard open 😓

Press to see videos with keyboard
emoji picker instead of the keyboard (the messages should not jump) showing a message which is covered by the keyboard and the bottom sheet
300482884-cbe3b610-fa85-4071-a3dd-bbc1a9d56e05.mp4
300483006-2e5ba833-c0fa-4fad-b383-b3854e5fa112.mp4

To achieve this goal we had to:

  • use our own scroll component on iOS for FlatList of ReportListView (it uses reanimated to move the view by the offset)
  • use KeyboardAvoiding so we're in sync with keyboard-avoiding style updates
  • implement animated style handling using a state machine
  • create ActionSheetAwareScrollViewContext to pass the transition function, so we can transition from one state to another by action triggered from different parts of the application

Below you can find more use cases that involves keyboard and other UI elements interactions:

Interactions with other UI elements
Add attachment Call popover
300483536-04991f52-a41b-4d3e-abaf-12712b94f1aa.mp4
300483723-91b076a9-4c7a-4fbb-bc50-1e8194763e9d.mp4

Note

Call popover, bottom sheet and emoji picker are implemented as Modal windows and on iOS keyboard instantly gets hidden when modal window is shown. But "Attachment" popup is implemented not as modal, so for this type of transition we are using progress-based animations. When we press on plus -> popup is already shown under keyboard and we dismiss the keyboard. Overall the mechanism is similar to what we've used in the past, except the moment where we substitute translationY value - in case when keyboard disappear/appears we interpolate it by progress variable to achieve smooth transitions.

Technical details ⚒️

So in order to calculate the offset for the message, we:

  1. Measure the message item in the window using ref.current.measureInWindow, so we can have height and y coordinate of the image
  2. Measure the height of the bottom sheet using onLayout callback
  3. Get the window height from Dimensions
  4. Get safeArea.top since y coordinate doesn't get into account safe area
  5. Calculate offset as elementOffset = (y + safeArea.top + itemHeight) - (windowHeight - popoverHeight)
    In order to animate item when they keyboard was open, we need to also know its height which is provided by useAnimatedKeyboard hook from react-native-reanimated, but we should always subtract safeArea.bottom from it since it's not part of the calculation for the offset, but only keyboardHeight.

Overall, the keyboard is the tricky part. On iOS, the keyboard doesn't resize the viewport. So for the content to not be covered by the keyboard, KeyboardAvoidingView is used. In React Native, KeyboardAvoidingView is implemented as a view that changes its height or resizes the children's view by the keyboard height. It's done using RN's Layout Animations. This results in the animation for the keyboard being applied using the native event emitter callback which uses the bridge. So open/close events of the keyboard will always be delayed that resulting in the delayed keyboard avoiding animation and, which is more important, scheduling layout animations.

In order to be consistent 100% of the time with animation and timings, we had to:

  • use KeyboardAvoidingView from react-native-keyboard-controller for iOS. It reacts on keyboard height/state change on UI thread in the same frame, so we can schedule an update for styles in the next frame
  • use just useDerivedValue for all the logic, since introducing useAnimatedReaction or having multiple shared values will delay updates for 1 frame, but we want to be 100% in sync with keyboard updates
  • use paddingTop: translateY (because ScrollView is inverted).

Another important aspect of the implementation is the usage of the state machine for the animation. State machine allows us to specify the chain of events and how to handle them, and more importantly what actions to ignore, for example:

  1. Initial state is idle. it can react to KEYBOARD_OPEN action
  2. we open emoji picker. it sends EMOJI_PICKER_OPEN action
  3. there is no handling for this action in idle state so we do nothing
  4. we close emoji picker and it sends EMOJI_PICKER_CLOSE action which again does nothing
  5. we open keyboard. it sends KEYBOARD_OPEN action. idle can react to this action by transitioning into keyboardOpen state
  6. our state is keyboardOpen. it can react to KEYBOARD_CLOSE, EMOJI_PICKER_OPEN actions
  7. we open emoji picker again. it sends EMOJI_PICKER_OPEN action which transitions our state into emojiPickerOpen state. now we react only to EMOJI_PICKER_CLOSE action
  8. before rendering the emoji picker, the app hides the keyboard. it sends KEYBOARD_CLOSE action. but we ignore it since our emojiPickerOpen state can only handle EMOJI_PICKER_CLOSE action. so we write the logic for handling 9. hiding the keyboard but maintaining the offset based on the keyboard state shared value
  9. we close the picker and send EMOJI_PICKER_CLOSE action which transitions us back into keyboardOpen state.

Fixed Issues

$ #10632
PROPOSAL: N/A

Tests

These steps were performed on all the platforms. The new behavior is iOS only for now.

  1. Open chat
  2. Long press on the first message - message should be pushed up to be displayed above the bottom sheet
  3. Long press on the top message - if the message is not covered by the bottom sheet, it should stay in place
  4. Long press on any attachment (document, message) should result in the same behavior

Before each next we make sure the keyboard is open:

  1. Long press on the message should either animate the message above the bottom sheet or keep it in place
  2. Messages should not "jump" because of keyboard hide/open when showing bottom sheet, emoji picker
  3. Press on emoji picker in the composer, messages should stay in place even though the keyboard was closer - emoji picker is rendered instead of keyboard
  4. Long press on the mesage, click on add reactions - close emoji reactions
  • Verify that no errors appear in the JS console

Offline tests

Should not affect the offline state.

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
    • MacOS: Desktop
  • 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 any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • 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 shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I verified the translation was requested/reviewed in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • 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)
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • 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)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • 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 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.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • 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

Android: Native
telegram-cloud-document-2-5460627828225625066.mp4
Android: mWeb Chrome
telegram-cloud-document-2-5460627828225625138.mp4
iOS: Native
1104ff6c-6eba-4295-a905-6caaaf3edf28.mp4
iOS: mWeb Safari
6eff007f-3c61-4f19-a60e-43f46d2f0e61.mp4
MacOS: Chrome / Safari
e5e28bd8-529f-4da3-8ae7-bff03bd14499.mp4
MacOS: Desktop
2c813bc9-6988-4fbc-8f36-3282c9cecd82.mp4

@kirillzyusko kirillzyusko requested a review from a team as a code owner May 14, 2024 09:59
@melvin-bot melvin-bot bot removed the request for review from a team May 14, 2024 09:59
Copy link

melvin-bot bot commented May 14, 2024

@Santhosh-Sellavel Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@kirillzyusko kirillzyusko force-pushed the fix/10632-when-long-press-on-message-add-space-for-image-squashed-commits branch from 54ae3d2 to 00d252f Compare May 14, 2024 11:26
ios/Podfile.lock Outdated Show resolved Hide resolved
@shubham1206agra
Copy link
Contributor

Tagging @rojiphil here.

@kirillzyusko
Copy link
Contributor Author

Just a quick update - after enabling new architecture I see very low FPS for animation:

Simulator.Screen.Recording.-.iPhone.15.Pro.-.2024-05-14.at.18.11.00.mp4

Currently trying to understand why it works so slow and what needs to be changed to speed it up 👀

Also found a crash and fixed it in kirillzyusko/react-native-keyboard-controller#444

Keep working on this PR 💪

@kirillzyusko kirillzyusko force-pushed the fix/10632-when-long-press-on-message-add-space-for-image-squashed-commits branch from 55eef53 to acfbdcb Compare June 4, 2024 15:32
@kirillzyusko
Copy link
Contributor Author

I continue to work on this problem. Still investigating why we observe performance drop - the problem is that even if we don't change layout of list/footer etc. and run a simple animation of red square (via translateY property), then it's still laggy 🤯

Screen.Recording.2024-06-06.at.13.02.07.mov

I think this investigation will take more time, than I initially expected.

@roryabraham
Copy link
Contributor

@kirillzyusko I'm going to mark this as WIP, feel free to remove the prefix when you feel it's ready for further reviews

@roryabraham roryabraham changed the title fix: messages content overlap when bottom sheet is shown [WIP] fix: messages content overlap when bottom sheet is shown Jun 6, 2024
…/10632-when-long-press-on-message-add-space-for-image-squashed-commits
Copy link
Contributor

@mountiny mountiny left a comment

Choose a reason for hiding this comment

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

Obviously it would be nice to add some tests here but I am not that close to these changes to suggest some meaningful tests. @kirillzyusko @shubham1206agra @rojiphil any helper methods where it would make sense to add tests?

@mountiny
Copy link
Contributor

The eslint check is failing @shubham1206agra @rojiphil what is left to test and fix on this PR?

@kirillzyusko
Copy link
Contributor Author

Btw, do we need to add unit tests to this change?

One thing I can think of is adding unit tests for useWorkletStateMachine, but I don't know if it makes a lot of sense for this hook 🤔

Changes from this PR touches mostly animated transitions and it's unreal to cover such changes via unit tests. Let me know what do you think 😊

@rojiphil
Copy link
Contributor

So, that leaves us with the button padding issue

I think exactly these changes added in 31ba00b were aimed to fix the problem described in #42143 (comment)

However it's not a problem of this PR (I mean in this PR the problem becomes clearly visible, but for example after merging edge-to-edge PR the same problem happens on Android right now).

Basically the fix in #44761 produced these jumps now. My understanding is that we shouldn't try to fix each problem related to the keyboard in this PR, right? Do you think it would be better to revert the changes that I made and fix this "jump" problem in a separate PR?

Yeah. Reverting makes sense to me as this is not a problem of this PR. Also, this PR is already having a lot of changes. So, it is better to get this to the shore as early as possible and address other issues as follow-up.

The eslint check is failing @shubham1206agra @rojiphil what is left to test and fix on this PR?

The eslint check failure is due to the presence of withOnyx which needs a migration to useOnyx. I think even this can be taken up later to avoid clutter here.

Changes from this PR touches mostly animated transitions and it's unreal to cover such changes via unit tests.

This makes good sense to me. I think we can skip this unless we find good reasons to have it.

@rojiphil
Copy link
Contributor

what is left to test and fix on this PR?

@mountiny I think we are very close to getting this done. Once the revert for the button padding issue is done, I can complete the checklist.

@kirillzyusko
Copy link
Contributor Author

@rojiphil I reverted changes, feel free to continue with remaining part of review 😊

Copy link
Contributor

🚧 @mountiny has triggered a test build. You can view the workflow run here.

Copy link
Contributor

Copy link
Contributor

@mountiny mountiny left a comment

Choose a reason for hiding this comment

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

I think it might be good to add a unit test for the worklet, the logic in the useAnimatedKeyboard depends on it so it would be nice to capture the expected states in the tests

Copy link
Contributor

@luacmartins luacmartins left a comment

Choose a reason for hiding this comment

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

Nice! Code looks pretty good. I think we're in a good spot to merge once @rojiphil is done testing.

@rojiphil
Copy link
Contributor

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible 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 checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified tests pass on all platforms & I tested again on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
    • MacOS: Desktop
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick).
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • 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 shown in the product is localized by adding it to src/languages/* files and using the translation method
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • 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)
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • 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 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.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • 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.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

iOS: Native
42143-ios-native-002.mp4
iOS: mWeb Safari
42143-mweb-safari-001.mp4
Android: Native
42143-android-native-001.mp4
Android: mWeb Chrome
42143-mweb-chrome-001.mp4
MacOS: Chrome / Safari
42143-web-safari-001.mp4
MacOS: Desktop
42143-desktop-001.mp4

Copy link
Contributor

@rojiphil rojiphil left a comment

Choose a reason for hiding this comment

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

Thanks @kirillzyusko for the changes and the only pending review comments to be addressed are here

@mountiny @luacmartins Changes LGTM and tests well too.
Although not related to our PR here but there is a console warning that comes up for which I have raised the issue in slack here.
All yours now. Thanks.

luacmartins
luacmartins previously approved these changes Nov 27, 2024
@mountiny
Copy link
Contributor

Both the reassure tests and lint failures seem related and should be addressed

@kirillzyusko
Copy link
Contributor Author

Tests started to fail after 6140d8f

I'll investigate why on Monday - now will focus on fixing all new issues coming from edge-to-edge mode

@shubham1206agra
Copy link
Contributor

@kirillzyusko Merge main, so we can retest with nav changes.

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

Successfully merging this pull request may close these issues.

10 participants