Skip to content

Commit 9ca519d

Browse files
caching into SQLite
1 parent 6abd7e9 commit 9ca519d

File tree

5 files changed

+39
-119
lines changed

5 files changed

+39
-119
lines changed

lib/backend/database.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,30 @@ import 'package:fetchingapp/backend/sql_queries.dart';
22
import 'package:path/path.dart';
33
import 'package:sqflite/sqflite.dart';
44

5-
Future<Database> cacheDatabase() async {
5+
Future<Database> initializeDB() async {
66
return openDatabase(join(await getDatabasesPath(), 'cache_database.db'),
77
onCreate: (db, version) {
88
return db.execute(Queries().createCacheTable);
99
}, version: 1);
1010
}
1111

1212
Future insertSQLite({required Map<String, dynamic> item}) async {
13-
final db = await cacheDatabase();
13+
final db = await initializeDB();
1414
db.insert('cache', item);
1515
}
1616

1717
Future updateSQLite({required Map<String, dynamic> item}) async {
18-
final db = await cacheDatabase();
18+
final db = await initializeDB();
1919
db.update('cache', item, where: 'doc_id = ?', whereArgs: [item['doc_id']]);
2020
}
2121

2222
Future deleteSQLite({required Map<String, dynamic> item}) async {
23-
final db = await cacheDatabase();
23+
final db = await initializeDB();
2424
db.delete('cache', where: 'doc_id = ?', whereArgs: [item['doc_id']]);
2525
}
2626

2727
Future insertBatchSQLite({required List<Map<String, dynamic>> list}) async {
28-
final db = await cacheDatabase();
28+
final db = await initializeDB();
2929
final batch = db.batch();
3030
for (var item in list) {
3131
batch.insert('cache', item, conflictAlgorithm: ConflictAlgorithm.replace);
@@ -34,7 +34,7 @@ Future insertBatchSQLite({required List<Map<String, dynamic>> list}) async {
3434
}
3535

3636
Future cleanDB() async {
37-
final db = await cacheDatabase();
37+
final db = await initializeDB();
3838
db.execute(Queries().dropCacheTable);
3939
db.execute(Queries().createCacheTable);
4040
}
Lines changed: 19 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import 'package:cloud_firestore/cloud_firestore.dart';
22
import 'package:fetchingapp/backend/database.dart';
33
import 'package:flutter/foundation.dart';
4-
import 'dart:convert';
5-
import 'package:cloud_firestore/cloud_firestore.dart';
6-
import 'package:fetchingapp/backend/database.dart';
7-
import 'package:http/http.dart' as http;
8-
import 'package:flutter/foundation.dart' show kIsWeb;
94

105
class FirestoreChanges extends ChangeNotifier {
116
List<Map<String, dynamic>> list = [];
@@ -45,94 +40,28 @@ class FirestoreChanges extends ChangeNotifier {
4540
notifyListeners();
4641
});
4742
}
48-
49-
Future<List<Map>> readSQLiteCache() async {
50-
final db = await cacheDatabase();
51-
var cache = db.query('cache');
52-
var value = cache.then((value) {
43+
44+
Future<List<Map>> readSQLiteCache() async {
45+
final db = await initializeDB();
46+
var cache = db.query('cache');
47+
var value = cache.then((value) {
48+
print(value.length);
49+
return value;
50+
});
5351
return value;
54-
});
55-
return value;
52+
}
5653
}
5754

58-
55+
removeDocFromFirebase(String doc) async {
56+
await FirebaseFirestore.instance
57+
.collection('flutter-caching')
58+
.doc(doc)
59+
.delete();
5960
}
6061

61-
// class Firestore {
62-
// Future getData() async {
63-
// return readCache().then((value) async {
64-
// return value;
65-
// });
66-
// }
67-
// }
68-
69-
class ApiProvider {
70-
// final url = 'https://jsonplaceholder.typicode.com/comments';
71-
72-
// Future getData() async {
73-
// if (kIsWeb) {
74-
// return _getFromApi();
75-
// }
76-
// return _getFromDB();
77-
// }
78-
79-
// Future getDataForFirestore() async {
80-
// return readSQLiteCache().then((value) async {
81-
// if (value.isNotEmpty) {
82-
// print('READING FROM DB, NUMBER OF ENTRIES: ${value.length}');
83-
// callFirestore();
84-
// return value;
85-
// } else {
86-
// return callFirestore();
87-
// }
88-
// });
89-
// }
90-
91-
Future callFirestore() async {
92-
cleanDB();
93-
print('fetching from network');
94-
var data = await FirebaseFirestore.instance
95-
.collection('flutter-caching')
96-
.orderBy('name')
97-
.get();
98-
var array = data.docs.map((e) {
99-
return {'name': e['name']};
100-
}).toList();
101-
// await insertSQLite(list: array);
102-
return array;
103-
}
104-
105-
// Future _getFromDB() async {
106-
// return readJsonCache().then((value) {
107-
// if (value.isNotEmpty) {
108-
// print('reading from db');
109-
// return value;
110-
// } else {
111-
// print('fetching from network');
112-
// return _getFromApi();
113-
// }
114-
// });
115-
// }
116-
117-
// Future _getFromApi() async {
118-
// try {
119-
// final req = await http.get(Uri.parse(url));
120-
// if (req.statusCode == 200) {
121-
// final body = req.body;
122-
// final jsonDecoded = json.decode(body);
123-
124-
// // cache data
125-
// if (!kIsWeb) {
126-
// await cleanDB();
127-
// await addBatchOfEntries(entries: jsonDecoded);
128-
// }
129-
// return json.decode(body);
130-
// } else {
131-
// return json.decode(req.body);
132-
// }
133-
// } catch (e) {
134-
// // ignore
135-
// }
136-
// }
137-
// }
62+
editDocInFirebase(String value, String doc) async {
63+
await FirebaseFirestore.instance
64+
.collection('flutter-caching')
65+
.doc(doc)
66+
.update({'name': value});
13867
}

lib/screens/firestore_collection.dart

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'package:cloud_firestore/cloud_firestore.dart';
2-
import 'package:fetchingapp/backend/database.dart';
32
import 'package:fetchingapp/backend/firestore_listener.dart';
43
import 'package:flutter/material.dart';
54
import 'package:provider/provider.dart';
@@ -32,20 +31,6 @@ class _FirestoreCollectionCachingState
3231
}
3332
}
3433

35-
removeDocFromFirebase(String doc) async {
36-
await FirebaseFirestore.instance
37-
.collection('flutter-caching')
38-
.doc(doc)
39-
.delete();
40-
}
41-
42-
editDocInFirebase(String value, String doc) async {
43-
await FirebaseFirestore.instance
44-
.collection('flutter-caching')
45-
.doc(doc)
46-
.update({'name': value});
47-
}
48-
4934
class DocList extends StatefulWidget {
5035
const DocList({Key? key}) : super(key: key);
5136

@@ -97,7 +82,7 @@ class _DocListState extends State<DocList> {
9782
Widget inputfield() {
9883
final textController = TextEditingController();
9984

100-
void addDocToFirebase() {
85+
addDocToFirebase() {
10186
// if (textController.text == '') {
10287
// return;
10388
// }
@@ -126,6 +111,19 @@ Widget inputfield() {
126111
)));
127112
}
128113

114+
removeDocFromFirebase(String doc) async {
115+
await FirebaseFirestore.instance
116+
.collection('flutter-caching')
117+
.doc(doc)
118+
.delete();
119+
}
120+
121+
editDocInFirebase(String value, String doc) async {
122+
await FirebaseFirestore.instance
123+
.collection('flutter-caching')
124+
.doc(doc)
125+
.update({'name': value});
126+
}
129127

130128

131129
// Widget list({required BuildContext context}) {

lib/screens/home.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import 'package:fetchingapp/backend/database.dart';
21
import 'package:fetchingapp/backend/google_authentication.dart';
32
import 'package:fetchingapp/screens/firestore_collection.dart';
43
import 'package:fetchingapp/screens/login.dart';
@@ -34,11 +33,6 @@ class HomePage extends StatelessWidget {
3433
'Firestore Collection',
3534
style: TextStyle(fontSize: 25),
3635
)),
37-
// ElevatedButton(
38-
// onPressed: () {
39-
// readCache();
40-
// },
41-
// child: const Text('Read Cache'))
4236
],
4337
),
4438
);

lib/screens/login.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class LoginPage extends StatefulWidget {
1212
class _LoginPageState extends State<LoginPage> {
1313
@override
1414
Widget build(BuildContext context) {
15-
// FirebaseService().signOutFromGoogle();
1615
return Scaffold(
1716
appBar: AppBar(
1817
automaticallyImplyLeading: false,

0 commit comments

Comments
 (0)