JsonStore added and Service created

This commit is contained in:
Eudes Inácio
2021-07-29 16:02:39 +01:00
parent 32017af732
commit 8870d2de05
6 changed files with 150 additions and 7 deletions
+2 -1
View File
@@ -163,7 +163,8 @@
"cordova-plugin-androidx": {}, "cordova-plugin-androidx": {},
"cordova-plugin-androidx-adapter": {}, "cordova-plugin-androidx-adapter": {},
"cordova-plugin-fingerprint-aio": {}, "cordova-plugin-fingerprint-aio": {},
"cordova-plugin-mfp-push": {} "cordova-plugin-mfp-push": {},
"cordova-plugin-mfp-jsonstore": {}
}, },
"platforms": [ "platforms": [
"browser", "browser",
Binary file not shown.

Before

Width:  |  Height:  |  Size: 59 KiB

After

Width:  |  Height:  |  Size: 47 KiB

+3 -4
View File
@@ -85,15 +85,15 @@ export class HomePage implements OnInit {
if(this.platform.is('desktop') || this.platform.is('mobileweb')) { if(this.platform.is('desktop') || this.platform.is('mobileweb')) {
console.log('Notifications not supported') console.log('Notifications not supported')
} else { } else {
/* this.mobilefirstConnect(); this.mobilefirstConnect();
this.notificatinsservice.onReceviNotification(); */ this.notificatinsservice.onReceviNotification();
} }
} }
mobilefirstConnect() { mobilefirstConnect() {
//try {
window['WLAuthorizationManager'].obtainAccessToken("").then( (token) => { window['WLAuthorizationManager'].obtainAccessToken("").then( (token) => {
console.log('MobileFirst Server connect: Success ' + token); console.log('MobileFirst Server connect: Success ' + token);
@@ -121,7 +121,6 @@ export class HomePage implements OnInit {
alert("Failed to connect to MobileFirst Server"); alert("Failed to connect to MobileFirst Server");
}); */ }); */
}); });
//} catch {}
} }
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { JsonStore } from './jsonStore.service';
describe('JsonStore', () => {
let service: JsonStore;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(JsonStore);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
+121
View File
@@ -0,0 +1,121 @@
///<reference path="../../../plugins/cordova-plugin-mfp/typings/worklight.d.ts" />
///<reference path="../../../plugins/cordova-plugin-mfp-jsonstore/typings/jsonstore.d.ts" />
import { Injectable } from "@angular/core";
@Injectable({
providedIn: 'root'
})
export class JsonStore {
createCollection(name, data) {
var collectionName = name;
var addOptions = {
// Mark data as dirty (true = yes, false = no), default true.
markDirty: true
};
var JSONStoreCollections = {};
JSONStoreCollections[collectionName] = {};
JSONStoreCollections[collectionName].searchFields = { UserId: 'integer' };
WL.JSONStore.init(JSONStoreCollections)
.then(function () {
WL.Logger.debug('Init done');
console.log('Init done');
return WL.JSONStore.get(collectionName).add(data, addOptions);
}).fail(function (err) {
WL.Logger.error(err);
console.log(err)
});
}
getCollection(collectionName) {
var allOptions = {
// Returns a maximum of 10 documents, default no limit.
limit: 10,
// Skip 0 documents, default no offset.
offset: 0,
// Search fields to return, default: ['_id', 'json'].
filter: ['UserId', 'json'],
// How to sort the returned values, default no sort.}]
};
var JSONStoreCollections = {};
JSONStoreCollections[collectionName] = {};
JSONStoreCollections[collectionName].searchFields = { UserId: 'integer' };
WL.JSONStore.init(JSONStoreCollections)
.then(function () {
WL.Logger.debug('Find all colletion data');
console.log('Find all colletion data');
return WL.JSONStore.get(collectionName).findAll(allOptions).then((value) => {
console.log('Find all colletion data', value);
});
}).fail(function (err) {
WL.Logger.error(err);
console.log("JsonStore getColletion error ",err)
});
}
getColletionById(collectionName, value) {
var query = { UserId: value };
var allOptions = {
// Returns a maximum of 10 documents, default no limit.
limit: 10,
// Skip 0 documents, default no offset.
offset: 0,
// Search fields to return, default: ['_id', 'json'].
filter: ['UserId', 'json'],
// How to sort the returned values, default no sort.}]
};
var JSONStoreCollections = {};
JSONStoreCollections[collectionName] = {};
JSONStoreCollections[collectionName].searchFields = { UserId: 'integer' };
WL.JSONStore.init(JSONStoreCollections)
.then(function () {
WL.Logger.debug('Find colletion data by id');
console.log('Find colletion data by id');
return WL.JSONStore.get(collectionName).find(query, allOptions).then((value) => {
console.log('Find colletion data by id', value);
});
}).fail(function (err) {
WL.Logger.error(err);
console.log(err)
});
}
replaceDocument(collectionName: string, document: any) {
/* var document = { Example of how identify the document to replace
_id: 1, json: {name: 'chevy', age: 23}
}; */
var options = {};
WL.JSONStore.get(collectionName).replace(document, options).then(function (numberOfDocsReplaced) {
console.log("JsonStore replace document sucess: ", numberOfDocsReplaced)
}).fail(function (error) {
console.log("JsonStore replace document error: ", error)
});
}
removeDocument(collectionName: any,query: any,) {
/* var query = { _id: 1 }; Exemple of query*/
var options = { exact: true };
WL.JSONStore.get(collectionName).remove(query, options).then(function (numberOfDocsRemoved) {
console.log("JsonStore remove document sucess: ", numberOfDocsRemoved)
}).fail(function (error) {
console.log("JsonStore remove document erro: ", error)
});
}
}
+7 -1
View File
@@ -12,6 +12,7 @@ import { ModalController, AlertController, AnimationController, Platform } from
import { NavigationExtras,Router } from '@angular/router'; import { NavigationExtras,Router } from '@angular/router';
import { ToastService } from '../services/toast.service'; import { ToastService } from '../services/toast.service';
import { Optional } from '@angular/core'; import { Optional } from '@angular/core';
import { JsonStore } from './jsonStore.service';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@@ -31,7 +32,8 @@ export class NotificationsService {
private router: Router, private router: Router,
private toastService: ToastService, private toastService: ToastService,
private zone: NgZone, private zone: NgZone,
private activeroute: ActivatedRoute) { } private activeroute: ActivatedRoute,
private jsonstore: JsonStore) { }
getTokenByUserIdAndId(user, userID) { getTokenByUserIdAndId(user, userID) {
const geturl = environment.apiURL + 'notifications/user/' + userID; const geturl = environment.apiURL + 'notifications/user/' + userID;
@@ -40,6 +42,7 @@ export class NotificationsService {
} }
getAndpostToken(username) { getAndpostToken(username) {
if (this.platform.is('desktop') || this.platform.is('mobileweb')) { if (this.platform.is('desktop') || this.platform.is('mobileweb')) {
console.log('Notifications not supported') console.log('Notifications not supported')
} else { } else {
@@ -109,6 +112,9 @@ export class NotificationsService {
} }
); );
var notificationReceived = (message) => { var notificationReceived = (message) => {
this.jsonstore.createCollection('Notifications',message);
this.jsonstore.getCollection('Notifications')
console.log("Notification Store: ", this.jsonstore.getCollection('Notifications')[0]);
console.log(message); console.log(message);
var data = JSON.parse(message.payload); var data = JSON.parse(message.payload);
console.log(data.Service); console.log(data.Service);