Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Change network status check feature in NetInfo, disable/enable mediator check and provide new method to check isInternetReachable #1390

Merged
merged 17 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 13 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
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
17 changes: 12 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: (_urls?: string[]) => Promise<boolean>
assertMediatorReachable: () => Promise<boolean>
}

export const NetworkContext = createContext<NetworkContext>(null as unknown as NetworkContext)
Expand All @@ -28,19 +30,23 @@ 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> => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const assertInternetReachable = async (_urls?: string[]): 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 +66,8 @@ export const NetworkProvider: React.FC<React.PropsWithChildren> = ({ children })
assertNetworkConnected,
displayNetInfoModal,
hideNetInfoModal,
assertNetworkReachable,
assertInternetReachable,
assertMediatorReachable
}}
>
{children}
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 @@ -37,6 +37,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