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

Fixed Android - Chat - Message gets displayed from right to left #32297

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
22 changes: 22 additions & 0 deletions src/libs/convertToLTR/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,26 @@ import ConvertToLTR from './types';
*/
const convertToLTR: ConvertToLTR = (text) => `${CONST.UNICODE.LTR}${text}`;

/** This is necessary to convert the input to LTR, there is a delay that causes the cursor not to go to the end of the input line when pasting text or typing fast. */
samilabud marked this conversation as resolved.
Show resolved Hide resolved
const moveCursorToEndOfLine = (commentLength: number, setSelection:(value: React.SetStateAction<{
start: number;
end: number;
}>)=>void) => {
setSelection({
start: commentLength + 1,
end: commentLength + 1,
});
}

/** We should remove the LTR unicode when the input is empty to prevent: Sending an empty message, bad function of metion suggestions (force option: always remove the unicode), or placeholder issues */
const removeUnicodeLTRWhenEmpty = (newComment:string, newCommentConverted:string, force?:boolean) => {
samilabud marked this conversation as resolved.
Show resolved Hide resolved

const removeRegEx = new RegExp(`${CONST.UNICODE.LTR}`,'g')
samilabud marked this conversation as resolved.
Show resolved Hide resolved

const result = newComment.length <= 1 || force ? newCommentConverted.replace(removeRegEx, '') : newCommentConverted;
return result;
}

export {moveCursorToEndOfLine, removeUnicodeLTRWhenEmpty};

export default convertToLTR;
9 changes: 9 additions & 0 deletions src/libs/convertToLTR/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ import ConvertToLTR from './types';

const convertToLTR: ConvertToLTR = (text) => text;

const moveCursorToEndOfLine = (commentLength: number, setSelection:(value: React.SetStateAction<{
samilabud marked this conversation as resolved.
Show resolved Hide resolved
start: number;
end: number;
}>)=>void) => setSelection

const removeUnicodeLTRWhenEmpty = (newComment:string) => newComment;

export {moveCursorToEndOfLine, removeUnicodeLTRWhenEmpty};

export default convertToLTR;
3 changes: 2 additions & 1 deletion src/libs/convertToLTRForComposer/index.android.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import CONST from '@src/CONST';
import ConvertToLTRForComposer from './types';

/**
* Android only - Do not convert RTL text to a LTR text for input box using Unicode controls.
* Android does not properly support bidirectional text for mixed content for input box
*/
const convertToLTRForComposer: ConvertToLTRForComposer = (text) => text;
const convertToLTRForComposer: ConvertToLTRForComposer = (text, isComposerEmpty) => (isComposerEmpty ? `${CONST.UNICODE.LTR}${text}` : text);
samilabud marked this conversation as resolved.
Show resolved Hide resolved
export default convertToLTRForComposer;
2 changes: 1 addition & 1 deletion src/libs/convertToLTRForComposer/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
type ConvertToLTRForComposer = (text: string) => string;
type ConvertToLTRForComposer = (text: string, isComposerEmpty?: boolean) => string;

export default ConvertToLTRForComposer;
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import canFocusInputOnScreenFocus from '@libs/canFocusInputOnScreenFocus';
import compose from '@libs/compose';
import * as ComposerUtils from '@libs/ComposerUtils';
import getDraftComment from '@libs/ComposerUtils/getDraftComment';
import {moveCursorToEndOfLine, removeUnicodeLTRWhenEmpty} from '@libs/convertToLTR';
import convertToLTRForComposer from '@libs/convertToLTRForComposer';
import * as EmojiUtils from '@libs/EmojiUtils';
import focusComposerWithDelay from '@libs/focusComposerWithDelay';
Expand Down Expand Up @@ -105,6 +106,7 @@ function ComposerWithSuggestions({
const styles = useThemeStyles();
const {preferredLocale} = useLocalize();
const isFocused = useIsFocused();
const [composerIsEmpty, setComposerIsEmpty] = useState(true);
samilabud marked this conversation as resolved.
Show resolved Hide resolved
const navigation = useNavigation();
const emojisPresentBefore = useRef([]);
const [value, setValue] = useState(() => {
Expand Down Expand Up @@ -220,26 +222,45 @@ function ComposerWithSuggestions({
debouncedUpdateFrequentlyUsedEmojis();
}
}
const newCommentConverted = convertToLTRForComposer(newComment);

let newCommentConverted = newComment;
samilabud marked this conversation as resolved.
Show resolved Hide resolved
const prevComment = commentRef.current;
samilabud marked this conversation as resolved.
Show resolved Hide resolved

// This prevent the double execution of setting input value that could affect the place holder and could send an empty message or draft messages in android
if (prevComment !== newComment) {
newCommentConverted = removeUnicodeLTRWhenEmpty(newComment, newCommentConverted);
newCommentConverted = convertToLTRForComposer(newCommentConverted, composerIsEmpty);
samilabud marked this conversation as resolved.
Show resolved Hide resolved
if (['@'].includes(newComment)) {
newCommentConverted = removeUnicodeLTRWhenEmpty(newComment, newCommentConverted, true);
}
setValue(newCommentConverted);
moveCursorToEndOfLine(newComment.length, setSelection);
setComposerIsEmpty(false);
}

const isNewCommentEmpty = !!newCommentConverted.match(/^(\s)*$/);
const isPrevCommentEmpty = !!commentRef.current.match(/^(\s)*$/);
const isPrevCommentEmpty = !!prevComment.match(/^(\s)*$/);

/** Only update isCommentEmpty state if it's different from previous one */
if (isNewCommentEmpty !== isPrevCommentEmpty) {
setIsCommentEmpty(isNewCommentEmpty);
if (isNewCommentEmpty) {
setComposerIsEmpty(true);
}
}

emojisPresentBefore.current = emojis;
setValue(newCommentConverted);

if (commentValue !== newComment) {
const position = Math.max(selection.end + (newComment.length - commentRef.current.length), cursorPosition || 0);
const position = Math.max(selection.end + (newComment.length - prevComment.length), cursorPosition || 0);
setSelection({
start: position,
end: position,
});
}

// Indicate that draft has been created.
if (commentRef.current.length === 0 && newCommentConverted.length !== 0) {
if (prevComment.length === 0 && newCommentConverted.length !== 0) {
Report.setReportWithDraft(reportID, true);
}

Expand Down Expand Up @@ -268,6 +289,7 @@ function ComposerWithSuggestions({
raiseIsScrollLikelyLayoutTriggered,
debouncedSaveReportComment,
selection.end,
composerIsEmpty,
],
);

Expand Down
Loading