diff --git a/android/app/build.gradle b/android/app/build.gradle index ea07413..de67e0c 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -2,6 +2,7 @@ plugins { id "com.android.application" id "kotlin-android" id "dev.flutter.flutter-gradle-plugin" + id 'com.google.gms.google-services' } def localProperties = new Properties() @@ -64,4 +65,7 @@ flutter { source '../..' } -dependencies {} +dependencies { + implementation platform('com.google.firebase:firebase-bom:32.3.1') + implementation 'com.google.firebase:firebase-analytics-ktx' +} diff --git a/lib/database/FireDb.dart b/lib/database/FireDb.dart new file mode 100644 index 0000000..a0775a8 --- /dev/null +++ b/lib/database/FireDb.dart @@ -0,0 +1,167 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:flutter/material.dart'; +import 'package:progetto_m335_flutter/model/promemoria.dart'; +import 'package:progetto_m335_flutter/model/note.dart'; + +class FireDb { + + Future createPromemoria(Promemoria promemoria) async { + final docPromemoria = + FirebaseFirestore.instance.collection('promemoria').doc(); + + final json = { + 'id': docPromemoria.id, + 'title': promemoria.getTitle(), + 'creationDate': promemoria.getCreationDate(), + 'lastModificationDate': promemoria.getLastModificationDate(), + 'expirationDate': promemoria.getExpirationDate(), + 'arrayPromemoria': promemoria.getArrayPromemoria(), + 'description': promemoria.getDescription(), + 'priority': promemoria.getPriority(), + 'color': promemoria.getColor(), + }; + + await docPromemoria.set(json); + } + + Future createAllPromemoria(List promemorias) async { + for (var promemoria in promemorias) { + await createPromemoria(promemoria); + } + } + + Future createNote(Note note) async { + final docNote = FirebaseFirestore.instance.collection('note').doc(); + + final json = { + 'id': docNote.id, + 'title': note.getTitle(), + 'creationDate': note.getCreationDate(), + 'lastModificationDate': note.getLastModificationDate(), + 'arrayPromemoria': note.getArrayPromemoria(), + 'description': note.getDescription(), + }; + + await docNote.set(json); + } + + Future createAllNotes(List notes) async { + for (var note in notes) { + await createNote(note); + } + } + + Future> readAllPromemoria() async { + var promemorias = + await FirebaseFirestore.instance.collection('promemoria').get(); + + List promemoriaList = []; + + for (var promemoria in promemorias.docs) { + promemoriaList.add(Promemoria.fromJson(promemoria.data())); + } + + return promemoriaList; + } + + Future> readAllNotes() async { + var notes = await FirebaseFirestore.instance.collection('note').get(); + + List noteList = []; + + for (var note in notes.docs) { + noteList.add(Note.fromJson(note.data())); + } + + return noteList; + } + + Future readNoteById(String id) async { + var docNote = await FirebaseFirestore.instance.collection('note').doc(id); + final snapshot = await docNote.get(); + + if (snapshot.exists) { + return Note.fromJson(snapshot.data()!); + } else { + return null; + } + } + + Future readPromemoriaById(String id) async { + final docPromemoria = + await FirebaseFirestore.instance.collection('promemoria').doc(id); + final snapshot = await docPromemoria.get(); + + if (snapshot.exists) { + return Promemoria.fromJson(snapshot.data()!); + } else { + return null; + } + } + + /* + + Future updateNote(Note note) async { + final docNote = FirebaseFirestore.instance.collection('note').doc(note.getId()); + + final json = { + 'id': note.getId(), + 'title': note.getTitle(), + 'creationDate': note.getCreationDate(), + 'lastModificationDate': note.getLastModificationDate(), + 'arrayPromemoria': note.getArrayPromemoria(), + 'description': note.getDescription(), + }; + + await docNote.update(json); + } + + Future updatePromemoria(Promemoria promemoria) async { + final docPromemoria = FirebaseFirestore.instance.collection('promemoria').doc(promemoria.getId()); + + final json = { + 'id': docPromemoria.id, + 'title': promemoria.getTitle(), + 'creationDate': promemoria.getCreationDate(), + 'lastModificationDate': promemoria.getLastModificationDate(), + 'expirationDate': promemoria.getExpirationDate(), + 'arrayPromemoria': promemoria.getArrayPromemoria(), + 'description': promemoria.getDescription(), + 'priority': promemoria.getPriority(), + 'color': promemoria.getColor(), + }; + + await docPromemoria.update(json); + } + + */ + + Future deleteNoteById(String id) async { + final docNote = FirebaseFirestore.instance.collection('note').doc(id); + + await docNote.delete(); + } + + Future deletePromemoriaById(String id) async { + final docPromemoria = FirebaseFirestore.instance.collection('promemoria').doc(id); + + await docPromemoria.delete(); + } + + Future deleteAllNotes() async { + var notes = await FirebaseFirestore.instance.collection('note').get(); + + for (var note in notes.docs) { + await deleteNoteById(note.id); + } + } + + Future deleteAllPromemoria() async { + var promemorias = await FirebaseFirestore.instance.collection('promemoria').get(); + + for (var promemoria in promemorias.docs) { + await deletePromemoriaById(promemoria.id); + } + } + +} diff --git a/lib/database/database.dart b/lib/database/database.dart index 8d9aea3..84d67ed 100644 --- a/lib/database/database.dart +++ b/lib/database/database.dart @@ -22,55 +22,63 @@ class NoteDatabase { } Future _initDB(String filePath) async { - // On Android, it is typically data/data//databases. - // On iOS and MacOS, it is the Documents directory. final databasePath = await getDatabasesPath(); - // Directory databasePath = await getApplicationDocumentsDirectory(); final path = join(databasePath, filePath); return await openDatabase(path, version: 1, onCreate: _createDB); } Future _createDB(Database database, int version) async { - //check if the database is created - if (database.query(noteTable) != null) { + if (await isDatabaseExists()) { print("Database already created"); + deleteDatabase(); + print("Database deleted"); + } - }else{ - print("demo data inserting"); - await database.execute('''CREATE TABLE promemoria ( + 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 ); '''); - await database.execute('''CREATE TABLE note ( + 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 ); '''); - print("database created"); - } - - + print("database created"); await fillDemoData(database, version); } Future fillDemoData(Database database, int version) async { - - print("boh speriamo funzioni"); - // Add some fake accounts + var nota = Note(); + nota.setTitle("Nota 1"); + nota.setCreationDate("2023-09-56"); + nota.setLastModificationDate("2023-09-56"); + nota.setArrayPromemoria("1,2,3,4,5"); + nota.setDescription("Questo è un esempio di nota 1."); + + await createNote(database, nota); + + print(await readNote(1)); + + nota.setDescription("ciao"); + await updateNote (nota); + + print(await readNote (1)); // Add fake categories await database.execute(''' @@ -78,6 +86,7 @@ class NoteDatabase { title, creationDate, lastModificationDate, + arrayPromemoria, description ) VALUES ( 'Nota 2', @@ -87,12 +96,12 @@ class NoteDatabase { ) '''); - await database.execute(''' INSERT INTO note ( title, creationDate, lastModificationDate, + arrayPromemoria, description ) VALUES ( 'Nota 2', @@ -109,6 +118,7 @@ class NoteDatabase { creationDate, lastModificationDate, expirationDate, + arrayPromemoria, description, priority, color @@ -130,6 +140,7 @@ class NoteDatabase { creationDate, lastModificationDate, expirationDate, + arrayPromemoria, description, priority, color @@ -159,6 +170,27 @@ class NoteDatabase { } } + Future close() async { + final database = await instance.database; + database.close(); + } + + Future deleteDatabase() async { + final databasePath = await getDatabasesPath(); + final path = join(databasePath, 'note.db'); + databaseFactory.deleteDatabase(path); + } + + Future isDatabaseExists() async { + final databasePath = await getDatabasesPath(); + final path = join(databasePath, 'note.db'); + // Apre il database in modalità sola lettura. + Database database = await openDatabase(path, readOnly: true); + + // Restituisce true se il database è stato aperto correttamente, altrimenti false. + return database.isOpen; + } + Future> selectAllPromemoria() async { final db = await database; @@ -167,15 +199,61 @@ class NoteDatabase { return maps; } - Future close() async { - final database = await instance.database; - database.close(); + Future> selectAllNotes() async { + final db = await database; + + final List> maps = await db.query(noteTable); + + return maps; } - // WARNING: FOR DEV/TEST PURPOSES ONLY!! - Future deleteDatabase() async { - final databasePath = await getDatabasesPath(); - final path = join(databasePath, 'note.db'); - databaseFactory.deleteDatabase(path); + Future createNote(Database database, Note note) async { + await database.insert( + 'note', + note.toMap(), + conflictAlgorithm: ConflictAlgorithm.replace, + ); + + print('note $note.title inserted'); + } + + Future createPromemoria( + Database database, Promemoria promemoria) async { + await database.insert( + 'promemoria', + promemoria.toMap(), + conflictAlgorithm: ConflictAlgorithm.replace, + ); + + print('promemoria $promemoria.title inserted'); + } + + Future> readPromemoria(int id) async { + final db = await database; + + final results = await db.query('SELECT * FROM note where id=$id'); + return results.first; + } + + Future> readNote(int id) async { + final db = await database; + + final results = await db.query('SELECT * FROM promemoria where id=$id'); + + return results.first; + } + + Future updatePromemoria(Promemoria promemoria) async { + final db = await database; + + await db.update('promemoria', promemoria.toMap(), + where: 'id = ?', whereArgs: [promemoria.getId()]); + } + + Future updateNote(Note note) async { + final db = await database; + + await db.update('note', note.toMap(), + where: 'id = ?', whereArgs: [note.getId()]); } } diff --git a/lib/main.dart b/lib/main.dart index cbaaa02..f743340 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,10 @@ +import 'package:firebase_core/firebase_core.dart'; import 'package:flutter/material.dart'; import 'myApp.dart'; -void main() { +Future main() async { + WidgetsFlutterBinding.ensureInitialized(); + await Firebase.initializeApp(); runApp(MyApp()); + print("App started"); } diff --git a/lib/model/note.dart b/lib/model/note.dart index d542561..f6c8b12 100644 --- a/lib/model/note.dart +++ b/lib/model/note.dart @@ -2,11 +2,87 @@ import 'base_entity.dart'; const String noteTable = 'note'; -class Note extends BaseEntity { - static String id = BaseEntity.getId; - static String title = BaseEntity.getTitle; - static String creationDate = BaseEntity.getCreationDate; - static String lastModificationDate = BaseEntity.getLastEditDate; - static String arrayPromemoria = ''; - static String description = ''; +class Note{ + String id; + String title; + String creationDate; + String lastModificationDate; + String arrayPromemoria; + String description; + + Map toMap() { + return { + 'id': id, + 'title': title, + 'creationDate': creationDate, + 'lastModificationDate': lastModificationDate, + 'arrayPromemoria': arrayPromemoria, + 'description': description + }; + } + + Note( + this.id, + this.title, + this.creationDate, + this.lastModificationDate, + this.arrayPromemoria, + this.description, + ); + + String getId() { + return id; + } + + void setId(String id1) { + id = id1; + } + + String getTitle() { + return title; + } + + void setTitle(String title1) { + title = title1; + } + + String getCreationDate() { + return creationDate; + } + + void setCreationDate(String creationDate1) { + creationDate = creationDate1; + } + + String getLastModificationDate() { + return lastModificationDate; + } + + void setLastModificationDate(String lastModificationDate1) { + lastModificationDate = lastModificationDate1; + } + + String getArrayPromemoria() { + return arrayPromemoria; + } + + void setArrayPromemoria(String arrayPromemoria1) { + arrayPromemoria = arrayPromemoria1; + } + + String getDescription() { + return description; + } + + void setDescription(String description1) { + description = description1; + } + + static Note fromJson(Map data) => Note( + data['id'], + data['title'], + data['creationDate'], + data['lastModificationDate'], + data['arrayPromemoria'], + data['description']); } diff --git a/lib/model/promemoria.dart b/lib/model/promemoria.dart index c49590e..40a5f2f 100644 --- a/lib/model/promemoria.dart +++ b/lib/model/promemoria.dart @@ -4,16 +4,138 @@ import 'identifiers/enum/priority.dart'; const String promemoriaTable = 'promemoria'; -class Promemoria extends BaseEntity { - static String expirationDate = ''; - static String arrayPromemoria = ''; - String description = ''; - static Priority priority = Priority.none; +class Promemoria { + String id = ''; + String title; + String creationDate; + String lastModificationDate; + String expirationDate; + String arrayPromemoria; + String description; + String priority; + String color; - static Color color = Color.none; + Promemoria( + this.id, + this.title, + this.creationDate, + this.lastModificationDate, + this.expirationDate, + this.arrayPromemoria, + this.description, + this.priority, + this.color); - Promemoria(String description){ - this.description = description; + Promemoria.newConstructor( + this.title, + this.creationDate, + this.lastModificationDate, + this.expirationDate, + this.arrayPromemoria, + this.description, + this.priority, + this.color); + + Map toMap() { + return { + 'id': id, + 'title': title, + 'creationDate': creationDate, + 'lastModificationDate': lastModificationDate, + 'expirationDate': expirationDate, + 'arrayPromemoria': arrayPromemoria, + 'description': description, + 'priority': priority, + 'color': color, + }; } + String getId() { + return id; + } + + void setId(String id1) { + id = id1; + } + + String getTitle() { + return title; + } + + void setTitle(String title1) { + title = title1; + } + + String getCreationDate() { + return creationDate; + } + + void setCreationDate(String creationDate1) { + creationDate = creationDate1; + } + + String getLastModificationDate() { + return lastModificationDate; + } + + void setLastModificationDate(String lastModificationDate1) { + lastModificationDate = lastModificationDate1; + } + + String getExpirationDate() { + return expirationDate; + } + + void setExpirationDate(String expirationDate1) { + expirationDate = expirationDate1; + } + + String getArrayPromemoria() { + return arrayPromemoria; + } + + void setArrayPromemoria(String arrayPromemoria1) { + arrayPromemoria = arrayPromemoria1; + } + + String getDescription() { + return description; + } + + void setDescription(String description1) { + description = description1; + } + + String getPriority() { + return priority; + } + + void setPriority(Priority priority1) { + priority = priority1.toString(); + } + + String getColor() { + return color; + } + + void setColor(Color color1) { + color = color1.toString(); + } + + static Promemoria fromJson(Map data) { + Promemoria promemoria = Promemoria( + data['id'], + data['title'], + data['creationDate'], + data['lastModificationDate'], + data['expirationDate'], + data['arrayPromemoria'], + data['description'], + data['priority'], + data['color']); + + print(promemoria.getId().toString()); + + return promemoria; + } } diff --git a/lib/myApp.dart b/lib/myApp.dart index 49d3545..311f7df 100644 --- a/lib/myApp.dart +++ b/lib/myApp.dart @@ -4,6 +4,7 @@ import 'navigation.dart'; class MyApp extends StatelessWidget { const MyApp({ Key? key }) : super(key: key); + @override Widget build(BuildContext context) { return MaterialApp( diff --git a/pubspec.yaml b/pubspec.yaml index f735786..4c778b5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -39,6 +39,7 @@ dependencies: sqflite: ^2.3.0 path: ^1.8.3 sqflite_common_ffi: ^2.3.0+2 + cloud_firestore: ^4.9.2 dev_dependencies: flutter_test: