import { keyValue } from '../../storage' ; import * as CONSTANTS from '../../constants' ; export class Data { private readonly store : typeof keyValue ; constructor ( store = keyValue ) { this . store = store ; } public async clearAll ( ) { await this . store . set ( 'params.applicationCode' , undefined ) ; await this . store . set ( 'params.hwid' , undefined ) ; await this . store . set ( 'params.deviceType' , undefined ) ; await this . store . set ( 'params.deviceModel' , undefined ) ; await this . store . set ( 'params.language' , undefined ) ; await this . store . set ( 'params.apiEntrypoint' , undefined ) ; await this . store . set ( 'params.tokens' , undefined ) ; await this . store . set ( 'params.applicationServerKey' , undefined ) ; await this . store . set ( 'params.senderId' , undefined ) ; await this . store . set ( 'params.webSitePushId' , undefined ) ; await this . store . set ( 'params.defaultNotificationImage' , undefined ) ; await this . store . set ( 'params.defaultNotificationTitle' , undefined ) ; await this . store . set ( 'params.userId' , undefined ) ; await this . store . set ( 'params.userIdWasChanged' , undefined ) ; await this . store . set ( 'params.isLastPermissionStatus' , undefined ) ; await this . store . set ( 'params.isManualUnsubscribed' , undefined ) ; await this . store . set ( 'params.isCommunicationDisabled' , undefined ) ; await this . store . set ( 'params.isDropAllData' , undefined ) ; await this . store . set ( 'params.sdkVersion' , undefined ) ; await this . store . set ( 'params.serviceWorkerVersion' , undefined ) ; await this . store . set ( 'params.serviceWorkerUrl' , undefined ) ; await this . store . set ( 'params.serviceWorkerScope' , undefined ) ; await this . store . set ( 'params.lastOpenMessage' , undefined ) ; await this . store . set ( 'params.lastOpenApplicationTime' , undefined ) ; await this . store . set ( 'params.features' , undefined ) ; await this . store . set ( 'params.init' , undefined ) ; await this . store . set ( 'API_PARAMS' , undefined ) ; await this . store . set ( 'SENDER_ID' , undefined ) ; await this . store . set ( 'COMMUNICATION_ENABLED' , undefined ) ; await this . store . set ( 'DEVICE_DATA_REMOVED' , undefined ) ; await this . store . set ( 'LAST_OPEN_MESSAGE' , undefined ) ; await this . store . set ( 'DELAYED_EVENT' , undefined ) ; } public async setApplicationCode ( application ) { await this . store . set ( 'params.applicationCode' , application ) ; } public async getApplicationCode ( ) { return await this . store . get ( 'params.applicationCode' ) ; } public async setHwid ( hwid ) { await this . store . set ( 'params.hwid' , hwid ) ; } public async getHwid ( ) { return await this . store . get ( 'params.hwid' ) ; } public async setDeviceType ( type ) { await this . store . set ( 'params.deviceType' , type ) ; } public async getDeviceType ( ) { return await this . store . get ( 'params.deviceType' ) ; } public async setDeviceModel ( model ) { await this . store . set ( 'params.deviceModel' , model ) ; } public async getDeviceModel ( ) { return await this . store . get ( 'params.deviceModel' ) ; } public async setLanguage ( language ) { await this . store . set ( 'params.language' , language ) ; } public async getLanguage ( ) { return await this . store . get ( 'params.language' , 'en' ) ; } public async setApiEntrypoint ( url ) { await this . store . set ( 'params.apiEntrypoint' , url ) ; } public async getApiEntrypoint ( ) { return await this . store . get ( 'params.apiEntrypoint' , CONSTANTS . DEFAULT_API_URL ) ; } public async setTokens ( tokens ) { const hwid = await this . getHwid ( ) ; await this . store . set ( 'API_PARAMS' , { hwid , ... tokens } ) ; await this . store . set ( 'params.tokens' , tokens ) ; } public async getTokens ( ) { const oldValue = await this . store . get ( 'API_PARAMS' ) ; const newValue = await this . store . get ( 'params.tokens' ) ; return typeof newValue !== 'undefined' ? newValue : oldValue ; } public async setApplicationServerKey ( key ) { await this . store . set ( 'params.applicationServerKey' , key ) ; } public async getApplicationServerKey ( ) { return await this . store . get ( 'params.applicationServerKey' ) ; } public async setSenderId ( senderId ) { await this . store . set ( 'GCM_SENDER_ID' , senderId ) ; await this . store . set ( 'params.senderId' , senderId ) ; } public async getSenderId ( ) { const oldValue = await this . store . get ( 'GCM_SENDER_ID' ) ; const newValue = await this . store . get ( 'params.senderId' ) ; return typeof newValue !== 'undefined' ? newValue : oldValue ; } public async setWebSitePushId ( senderId ) { await this . store . set ( 'params.webSitePushId' , senderId ) ; } public async getWebSitePushId ( ) { return await this . store . get ( 'params.webSitePushId' ) ; } public async setDefaultNotificationImage ( url ? ) { await this . store . set ( 'params.defaultNotificationImage' , url ) ; } public async getDefaultNotificationImage ( ) { return await this . store . get ( 'params.defaultNotificationImage' , CONSTANTS . DEFAULT_NOTIFICATION_IMAGE ) ; } public async setDefaultNotificationTitle ( text ? ) { await this . store . set ( 'params.defaultNotificationTitle' , text ) ; } public async getDefaultNotificationTitle ( ) { return await this . store . get ( 'params.defaultNotificationTitle' , CONSTANTS . DEFAULT_NOTIFICATION_TITLE ) ; } public async setUserId ( userId ? ) { if ( ! userId ) { await this . store . set ( 'params.userId' , undefined ) ; return ; } await this . store . set ( 'params.userId' , userId . toString ( ) ) ; } public async getUserId ( ) { return await this . store . get ( 'params.userId' ) ; } public async setStatusUserIdWasChanged ( status ) { await this . store . set ( 'params.userIdWasChanged' , status ) ; } public async getStatusUserIdWasChanged ( ) { return await this . store . get ( 'params.userIdWasChanged' , false ) ; } public async setLastPermissionStatus ( status ) { await this . store . set ( 'params.isLastPermissionStatus' , status ) ; } public async getLastPermissionStatus ( ) { return await this . store . get ( 'params.isLastPermissionStatus' ) ; } public async setStatusManualUnsubscribed ( status ) { await this . store . set ( 'MANUAL_UNSUBSCRIBE' , status ) ; await this . store . set ( 'params.isManualUnsubscribed' , status ) ; } public async getStatusManualUnsubscribed ( ) { const oldValue = await this . store . get ( 'MANUAL_UNSUBSCRIBE' , false ) ; const newValue = await this . store . get ( 'params.isManualUnsubscribed' , false ) ; return typeof newValue !== 'undefined' ? newValue : oldValue ; } public async setStatusCommunicationDisabled ( status ) { await this . store . set ( 'COMMUNICATION_ENABLED' , status ? 0 : 1 ) ; await this . store . set ( 'params.isCommunicationDisabled' , status ) ; } public async getStatusCommunicationDisabled ( ) { const oldValue = await this . store . get ( 'COMMUNICATION_ENABLED' ) ; const newValue = await this . store . get ( 'params.isCommunicationDisabled' , false ) ; return typeof newValue !== 'undefined' ? newValue : oldValue ! == 0 ; } public async setStatusDropAllData ( status ) { await this . store . set ( 'DEVICE_DATA_REMOVED' , status ) ; await this . store . set ( 'params.isDropAllData' , status ) ; } public async getStatusDropAllData ( ) { const oldValue = await this . store . get ( 'DEVICE_DATA_REMOVED' , false ) ; const newValue = await this . store . get ( 'params.isDropAllData' , false ) ; return typeof newValue !== 'undefined' ? newValue : oldValue ; } public async setSdkVersion ( version ) { await this . store . set ( 'params.sdkVersion' , version ) ; } public async getSdkVersion ( ) { return await this . store . get ( 'params.sdkVersion' ) ; } public async setServiceWorkerVersion ( version ) { await this . store . set ( 'WORKER_VERSION' , version ) ; await this . store . set ( 'params.serviceWorkerVersion' , version ) ; } public async getServiceWorkerVersion ( ) { const oldValue = await this . store . get ( 'WORKER_VERSION' ) ; const newValue = await this . store . get ( 'params.serviceWorkerVersion' ) ; return typeof newValue !== 'undefined' ? newValue : oldValue ; } public async setServiceWorkerUrl ( url ? ) { if ( ! url ) { return ; } await this . store . set ( 'params.serviceWorkerUrl' , url ) ; } public async getServiceWorkerUrl ( ) { return await this . store . get ( 'params.serviceWorkerUrl' , CONSTANTS . DEFAULT_SERVICE_WORKER_URL ) ; } public async setServiceWorkerScope ( scope ? ) { if ( ! scope ) { return ; } await this . store . set ( 'params.serviceWorkerScope' , scope ) ; } public async getServiceWorkerScope ( ) { return await this . store . get ( 'params.serviceWorkerScope' ) ; } public async setLastOpenMessage ( message ) { await this . store . set ( 'LAST_OPEN_MESSAGE' , message ) ; await this . store . set ( 'params.lastOpenMessage' , message ) ; } public async getLastOpenMessage ( ) { const oldValue = await this . store . get ( 'LAST_OPEN_MESSAGE' ) ; const newValue = await this . store . get ( 'params.lastOpenMessage' ) ; return typeof newValue !== 'undefined' ? newValue : oldValue ; } public async setLastOpenApplicationTime ( time ) { await this . store . set ( 'params.lastOpenApplicationTime' , time ) ; } public async getLastOpenApplicationTime ( ) { return await this . store . get ( 'params.lastOpenApplicationTime' ) ; } public async setFeatures ( features ) { await this . store . set ( 'params.features' , features ) ; } public async getFeatures ( ) { return await this . store . get ( 'params.features' ) ; } public async setInitParams ( params ) { await this . store . set ( 'params.init' , params ) ; } public async getInitParams ( ) { return await this . store . get ( 'params.init' ) ; } public async setInboxLastRequestCode ( lastCode ) { await this . store . set ( 'params.inbox.lastRequestCode' , lastCode ) ; } public async getInboxLastRequestCode ( ) { return await this . store . get ( 'params.inbox.lastRequestCode' , '' ) ; } public async setInboxLastRequestTime ( lastRequestTime ) { await this . store . set ( 'params.inbox.lastRequestTime' , lastRequestTime ) ; } public async getInboxLastRequestTime ( ) { return this . store . get ( 'params.inbox.lastRequestTime' , 0 ) ; } public async setInboxNewMessagesCount ( count ) { await this . store . set ( 'params.inbox.newMessagesCount' , count ) ; } public async getInboxNewMessagesCount ( ) { return this . store . get ( 'params.inbox.newMessagesCount' , 0 ) ; } public async setDelayedEvent ( event ) { await this . store . set ( 'DELAYED_EVENT' , event ) ; await this . store . set ( 'params.delayedEvent' , event ) ; } public async getDelayedEvent ( ) { const oldValue = await this . store . get ( 'DELAYED_EVENT' ) ; const newValue = await this . store . get ( 'params.delayedEvent' ) ; return typeof newValue !== 'undefined' ? newValue : oldValue ; } public async setInApps ( inApps ) { await this . store . set ( 'params.inApps' , inApps ) ; } public async getInApps ( ) { return this . store . get ( 'params.inApps' ) ; } public async setPromptDisplayCount ( count ) { await this . store . set ( 'params.promptDisplayCount' , count ) ; } public async getPromptDisplayCount ( ) { return this . store . get ( 'params.promptDisplayCount' , 0 ) ; } public async setPromptLastSeenTime ( time ) { await this . store . set ( 'params.promptLastSeenTime' , time ) ; } public async getPromptLastSeenTime ( ) { return this . store . get ( 'params.promptLastSeenTime' , 0 ) ; } }