diff --git a/lib/database/controller.dart b/lib/database/controller.dart deleted file mode 100644 index 7e01b19..0000000 --- a/lib/database/controller.dart +++ /dev/null @@ -1,81 +0,0 @@ -import '../model/note.dart'; -import '../model/promemoria.dart'; -import 'database.dart'; - -class Controller { - NoteDatabase database = NoteDatabase.instance; - - Future> getAllNote() async { - final db = await database.database; - var notes = await db.query(noteTable); - List noteList = notes.map((e) => Note.fromJson(e)).toList(); - return noteList; - } - - Future> getAllPromemoria() async { - final db = await database.database; - var promemorias = await db.query(promemoriaTable); - List promemoriaList = - promemorias.map((e) => Promemoria.fromJson(e)).toList(); - return promemoriaList; - } - - Future getNoteById(int id) async { - final db = await database.database; - var note = await db.query(noteTable, where: 'id = ?', whereArgs: [id]); - return Note.fromJson(note.first); - } - - Future getPromemoriaById(int id) async { - final db = await database.database; - var promemoria = - await db.query(promemoriaTable, where: 'id = ?', whereArgs: [id]); - return Promemoria.fromJson(promemoria.first); - } - - //add note - void addNote(Note note) async { - final db = await database.database; - await db.execute(''' - INSERT INTO note ( - title, - creationDate, - lastModificationDate, - arrayPromemoria, - description - ) VALUES ( - '${note.title}', - '${note.creationDate}', - '${note.lastModificationDate}', - '${note.arrayPromemoria}', - '${note.description}' - ) -'''); - } - - //add Promemoria - void addPromemoria(Promemoria promemoria) async { - final db = await database.database; - await db.execute(''' - INSERT INTO promemoria ( - title, - creationDate, - lastModificationDate, - expirationDate, - arrayPromemoria, - description, - priority, - color - ) VALUES ( - '${promemoria.title}', - '${promemoria.creationDate}', - '${promemoria.lastModificationDate}', - '${promemoria.expirationDate}', - '${promemoria.arrayPromemoria}', - '${promemoria.description}', - '${promemoria.priority}', - '${promemoria.color}' - ) - '''); - } -} diff --git a/lib/database/database.dart b/lib/database/database.dart index 8597e3a..83b0d61 100644 --- a/lib/database/database.dart +++ b/lib/database/database.dart @@ -1,11 +1,13 @@ import 'package:path/path.dart'; import 'package:sqflite/sqflite.dart'; - +import '../model/note.dart'; +import 'FireDb.dart'; import 'package:progetto_m335_flutter/model/promemoria.dart'; class NoteDatabase { static final NoteDatabase instance = NoteDatabase._init(); static Database? _database; + FireDb fireDb = FireDb(); NoteDatabase._init(); @@ -24,114 +26,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"); + getDataFromFirebase(database, version); } Future> selectAllPromemoria() async { @@ -142,6 +61,108 @@ class NoteDatabase { return maps; } + Future> getAllNote() async { + var notes = await _database?.query(noteTable); + List noteList = notes!.map((e) => Note.fromJson(e)).toList(); + return noteList; + } + + Future> getAllPromemoria() async { + var promemorias = await _database?.query(promemoriaTable); + List promemoriaList = + promemorias!.map((e) => Promemoria.fromJson(e)).toList(); + return promemoriaList; + } + + Future getNoteById(int id) async { + var note = await _database?.query(noteTable, where: 'id = ?', whereArgs: [id]); + return Note.fromJson(note!.first); + } + + Future getPromemoriaById(int id) async { + var promemoria = + await _database?.query(promemoriaTable, where: 'id = ?', whereArgs: [id]); + return Promemoria.fromJson(promemoria!.first); + } + + //add note + void addNote(Note note) async { + await _database?.execute(''' + INSERT INTO note ( + title, + creationDate, + lastModificationDate, + arrayPromemoria, + description + ) VALUES ( + '${note.title}', + '${note.creationDate}', + '${note.lastModificationDate}', + '${note.arrayPromemoria}', + '${note.description}' + ) +'''); + } + + //add Promemoria + void addPromemoria(Promemoria promemoria) async { + await _database?.execute(''' + INSERT INTO promemoria ( + title, + creationDate, + lastModificationDate, + expirationDate, + arrayPromemoria, + description, + priority, + color + ) VALUES ( + '${promemoria.title}', + '${promemoria.creationDate}', + '${promemoria.lastModificationDate}', + '${promemoria.expirationDate}', + '${promemoria.arrayPromemoria}', + '${promemoria.description}', + '${promemoria.priority}', + '${promemoria.color}' + ) + '''); + } + + void deleteAll() async { + await _database?.execute(''' + DELETE FROM promemoria + '''); + + await _database?.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 getAllPromemoria(); + var notes = await getAllNote(); + + await fireDb.deleteAllPromemoria(); + await fireDb.deleteAllNotes(); + + await fireDb.createAllPromemoria(promemorias); + await fireDb.createAllNotes(notes); + } } diff --git a/lib/main.dart b/lib/main.dart index f743340..c254390 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,6 +2,7 @@ import 'package:firebase_core/firebase_core.dart'; import 'package:flutter/material.dart'; import 'myApp.dart'; + Future main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); diff --git a/lib/pages/test.dart b/lib/pages/test.dart index 6758bc8..6126e1f 100644 --- a/lib/pages/test.dart +++ b/lib/pages/test.dart @@ -1,5 +1,4 @@ import 'package:flutter/material.dart'; -import 'package:progetto_m335_flutter/database/controller.dart'; import 'package:progetto_m335_flutter/database/database.dart'; import 'package:progetto_m335_flutter/model/note.dart'; import 'package:progetto_m335_flutter/model/promemoria.dart'; @@ -15,7 +14,6 @@ class Test extends StatefulWidget { class _TestState extends State { NoteDatabase noteDatabase = NoteDatabase.instance; - Controller controller = Controller(); Future _pressed() async { print("Inserting demo data"); @@ -44,21 +42,23 @@ class _TestState extends State { "alta", "rosso"); - controller.addNote(nota); - controller.addPromemoria(promemoria); + noteDatabase.addNote(nota); + noteDatabase.addPromemoria(promemoria); final db = await noteDatabase.database; print("Printing data"); - print((await controller.getAllPromemoria()).first); - print((await controller.getAllNote()).first); + print((await noteDatabase.getAllPromemoria()).first); + print((await noteDatabase.getAllNote()).first); print("promemoria"); print(await db.query(promemoriaTable)); print("note"); print(await db.query(noteTable)); print("Data printed"); - + print("savedata from database to firebase"); + noteDatabase.syncData(); + print("data saved"); }