From 1059781fdd988ccda14c94b9e7247798010845c7 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Wed, 30 May 2018 10:59:43 +0200 Subject: [PATCH] MOBILE-2404 core: Fix desktop notifications store --- .../mod/scorm/providers/scorm-offline.ts | 2 +- src/core/emulator/emulator.module.ts | 7 +-- .../emulator/providers/local-notifications.ts | 46 ++++++++++++++++--- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/addon/mod/scorm/providers/scorm-offline.ts b/src/addon/mod/scorm/providers/scorm-offline.ts index 86d7969f8..cf68996fc 100644 --- a/src/addon/mod/scorm/providers/scorm-offline.ts +++ b/src/addon/mod/scorm/providers/scorm-offline.ts @@ -358,7 +358,7 @@ export class AddonModScormOfflineProvider { }).then((entry) => { entry.snapshot = this.textUtils.parseJSON(entry.snapshot); - return entry;; + return entry; }); } diff --git a/src/core/emulator/emulator.module.ts b/src/core/emulator/emulator.module.ts index 93a7fdb5e..68260edc6 100644 --- a/src/core/emulator/emulator.module.ts +++ b/src/core/emulator/emulator.module.ts @@ -159,10 +159,11 @@ export const IONIC_NATIVE_PROVIDERS = [ Keyboard, { provide: LocalNotifications, - deps: [CoreAppProvider, CoreUtilsProvider], - useFactory: (appProvider: CoreAppProvider, utils: CoreUtilsProvider): LocalNotifications => { + deps: [CoreAppProvider, CoreUtilsProvider, CoreTextUtilsProvider], + useFactory: (appProvider: CoreAppProvider, utils: CoreUtilsProvider, txtUtils: CoreTextUtilsProvider) + : LocalNotifications => { // Use platform instead of CoreAppProvider to prevent circular dependencies. - return appProvider.isMobile() ? new LocalNotifications() : new LocalNotificationsMock(appProvider, utils); + return appProvider.isMobile() ? new LocalNotifications() : new LocalNotificationsMock(appProvider, utils, txtUtils); } }, { diff --git a/src/core/emulator/providers/local-notifications.ts b/src/core/emulator/providers/local-notifications.ts index 50f9ea52a..47ba21038 100644 --- a/src/core/emulator/providers/local-notifications.ts +++ b/src/core/emulator/providers/local-notifications.ts @@ -15,6 +15,7 @@ import { Injectable } from '@angular/core'; import { LocalNotifications, ILocalNotification } from '@ionic-native/local-notifications'; import { CoreAppProvider } from '@providers/app'; +import { CoreTextUtilsProvider } from '@providers/utils/text'; import { CoreUtilsProvider } from '@providers/utils/utils'; import { SQLiteDB } from '@classes/sqlitedb'; import { CoreConstants } from '@core/constants'; @@ -49,6 +50,22 @@ export class LocalNotificationsMock extends LocalNotifications { type: 'INTEGER', primaryKey: true }, + { + name: 'title', + type: 'TEXT' + }, + { + name: 'text', + type: 'TEXT' + }, + { + name: 'at', + type: 'INTEGER' + }, + { + name: 'data', + type: 'TEXT' + }, { name: 'triggered', type: 'INTEGER' @@ -71,7 +88,7 @@ export class LocalNotificationsMock extends LocalNotifications { at: undefined }; - constructor(private appProvider: CoreAppProvider, private utils: CoreUtilsProvider) { + constructor(private appProvider: CoreAppProvider, private utils: CoreUtilsProvider, private textUtils: CoreTextUtilsProvider) { super(); this.appDB = appProvider.getDB(); @@ -281,7 +298,15 @@ export class LocalNotificationsMock extends LocalNotifications { * @return {Promise} Promise resolved with the notifications. */ protected getAllNotifications(): Promise { - return this.appDB.getAllRecords(this.DESKTOP_NOTIFS_TABLE); + return this.appDB.getAllRecords(this.DESKTOP_NOTIFS_TABLE).then((notifications) => { + notifications.forEach((notification) => { + notification.at = new Date(notification.at); + notification.data = this.textUtils.parseJSON(notification.data); + notification.triggered = !!notification.triggered; + }); + + return notifications; + }); } /** @@ -663,15 +688,22 @@ export class LocalNotificationsMock extends LocalNotifications { /** * Store a notification in local DB. * - * @param {any} notification Notification to store. + * @param {ILocalNotification} notification Notification to store. * @param {boolean} triggered Whether the notification has been triggered. * @return {Promise} Promise resolved when stored. */ - protected storeNotification(notification: any, triggered: boolean): Promise { - notification = Object.assign({}, notification); // Clone the object. - notification.triggered = !!triggered; + protected storeNotification(notification: ILocalNotification, triggered: boolean): Promise { + // Only store some of the properties. + const entry = { + id : notification.id, + title: notification.title, + text: notification.text, + at: notification.at ? notification.at.getTime() : 0, + data: notification.data ? JSON.stringify(notification.data) : '{}', + triggered: triggered ? 1 : 0 + }; - return this.appDB.insertRecord(this.DESKTOP_NOTIFS_TABLE, notification); + return this.appDB.insertRecord(this.DESKTOP_NOTIFS_TABLE, entry); } /**