Add `Notifications.getStatus`
parent
5fe5320760
commit
d5b391757d
@ -0,0 +1,60 @@
|
||||
interface Environment {
|
||||
isAppFocused: boolean;
|
||||
isAudioNotificationEnabled: boolean;
|
||||
isAudioNotificationSupported: boolean;
|
||||
isEnabled: boolean;
|
||||
numNotifications: number;
|
||||
userSetting: UserSetting;
|
||||
}
|
||||
|
||||
interface Status {
|
||||
shouldClearNotifications: boolean;
|
||||
shouldPlayNotificationSound: boolean;
|
||||
shouldShowNotifications: boolean;
|
||||
type: Type;
|
||||
}
|
||||
|
||||
type UserSetting = 'off' | 'count' | 'name' | 'message';
|
||||
|
||||
type Type =
|
||||
| 'ok'
|
||||
| 'disabled'
|
||||
| 'appIsFocused'
|
||||
| 'noNotifications'
|
||||
| 'userSetting';
|
||||
|
||||
export const getStatus = (environment: Environment): Status => {
|
||||
const type = ((): Type => {
|
||||
if (!environment.isEnabled) {
|
||||
return 'disabled';
|
||||
}
|
||||
|
||||
const hasNotifications = environment.numNotifications > 0;
|
||||
if (!hasNotifications) {
|
||||
return 'noNotifications';
|
||||
}
|
||||
|
||||
if (environment.isAppFocused) {
|
||||
return 'appIsFocused';
|
||||
}
|
||||
|
||||
if (environment.userSetting === 'off') {
|
||||
return 'userSetting';
|
||||
}
|
||||
|
||||
return 'ok';
|
||||
})();
|
||||
|
||||
const shouldPlayNotificationSound =
|
||||
environment.isAudioNotificationSupported &&
|
||||
environment.isAudioNotificationEnabled;
|
||||
const shouldShowNotifications = type === 'ok';
|
||||
const shouldClearNotifications = type === 'appIsFocused';
|
||||
|
||||
return {
|
||||
shouldClearNotifications,
|
||||
shouldPlayNotificationSound,
|
||||
shouldShowNotifications,
|
||||
type,
|
||||
};
|
||||
};
|
@ -0,0 +1,3 @@
|
||||
import { getStatus } from './getStatus';
|
||||
|
||||
export { getStatus };
|
Loading…
Reference in New Issue