Skip to content

Commit

Permalink
Add Inbox tab and unread message count: implement Inbox tab with badg…
Browse files Browse the repository at this point in the history
…e for unread messages, update navigation routes, and enhance state management in useIterableApp
  • Loading branch information
lposen committed Oct 23, 2024
1 parent fd85e40 commit 5478edc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
51 changes: 50 additions & 1 deletion example/src/components/App/Main.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { useState, useEffect } from 'react';

import { Iterable } from '@iterable/react-native-sdk';

import { colors, Route } from '../../constants';
import type { MainScreenParamList } from '../../types';
import { routeIcon } from './App.constants';
import { getIcon } from './App.utils';
import { User } from '../User';
import Inbox from '../Inbox';
import { useIterableApp } from '../../hooks';

const Tab = createBottomTabNavigator<MainScreenParamList>();

export const Main = () => {
const {
isInboxTab,
isLoggedIn,
loginInProgress,
returnToInboxTrigger,
setIsInboxTab,
setReturnToInboxTrigger,
userId,
} = useIterableApp();
const [unreadMessageCount, setUnreadMessageCount] = useState<number>(0);

useEffect(() => {
if (loginInProgress) return;
if (isLoggedIn) {
Iterable.inAppManager.getMessages().then((messages) => {
setUnreadMessageCount(messages.length);
});
} else {
// Reset unread message count when user logs out
setUnreadMessageCount(0);
}
}, [isLoggedIn, loginInProgress, userId]);

return (
<>
<Tab.Navigator
Expand All @@ -22,7 +50,28 @@ export const Main = () => {
};
}}
>
<Tab.Screen name={Route.User} component={User} />
<Tab.Screen
name={Route.Inbox}
component={Inbox}
options={
unreadMessageCount ? { tabBarBadge: unreadMessageCount } : {}
}
listeners={() => ({
tabPress: () => {
if (isInboxTab) {
setReturnToInboxTrigger(!returnToInboxTrigger);
}
setIsInboxTab(true);
},
})}
/>
<Tab.Screen
name={Route.User}
component={User}
listeners={() => ({
tabPress: () => setIsInboxTab(false),
})}
/>
</Tab.Navigator>
</>
);
Expand Down
2 changes: 1 addition & 1 deletion example/src/hooks/useIterableApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export const IterableAppProvider: FunctionComponent<
config.inAppDisplayInterval = 1.0; // Min gap between in-apps. No need to set this in production.

config.urlHandler = (url: string) => {
const routeNames = [Route.User];
const routeNames = [Route.Inbox, Route.User];
for (const route of routeNames) {
if (url.includes(route.toLowerCase())) {
// TODO: Figure out typing for this
Expand Down
2 changes: 1 addition & 1 deletion src/IterableInboxMessageDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export const IterableInboxMessageDisplay = ({
}}
>
<View style={returnButton}>
<Icon name="ios-chevron-back" style={returnButtonIcon} />
<Icon name="chevron-back-outline" style={returnButtonIcon} />
<Text style={returnButtonText}>Inbox</Text>
</View>
</TouchableWithoutFeedback>
Expand Down
4 changes: 1 addition & 3 deletions src/IterableInboxMessageList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ export const IterableInboxMessageList = ({
swipingCheck={setSwiping}
messageListItemLayout={messageListItemLayout}
deleteRow={(messageId: string) => deleteRow(messageId)}
handleMessageSelect={(messageId: string, i: number) =>
handleMessageSelect(messageId, i)
}
handleMessageSelect={handleMessageSelect}
contentWidth={contentWidth}
isPortrait={isPortrait}
/>
Expand Down

0 comments on commit 5478edc

Please sign in to comment.