Skip to content

Commit

Permalink
Simplify config to only pass the callType without pressActions
Browse files Browse the repository at this point in the history
  • Loading branch information
dprevost-LMI committed Oct 31, 2024
1 parent 2f99ad6 commit 4b588ae
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
21 changes: 16 additions & 5 deletions packages/react-native/src/types/NotificationAndroid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,17 @@ export interface AndroidMessagingStyleMessage {
person?: AndroidPerson;
}


export enum DefaultActionId {
ANSWER = 'answer',
DECLINE = 'decline',
HANG_UP = 'hangUp',
}

export interface CallStyleAction {
pressAction: Omit<NotificationPressAction, 'id'> & { id?: string };
}

export const enum AndroidCallType {
INCOMING = 1,
ONGOING = 2,
Expand All @@ -761,19 +772,19 @@ export const enum AndroidCallType {

export interface AndroidCallTypeIncoming {
callType: AndroidCallType.INCOMING;
answerAction: AndroidAction;
declineAction: AndroidAction;
answerAction?: CallStyleAction;
declineAction?: CallStyleAction;
}

export interface AndroidCallTypeOngoing {
callType: AndroidCallType.ONGOING;
hangUpAction: AndroidAction;
hangUpAction?: CallStyleAction;
}

export interface AndroidCallTypeScreening {
callType: AndroidCallType.SCREENING;
answerAction: AndroidAction;
hangUpAction: AndroidAction;
answerAction?: CallStyleAction;
hangUpAction?: CallStyleAction;
}

/**
Expand Down
24 changes: 18 additions & 6 deletions packages/react-native/src/validators/validateAndroidStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import {
AndroidCallStyle,
AndroidStyle,
AndroidCallType,
CallStyleAction,
DefaultActionId,
} from '../types/NotificationAndroid';
import { objectHasProperty, isArray, isBoolean, isNumber, isObject, isString } from '../utils';
import validateAndroidAction from './validateAndroidAction';
import validateAndroidPressAction from './validateAndroidPressAction';

/**
* Validates a BigPictureStyle
Expand Down Expand Up @@ -345,8 +347,8 @@ export function validateAndroidCallStyle(style: AndroidCallStyle): AndroidCallSt

switch (style.callTypeActions.callType) {
case AndroidCallType.INCOMING: {
const answerAction = validateAndroidAction(style.callTypeActions.answerAction)
const declineAction = validateAndroidAction(style.callTypeActions.declineAction)
const answerAction = validateCallStyleAction(style.callTypeActions.answerAction, DefaultActionId.ANSWER)
const declineAction = validateCallStyleAction(style.callTypeActions.declineAction, DefaultActionId.DECLINE)
return {
type: AndroidStyle.CALL,
person,
Expand All @@ -358,7 +360,7 @@ export function validateAndroidCallStyle(style: AndroidCallStyle): AndroidCallSt
};
}
case AndroidCallType.ONGOING: {
const hangUpAction = validateAndroidAction(style.callTypeActions.hangUpAction)
const hangUpAction = validateCallStyleAction(style.callTypeActions.hangUpAction, DefaultActionId.HANG_UP)
return {
type: AndroidStyle.CALL,
person,
Expand All @@ -369,8 +371,8 @@ export function validateAndroidCallStyle(style: AndroidCallStyle): AndroidCallSt
};
}
case AndroidCallType.SCREENING: {
const answerAction = validateAndroidAction(style.callTypeActions.answerAction)
const hangUpAction = validateAndroidAction(style.callTypeActions.hangUpAction)
const answerAction = validateCallStyleAction(style.callTypeActions.answerAction, DefaultActionId.ANSWER)
const hangUpAction = validateCallStyleAction(style.callTypeActions.hangUpAction, DefaultActionId.HANG_UP)
return {
type: AndroidStyle.CALL,
person,
Expand All @@ -384,3 +386,13 @@ export function validateAndroidCallStyle(style: AndroidCallStyle): AndroidCallSt
default: throw new Error("'callType' expected a value of 0, 1 or 2.");
}
}

function validateCallStyleAction(action: CallStyleAction | undefined, defaultPressActionId: string): CallStyleAction {
const { pressAction } = (action || { pressAction : { id: defaultPressActionId }});
try {
const out = validateAndroidPressAction({...pressAction, id: pressAction.id || defaultPressActionId});
return { pressAction: out };
} catch (e: any) {
throw new Error(`'action' ${e.message}.`);
}
}
4 changes: 2 additions & 2 deletions tests_react_native/example/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export const notifications: { key: string; notification: Notification | Notifica
person: { name: 'John Doe' },
callTypeActions: {
callType: 1,
answerAction: { title: 'Answer', pressAction: { id: 'answer' } },
declineAction: { title: 'Decline', pressAction: { id: 'decline' } },
answerAction: { pressAction: { id: 'answer' } },
declineAction: { pressAction: { id: 'stop' } },
},
},
},
Expand Down

0 comments on commit 4b588ae

Please sign in to comment.