Skip to content

Commit 03d7e81

Browse files
committed
fix: manage agent state
Signed-off-by: Jason C. Leach <jason.leach@fullboar.ca>
1 parent be83ea1 commit 03d7e81

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

app/src/screens/Splash.tsx

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { GetCredentialDefinitionRequest, GetSchemaRequest } from '@hyperledger/i
2424
import AsyncStorage from '@react-native-async-storage/async-storage'
2525
import { CommonActions, useNavigation } from '@react-navigation/native'
2626
import moment from 'moment'
27-
import React, { useEffect, useState } from 'react'
27+
import React, { useEffect, useState, useRef } from 'react'
2828
import { useTranslation } from 'react-i18next'
2929
import { StyleSheet, View, Text, Image, useWindowDimensions, ScrollView } from 'react-native'
3030
import { Config } from 'react-native-config'
@@ -62,6 +62,7 @@ const resumeOnboardingAt = (
6262
showPreface?: boolean
6363
}
6464
): Screens => {
65+
console.log('********** resumeOnboardingAt', state, params)
6566
const termsVer = params.termsVersion ?? true
6667
if (
6768
(state.didSeePreface || !params.showPreface) &&
@@ -114,13 +115,14 @@ const resumeOnboardingAt = (
114115
*/
115116
const Splash = () => {
116117
const { width } = useWindowDimensions()
117-
const { setAgent } = useAgent()
118+
const { agent, setAgent } = useAgent()
118119
const { t } = useTranslation()
119120
const [store, dispatch] = useStore<BCState>()
120121
const navigation = useNavigation()
121122
const { walletSecret } = useAuth()
122123
const { ColorPallet, Assets } = useTheme()
123124
const [mounted, setMounted] = useState(false)
125+
const hasMounted = useRef(false)
124126
const [stepText, setStepText] = useState<string>(t('Init.Starting'))
125127
const [progressPercent, setProgressPercent] = useState(0)
126128
const [initOnboardingCount, setInitOnboardingCount] = useState(0)
@@ -145,6 +147,10 @@ const Splash = () => {
145147
TOKENS.CACHE_SCHEMAS,
146148
])
147149

150+
useEffect(() => {
151+
console.log(`************* Splash screen mounted, ${mounted}, ${Math.floor(Math.random() * 10) + 1}`)
152+
}, [mounted])
153+
148154
const steps: string[] = [
149155
t('Init.Starting'),
150156
t('Init.FetchingPreferences'),
@@ -201,7 +207,10 @@ const Splash = () => {
201207

202208
// navigation calls that occur before the screen is fully mounted will fail
203209
useEffect(() => {
204-
setMounted(true)
210+
if (!hasMounted.current) {
211+
setMounted(true)
212+
hasMounted.current = true
213+
}
205214
}, [])
206215

207216
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -321,14 +330,40 @@ const Splash = () => {
321330
return
322331
}
323332

333+
if (agent) {
334+
logger.info('Agent already initialized, restarting...')
335+
336+
try {
337+
await agent.wallet.open({
338+
id: walletSecret.id,
339+
key: walletSecret.key,
340+
})
341+
} catch (error) {
342+
logger.error('Error opening existing wallet', error)
343+
}
344+
345+
await agent.mediationRecipient.initiateMessagePickup()
346+
347+
setStep(9)
348+
navigation.dispatch(
349+
CommonActions.reset({
350+
index: 0,
351+
routes: [{ name: Stacks.TabStack }],
352+
})
353+
)
354+
355+
return
356+
}
357+
358+
logger.info('No agent initialized, creating a new one')
359+
324360
setStep(3)
325361

326362
await (ocaBundleResolver as RemoteOCABundleResolver).checkForUpdates?.()
327363

328364
setStep(4)
329365
const cachedLedgers = await loadCachedLedgers()
330366
const ledgers = cachedLedgers ?? indyLedgers
331-
332367
const options = {
333368
config: {
334369
label: store.preferences.walletName || 'BC Wallet',

0 commit comments

Comments
 (0)