Skip to content

Commit

Permalink
Merge branch 'main' into feat/export-connection-alert-model
Browse files Browse the repository at this point in the history
  • Loading branch information
ZouidiSamih authored Jan 22, 2025
2 parents c1f75ca + 410b369 commit 0c1f614
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 51 deletions.
22 changes: 12 additions & 10 deletions packages/legacy/core/App/components/misc/QRScanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface Props {

const QRScanner: React.FC<Props> = ({ handleCodeScan, error, enableCameraOnError }) => {
const navigation = useNavigation<StackNavigationProp<ConnectStackParams>>()
const [{ showScanHelp, showScanButton }] = useServices([TOKENS.CONFIG])
const [{ showScanHelp, showScanButton, showScanErrorButton = true }] = useServices([TOKENS.CONFIG])
const [torchActive, setTorchActive] = useState(false)
const [showInfoBox, setShowInfoBox] = useState(false)
const [showErrorDetailsModal, setShowErrorDetailsModal] = useState(false)
Expand Down Expand Up @@ -109,15 +109,17 @@ const QRScanner: React.FC<Props> = ({ handleCodeScan, error, enableCameraOnError
<Text testID={testIdWithKey('ErrorMessage')} style={styles.textStyle}>
{error.message}
</Text>
<Pressable
onPress={() => setShowErrorDetailsModal(true)}
accessibilityLabel={t('Scan.ShowDetails')}
accessibilityRole={'button'}
testID={testIdWithKey('ShowDetails')}
hitSlop={hitSlop}
>
<Icon name="information-outline" size={40} style={styles.icon} />
</Pressable>
{showScanErrorButton && (
<Pressable
onPress={() => setShowErrorDetailsModal(true)}
accessibilityLabel={t('Scan.ShowDetails')}
accessibilityRole={'button'}
testID={testIdWithKey('ShowDetails')}
hitSlop={hitSlop}
>
<Icon name="information-outline" size={40} style={styles.icon} />
</Pressable>
)}
</>
) : (
<>
Expand Down
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 @@ -62,6 +62,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
1 change: 1 addition & 0 deletions packages/legacy/core/App/localization/en/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,7 @@ const translation = {
"EnterPIN": "Enter PIN",
"Home": "Notifications",
"Scan": "Scan",
"ScanHelp": "Scan Help",
"Credentials": "Credentials",
"CredentialDetails": "Credential Details",
"Notifications": "Notifications",
Expand Down
1 change: 1 addition & 0 deletions packages/legacy/core/App/localization/fr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ const translation = {
"EnterPIN": "Saisir le NIP",
"Home": "Notifications",
"Scan": "Numériser",
"ScanHelp": "Aide à la numérisation",
"Credentials": "Justificatifs",
"CredentialDetails": "Détails des attestations",
"Notifications": "Notifications",
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 }
1 change: 1 addition & 0 deletions packages/legacy/core/App/navigators/ConnectStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const ConnectStack: React.FC = () => {
name={Screens.ScanHelp}
component={ScanHelp}
options={{
title: t('Screens.ScanHelp'),
...ScreenOptionsDictionary[Screens.ScanHelp]
}}
/>
Expand Down
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
6 changes: 3 additions & 3 deletions packages/legacy/core/App/screens/ScanHelp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ const ScanHelp: React.FC = () => {
<Text style={TextTheme.headingThree}>{t('Scan.WhatToScan')}</Text>
<Text style={[style.text, { marginTop: 20 }]}>{t('Scan.ScanOnySpecial')}</Text>
<Text style={style.text}>{t('Scan.ScanOnlySpecial2')}</Text>
<Link
{whereToUseWalletUrl && (<Link
linkText={t('Scan.WhereToUseLink')}
style={style.text}
onPress={() => whereToUseWalletUrl && Linking.openURL(whereToUseWalletUrl)}
onPress={() => Linking.openURL(whereToUseWalletUrl)}
testID={testIdWithKey('WhereToUseLink')}
/>
/> )}
<Text style={style.text}>{t('Scan.ScanOnlySpecial3')}</Text>
</ScrollView>
</SafeAreaView>
Expand Down
3 changes: 3 additions & 0 deletions packages/legacy/core/App/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ export interface Config {
whereToUseWalletUrl?: string
showScanHelp?: boolean
showScanButton?: boolean
showScanErrorButton?: boolean
globalScreenOptions?: StackNavigationOptions
showDetailsInfo?: boolean
contactHideList?: string[]
contactDetailsOptions?: ContactDetailsOptionsParams
credentialHideList?: string[]
disableContactsInSettings?: boolean
disableMediatorCheck?: boolean
internetReachabilityUrls: string[]
}

export interface HistoryEventsLoggerConfig {
Expand Down
2 changes: 1 addition & 1 deletion packages/legacy/core/App/types/proof-items.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AnonCredsCredentialsForProofRequest } from '@credo-ts/anoncreds'
import { CredentialExchangeRecord } from '@credo-ts/core'
import { Attribute, Predicate } from '@hyperledger/aries-oca/build/legacy'
import { DescriptorMetadata } from 'utils/anonCredsProofRequestMapper'
import { DescriptorMetadata } from '../utils/anonCredsProofRequestMapper'

export type CredentialDataForProof = {
groupedProof: (ProofCredentialPredicates & ProofCredentialAttributes)[],
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 0c1f614

Please sign in to comment.