Skip to content

Commit

Permalink
Rearrange HybridApp module
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuuszzzzz committed Nov 25, 2024
1 parent cac942f commit e1a6154
Showing 1 changed file with 36 additions and 34 deletions.
70 changes: 36 additions & 34 deletions src/libs/HybridApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@ import {
import type {Init} from './ActiveClientManager/types';
import Log from './Log';

function shouldUseOldApp(tryNewDot?: TryNewDot) {
return tryNewDot?.classicRedirect.dismissed === true;
}

let credentials: OnyxEntry<Credentials>;
Onyx.connect({
key: ONYXKEYS.CREDENTIALS,
callback: (newCredentials) => {
credentials = newCredentials;
},
});

let currentHybridApp: OnyxEntry<HybridApp>;
let currentTryNewDot: OnyxEntry<TryNewDot>;

Expand All @@ -45,6 +33,29 @@ Onyx.connect({
},
});

let currentSession: OnyxEntry<Session>;
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (session: OnyxEntry<Session>) => {
if (!currentSession?.authToken && session?.authToken && currentHybridApp?.newDotSignInState === CONST.HYBRID_APP_SIGN_IN_STATE.STARTED) {
setNewDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.FINISHED);
}
currentSession = session;
},
});

let credentials: OnyxEntry<Credentials>;
Onyx.connect({
key: ONYXKEYS.CREDENTIALS,
callback: (newCredentials) => {
credentials = newCredentials;
},
});

function shouldUseOldApp(tryNewDot?: TryNewDot) {
return tryNewDot?.classicRedirect.dismissed === true;
}

function handleSignInFlow(hybridApp: OnyxEntry<HybridApp>, tryNewDot: OnyxEntry<TryNewDot>) {
if (!NativeModules.HybridAppModule) {
return;
Expand Down Expand Up @@ -104,35 +115,26 @@ function handleSignInFlow(hybridApp: OnyxEntry<HybridApp>, tryNewDot: OnyxEntry<
currentTryNewDot = tryNewDot;
}

let currentSession: OnyxEntry<Session>;
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (session: OnyxEntry<Session>) => {
if (!currentSession?.authToken && session?.authToken && currentHybridApp?.newDotSignInState === CONST.HYBRID_APP_SIGN_IN_STATE.STARTED) {
setNewDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.FINISHED);
}
currentSession = session;
},
});
function onOldDotSignInFinished(data: string) {
Log.info(`[HybridApp] onSignInFinished event received with data: ${data}`, true);
const eventData = JSON.parse(data) as {errorMessage: string};

setIsSigningIn(false);
setOldDotSignInError(eventData.errorMessage);
setOldDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.FINISHED);

if (eventData.errorMessage === null) {
setReadyToSwitchToClassicExperience(true);
}
}

const init: Init = () => {
if (!NativeModules.HybridAppModule) {
return;
}

// Setup event listeners
DeviceEventEmitter.addListener(CONST.EVENTS.HYBRID_APP.ON_SIGN_IN_FINISHED, (data) => {
Log.info(`[HybridApp] onSignInFinished event received with data: ${data}`, true);
const eventData = JSON.parse(data as string) as {errorMessage: string};

setIsSigningIn(false);
setOldDotSignInError(eventData.errorMessage);
setOldDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.FINISHED);

if (eventData.errorMessage === null) {
setReadyToSwitchToClassicExperience(true);
}
});
DeviceEventEmitter.addListener(CONST.EVENTS.HYBRID_APP.ON_SIGN_IN_FINISHED, onOldDotSignInFinished);
};

export default {init};
Expand Down

0 comments on commit e1a6154

Please sign in to comment.