collegato lightsql e firebase
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
import 'package:progetto_m335_flutter/database/FireDb.dart';
|
||||
|
||||
import '../model/note.dart';
|
||||
import '../model/promemoria.dart';
|
||||
import 'database.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
|
||||
class Controller {
|
||||
NoteDatabase database = NoteDatabase.instance;
|
||||
FireDb fireDb = FireDb();
|
||||
|
||||
|
||||
Future<List<Note>> getAllNote() async {
|
||||
final db = await database.database;
|
||||
@@ -78,4 +83,40 @@ class Controller {
|
||||
)
|
||||
''');
|
||||
}
|
||||
|
||||
void deleteAll() async {
|
||||
final db = await database.database;
|
||||
await db.execute('''
|
||||
DELETE FROM promemoria
|
||||
''');
|
||||
|
||||
await db.execute('''
|
||||
DELETE FROM note
|
||||
''');
|
||||
}
|
||||
|
||||
|
||||
void getDataFromFirebase(Database database, int version) async {
|
||||
this.deleteAll();
|
||||
|
||||
var promemorias = await fireDb.readAllPromemoria();
|
||||
var notes = await fireDb.readAllNotes();
|
||||
|
||||
for (var promemoria in promemorias) {
|
||||
this.addPromemoria(promemoria);
|
||||
}
|
||||
|
||||
for (var note in notes) {
|
||||
this.addNote(note);
|
||||
}
|
||||
}
|
||||
|
||||
void syncData() async {
|
||||
var promemorias = await this.getAllPromemoria();
|
||||
var notes = await this.getAllNote();
|
||||
|
||||
await fireDb.createAllPromemoria(promemorias);
|
||||
await fireDb.createAllNotes(notes);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
import 'package:path/path.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
|
||||
import 'FireDb.dart';
|
||||
import 'package:progetto_m335_flutter/model/promemoria.dart';
|
||||
import 'controller.dart';
|
||||
|
||||
class NoteDatabase {
|
||||
static final NoteDatabase instance = NoteDatabase._init();
|
||||
static Database? _database;
|
||||
FireDb fireDb = FireDb();
|
||||
Controller controller = Controller();
|
||||
|
||||
NoteDatabase._init();
|
||||
|
||||
@@ -24,114 +27,31 @@ class NoteDatabase {
|
||||
}
|
||||
|
||||
Future _createDB(Database database, int version) async {
|
||||
|
||||
await database.execute('''CREATE TABLE promemoria (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
title TEXT NOT NULL,
|
||||
creationDate TEXT NOT NULL,
|
||||
lastModificationDate TEXT,
|
||||
expirationDate TEXT,
|
||||
arrayPromemoria TEXT,
|
||||
description TEXT,
|
||||
priority TEXT,
|
||||
color TEXT
|
||||
);
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
title TEXT NOT NULL,
|
||||
creationDate TEXT NOT NULL,
|
||||
lastModificationDate TEXT,
|
||||
expirationDate TEXT,
|
||||
arrayPromemoria TEXT,
|
||||
description TEXT,
|
||||
priority TEXT,
|
||||
color TEXT
|
||||
);
|
||||
''');
|
||||
|
||||
await database.execute('''CREATE TABLE note (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
title TEXT NOT NULL,
|
||||
creationDate TEXT NOT NULL,
|
||||
lastModificationDate TEXT,
|
||||
arrayPromemoria TEXT,
|
||||
description TEXT
|
||||
);
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
title TEXT NOT NULL,
|
||||
creationDate TEXT NOT NULL,
|
||||
lastModificationDate TEXT,
|
||||
arrayPromemoria TEXT,
|
||||
description TEXT
|
||||
);
|
||||
''');
|
||||
|
||||
print("database created");
|
||||
await fillDemoData(database, version);
|
||||
}
|
||||
|
||||
Future fillDemoData(Database database, int version) async {
|
||||
|
||||
// Add fake categories
|
||||
await database.execute('''
|
||||
INSERT INTO note (
|
||||
title,
|
||||
creationDate,
|
||||
lastModificationDate,
|
||||
arrayPromemoria,
|
||||
description
|
||||
) VALUES (
|
||||
'Nota 2',
|
||||
'2023-09-28',
|
||||
'2023-09-28',
|
||||
'1,1',
|
||||
'Questo è un esempio di nota 2.'
|
||||
)
|
||||
''');
|
||||
|
||||
await database.execute('''
|
||||
INSERT INTO note (
|
||||
title,
|
||||
creationDate,
|
||||
lastModificationDate,
|
||||
arrayPromemoria,
|
||||
description
|
||||
) VALUES (
|
||||
'Nota 2',
|
||||
'2023-09-28',
|
||||
'2023-09-28',
|
||||
'1,1',
|
||||
'Questo è un esempio di nota 2.'
|
||||
)
|
||||
''');
|
||||
|
||||
await database.execute('''
|
||||
INSERT INTO promemoria (
|
||||
title,
|
||||
creationDate,
|
||||
lastModificationDate,
|
||||
expirationDate,
|
||||
arrayPromemoria,
|
||||
description,
|
||||
priority,
|
||||
color
|
||||
) VALUES (
|
||||
'Promemoria 1',
|
||||
'2023-09-27',
|
||||
'2023-09-27',
|
||||
'2023-10-05',
|
||||
'2,1',
|
||||
'Questo è un esempio di promemoria 1.',
|
||||
'Alta',
|
||||
'Rosso'
|
||||
)
|
||||
''');
|
||||
|
||||
// Add fake budgets
|
||||
await database.execute('''
|
||||
INSERT INTO promemoria (
|
||||
title,
|
||||
creationDate,
|
||||
lastModificationDate,
|
||||
expirationDate,
|
||||
arrayPromemoria,
|
||||
description,
|
||||
priority,
|
||||
color
|
||||
) VALUES (
|
||||
'Promemoria 2',
|
||||
'2023-09-28',
|
||||
'2023-09-28',
|
||||
'2023-10-10',
|
||||
'1,1',
|
||||
'Questo è un esempio di promemoria 2.',
|
||||
'Media',
|
||||
'Verde'
|
||||
)
|
||||
''');
|
||||
print("Demo data inserted");
|
||||
controller.getDataFromFirebase(database, version);
|
||||
}
|
||||
|
||||
Future<List<Map>> selectAllPromemoria() async {
|
||||
@@ -141,7 +61,4 @@ class NoteDatabase {
|
||||
|
||||
return maps;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,18 @@
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:progetto_m335_flutter/database/controller.dart';
|
||||
import 'myApp.dart';
|
||||
import 'dart:async';
|
||||
|
||||
Timer? timer;
|
||||
|
||||
Future<void> main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await Firebase.initializeApp();
|
||||
runApp(MyApp());
|
||||
print("App started");
|
||||
Controller controller = Controller();
|
||||
timer = Timer.periodic(Duration(seconds: 60), (Timer t) => (
|
||||
controller.syncData()
|
||||
));
|
||||
}
|
||||
|
||||
@@ -58,7 +58,9 @@ class _TestState extends State<Test> {
|
||||
print(await db.query(noteTable));
|
||||
print("Data printed");
|
||||
|
||||
|
||||
print("savedata from database to firebase");
|
||||
controller.syncData();
|
||||
print("data saved");
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user