I have no problem to get my data stored but the problem is when I start the app for the first start with no data stored. If I try to get data, I have errors and the progam can’t work.
On one page I have this :
getData() {
this.storage.get('myData').then((data0) => {
this.data = data0[0];
this.items = data0[1];
this.totalActionsArray["home"] = data0[2];
this.totalPointsArray["home"] = data0[3];
this.events.publish('hello', this.name);
});
}
And this to set :
never(SlidingItem, item) {
item.points = 0;
item.action = 0;
item.label = item.label0,
this.totalActionsArray["home"] = this.totalActions();
this.totalPointsArray["home"] = this.totalPoints();
this.storage.set('myData', [this.data, this.items, this.totalActionsArray["home"], this.totalPointsArray["home"]]);
this.storage.set('firstStart', 'false');
this.events.publish('hello', this.name);
SlidingItem.close();
}
The whole page look like this :
import { Component } from '@angular/core';
import { NavController, App, MenuController, Events } from 'ionic-angular';
import { Storage } from '@ionic/storage';
import { Action1 } from '../action1/action1';
import { Information } from '../information/information';
import { MyService } from '../../app/my.service';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class Home {
//total = this.totalActions();
items = [];
data = [];
totalActionsArray = {};
totalPointsArray = {};
btnName: any = 'Edit';
flag: any = false;
name: string;
hideElement: boolean = false;
firstStart: boolean = true;
selectContext = {
title: 'Context',
};
selectCategory = {
title: 'Category',
};
constructor(app: App, menu: MenuController, public navCtrl: NavController, private myservice: MyService, public events: Events, public storage: Storage) {
this.data = myservice.getItemsHome();
this.items = myservice.getItemsHome();
this.totalActionsArray = myservice.getTotalActions();
this.totalPointsArray = myservice.getTotalPoints();
}
getData() {
this.storage.get('myData').then((data0) => {
this.data = data0[0];
this.items = data0[1];
this.totalActionsArray["home"] = data0[2];
this.totalPointsArray["home"] = data0[3];
this.events.publish('hello', this.name);
});
}
onContextChange(value): void {
if (value == 'all') {
this.items = this.data;
}
else {
this.items = this.data;
this.items = this.items.filter(item => item.context === value);
}
}
onCategoryChange(value): void {
if (value == 'all') {
this.items = this.data;
}
else {
this.items = this.data;
this.items = this.items.filter(item => item.category === value);
}
}
reorderItems(indexes) {
let element = this.items[indexes.from];
this.items.splice(indexes.from, 1);
this.items.splice(indexes.to, 0, element);
};
actionBtn() {
if (this.btnName == 'Edit') {
this.btnName = 'Done';
this.flag = true;
}
else {
this.btnName = 'Edit';
this.flag = false;
}
};
goToInformation() {
this.navCtrl.push(Information);
}
goTo(item) {
if (item.link == "Action1") {
this.navCtrl.push(Action1);
}
}
never(SlidingItem, item) {
item.points = 0;
item.action = 0;
item.label = item.label0,
this.totalActionsArray["home"] = this.totalActions();
this.totalPointsArray["home"] = this.totalPoints();
this.storage.set('myData', [this.data, this.items, this.totalActionsArray["home"], this.totalPointsArray["home"]]);
this.storage.set('firstStart', 'false');
this.events.publish('hello', this.name);
SlidingItem.close();
}
rarely(SlidingItem, item) {
item.points = 25;
item.action = 1;
item.label = item.label25,
this.totalActionsArray["home"] = this.totalActions();
this.totalPointsArray["home"] = this.totalPoints();
this.storage.set('myData', [this.data, this.items, this.totalActionsArray["home"], this.totalPointsArray["home"]]);
this.storage.set('firstStart', 'false');
this.events.publish('hello', this.name);
SlidingItem.close();
}
sometimes(SlidingItem, item) {
item.points = 50;
item.action = 1;
item.label = item.label50,
this.totalActionsArray["home"] = this.totalActions();
this.totalPointsArray["home"] = this.totalPoints();
this.storage.set('myData', [this.data, this.items, this.totalActionsArray["home"], this.totalPointsArray["home"]]);
this.storage.set('firstStart', 'false');
this.events.publish('hello', this.name);
SlidingItem.close();
}
often(SlidingItem, item) {
item.points = 75;
item.action = 1;
item.label = item.label75,
this.totalActionsArray["home"] = this.totalActions();
this.totalPointsArray["home"] = this.totalPoints();
this.storage.set('myData', [this.data, this.items, this.totalActionsArray["home"], this.totalPointsArray["home"]]);
this.storage.set('firstStart', 'false');
this.events.publish('hello', this.name);
SlidingItem.close();;
}
always(SlidingItem, item) {
item.points = 100;
item.action = 1;
item.label = item.label100,
this.totalActionsArray["home"] = this.totalActions();
this.totalPointsArray["home"] = this.totalPoints();
this.storage.set('myData', [this.data, this.items, this.totalActionsArray["home"], this.totalPointsArray["home"]]);
this.storage.set('firstStart', 'false');
this.events.publish('hello', this.name);
SlidingItem.close();
}
plural() {
var total = this.totalActions()
if (total > 1) {
return "actions"
}
else {
return "action"
}
}
totalPoints() {
var total = 0;
for (var i = 0; i < this.data.length; i++) {
var data = this.data[i];
total += data.points;
}
if (total > 1) {
return Math.floor(total / this.data.length); // + " points";
}
else {
return Math.floor(total / this.data.length); // + " point";
}
}
totalActions() {
;
var total = 0;
for (var i = 0; i < this.data.length; i++) {
var data = this.data[i];
total += data.action;
}
if (total > 1) {
return total; // + " actions";
}
else {
return total; // + " action";
}
};
}