Skip to content

Commit

Permalink
fix: restart pickup (#1310)
Browse files Browse the repository at this point in the history
Signed-off-by: Jason C. Leach <[email protected]>
  • Loading branch information
jleach authored Nov 12, 2024
1 parent a2faf30 commit 8900029
Showing 1 changed file with 44 additions and 29 deletions.
73 changes: 44 additions & 29 deletions packages/legacy/core/App/navigators/RootStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,26 @@ const RootStack: React.FC = () => {
const [store, dispatch] = useStore()
const { agent } = useAgent()
const appState = useRef(AppState.currentState)
const [inBackground, setInBackground] = useState<boolean>(false)
const [currentState, setCurrentState] = useState<string>(appState.current)
const { t } = useTranslation()
const navigation = useNavigation<StackNavigationProp<AuthenticateStackParams>>()
const theme = useTheme()
const defaultStackOptions = useDefaultStackOptions(theme)
const [splash, { enableImplicitInvitations, enableReuseConnections }, logger, OnboardingStack, CustomNavStack1, loadState] =
useServices([TOKENS.SCREEN_SPLASH, TOKENS.CONFIG, TOKENS.UTIL_LOGGER, TOKENS.STACK_ONBOARDING, TOKENS.CUSTOM_NAV_STACK_1, TOKENS.LOAD_STATE])
const [
splash,
{ enableImplicitInvitations, enableReuseConnections },
logger,
OnboardingStack,
CustomNavStack1,
loadState,
] = useServices([
TOKENS.SCREEN_SPLASH,
TOKENS.CONFIG,
TOKENS.UTIL_LOGGER,
TOKENS.STACK_ONBOARDING,
TOKENS.CUSTOM_NAV_STACK_1,
TOKENS.LOAD_STATE,
])

useDeepLinks()

Expand Down Expand Up @@ -113,7 +126,7 @@ const RootStack: React.FC = () => {
})
}

if (inBackground) {
if (currentState === 'background') {
return
}

Expand All @@ -128,18 +141,39 @@ const RootStack: React.FC = () => {
enableImplicitInvitations,
enableReuseConnections,
t,
inBackground,
currentState,
agent?.isInitialized,
store.deepLink,
store.authentication.didAuthenticate,
])

useEffect(() => {
if (!agent) {
const sub = AppState.addEventListener('change', (nextAppState) => {
if (nextAppState === 'inactive') {
// on iOS this happens when any OS prompt is shown. We
// don't want do anything in this case.
return
}

if (appState.current === nextAppState) {
return // no change
}

appState.current = nextAppState
setCurrentState(appState.current)
})

return () => {
sub.remove()
}
}, [])

useEffect(() => {
if (!agent || !agent.isInitialized) {
return
}

if (inBackground) {
if (currentState === 'background') {
agent.mediationRecipient
.stopMessagePickup()
.then(() => {
Expand All @@ -152,7 +186,7 @@ const RootStack: React.FC = () => {
return
}

if (!inBackground) {
if (currentState === 'active') {
agent.mediationRecipient
.initiateMessagePickup()
.then(() => {
Expand All @@ -164,24 +198,7 @@ const RootStack: React.FC = () => {

return
}
}, [agent, inBackground, logger])

useEffect(() => {
AppState.addEventListener('change', (nextAppState) => {
if (appState.current === 'active' && ['inactive', 'background'].includes(nextAppState)) {
if (nextAppState === 'inactive') {
// on iOS this happens when any OS prompt is shown. We
// don't want to lock the user out in this case or preform
// background tasks.
return
}

setInBackground(true)
}

appState.current = nextAppState
})
}, [])
}, [agent, currentState, logger])

const mainStack = () => {
const Stack = createStackNavigator()
Expand Down Expand Up @@ -243,9 +260,7 @@ const RootStack: React.FC = () => {
cardStyleInterpolator: forFade,
}}
/>
{CustomNavStack1 ? (
<Stack.Screen name={Stacks.CustomNavStack1} component={CustomNavStack1} />
) : null}
{CustomNavStack1 ? <Stack.Screen name={Stacks.CustomNavStack1} component={CustomNavStack1} /> : null}
</Stack.Navigator>
)
}
Expand Down

0 comments on commit 8900029

Please sign in to comment.