· 7 years ago · Nov 08, 2018, 06:22 AM
1import { Component } from '@angular/core';
2import { IonicPage, NavController, NavParams, Platform , AlertController} from 'ionic-angular';
3import { Storage}from '@ionic/storage';
4import { Observable } from 'rxjs/Observable';
5import {HttpClient} from '@angular/common/http';
6import {File} from '@ionic-native/file';
7import {DatabaseProvider} from '../../providers/database/database';
8import { SQLiteObject, SQLite } from '@ionic-native/sqlite';
9import { TitleCasePipe } from '@angular/common';
10import { THIS_EXPR } from '@angular/compiler/src/output/output_ast';
11
12/**
13 * Generated class for the DownloadPage page.
14 *
15 * See https://ionicframework.com/docs/components/#navigation for more info on
16 * Ionic pages and navigation.
17 */
18
19@IonicPage()
20@Component({
21
22 selector: 'page-download',
23 templateUrl: 'download.html',
24})
25export class DownloadPage {
26
27 items:any =[];
28 expenses: any = [];
29 datas =[];
30 productFound:boolean = false;
31 data = { id:"", userid:"", title:"", body:"" };
32
33
34 key:string ='items';
35 url:string = 'https://jsonplaceholder.typicode.com/posts';
36
37 public hasData : boolean = false;
38 public technologies : any;
39 public dataImported : boolean = false;
40
41
42
43
44
45 constructor(public navCtrl: NavController,private sqlite : SQLite,private _ALERT : AlertController, private _PLAT : Platform,public navParams: NavParams, private storage: Storage, public http: HttpClient, public file: File, private _DB : DatabaseProvider) {
46
47
48 let data:Observable<any> = this.http.get(this.url);
49 data.subscribe(result =>{
50
51
52 this.items = result;
53
54
55
56 console.log(this.items);
57
58
59
60 console.log(this.items.length);
61
62 for(let i in this.items)
63 {
64 this.data.id = this.items[i].id;
65 this.data.userid= this.items[i].userid;
66 this.data.title = this.items[i].title;
67 this.data.body = this.items[i].body;
68
69
70
71 }
72
73
74 })
75
76 this.sqlite.create({
77 name: 'ionicdb.db',
78 location: 'default'
79 }).then((db: SQLiteObject) => {
80 db.executeSql('CREATE TABLE IF NOT EXISTS holder(id INTEGER PRIMARY KEY, userid INT, title TEXT, body TEXT)', [])
81 .then(res =>
82 console.log('Table Create!'))
83 .catch(e =>
84 console.log('error!'));
85
86
87
88 db.executeSql('INSERT INTO holder VALUES (NULL,?,?,?)', [ '2','hai','hello'])
89 .then(res =>{
90 console.log("Data Insert Yahoo !");
91 })
92 .catch(e=>{
93 console.log("errorrrrrrr");
94 });
95
96
97
98 })
99 }
100
101
102 getData()
103 {
104 let data:Observable<any> = this.http.get(this.url);
105 data.subscribe(result =>{
106 this.items = JSON.stringify(result);
107 console.log(this.items);
108
109 })
110 }
111
112 saveData(){
113
114 //this.storage.set(this.key, JSON.stringify(this.items));
115
116 this.sqlite.create({
117 name:'ionicdb.db',
118 location:'default'
119 }).then((db: SQLiteObject) =>{
120 db.executeSql('INSERT INTO expense VALUES (?,?,?,?)',[this.items.userid, this.items.id, this.items.title, this.items.body ] )
121 .then(res => {
122 console.log('Table create !');
123
124 })
125 .catch(e=> console.log(e));
126 db.executeSql('SELECT * FROM expense ORDER BY userid DESC',[])
127 .then(res => {
128 this.expenses = [];
129
130 })
131 })
132
133 }
134
135 loadData(){
136 this.storage.get(this.key).then((val)=>{
137 if(val!=null && val!= undefined){
138 this.items = JSON.parse(val);
139 }
140 })
141 }
142
143 ionViewDidLoad(): void {
144 this._PLAT
145 .ready()
146 .then(() =>
147 {
148 setTimeout(() =>
149 {
150 this._DB
151 .dataExistsCheck('technologies')
152 .then((data) =>
153 {
154 this.loadRecords();
155 })
156 .catch((error) =>
157 {
158 console.dir(error);
159 });
160 }, 1500);
161 });
162 }
163
164 loadRecords() : void
165 {
166 this._DB
167 .retrieveAllRecords()
168 .then((data : any) =>
169 {
170 this.hasData = true;
171 this.technologies = data;
172 })
173 .catch((error : any) =>
174 {
175 console.dir(error);
176 });
177 }
178
179
180
181
182}