Skip to content
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
16 changes: 7 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
"react-native-linear-gradient": "^2.8.1",
"react-native-localize": "^2.2.6",
"react-native-modal": "^13.0.0",
"react-native-onyx": "1.0.52",
"react-native-onyx": "1.0.58",
"react-native-pager-view": "^6.2.0",
"react-native-pdf": "^6.6.2",
"react-native-performance": "^4.0.0",
Expand Down
6 changes: 1 addition & 5 deletions src/components/EmojiPicker/EmojiPicker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {useState, useEffect, useRef, forwardRef, useImperativeHandle} from 'react';
import {Dimensions, Keyboard} from 'react-native';
import {Dimensions} from 'react-native';
import _ from 'underscore';
import EmojiPickerMenu from './EmojiPickerMenu';
import CONST from '../../CONST';
Expand Down Expand Up @@ -119,10 +119,6 @@ const EmojiPicker = forwardRef((props, ref) => {
useImperativeHandle(ref, () => ({showEmojiPicker, isActiveReportAction, hideEmojiPicker, isEmojiPickerVisible, resetEmojiPopoverAnchor}));

useEffect(() => {
if (isEmojiPickerVisible) {
Keyboard.dismiss();
}
Comment on lines -122 to -124

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What issue was fixed by this removal?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This one #24041 (comment)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This was originally added in #8221 to fix emoji select on first tap not working on android.
I confirmed that's not reproducible after revert.

8.mov

@ntdiary ntdiary Aug 24, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi, in my test, after removing this code, the keyboard no longer appears when dismissing the emoji modal on Android native.

test.mp4

How about moving Keyboard.dismiss() into the onPress handler in EmojiPickerButton? This could fix #24041 (comment) without impacting Android platform.

onPress={() => {
if (!EmojiPickerAction.emojiPickerRef.current.isEmojiPickerVisible) {
EmojiPickerAction.showEmojiPicker(props.onModalHide, props.onEmojiSelected, emojiPopoverAnchor.current, undefined, () => {}, props.reportAction);
} else {
EmojiPickerAction.emojiPickerRef.current.hideEmojiPicker();
}
}}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hey @ntdiary

You are saying

the keyboard no longer appears

The code here is about dismissing the keyboard. I'd never expect this code to open the keyboard?

@ntdiary ntdiary Aug 24, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

image

Ah, this may seem a bit unbelievable 😂, but let me explain a bit more: as shown in the image above, for android, if we don't manually call blur ( or dismiss), the focus state inside the composer input box will not change when opening the emoji modal. Then after we close the modal, even though we call focus on the composer input box, because the internal focus state did not change, the part where the keyboard is supposed to show gets ignored.

So here, dismiss can clean up the internal focus state, allowing the subsequent focus to successfully show the keyboard.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

oh wow, I didn't know that, thanks for sharing!! So you say we should add this code back? 😊

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I was also surprised when I first discovered this android behavior. And don't worry, I just made a note here, we still have another issue and may do something there as needed. 😄


const emojiPopoverDimensionListener = Dimensions.addEventListener('change', () => {
if (!emojiPopoverAnchor.current) {
// In small screen width, the window size change might be due to keyboard open/hide, we should avoid hide EmojiPicker in those cases
Expand Down
1 change: 0 additions & 1 deletion src/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ const defaultProps = {
isSubmitButtonVisible: true,
formState: {
isLoading: false,
errors: null,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

NAB: I assume crash was fixed in onyx repo. Now what's this removal for?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The crash wasn't fixed in the onyx repo. It is invalid when looking at the logic of the component to have errors as null. It's never expected to be null.
Its a fix for this bug:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Can't find the exact bug ticket at the moment. I know it was one of the bugs that was found during going through the regressions here: https://expensify.slack.com/archives/C01GTK53T8Q/p1690897222550089

Have to leave for vacation in a few minutes so I can't find it right now, sorry 😓

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nvm. Hopefully this PR and #18648 would be merged without further change requests from you. Have a good vacation!

},
draftValues: {},
enabledWhenOffline: false,
Expand Down
14 changes: 2 additions & 12 deletions src/components/OptionsSelector/BaseOptionsSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,6 @@ class BaseOptionsSelector extends Component {
);

this.scrollToIndex(this.props.selectedOptions.length ? 0 : this.state.focusedIndex, false);

if (!this.props.autoFocus) {
return;
}

if (this.props.shouldShowTextInput) {
if (this.props.shouldDelayFocus) {
this.focusTimeout = setTimeout(() => this.textInput.focus(), CONST.ANIMATED_TRANSITION);
} else {
this.textInput.focus();
}
}
}

componentDidUpdate(prevProps) {
Expand Down Expand Up @@ -339,6 +327,8 @@ class BaseOptionsSelector extends Component {
selectTextOnFocus
blurOnSubmit={Boolean(this.state.allOptions.length)}
spellCheck={false}
autoFocus={this.props.autoFocus}
shouldDelayFocus={this.props.shouldDelayFocus}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Confirmed that this fixes Keyboard opens multiple times when selecting members to request money.
Curious about the root cause though?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is mostly an order of event issue.
By this we call textInputRef.focus in the useEffect of the BaseTextInput.js component. This call will happen later than a call in componentDidMount. A (simplified) general rule in react is is that componentDidMount will always run before any useEffect hooks in the current batch of work.

Now, when we navigate to a new screen there is some code in react-navigation that will dismiss the keyboard when we transition to a new screen. When the focus happens on the class component componentDidMount the order of events is the following:

  • Transition to a new screen starts
  • Component gets rendered, calls .focus(). The keyboard starts to open
  • Transition causes Keyboard.dismiss to get called
  • (If we are lucky in terms of timing), the timeout of selectTextOnFocus kicks in and focuses the input again (keyboard opens again) [if you are curious check the JS code of the react-native-web textinput component]

Now, we call the focus from the useEffect, and the order of events is roughly this:

  • Transition to a new screen starts
  • Component gets rendered
  • Transition causes Keyboard.dismiss to get called
  • useEffect fires focusing the input

/>
);
const optionsList = (
Expand Down
54 changes: 41 additions & 13 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ Onyx.connect({
/**
* Reset money request info from the store with its initial value
* @param {String} id
* @returns {Promise}
*/
function resetMoneyRequestInfo(id = '') {
const date = currentDate || moment().format('YYYY-MM-DD');
Onyx.merge(ONYXKEYS.IOU, {
return Onyx.merge(ONYXKEYS.IOU, {
id,
amount: 0,
currency: lodashGet(currentUserPersonalDetails, 'localCurrencyCode', CONST.CURRENCY.USD),
Expand Down Expand Up @@ -1464,53 +1465,61 @@ function payMoneyRequest(paymentType, chatReport, iouReport) {
* Initialize money request info and navigate to the MoneyRequest page
* @param {String} iouType
* @param {String} reportID
* @returns {Promise}
*/
function startMoneyRequest(iouType, reportID = '') {
resetMoneyRequestInfo(`${iouType}${reportID}`);
Navigation.navigate(ROUTES.getMoneyRequestRoute(iouType, reportID));
return resetMoneyRequestInfo(`${iouType}${reportID}`).then(() => {
Navigation.navigate(ROUTES.getMoneyRequestRoute(iouType, reportID));
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's pretty uncommon to have to wait for a merge to navigate. If we are going to chain off of Onyx.merge() I think we should leave some kind of comment to explain why this is the exception to the rule.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think the problem here is that now when we have withOnyx improvement we may sometimes read outdate value. For instance we have a screen that operates on value "X". Let say we had a flow that first modifies the value "X" then navigates to our screen. Before what was happening is withOnyx wrapped screen was waiting for onyx values by waiting for connect method and then info about value "X". in connect method there were a lot of promises so we were sure that our modification was succeeded before we actually rendered content of the new screen. Now it's possible that we will modify the "X" navigate to screen read previous value of "X" from cache then once modification takes place we render the component one more time.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

waiting for merge prevents reading outdate value from what I understand

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we can easily miss some cases as previously we didn't need to care about it as we were sure that we won't read from onyx before the modification happen.
Maybe we should persist what is stale and don't read from cache when it is or indicate that we don't worry about reading outdated data.

@Szymon20000 Szymon20000 Aug 16, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe we can do it in a bit safer way (but you better now how many such cases we can have in the app. Maybe those from the pr are the only ones)
If we don't mind reading outdated data (want to show messages as soon as possible we can add a flag)

withOnyx({
  allowReadingStaleData: true
})

Then on every merge/set/remove we update two simple maps so we know if the data in cache is up to date
StaleStorageLastOperation[key] = operation_number_that_modifies_the_key
toDoOperations.push(operationId)
Then
isDataStale = (key) => (toDoOperation.has(StaleStorageLastOperation[key])).

if the data is stale we don't get it from cache in withOnyx wrapper unless someone set allowReadingStaleData to true

}

/**
* @param {String} id
* @returns {Promise}
*/
function setMoneyRequestId(id) {
Onyx.merge(ONYXKEYS.IOU, {id});
return Onyx.merge(ONYXKEYS.IOU, {id});
}

/**
* @param {Number} amount
* @returns {Promise}
*/
function setMoneyRequestAmount(amount) {
Onyx.merge(ONYXKEYS.IOU, {amount});
return Onyx.merge(ONYXKEYS.IOU, {amount});
}

/**
* @param {String} currency
* @returns {Promise}
*/
function setMoneyRequestCurrency(currency) {
Onyx.merge(ONYXKEYS.IOU, {currency});
return Onyx.merge(ONYXKEYS.IOU, {currency});
}

/**
* @param {String} comment
* @returns {Promise}
*/
function setMoneyRequestDescription(comment) {
Onyx.merge(ONYXKEYS.IOU, {comment: comment.trim()});
return Onyx.merge(ONYXKEYS.IOU, {comment: comment.trim()});
}

/**
* @param {Object[]} participants
* @returns {Promise}
*/
function setMoneyRequestParticipants(participants) {
Onyx.merge(ONYXKEYS.IOU, {participants});
return Onyx.merge(ONYXKEYS.IOU, {participants});
}

/**
* @param {String} receiptPath
* @param {String} receiptSource
* @returns {Promise}
*/
function setMoneyRequestReceipt(receiptPath, receiptSource) {
Onyx.merge(ONYXKEYS.IOU, {receiptPath, receiptSource});
return Onyx.merge(ONYXKEYS.IOU, {receiptPath, receiptSource});
}

function createEmptyTransaction() {
Expand All @@ -1530,10 +1539,11 @@ function createEmptyTransaction() {
function navigateToNextPage(iou, iouType, reportID, report) {
const moneyRequestID = `${iouType}${reportID}`;
const shouldReset = iou.id !== moneyRequestID;
const promises = [];
// If the money request ID in Onyx does not match the ID from params, we want to start a new request
// with the ID from params. We need to clear the participants in case the new request is initiated from FAB.
if (shouldReset) {
resetMoneyRequestInfo(moneyRequestID);
promises.push(resetMoneyRequestInfo(moneyRequestID));
}

// If a request is initiated on a report, skip the participants selection step and navigate to the confirmation page.
Expand All @@ -1547,12 +1557,29 @@ function navigateToNextPage(iou, iouType, reportID, report) {
.filter((accountID) => currentUserAccountID !== accountID)
.map((accountID) => ({accountID, selected: true}))
.value();
setMoneyRequestParticipants(participants);
promises.push(setMoneyRequestParticipants(participants));
}
Navigation.navigate(ROUTES.getMoneyRequestConfirmationRoute(iouType, reportID));
Promise.all(promises).then(() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Similar comment here. Pushing all these merge promises into the array has made this code harder to reason about I think. We should document every reason why we will need to await an Onyx.merge() call.

Navigation.navigate(ROUTES.getMoneyRequestConfirmationRoute(iouType, reportID));
});
return;
}
Navigation.navigate(ROUTES.getMoneyRequestParticipantsRoute(iouType));

Promise.all(promises).then(() => {
Navigation.navigate(ROUTES.getMoneyRequestParticipantsRoute(iouType));
});
}

/**
* @param {String} receiptPath
* @param {String} receiptSource
* @param {String} iouType
* @param {String} reportID
* @param {Object} report
* @returns {Promise}
*/
function setMoneyRequestReceiptAndNavigateToNextPage(receiptPath, receiptSource, iouType, reportID, report) {
return setMoneyRequestReceipt(receiptPath, receiptSource).then(() => navigateToNextPage(ONYXKEYS.IOU, iouType, reportID, report));
}

export {
Expand All @@ -1574,4 +1601,5 @@ export {
setMoneyRequestReceipt,
createEmptyTransaction,
navigateToNextPage,
setMoneyRequestReceiptAndNavigateToNextPage,
};
2 changes: 1 addition & 1 deletion src/libs/actions/QueuedOnyxUpdates.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Onyx.connect({
* @returns {Promise}
*/
function queueOnyxUpdates(updates) {
return Onyx.merge(ONYXKEYS.QUEUED_ONYX_UPDATES, updates);
return Onyx.set(ONYXKEYS.QUEUED_ONYX_UPDATES, [...queuedOnyxUpdates, ...updates]);
Comment thread
hannojg marked this conversation as resolved.
Resolved
}

function clear() {
Expand Down
3 changes: 1 addition & 2 deletions src/pages/iou/ReceiptSelector/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ function ReceiptSelector(props) {
flash: flash ? 'on' : 'off',
})
.then((photo) => {
IOU.setMoneyRequestReceipt(`file://${photo.path}`, photo.path);
IOU.navigateToNextPage(props.iou, iouType, reportID, props.report);
IOU.setMoneyRequestReceiptAndNavigateToNextPage(`file://${photo.path}`, photo.path, props.iou, iouType, reportID, props.report);
})
.catch(() => {
showCameraAlert();
Expand Down
7 changes: 5 additions & 2 deletions src/pages/iou/steps/MoneyRequestConfirmPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,15 @@ function MoneyRequestConfirmPage(props) {
// Reset the money request Onyx if the ID in Onyx does not match the ID from params
const moneyRequestId = `${iouType.current}${reportID.current}`;
const shouldReset = props.iou.id !== moneyRequestId;
let resetPromise = Promise.resolve();
if (shouldReset) {
IOU.resetMoneyRequestInfo(moneyRequestId);
resetPromise = IOU.resetMoneyRequestInfo(moneyRequestId);
}

if (_.isEmpty(props.iou.participants) || (props.iou.amount === 0 && !props.iou.receiptPath) || shouldReset) {
Navigation.goBack(ROUTES.getMoneyRequestRoute(iouType.current, reportID.current), true);
resetPromise.then(() => {
Navigation.goBack(ROUTES.getMoneyRequestRoute(iouType.current, reportID.current), true);
});
}

return () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,15 @@ function MoneyRequestParticipantsPage(props) {
const iouType = useRef(lodashGet(props.route, 'params.iouType', ''));
const reportID = useRef(lodashGet(props.route, 'params.reportID', ''));

const addingParticipantsPromiseRef = useRef(Promise.resolve());
const onAddParticipants = (participants) => {
addingParticipantsPromiseRef.current = IOU.setMoneyRequestParticipants(participants);
};

const navigateToNextStep = () => {
Navigation.navigate(ROUTES.getMoneyRequestConfirmationRoute(iouType.current, reportID.current));
addingParticipantsPromiseRef.current.then(() => {
Navigation.navigate(ROUTES.getMoneyRequestConfirmationRoute(iouType.current, reportID.current));
});
};

const navigateBack = (forceFallback = false) => {
Expand All @@ -70,12 +77,15 @@ function MoneyRequestParticipantsPage(props) {
// Reset the money request Onyx if the ID in Onyx does not match the ID from params
const moneyRequestId = `${iouType.current}${reportID.current}`;
const shouldReset = props.iou.id !== moneyRequestId;
let resetPromise = Promise.resolve();
if (shouldReset) {
IOU.resetMoneyRequestInfo(moneyRequestId);
resetPromise = IOU.resetMoneyRequestInfo(moneyRequestId);
}

if ((props.iou.amount === 0 && !props.iou.receiptPath) || shouldReset) {
navigateBack(true);
resetPromise.then(() => {
navigateBack(true);
});
}

return () => {
Expand All @@ -98,13 +108,13 @@ function MoneyRequestParticipantsPage(props) {
<MoneyRequestParticipantsSplitSelector
onStepComplete={navigateToNextStep}
participants={props.iou.participants}
onAddParticipants={IOU.setMoneyRequestParticipants}
onAddParticipants={onAddParticipants}
safeAreaPaddingBottomStyle={safeAreaPaddingBottomStyle}
/>
) : (
<MoneyRequestParticipantsSelector
onStepComplete={navigateToNextStep}
onAddParticipants={IOU.setMoneyRequestParticipants}
onAddParticipants={onAddParticipants}
safeAreaPaddingBottomStyle={safeAreaPaddingBottomStyle}
iouType={iouType.current}
/>
Expand Down
12 changes: 7 additions & 5 deletions src/pages/iou/steps/NewRequestAmountPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,15 @@ function NewRequestAmountPage({route, iou, report}) {
}
const moneyRequestID = `${iouType}${reportID}`;
const shouldReset = iou.id !== moneyRequestID;
let resetPromise = Promise.resolve();
if (shouldReset) {
IOU.resetMoneyRequestInfo(moneyRequestID);
resetPromise = IOU.resetMoneyRequestInfo(moneyRequestID);
}

if (_.isEmpty(iou.participants) || iou.amount === 0 || shouldReset) {
Navigation.goBack(ROUTES.getMoneyRequestRoute(iouType, reportID), true);
resetPromise.then(() => {
Navigation.goBack(ROUTES.getMoneyRequestRoute(iouType, reportID), true);
});
}
}

Expand All @@ -144,15 +147,14 @@ function NewRequestAmountPage({route, iou, report}) {

const navigateToNextPage = (currentAmount) => {
const amountInSmallestCurrencyUnits = CurrencyUtils.convertToSmallestUnit(currency, Number.parseFloat(currentAmount));
IOU.setMoneyRequestAmount(amountInSmallestCurrencyUnits);
IOU.setMoneyRequestCurrency(currency);
const iouUpdates = [IOU.setMoneyRequestAmount(amountInSmallestCurrencyUnits), IOU.setMoneyRequestCurrency(currency)];

if (isEditing) {
Navigation.goBack(ROUTES.getMoneyRequestConfirmationRoute(iouType, reportID));
return;
}

IOU.navigateToNextPage(iou, iouType, reportID, report);
Promise.all(iouUpdates).then(() => IOU.navigateToNextPage(iou, iouType, reportID, report));
};

const content = (
Expand Down