Skip to content

Commit 974bf52

Browse files
feat: fixing types and updating example to Chat messaging
1 parent d833ab2 commit 974bf52

File tree

8 files changed

+190
-95
lines changed

8 files changed

+190
-95
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
<table>
2020
<tr>
21-
<td align='center' width="33%"><img src='https://user-images.githubusercontent.com/11586388/108675127-a5545680-74e6-11eb-89f9-b617b5ce6cc6.gif'/></td>
22-
<td align='center' width="33%"><img src='https://user-images.githubusercontent.com/11586388/108675105-9ff70c00-74e6-11eb-8abf-7c07b79338e2.gif'/></td>
21+
<td align='center' width="33%"><img src='https://user-images.githubusercontent.com/11586388/109138261-77774800-775a-11eb-806b-2add75755af7.gif' height="600" /></td>
22+
<td align='center' width="33%"><img src='https://user-images.githubusercontent.com/11586388/109139686-16507400-775c-11eb-893f-8cccfb47f9d7.gif' height="600"/></td>
2323
</tr>
2424
<tr></tr>
2525
<tr>

example/README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Example for react-native-bidirectional-infinite-scroll
1+
# Example of chat messaging
22

33
## How to run
44

@@ -12,12 +12,16 @@ npx react-native run-ios
1212

1313
<table>
1414
<tr>
15-
<td align='center' width="33%"><img src='https://user-images.githubusercontent.com/11586388/108675127-a5545680-74e6-11eb-89f9-b617b5ce6cc6.gif'/></td>
16-
<td align='center' width="33%"><img src='https://user-images.githubusercontent.com/11586388/108675105-9ff70c00-74e6-11eb-8abf-7c07b79338e2.gif'/></td>
15+
<td align='center' width="33%"><img src='https://user-images.githubusercontent.com/11586388/109138261-77774800-775a-11eb-806b-2add75755af7.gif' height="600" /></td>
16+
<td align='center' width="33%"><img src='https://user-images.githubusercontent.com/11586388/109139686-16507400-775c-11eb-893f-8cccfb47f9d7.gif' height="600"/></td>
1717
</tr>
1818
<tr></tr>
1919
<tr>
20-
<td align='center'>iOS</td>
21-
<td align='center'>Android</td>
20+
<td align='center'>
21+
<strong>iOS</strong>
22+
</td>
23+
<td align='center'>
24+
<strong>Android</strong>
25+
</td>
2226
</tr>
2327
</table>

example/src/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React from 'react';
2-
import FlatListExample from './FlatListExample';
2+
import MessageListExample from './MessageListExample';
33

44
const App = () => {
5-
return <FlatListExample />;
5+
return <MessageListExample />;
66
};
77

88
export default App;

example/src/FlatListExample.tsx

Lines changed: 0 additions & 85 deletions
This file was deleted.

example/src/MessageBubble.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import React from 'react';
2+
import { StyleSheet, Text, View } from 'react-native';
3+
import type { Message } from './utils';
4+
5+
type Props = {
6+
item: Message;
7+
};
8+
9+
export const MessageBubble: React.FC<Props> = ({ item }) => {
10+
if (item.isMyMessage) {
11+
return (
12+
<View
13+
key={`${item.id}`}
14+
style={[styles.messageBubble, styles.myMessageBubble]}
15+
>
16+
<Text style={styles.myMessageText}>{item.text}</Text>
17+
</View>
18+
);
19+
}
20+
21+
return (
22+
<View key={`${item.id}`} style={styles.messageBubble}>
23+
<Text style={styles.messageText}>{item.text}</Text>
24+
</View>
25+
);
26+
};
27+
28+
const styles = StyleSheet.create({
29+
messageBubble: {
30+
maxWidth: 300,
31+
padding: 10,
32+
borderRadius: 10,
33+
marginVertical: 5,
34+
marginHorizontal: 5,
35+
backgroundColor: '#F1F0F0',
36+
},
37+
myMessageBubble: {
38+
alignSelf: 'flex-end',
39+
// borderColor: '#989898',
40+
// borderWidth: 1,
41+
backgroundColor: '#3784FF',
42+
},
43+
messageText: {
44+
fontSize: 15,
45+
},
46+
myMessageText: {
47+
color: 'white',
48+
fontSize: 15,
49+
},
50+
});

