Skip to content

Commit

Permalink
Merge pull request #32 from idpass/develop
Browse files Browse the repository at this point in the history
Contributing branch "develop" from fork "idpass/develop"
  • Loading branch information
kneckinator authored Jun 8, 2022
2 parents 852c76b + 9928853 commit 8fbd3d2
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 29 deletions.
38 changes: 36 additions & 2 deletions machines/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import { createRequestMachine, requestMachine } from './request';
import { createScanMachine, scanMachine } from './scan';
import { pure, respond } from 'xstate/lib/actions';
import { AppServices } from '../shared/GlobalContext';
import { request } from '../shared/request';

const model = createModel(
{
info: {} as AppInfo,
backendInfo: {} as BackendInfo,
serviceRefs: {} as AppServices,
},
{
Expand All @@ -31,6 +33,7 @@ const model = createModel(
REQUEST_DEVICE_INFO: () => ({}),
READY: (data?: unknown) => ({ data }),
APP_INFO_RECEIVED: (info: AppInfo) => ({ info }),
BACKEND_INFO_RECEIVED: (info: BackendInfo) => ({ info }),
},
}
);
Expand Down Expand Up @@ -66,11 +69,22 @@ export const appMachine = model.createMachine(
},
on: {
APP_INFO_RECEIVED: {
target: '#ready',
target: 'devinfo',
actions: ['setAppInfo'],
},
},
},
devinfo: {
invoke: {
src: 'getBackendInfo',
},
on: {
BACKEND_INFO_RECEIVED: {
target: '#ready',
actions: ['setBackendInfo'],
},
},
},
},
},
ready: {
Expand Down Expand Up @@ -197,6 +211,10 @@ export const appMachine = model.createMachine(
setAppInfo: model.assign({
info: (_, event) => event.info,
}),

setBackendInfo: model.assign({
backendInfo: (_, event) => event.info,
}),
},

services: {
Expand All @@ -205,10 +223,14 @@ export const appMachine = model.createMachine(
deviceId: getDeviceId(),
deviceName: await getDeviceName(),
};

callback(model.events.APP_INFO_RECEIVED(appInfo));
},

getBackendInfo: () => async (callback) => {
const backendInfo = await request('GET', '/info');
callback(model.events.BACKEND_INFO_RECEIVED(backendInfo));
},

checkFocusState: () => (callback) => {
const changeHandler = (newState: AppStateStatus) => {
switch (newState) {
Expand Down Expand Up @@ -256,13 +278,25 @@ interface AppInfo {
deviceId: string;
deviceName: string;
}
interface BackendInfo {
application: {
name: string;
version: string;
};
build: object;
config: object;
}

type State = StateFrom<typeof appMachine>;

export function selectAppInfo(state: State) {
return state.context.info;
}

export function selectBackendInfo(state: State) {
return state.context.backendInfo;
}

export function selectIsReady(state: State) {
return state.matches('ready');
}
Expand Down
6 changes: 5 additions & 1 deletion machines/app.typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface Typegen0 {
'@@xstate/typegen': true;
'eventsCausingActions': {
setAppInfo: 'APP_INFO_RECEIVED';
setBackendInfo: 'BACKEND_INFO_RECEIVED';
requestDeviceInfo: 'REQUEST_DEVICE_INFO';
spawnStoreActor: 'xstate.init';
logStoreEvents: 'xstate.init';
Expand All @@ -16,6 +17,7 @@ export interface Typegen0 {
};
'invokeSrcNameMap': {
getAppInfo: 'done.invoke.app.init.info:invocation[0]';
getBackendInfo: 'done.invoke.app.init.devinfo:invocation[0]';
checkFocusState: 'done.invoke.app.ready.focus:invocation[0]';
checkNetworkState: 'done.invoke.app.ready.network:invocation[0]';
};
Expand All @@ -27,6 +29,7 @@ export interface Typegen0 {
};
'eventsCausingServices': {
getAppInfo: 'READY';
getBackendInfo: 'APP_INFO_RECEIVED';
checkFocusState: 'xstate.init';
checkNetworkState: 'xstate.init';
};
Expand All @@ -37,6 +40,7 @@ export interface Typegen0 {
| 'init.store'
| 'init.services'
| 'init.info'
| 'init.devinfo'
| 'ready'
| 'ready.focus'
| 'ready.focus.checking'
Expand All @@ -47,7 +51,7 @@ export interface Typegen0 {
| 'ready.network.online'
| 'ready.network.offline'
| {
init?: 'store' | 'services' | 'info';
init?: 'store' | 'services' | 'info' | 'devinfo';
ready?:
| 'focus'
| 'network'
Expand Down
18 changes: 14 additions & 4 deletions machines/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import { AppServices } from '../shared/GlobalContext';
import { ActivityLogEvents } from './activityLog';
import { VC_ITEM_STORE_KEY } from '../shared/constants';

const checkingAirplaneMode = '#checkingAirplaneMode';
const checkingLocationService = '#checkingLocationService';

const model = createModel(
{
serviceRefs: {} as AppServices,
Expand Down Expand Up @@ -76,26 +79,31 @@ export const scanMachine = model.createMachine(
entry: ['removeLoggers'],
},
checkingAirplaneMode: {
invoke: {
src: 'checkAirplaneMode',
id: 'checkingAirplaneMode',
on: {
APP_ACTIVE: '.checkingStatus',
},
initial: 'checkingStatus',
states: {
checkingStatus: {
invoke: {
src: 'checkAirplaneMode',
},
on: {
FLIGHT_DISABLED: '#checkingLocationService',
FLIGHT_DISABLED: checkingLocationService,
FLIGHT_ENABLED: 'enabled',
},
},
requestingToDisable: {
entry: ['requestToDisableFlightMode'],
on: {
FLIGHT_DISABLED: 'checkingStatus',
FLIGHT_DISABLED: checkingLocationService,
},
},
enabled: {
on: {
FLIGHT_REQUEST: 'requestingToDisable',
FLIGHT_DISABLED: checkingLocationService,
},
},
},
Expand All @@ -111,6 +119,7 @@ export const scanMachine = model.createMachine(
on: {
LOCATION_ENABLED: 'checkingPermission',
LOCATION_DISABLED: 'requestingToEnable',
FLIGHT_ENABLED: checkingAirplaneMode,
},
},
requestingToEnable: {
Expand Down Expand Up @@ -163,6 +172,7 @@ export const scanMachine = model.createMachine(
},
{ target: 'invalid' },
],
FLIGHT_ENABLED: checkingAirplaneMode,
},
},
preparingToConnect: {
Expand Down
4 changes: 2 additions & 2 deletions machines/scan.typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface Typegen0 {
'xstate.init': { type: 'xstate.init' };
};
'invokeSrcNameMap': {
checkAirplaneMode: 'done.invoke.scan.checkingAirplaneMode:invocation[0]';
checkAirplaneMode: 'done.invoke.scan.checkingAirplaneMode.checkingStatus:invocation[0]';
checkLocationStatus: 'done.invoke.checkingLocationService:invocation[0]';
checkLocationPermission: 'done.invoke.scan.checkingLocationService.checkingPermission:invocation[0]';
discoverDevice: 'done.invoke.scan.connecting:invocation[0]';
Expand All @@ -46,7 +46,7 @@ export interface Typegen0 {
delays: never;
};
'eventsCausingServices': {
checkAirplaneMode: 'SCREEN_FOCUS';
checkAirplaneMode: 'APP_ACTIVE';
checkLocationStatus: 'FLIGHT_DISABLED';
checkLocationPermission: 'LOCATION_ENABLED' | 'APP_ACTIVE';
discoverDevice: 'RECEIVE_DEVICE_INFO';
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion screens/AuthScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const AuthScreen: React.FC<RootRouteProps> = (props) => {
<Button
title={t('useBiometrics')}
margin="0 0 8 0"
disabled={!controller.isBiometricsEnrolled}
disabled={!controller.isBiometricsAvailable}
onPress={controller.useBiometrics}
/>
<Button
Expand Down
13 changes: 7 additions & 6 deletions screens/AuthScreenController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function useAuthScreen(props: RootRouteProps) {
const isAuthorized = useSelector(authService, selectAuthorized);

const [alertMsg, setHasAlertMsg] = useState('');
const [isBiometricsEnrolled, setIsBiometricsEnrolled] = useState(false);
const [isBiometricsAvailable, setIsBiometricsAvailable] = useState(false);
const [biometricState, biometricSend, bioService] =
useMachine(biometricsMachine);

Expand All @@ -44,11 +44,11 @@ export function useAuthScreen(props: RootRouteProps) {

const { t } = useTranslation('AuthScreen');

const fetchIsEnrolled = async () => {
const result = await LocalAuthentication.isEnrolledAsync();
setIsBiometricsEnrolled(result);
const fetchIsAvailable = async () => {
const result = await LocalAuthentication.hasHardwareAsync();
setIsBiometricsAvailable(result);
};
fetchIsEnrolled();
fetchIsAvailable();

useEffect(() => {
if (isAuthorized) {
Expand Down Expand Up @@ -82,6 +82,7 @@ export function useAuthScreen(props: RootRouteProps) {
}, [isSuccessBio, isUnavailableBio, errorMsgBio, unEnrolledNoticeBio]);

const useBiometrics = async () => {
const isBiometricsEnrolled = await LocalAuthentication.isEnrolledAsync();
if (isBiometricsEnrolled) {
if (biometricState.matches({ failure: 'unenrolled' })) {
biometricSend({ type: 'RETRY_AUTHENTICATE' });
Expand All @@ -99,7 +100,7 @@ export function useAuthScreen(props: RootRouteProps) {
};

return {
isBiometricsEnrolled,
isBiometricsAvailable,
isSettingUp,
alertMsg,
isEnabledBio,
Expand Down
10 changes: 8 additions & 2 deletions screens/Profile/ProfileScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ const LanguageSetting: React.FC = () => {
export const ProfileScreen: React.FC<MainRouteProps> = (props) => {
const { t } = useTranslation('ProfileScreen');
const controller = useProfileScreen(props);

return (
<Column fill padding="24 0" backgroundColor={Colors.LightGrey}>
<MessageOverlay
Expand Down Expand Up @@ -84,12 +83,19 @@ export const ProfileScreen: React.FC<MainRouteProps> = (props) => {
</ListItem>
<Text
weight="semibold"
margin="32"
margin="32 0 0 0"
align="center"
size="smaller"
color={Colors.Grey}>
Version: {getVersion()}
</Text>
<Text weight="semibold" align="center" size="smaller" color={Colors.Grey}>
{controller.backendInfo.application.name}:{' '}
{controller.backendInfo.application.version}
</Text>
<Text weight="semibold" align="center" size="smaller" color={Colors.Grey}>
MOSIP: {controller.backendInfo.config['mosip.host']}
</Text>
</Column>
);
};
2 changes: 2 additions & 0 deletions screens/Profile/ProfileScreenController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMachine, useSelector } from '@xstate/react';
import { useContext, useEffect, useState } from 'react';
import * as LocalAuthentication from 'expo-local-authentication';
import { selectBackendInfo } from '../../machines/app';
import {
AuthEvents,
selectBiometrics,
Expand Down Expand Up @@ -92,6 +93,7 @@ export function useProfileScreen({ navigation }: MainRouteProps) {
return {
alertMsg,
hideAlert,
backendInfo: useSelector(appService, selectBackendInfo),
name: useSelector(settingsService, selectName),
vcLabel: useSelector(settingsService, selectVcLabel),
isBiometricUnlockEnabled: useSelector(
Expand Down
7 changes: 2 additions & 5 deletions screens/Scan/ScanScreenController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ export function useScanScreen({ navigation }: MainRouteProps) {
? scanService.send(ScanEvents.FLIGHT_REQUEST())
: scanService.send(ScanEvents.LOCATION_REQUEST()),
SCAN: (qrCode: string) => scanService.send(ScanEvents.SCAN(qrCode)),
DISMISS_INVALID: () => {
if (isInvalid) {
scanService.send(ScanEvents.DISMISS());
}
},
DISMISS_INVALID: () =>
isInvalid ? scanService.send(ScanEvents.DISMISS()) : null,
};
}

0 comments on commit 8fbd3d2

Please sign in to comment.