1414
1515import { Injectable } from '@angular/core' ;
1616import { LocalNotifications , ILocalNotification , ILocalNotificationAction } from '@ionic-native/local-notifications' ;
17- import { CoreAppProvider } from '@providers/app' ;
17+ import { CoreAppProvider , CoreAppSchema } from '@providers/app' ;
1818import { CoreTextUtilsProvider } from '@providers/utils/text' ;
1919import { CoreUtilsProvider } from '@providers/utils/utils' ;
20- import { SQLiteDB , SQLiteDBTableSchema } from '@classes/sqlitedb' ;
20+ import { SQLiteDB } from '@classes/sqlitedb' ;
2121import { CoreConstants } from '@core/constants' ;
2222import { CoreConfigConstants } from '../../../configconstants' ;
2323import * as moment from 'moment' ;
@@ -43,41 +43,48 @@ export class LocalNotificationsMock extends LocalNotifications {
4343
4444 // Variables for database.
4545 protected DESKTOP_NOTIFS_TABLE = 'desktop_local_notifications' ;
46- protected tableSchema : SQLiteDBTableSchema = {
47- name : this . DESKTOP_NOTIFS_TABLE ,
48- columns : [
46+ protected tableSchema : CoreAppSchema = {
47+ name : 'LocalNotificationsMock' ,
48+ version : 1 ,
49+ tables : [
4950 {
50- name : 'id' ,
51- type : 'INTEGER' ,
52- primaryKey : true
53- } ,
54- {
55- name : 'title' ,
56- type : 'TEXT'
57- } ,
58- {
59- name : 'text' ,
60- type : 'TEXT'
61- } ,
62- {
63- name : 'at' ,
64- type : 'INTEGER'
65- } ,
66- {
67- name : 'data' ,
68- type : 'TEXT'
51+ name : this . DESKTOP_NOTIFS_TABLE ,
52+ columns : [
53+ {
54+ name : 'id' ,
55+ type : 'INTEGER' ,
56+ primaryKey : true
57+ } ,
58+ {
59+ name : 'title' ,
60+ type : 'TEXT'
61+ } ,
62+ {
63+ name : 'text' ,
64+ type : 'TEXT'
65+ } ,
66+ {
67+ name : 'at' ,
68+ type : 'INTEGER'
69+ } ,
70+ {
71+ name : 'data' ,
72+ type : 'TEXT'
73+ } ,
74+ {
75+ name : 'triggered' ,
76+ type : 'INTEGER'
77+ }
78+ ] ,
6979 } ,
70- {
71- name : 'triggered' ,
72- type : 'INTEGER'
73- }
74- ]
80+ ] ,
7581 } ;
7682
7783 protected appDB : SQLiteDB ;
7884 protected scheduled : { [ i : number ] : any } = { } ;
7985 protected triggered : { [ i : number ] : any } = { } ;
8086 protected observers : { [ event : string ] : Subject < any > } ;
87+ protected dbReady : Promise < any > ; // Promise resolved when the app DB is initialized.
8188 protected defaults = {
8289 actions : [ ] ,
8390 attachments : [ ] ,
@@ -117,7 +124,9 @@ export class LocalNotificationsMock extends LocalNotifications {
117124 super ( ) ;
118125
119126 this . appDB = appProvider . getDB ( ) ;
120- this . appDB . createTableFromSchema ( this . tableSchema ) ;
127+ this . dbReady = appProvider . createTablesFromSchema ( this . tableSchema ) . catch ( ( ) => {
128+ // Ignore errors.
129+ } ) ;
121130
122131 // Initialize observers.
123132 this . observers = {
@@ -550,20 +559,21 @@ export class LocalNotificationsMock extends LocalNotifications {
550559 *
551560 * @return Promise resolved with the notifications.
552561 */
553- protected getAllNotifications ( ) : Promise < any > {
554- return this . appDB . getAllRecords ( this . DESKTOP_NOTIFS_TABLE ) . then ( ( notifications ) => {
555- notifications . forEach ( ( notification ) => {
556- notification . trigger = {
557- at : new Date ( notification . at )
558- } ;
559- notification . data = this . textUtils . parseJSON ( notification . data ) ;
560- notification . triggered = ! ! notification . triggered ;
562+ protected async getAllNotifications ( ) : Promise < any > {
563+ await this . dbReady ;
561564
562- this . mergeWithDefaults ( notification ) ;
563- } ) ;
565+ const notifications = await this . appDB . getAllRecords ( this . DESKTOP_NOTIFS_TABLE ) ;
566+ notifications . forEach ( ( notification ) => {
567+ notification . trigger = {
568+ at : new Date ( notification . at ) ,
569+ } ;
570+ notification . data = this . textUtils . parseJSON ( notification . data ) ;
571+ notification . triggered = ! ! notification . triggered ;
564572
565- return notifications ;
573+ this . mergeWithDefaults ( notification ) ;
566574 } ) ;
575+
576+ return notifications ;
567577 }
568578
569579 /**
@@ -889,7 +899,9 @@ export class LocalNotificationsMock extends LocalNotifications {
889899 * @param id ID of the notification.
890900 * @return Promise resolved when done.
891901 */
892- protected removeNotification ( id : number ) : Promise < any > {
902+ protected async removeNotification ( id : number ) : Promise < any > {
903+ await this . dbReady ;
904+
893905 return this . appDB . deleteRecords ( this . DESKTOP_NOTIFS_TABLE , { id : id } ) ;
894906 }
895907
@@ -979,7 +991,9 @@ export class LocalNotificationsMock extends LocalNotifications {
979991 * @param triggered Whether the notification has been triggered.
980992 * @return Promise resolved when stored.
981993 */
982- protected storeNotification ( notification : ILocalNotification , triggered : boolean ) : Promise < any > {
994+ protected async storeNotification ( notification : ILocalNotification , triggered : boolean ) : Promise < any > {
995+ await this . dbReady ;
996+
983997 // Only store some of the properties.
984998 const entry = {
985999 id : notification . id ,
0 commit comments