Skip to content

Commit d283267

Browse files
authored
feat: initial unread count example (#1868)
* feat: set unread count on initial app mount * fix: unnecessary if check to set unread state * fix: linting error
1 parent f6ee974 commit d283267

File tree

5 files changed

+31
-26
lines changed

5 files changed

+31
-26
lines changed

examples/SampleApp/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const Drawer = createDrawerNavigator();
6565
const Stack = createStackNavigator<StackNavigatorParamList>();
6666
const UserSelectorStack = createStackNavigator<UserSelectorParamList>();
6767
const App = () => {
68-
const { chatClient, isConnecting, loginUser, logout, switchUser } = useChatClient();
68+
const { chatClient, isConnecting, loginUser, logout, switchUser, unreadCount } = useChatClient();
6969
const colorScheme = useColorScheme();
7070
const streamChatTheme = useStreamChatTheme();
7171

@@ -131,7 +131,7 @@ const App = () => {
131131
dark: colorScheme === 'dark',
132132
}}
133133
>
134-
<AppContext.Provider value={{ chatClient, loginUser, logout, switchUser }}>
134+
<AppContext.Provider value={{ chatClient, loginUser, logout, switchUser, unreadCount }}>
135135
{isConnecting && !chatClient ? (
136136
<LoadingScreen />
137137
) : chatClient ? (

examples/SampleApp/src/components/UnreadCountBadge.tsx

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,13 @@ export const UnreadCountBadge: React.FC = () => {
2626
},
2727
} = useTheme();
2828

29-
const { chatClient } = useAppContext();
30-
const [count, setCount] = useState<number>();
31-
32-
useEffect(() => {
33-
const listener = chatClient?.on((e) => {
34-
if (e.total_unread_count !== undefined) {
35-
setCount(e.total_unread_count);
36-
}
37-
});
38-
39-
return () => {
40-
if (listener) {
41-
listener.unsubscribe();
42-
}
43-
};
44-
}, [chatClient]);
29+
const { unreadCount } = useAppContext();
4530

4631
return (
4732
<View style={[styles.unreadContainer, { backgroundColor: accent_red }]}>
48-
{!!count && <Text style={styles.unreadText}>{count > 99 ? '99+' : count}</Text>}
33+
{!!unreadCount && (
34+
<Text style={styles.unreadText}>{unreadCount > 99 ? '99+' : unreadCount}</Text>
35+
)}
4936
</View>
5037
);
5138
};

examples/SampleApp/src/context/AppContext.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type AppContextType = {
99
loginUser: (config: LoginConfig) => void;
1010
logout: () => void;
1111
switchUser: (userId?: string) => void;
12+
unreadCount: number | undefined;
1213
};
1314

1415
export const AppContext = React.createContext({} as AppContextType);

examples/SampleApp/src/hooks/useChatClient.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ messaging().setBackgroundMessageHandler(async (remoteMessage) => {
6767
export const useChatClient = () => {
6868
const [chatClient, setChatClient] = useState<StreamChat<StreamChatGenerics> | null>(null);
6969
const [isConnecting, setIsConnecting] = useState(true);
70+
const [unreadCount, setUnreadCount] = useState<number>();
7071

7172
const unsubscribePushListenersRef = useRef<() => void>();
7273

@@ -81,15 +82,16 @@ export const useChatClient = () => {
8182
timeout: 6000,
8283
// logger: (type, msg) => console.log(type, msg)
8384
});
85+
setChatClient(client);
8486

8587
const user = {
8688
id: config.userId,
8789
image: config.userImage,
8890
name: config.userName,
8991
};
90-
const promise = client.connectUser(user, config.userToken);
91-
setChatClient(client);
92-
await promise;
92+
const connectedUser = await client.connectUser(user, config.userToken);
93+
const initialUnreadCount = connectedUser?.me?.total_unread_count;
94+
setUnreadCount(initialUnreadCount);
9395
await AsyncStore.setItem('@stream-rn-sampleapp-login-config', config);
9496

9597
const permissionAuthStatus = await messaging().hasPermission();
@@ -189,11 +191,29 @@ export const useChatClient = () => {
189191
return unsubscribePushListenersRef.current;
190192
}, []);
191193

194+
/**
195+
* Listen to changes in unread counts and update the badge count
196+
*/
197+
useEffect(() => {
198+
const listener = chatClient?.on((e) => {
199+
if (e.total_unread_count !== undefined) {
200+
setUnreadCount(e.total_unread_count);
201+
}
202+
});
203+
204+
return () => {
205+
if (listener) {
206+
listener.unsubscribe();
207+
}
208+
};
209+
}, [chatClient]);
210+
192211
return {
193212
chatClient,
194213
isConnecting,
195214
loginUser,
196215
logout,
197216
switchUser,
217+
unreadCount,
198218
};
199219
};

package/src/components/ChannelPreview/ChannelPreview.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,7 @@ const ChannelPreviewWithContext = <
6262
}
6363

6464
const newUnreadCount = channel.countUnread();
65-
66-
if (newUnreadCount !== unread) {
67-
setUnread(newUnreadCount);
68-
}
65+
setUnread(newUnreadCount);
6966
}, [channelLastMessageString]);
7067

7168
useEffect(() => {

0 commit comments

Comments
 (0)