example/src/MessageListExample.tsx

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import React, { useEffect, useState } from 'react';
2+
import { SafeAreaView, StyleSheet, Text, View } from 'react-native';
3+
4+
import { FlatList } from 'react-native-bidirectional-infinite-scroll';
5+
import { MessageBubble } from './MessageBubble';
6+
import { Message, queryMoreMessages } from './utils';
7+
8+
const App = () => {
9+
const [messages, setMessages] = useState<Array<Message>>([]);
10+
useEffect(() => {
11+
const initChat = async () => {
12+
const initialMessages = await queryMoreMessages(50);
13+
if (!initialMessages) return;
14+
15+
setMessages(initialMessages);
16+
};
17+
18+
initChat();
19+
}, []);
20+
21+
const loadMoreOlderMessages = async () => {
22+
const newMessages = await queryMoreMessages(10);
23+
setMessages((m) => {
24+
return m.concat(newMessages);
25+
});
26+
};
27+
28+
const loadMoreRecentMessages = async () => {
29+
const newMessages = await queryMoreMessages(10);
30+
setMessages((m) => {
31+
return newMessages.concat(m);
32+
});
33+
};
34+
35+
if (!messages.length) {
36+
return null;
37+
}
38+
39+
return (
40+
<SafeAreaView style={styles.safeArea}>
41+
<View style={styles.header}>
42+
<Text style={styles.headerTitle}>Chat between two users</Text>
43+
</View>
44+
<FlatList
45+
data={messages}
46+
inverted
47+
onEndReached={loadMoreOlderMessages}
48+
onStartReached={loadMoreRecentMessages}
49+
renderItem={MessageBubble}
50+
/>
51+
</SafeAreaView>
52+
);
53+
};
54+
55+
const styles = StyleSheet.create({
56+
header: {
57+
alignItems: 'center',
58+
paddingVertical: 10,
59+
borderBottomColor: '#BEBEBE',
60+
borderBottomWidth: 1,
61+
},
62+
headerTitle: { fontSize: 20, fontWeight: 'bold' },
63+
safeArea: {
64+
flex: 1,
65+
},
66+
sendMessageButton: {
67+
width: '100%',
68+
padding: 20,
69+
backgroundColor: '#FF4500',
70+
alignItems: 'center',
71+
},
72+
sendButtonTitle: {
73+
color: 'white',
74+
fontSize: 15,
75+
fontWeight: 'bold',
76+
},
77+
});
78+
79+
export default App;

example/src/utils.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Generate random integer, we will use this to use random message from list of dummy messages.
2+
export const getRandomInt = (min: number, max: number) => {
3+
return Math.floor(Math.random() * (max - min)) + min;
4+
};
5+
6+
// Generate unique key for message component of FlatList.
7+
export const generateUniqueKey = () =>
8+
`_${Math.random().toString(36).substr(2, 9)}`;
9+
10+
export type Message = {
11+
id: string;
12+
text: string;
13+
isMyMessage: boolean;
14+
};
15+
16+
// Mocks the api call to query 'n' number of messages.
17+
export const queryMoreMessages: (n: number) => Promise<Array<Message>> = (
18+
n
19+
) => {
20+
return new Promise((resolve) => {
21+
const newMessages: Array<Message> = [];
22+
23+
for (let i = 0; i < n; i++) {
24+
const messageText = testMessages[getRandomInt(0, testMessages.length)];
25+
newMessages.push({
26+
id: generateUniqueKey(),
27+
text: messageText,
28+
isMyMessage: Boolean(getRandomInt(0, 2)), // Randomly assign true or false.
29+
});
30+
}
31+
32+
// Lets resolve after 500 ms, to simulate network latency.
33+
setTimeout(() => {
34+
resolve(newMessages);
35+
}, 500);
36+
});
37+
};
38+
39+
// List of test messages to generate chat data.
40+
export const testMessages = [
41+
'Hey, where were you yesterday? I was trying to call you',
42+
'Yeah dude!! Had a really bad night. I was really hungover',
43+
'lol, thats so typical you. Who did you go out with?',
44+
'Dont even ask me about it, I am never going drink with Uthred again. That dude is a beast',
45+
'hahahaha, I can totally imagine!!',
46+
'Ciao :)',
47+
];

src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const styles = StyleSheet.create({
1515
},
1616
});
1717

18-
type Props<T> = FlatListProps<T> & {
18+
type Props<T> = Omit<FlatListProps<T>, 'maintainVisibleContentPosition'> & {
1919
/**
2020
* Called once when the scroll position gets close to end of list. This must return a promise.
2121
* You can `onEndReachedThreshold` as distance from end of list, when this function should be called.

0 commit comments

Comments
 (0)