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: properly invert list manually android #2685

Merged
merged 4 commits into from
Sep 23, 2024
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
42 changes: 29 additions & 13 deletions package/src/components/MessageList/MessageList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ const styles = StyleSheet.create({
},
});

const InvertedCellRendererComponent = (props: React.PropsWithChildren<unknown>) => (
<View {...props} style={styles.invertAndroid} />
);

const keyExtractor = <
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
>(
Expand Down Expand Up @@ -634,15 +630,15 @@ const MessageListWithContext = <

if (message.type === 'system') {
return (
<>
<View style={[shouldApplyAndroidWorkaround ? styles.invertAndroid : undefined]}>
<View testID={`message-list-item-${index}`}>
<MessageSystem
message={message}
style={[{ paddingHorizontal: screenPadding }, messageContainer]}
/>
</View>
{insertInlineUnreadIndicator && <InlineUnreadIndicator />}
</>
</View>
);
}

Expand All @@ -668,15 +664,23 @@ const MessageListWithContext = <
<>
{shouldApplyAndroidWorkaround && renderDateSeperator}
<ThemeProvider mergedStyle={modifiedTheme}>
<View testID={`message-list-item-${index}`}>{renderMessage}</View>
<View
style={[shouldApplyAndroidWorkaround ? styles.invertAndroid : undefined]}
testID={`message-list-item-${index}`}
>
{renderMessage}
</View>
</ThemeProvider>
{!shouldApplyAndroidWorkaround && renderDateSeperator}
{/* Adding indicator below the messages, since the list is inverted */}
{insertInlineUnreadIndicator && <InlineUnreadIndicator />}
</>
) : (
<>
<View testID={`message-list-item-${index}`}>
<View
style={[shouldApplyAndroidWorkaround ? styles.invertAndroid : undefined]}
testID={`message-list-item-${index}`}
>
{shouldApplyAndroidWorkaround && renderDateSeperator}
{renderMessage}
</View>
Expand Down Expand Up @@ -1081,14 +1085,28 @@ const MessageListWithContext = <
[shouldApplyAndroidWorkaround, HeaderComponent],
);

const ItemSeparatorComponent = additionalFlatListProps?.ItemSeparatorComponent;
const WrappedItemSeparatorComponent = useCallback(
() => (
<View style={[shouldApplyAndroidWorkaround ? styles.invertAndroid : undefined]}>
{ItemSeparatorComponent ? <ItemSeparatorComponent /> : null}
</View>
),
[ItemSeparatorComponent, shouldApplyAndroidWorkaround],
);

// We need to omit the style related props from the additionalFlatListProps and add them directly instead of spreading
let additionalFlatListPropsExcludingStyle:
| Omit<NonNullable<typeof additionalFlatListProps>, 'style' | 'contentContainerStyle'>
| Omit<
NonNullable<typeof additionalFlatListProps>,
'style' | 'contentContainerStyle' | 'ItemSeparatorComponent'
>
| undefined;

if (additionalFlatListProps) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { contentContainerStyle, style, ...rest } = additionalFlatListProps;
const { contentContainerStyle, ItemSeparatorComponent, style, ...rest } =
additionalFlatListProps;
additionalFlatListPropsExcludingStyle = rest;
}

Expand All @@ -1114,9 +1132,6 @@ const MessageListWithContext = <
</View>
) : (
<FlatList
CellRendererComponent={
shouldApplyAndroidWorkaround ? InvertedCellRendererComponent : undefined
}
contentContainerStyle={[
styles.contentContainer,
additionalFlatListProps?.contentContainerStyle,
Expand All @@ -1126,6 +1141,7 @@ const MessageListWithContext = <
data={processedMessageList}
extraData={disabled || !hasNoMoreRecentMessagesToLoad}
inverted={shouldApplyAndroidWorkaround ? false : inverted}
ItemSeparatorComponent={WrappedItemSeparatorComponent}
keyboardShouldPersistTaps='handled'
keyExtractor={keyExtractor}
ListFooterComponent={ListFooterComponent}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ exports[`Thread should match thread snapshot 1`] = `
testID="message-flat-list-wrapper"
>
<RCTScrollView
ItemSeparatorComponent={[Function]}
ListFooterComponent={null}
ListHeaderComponent={[Function]}
contentContainerStyle={
Expand Down Expand Up @@ -240,6 +241,11 @@ exports[`Thread should match thread snapshot 1`] = `
}
>
<View
style={
[
undefined,
]
}
testID="message-list-item-0"
>
<View
Expand Down Expand Up @@ -534,6 +540,13 @@ exports[`Thread should match thread snapshot 1`] = `
</View>
</View>
</View>
<View
style={
[
undefined,
]
}
/>
</View>
<View
onFocusCapture={[Function]}
Expand All @@ -554,6 +567,11 @@ exports[`Thread should match thread snapshot 1`] = `
}
>
<View
style={
[
undefined,
]
}
testID="message-list-item-1"
>
<View
Expand Down Expand Up @@ -848,6 +866,13 @@ exports[`Thread should match thread snapshot 1`] = `
</View>
</View>
</View>
<View
style={
[
undefined,
]
}
/>
</View>
<View
onFocusCapture={[Function]}
Expand All @@ -868,6 +893,11 @@ exports[`Thread should match thread snapshot 1`] = `
}
>
<View
style={
[
undefined,
]
}
testID="message-list-item-2"
>
<View
Expand Down
Loading