Skip to content

Commit

Permalink
rebase with main
Browse files Browse the repository at this point in the history
Signed-off-by: fc-santos <[email protected]>
  • Loading branch information
fc-santos committed Jan 23, 2025
2 parents c95d501 + 410b369 commit 21649a8
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 37 deletions.
77 changes: 53 additions & 24 deletions packages/legacy/core/App/components/network/NetInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,73 @@ import { useTranslation } from 'react-i18next'
import Toast from 'react-native-toast-message'
import { useNetwork } from '../../contexts/network'
import { ToastType } from '../../components/toast/BaseToast'
import { TOKENS, useServices } from '../../container-api'

const NetInfo: React.FC = () => {
const { silentAssertConnectedNetwork, assertNetworkReachable } = useNetwork()
const { silentAssertConnectedNetwork, assertInternetReachable, assertMediatorReachable } = useNetwork()
const [{ disableMediatorCheck }] = useServices([TOKENS.CONFIG])
const { t } = useTranslation()
const [hasShown, setHasShown] = useState(false)

const isConnected = silentAssertConnectedNetwork()
useEffect(() => {
// Network is connected
if (isConnected) {
// Assert that internet is available
assertNetworkReachable().then((status) => {
// Connected to a network, reset toast
setHasShown(false)
if (status) {
return
}

// User is connected to a network but has no internet, display toast
Toast.show({
type: ToastType.Error,
autoHide: true,
text1: t('NetInfo.NoInternetConnectionTitle'),
})
})
return
}

// Only show the toast if the user hasn't seen it already
if (!hasShown) {
useEffect(() => {
const _showNetworkWarning = () => {
setHasShown(true)
Toast.show({
type: ToastType.Error,
autoHide: true,
text1: t('NetInfo.NoInternetConnectionTitle'),
})
}
}, [isConnected, assertNetworkReachable, t, hasShown])
// Network is available, do further testing according to CFG.disableMediatorCheck
if (!disableMediatorCheck) {
// Network is available
if (isConnected) {
// Check mediator socket, also assert internet reachable
assertMediatorReachable().then((status) => {
if (status) {
Toast.hide()
return
} else {
// Network is available but cannot access nediator, display toast
_showNetworkWarning()
}
})
return
} else if (!hasShown) {
_showNetworkWarning()
}
return
} else {
// Check internetReachable by connecting test beacon urls
assertInternetReachable().then((status) => {
if (status) {
Toast.hide()
return
} else if (null === status) {
// keep silent when the internet status not yet assert
return
/*
Toast.show({
type: ToastType.Info,
autoHide: false,
text1: "Checking internet reachable",
})
*/
} else if (!hasShown) {
_showNetworkWarning()
}
})
}
}, [
isConnected,
disableMediatorCheck,
assertInternetReachable,
assertMediatorReachable,
t,
hasShown
])

return null
}
Expand Down
2 changes: 2 additions & 0 deletions packages/legacy/core/App/container-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export const defaultConfig: Config = {
showPreface: false,
disableOnboardingSkip: false,
disableContactsInSettings: false,
disableMediatorCheck: false,
internetReachabilityUrls: ['https://clients3.google.com/generate_204'],
whereToUseWalletUrl: 'https://example.com',
showScanHelp: true,
showScanButton: true,
Expand Down
16 changes: 11 additions & 5 deletions packages/legacy/core/App/contexts/network.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { createContext, useContext, useState } from 'react'
import NetInfoModal from '../components/modals/NetInfoModal'
import { hostnameFromURL, canConnectToHost } from '../utils/network'
import { Config } from 'react-native-config'

export interface NetworkContext {
silentAssertConnectedNetwork: () => boolean
assertNetworkConnected: () => boolean
displayNetInfoModal: () => void
hideNetInfoModal: () => void
assertNetworkReachable: () => Promise<boolean>
assertInternetReachable: () => Promise<boolean>
assertMediatorReachable: () => Promise<boolean>
}

export const NetworkContext = createContext<NetworkContext>(null as unknown as NetworkContext)
Expand All @@ -28,19 +30,22 @@ export const NetworkProvider: React.FC<React.PropsWithChildren> = ({ children })
}

const silentAssertConnectedNetwork = () => {
return netInfo.isConnected || netInfo.type !== NetInfoStateType.none
return netInfo.isConnected || [NetInfoStateType.wifi, NetInfoStateType.cellular].includes(netInfo.type)
}

const assertNetworkConnected = () => {
const isConnected = silentAssertConnectedNetwork()
if (!isConnected) {
displayNetInfoModal()
}

return isConnected
}

const assertNetworkReachable = async (): Promise<boolean> => {
const assertInternetReachable = async (): Promise<boolean> => {
return netInfo.isInternetReachable as boolean
}

const assertMediatorReachable = async (): Promise<boolean> => {
const hostname = hostnameFromURL(Config.MEDIATOR_URL!)

if (hostname === null || hostname.length === 0) {
Expand All @@ -60,7 +65,8 @@ export const NetworkProvider: React.FC<React.PropsWithChildren> = ({ children })
assertNetworkConnected,
displayNetInfoModal,
hideNetInfoModal,
assertNetworkReachable,
assertInternetReachable,
assertMediatorReachable
}}
>
{children}
Expand Down
4 changes: 3 additions & 1 deletion packages/legacy/core/App/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ export { BifoldError } from './types/error'
export { EventTypes } from './constants'
export { migrateToAskar } from './utils/migration'
export { createLinkSecretIfRequired, getAgentModules } from './utils/agent'
export { removeExistingInvitationIfRequired, connectFromScanOrDeepLink } from './utils/helpers'
export { removeExistingInvitationIfRequired, connectFromScanOrDeepLink, formatTime, useCredentialConnectionLabel, getConnectionName } from './utils/helpers'
export { isValidAnonCredsCredential, getCredentialIdentifiers } from './utils/credential'
export { buildFieldsFromAnonCredsCredential } from './utils/oca'

export type { AnimatedComponents } from './animated-components'
export type {
Expand Down
4 changes: 2 additions & 2 deletions packages/legacy/core/App/modules/history/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import { IHistoryManager } from './types'
export type { IHistoryManager }
import { IHistoryManager, HistoryRecord} from './types'
export type { IHistoryManager, HistoryRecord }
5 changes: 3 additions & 2 deletions packages/legacy/core/App/navigators/SettingStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,14 @@ const SettingStack: React.FC = () => {
/>
<Stack.Screen
name={Screens.CreatePIN}
component={(props) => <PINCreate explainedStatus {...props} />}
options={{
title: t('Screens.ChangePIN'),
headerBackTestID: testIdWithKey('Back'),
...ScreenOptionsDictionary[Screens.CreatePIN],
}}
/>
>
{(props: any) => <PINCreate explainedStatus {...props} />}
</Stack.Screen>
<Stack.Screen
name={Screens.UsePushNotifications}
component={PushNotification}
Expand Down
2 changes: 2 additions & 0 deletions packages/legacy/core/App/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export interface Config {
contactDetailsOptions?: ContactDetailsOptionsParams
credentialHideList?: string[]
disableContactsInSettings?: boolean
disableMediatorCheck?: boolean
internetReachabilityUrls: string[]
}

export interface HistoryEventsLoggerConfig {
Expand Down
4 changes: 3 additions & 1 deletion packages/legacy/core/__tests__/contexts/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const networkContext = {
silentAssertConnectedNetwork: jest.fn(),
displayNetInfoModal: jest.fn(),
hideNetInfoModal: jest.fn(),
assertNetworkReachable: jest.fn(),
// assertNetworkReachable: jest.fn(),
assertInternetReachable: jest.fn(),
assertMediatorReachable: jest.fn(),
}

export default networkContext
6 changes: 4 additions & 2 deletions packages/legacy/core/__tests__/screens/Chat.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ describe('Chat Screen', () => {
jest.spyOn(network, 'useNetwork').mockImplementation(() => ({
silentAssertConnectedNetwork: () => true,
assertNetworkConnected: () => true,
assertNetworkReachable: jest.fn(),
assertInternetReachable: jest.fn(),
assertMediatorReachable: jest.fn(),
displayNetInfoModal: jest.fn(),
hideNetInfoModal: jest.fn(),
}))
Expand All @@ -108,7 +109,8 @@ describe('Chat screen with messages', () => {
jest.spyOn(network, 'useNetwork').mockImplementation(() => ({
silentAssertConnectedNetwork: () => true,
assertNetworkConnected: () => true,
assertNetworkReachable: jest.fn(),
assertInternetReachable: jest.fn(),
assertMediatorReachable: jest.fn(),
displayNetInfoModal: jest.fn(),
hideNetInfoModal: jest.fn(),
}))
Expand Down

0 comments on commit 21649a8

Please sign in to comment.