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

When you long-press on a message in iOS mobile, the message moves out of the screen frame and is hidden (iOS only) #16356

Conversation

terrysahaidak
Copy link
Contributor

@terrysahaidak terrysahaidak commented Mar 21, 2023

Details

Important notice

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
Simulator.Screen.Recording.-.iPhone.15.Pro.-.2024-01-29.at.14.41.55.mp4
Simulator.Screen.Recording.-.iPhone.15.Pro.-.2024-01-29.at.14.42.56.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
Simulator.Screen.Recording.-.iPhone.15.Pro.-.2024-01-29.at.14.47.17.mp4
Simulator.Screen.Recording.-.iPhone.15.Pro.-.2024-01-29.at.14.49.27.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
Simulator.Screen.Recording.-.iPhone.15.Pro.-.2024-01-29.at.14.51.22.mp4
Simulator.Screen.Recording.-.iPhone.15.Pro.-.2024-01-29.at.14.52.04.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 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: GH_LINK_ISSUE(COMMENT)

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
  5. Pressing on reaction icon on the message should push the message above it too.
  6. Long press on the message and then pressing on the reactions icon should open the reactions sheet and if the message is covered by the emoji picker it should animate the message so it's above the picker too

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 / Chrome
    • iOS / native
    • iOS / 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 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 correct English and approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • 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 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
      • 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(themeColors.componentBG)
  • 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 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 author checklist, including those that don't apply to this PR.

Screenshots/Videos

Web
Mobile Web - Chrome
Mobile Web - Safari
Desktop
iOS
Android

@github-actions
Copy link
Contributor

github-actions bot commented Mar 21, 2023

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@terrysahaidak terrysahaidak changed the title WIP iOS When you long-press on a message in iOS mobile, the message moves out of the screen frame and is hidden Mar 21, 2023
@terrysahaidak
Copy link
Contributor Author

I have read the CLA Document and I hereby sign the CLA

@terrysahaidak
Copy link
Contributor Author

recheck

@twisterdotcom
Copy link
Contributor

Assigning a C+ here as well

@roryabraham
Copy link
Contributor

@shubham1206agra I think before you dive in here, we should wait for consensus on #35270

@cubuspl42
Copy link
Contributor

  • Conflicts
  • "Verify signed commits"

popoverHeight?: number;
height?: number;
composerHeight?: number;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Add a blank line? Here and other similar places. I think we do that in general.

Copy link
Contributor

Choose a reason for hiding this comment

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

In this file and other files


const invertedKeyboardHeight = keyboard.state.value === KeyboardState.CLOSED ? lastKeyboardHeight : 0;

let elementOffset = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

  • const + ternary?
  • ...or a local function?

// when the sate is not idle we know for sure we have previous state
const previousPayload = previous.payload ?? {};

let previousElementOffset = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

  • const + ternary?
  • ...or a local function?

@cubuspl42
Copy link
Contributor

I understand that this is conflict-prone. So I guess we'll need to parallelize review and testing with conflict resolution.

@kirillzyusko Would you handle minor comments I left and ensure to keep all commits signed? We'll need at least one force-push to fix the commit signing thing.

@kirillzyusko
Copy link
Contributor

@cubuspl42 @roryabraham I couldn't sign unsigned commits 😔 All ways come to rebase - and it's close to unreal to rebase 137 commits over 50000 other commits (we literally have incompatible changes even in first commit, because from 5 modified files 4 were removed (migrated to TS I guess) - so it's unbelievably difficult to rebase these changes to latest branch.

I suggest to keep this PR in the current shape and move all work in a new PR with squashed commit - #38232

Such approach has several benefits:

  • In case if we need to have an exact history of changes we always can go to this PR and see why a particular commit was added
  • we'll not send an unbelievable amount of time for rebasing and further post fixing
  • I opened a PR, so I have all access to PR description etc, so we don't need to involve more people to change PR description, check/uncheck mark etc.

Let me know what do you think 😊

@kirillzyusko
Copy link
Contributor

@kirillzyusko Would you handle minor comments I left and ensure to keep all commits signed? We'll need at least one force-push to fix the commit signing thing.

@cubuspl42 yeah, sure, let's come to conclusion what should we od with unsigned commits and then I'll address your comments 👍

@cubuspl42
Copy link
Contributor

Expensify has a policy of accepting only signed commits to the main branch, and I don't think this situation is special enough to make an exception.

From my perspective, squashing is fine.

@kirillzyusko
Copy link
Contributor

Expensify has a policy of accepting only signed commits to the main branch, and I don't think this situation is special enough to make an exception.

Yeah, I know - and I agree that it's not exceptional situation, that's why I proposed squashing and new PR (to keep history of changes) 😅

Waiting for @roryabraham feedback 👀

@roryabraham
Copy link
Contributor

closing this in favor of #38232

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.