Skip to content
Open
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
13 changes: 9 additions & 4 deletions src/components/common/DefaultHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { useNavigation } from '@react-navigation/native';
import { StackNavigationProp } from '@react-navigation/stack';
import { Text } from '@rneui/themed';
import { useAtom, useAtomValue } from 'jotai';
import { useAtom, useAtomValue, useSetAtom } from 'jotai';
import React from 'react';
import { StyleSheet, TouchableOpacity, View } from 'react-native';
import { bridgefyStatusAtom, currentUserInfoAtom, disableRefreshAtom } from '../../services/atoms';
import { refreshSDK } from '../../services/bridgefy-link';
import {
bridgefyStatusAtom,
currentUserInfoAtom,
disableRefreshAtom,
refreshSDKAtom,
} from '../../services/atoms';
import { BridgefyStatus } from '../../utils/globals';
import { theme } from '../../utils/theme';
import SettingsIcon from '../ui/Icons/HeaderIcons/SettingsIcon';
Expand All @@ -18,14 +22,15 @@ const DefaultHeader = ({ pageName }: { pageName: string }) => {
const userInfo = useAtomValue(currentUserInfoAtom);
const [disableRefresh, setDisableRefresh] = useAtom(disableRefreshAtom);
const [bridgefyStatus] = useAtom(bridgefyStatusAtom);
const refreshSDK = useSetAtom(refreshSDKAtom);

if (!userInfo.userID && bridgefyStatus !== BridgefyStatus.DESTROYED) {
throw new Error('No user info found.');
}

async function refreshApp() {
setDisableRefresh(true);
await refreshSDK();
refreshSDK();
// Wait 5 seconds before re-enabling the refresh button
setTimeout(() => {
setDisableRefresh(false);
Expand Down
10 changes: 10 additions & 0 deletions src/hooks/useInitializeApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
publicChatInfoAtom,
removeConnectionAtom,
SecureStatus,
shouldStartSDKAtom,
} from '../services/atoms';
import {
addMessageToConversationAtom,
Expand Down Expand Up @@ -127,6 +128,8 @@ export default function useInitializeApp() {
const appState = useRef(AppState.currentState);
const [appStateVisible, setAppStateVisible] = useAtom(appVisibleAtom);

const [shouldStartSDK, setShouldStartSDK] = useAtom(shouldStartSDKAtom);

const setNotificationContent = useSetAtom(notificationContentAtom);

const addMessageToConversation = useSetAtom(addMessageToConversationAtom);
Expand Down Expand Up @@ -350,6 +353,13 @@ export default function useInitializeApp() {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [event]);

useEffect(() => {
if (shouldStartSDK && bridgefyStatus === BridgefyStatus.OFFLINE) {
startSDK();
setShouldStartSDK(false);
}
}, [shouldStartSDK, bridgefyStatus, setShouldStartSDK]);

/*

EVENT LISTENERS
Expand Down
6 changes: 4 additions & 2 deletions src/pages/DiscoverPage/CreateChatModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import {
connectionInfoAtom,
connectionInfoAtomInterface,
createChatWithUserAtom,
refreshSDKAtom,
SecureStatus,
StoredConnectionInfo,
} from '../../services/atoms';
import { establishSecureConnection, refreshSDK } from '../../services/bridgefy-link';
import { establishSecureConnection } from '../../services/bridgefy-link';
import { getConnectionName } from '../../services/connections';
import { vars } from '../../utils/theme';

Expand All @@ -40,6 +41,7 @@ const CreateChatModal = () => {
const [modalState, setModalState] = useState<number>(ModalState.CREATE);
const [connections] = useAtom(activeConnectionsAtom);
const [allContacts] = useAtom(allContactsAtom);
const refreshSDK = useSetAtom(refreshSDKAtom);

const insets = useSafeAreaInsets();
const navigation = useNavigation<StackNavigationProp<any>>();
Expand Down Expand Up @@ -165,7 +167,7 @@ const CreateChatModal = () => {
) {
refreshSDK();
}
}, [chatContact, connectionInfo, modalState]);
}, [chatContact, connectionInfo, modalState, refreshSDK]);

// When the user accepts the chat request, navigate to the chat screen
useEffect(() => {
Expand Down
6 changes: 4 additions & 2 deletions src/pages/DiscoverPage/DiscoverPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import {
createChatWithUserAtom,
disableRefreshAtom,
getActiveConnectionsAtom,
refreshSDKAtom,
} from '../../services/atoms';
import { refreshSDK } from '../../services/bridgefy-link';
import { theme, vars } from '../../utils/theme';

interface EllipsisTextProps {
Expand Down Expand Up @@ -48,12 +48,14 @@ const DiscoverPage = () => {
const [connections] = useAtom(getActiveConnectionsAtom);
const [disableRefresh, setDisableRefresh] = useAtom(disableRefreshAtom);
const [contacts] = useAtom(allContactsAtom);
const refreshSDK = useSetAtom(refreshSDKAtom);

const createChatWithUser = useSetAtom(createChatWithUserAtom);
const navigation = useNavigation<StackNavigationProp<any>>();

async function refreshApp() {
setDisableRefresh(true);
await refreshSDK();
refreshSDK();
// Wait 5 seconds before re-enabling the refresh button
setTimeout(() => {
setDisableRefresh(false);
Expand Down
8 changes: 8 additions & 0 deletions src/services/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { atom } from 'jotai';
import { Content } from '../components/ui/InAppNotification';
import { atomWithMMKV } from '../utils/atomWithMMKV';
import { BridgefyState, BridgefyStatus } from '../utils/globals';
import { stopSDK } from './bridgefy-link';
import {
ContactInfo,
CONTACT_INFO_KEY,
Expand Down Expand Up @@ -61,6 +62,8 @@ export const disableRefreshAtom = atom<boolean>(false);

export const createChatWithUserAtom = atom<string | undefined>(undefined);

export const shouldStartSDKAtom = atom<boolean>(false);

// allContactsAtom: List of all contacts.
export const allContactsAtom = atom<string[]>((get) => {
const contactInfo = get(contactInfoAtom);
Expand Down Expand Up @@ -128,6 +131,11 @@ export const connectionInfoAtomInterface = atom(
}
);

export const refreshSDKAtom = atom(null, (get, set) => {
set(shouldStartSDKAtom, true);
stopSDK();
});
Comment on lines +134 to +137
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not completely necessary to have this abstraction but wanted to add it since that's how we had it before


// TODO: (adriangri) Find a better solution to this
export const resetAllDataAtom = atom(null, (get, set) => {
wipeDatabase();
Expand Down
13 changes: 0 additions & 13 deletions src/services/bridgefy-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,6 @@ export const linkListenersToEvents = (handleEvent: (event: EventPacket) => void)
);
};

export async function refreshSDK(): Promise<void> {
console.log('(refreshSDK) Restarting Bridgefy...');
await stopSDK().catch((e) => {
console.warn(e);
return;
});
await startSDK().catch((e) => {
console.warn(e);
return;
});
return Promise.resolve();
}

export async function startSDK(): Promise<string> {
console.log('(startSDK) Starting Bridgefy...');
return new Promise((resolve, reject) => {
Expand Down