From 58e013a709ae0dc93ffc7a56e171c1ca1f1f8cf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=20Ku=CC=88ng?= Date: Wed, 27 Sep 2023 13:43:04 +0200 Subject: [PATCH 01/10] database --- lib/database/database.dart | 36 ++++++++++++++++++++++++++++++++++++ lib/database/promemoria.dart | 9 ++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 lib/database/database.dart diff --git a/lib/database/database.dart b/lib/database/database.dart new file mode 100644 index 0000000..4a68ed8 --- /dev/null +++ b/lib/database/database.dart @@ -0,0 +1,36 @@ + +import 'package:progetto_m335_flutter/model/note.dart'; + +class Database{ + static final Database _instance = Database._init(); + static Database? _database; + + Database._init(); + + Future get database async { + if (_database != null) return _database!; + _database = await _initDB('database.db'); + return _database!; + } + + Future _createDB(Database database) async{ + const integerPrimaryKeyAutoincrement = 'INTEGER PRIMARY KEY AUTOINCREMENT'; + const textNotNull = 'TEXT NOT NULL'; + const integerNotNull = 'INTEGER NOT NULL'; + const integer = 'INTEGER'; + const real = 'REAL'; + const text = 'TEXT'; + + + await database.execute(''' + CREATE TABLE $Note ( + ${Note.id} $integerPrimaryKeyAutoincrement, + + ) + '''); + } + + execute(String s) { + + } +} \ No newline at end of file diff --git a/lib/database/promemoria.dart b/lib/database/promemoria.dart index baa8ddd..66c8d27 100644 --- a/lib/database/promemoria.dart +++ b/lib/database/promemoria.dart @@ -1,11 +1,18 @@ +import 'dart:ui'; + import 'base_entity.dart'; import 'identifiers/enum/priority.dart'; +import 'identifiers/enum/color.dart'; class Note extends BaseEntity{ static String id = BaseEntity.getId; static String title = BaseEntity.getTitle; static String creationDate = BaseEntity.getCreationDate; + static String expirationDate = 'expirationDate'; - Priority priority = Priority.low; + static String description = 'description'; + Priority priority = Priority.none; + Color color = Color.none; + } From ecf7011302f391b9facd0d8e7c46d7f23c19d294 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=20Ku=CC=88ng?= Date: Wed, 27 Sep 2023 13:50:56 +0200 Subject: [PATCH 02/10] new directory --- lib/database/database.dart | 1 + lib/{database => model}/base_entity.dart | 0 lib/{database => model}/identifiers/enum/color.dart | 0 lib/{database => model}/identifiers/enum/priority.dart | 0 lib/{database => model}/identifiers/tag.dart | 0 lib/{database => model}/note.dart | 0 lib/{database => model}/promemoria.dart | 2 +- test/widget_test.dart | 2 +- 8 files changed, 3 insertions(+), 2 deletions(-) rename lib/{database => model}/base_entity.dart (100%) rename lib/{database => model}/identifiers/enum/color.dart (100%) rename lib/{database => model}/identifiers/enum/priority.dart (100%) rename lib/{database => model}/identifiers/tag.dart (100%) rename lib/{database => model}/note.dart (100%) rename lib/{database => model}/promemoria.dart (100%) diff --git a/lib/database/database.dart b/lib/database/database.dart index 4a68ed8..10474b8 100644 --- a/lib/database/database.dart +++ b/lib/database/database.dart @@ -10,6 +10,7 @@ class Database{ Future get database async { if (_database != null) return _database!; _database = await _initDB('database.db'); + return _database!; } diff --git a/lib/database/base_entity.dart b/lib/model/base_entity.dart similarity index 100% rename from lib/database/base_entity.dart rename to lib/model/base_entity.dart diff --git a/lib/database/identifiers/enum/color.dart b/lib/model/identifiers/enum/color.dart similarity index 100% rename from lib/database/identifiers/enum/color.dart rename to lib/model/identifiers/enum/color.dart diff --git a/lib/database/identifiers/enum/priority.dart b/lib/model/identifiers/enum/priority.dart similarity index 100% rename from lib/database/identifiers/enum/priority.dart rename to lib/model/identifiers/enum/priority.dart diff --git a/lib/database/identifiers/tag.dart b/lib/model/identifiers/tag.dart similarity index 100% rename from lib/database/identifiers/tag.dart rename to lib/model/identifiers/tag.dart diff --git a/lib/database/note.dart b/lib/model/note.dart similarity index 100% rename from lib/database/note.dart rename to lib/model/note.dart diff --git a/lib/database/promemoria.dart b/lib/model/promemoria.dart similarity index 100% rename from lib/database/promemoria.dart rename to lib/model/promemoria.dart index 66c8d27..4838317 100644 --- a/lib/database/promemoria.dart +++ b/lib/model/promemoria.dart @@ -1,8 +1,8 @@ import 'dart:ui'; import 'base_entity.dart'; -import 'identifiers/enum/priority.dart'; import 'identifiers/enum/color.dart'; +import 'identifiers/enum/priority.dart'; class Note extends BaseEntity{ static String id = BaseEntity.getId; diff --git a/test/widget_test.dart b/test/widget_test.dart index 5e5d15e..bb5dffa 100644 --- a/test/widget_test.dart +++ b/test/widget_test.dart @@ -8,7 +8,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:progetto_m335_flutter/main.dart'; +import 'package:progetto_m335_flutter/myApp.dart'; void main() { testWidgets('Counter increments smoke test', (WidgetTester tester) async { From c35684c8f15bf1c43182e22fbdf9cb2d7bbaca12 Mon Sep 17 00:00:00 2001 From: Tito Arrigo Date: Wed, 27 Sep 2023 14:02:53 +0200 Subject: [PATCH 03/10] inizzializazione pagine --- lib/navigation.dart | 11 +++++++---- lib/pages/InboxView.dart | 19 +++++++++++++++++++ lib/pages/NoteDetailView.dart | 19 +++++++++++++++++++ lib/pages/NotesView.dart | 19 +++++++++++++++++++ lib/pages/TodayView.dart | 19 +++++++++++++++++++ lib/{ => pages}/testUI.dart | 0 6 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 lib/pages/InboxView.dart create mode 100644 lib/pages/NoteDetailView.dart create mode 100644 lib/pages/NotesView.dart create mode 100644 lib/pages/TodayView.dart rename lib/{ => pages}/testUI.dart (100%) diff --git a/lib/navigation.dart b/lib/navigation.dart index ac724e9..180a383 100644 --- a/lib/navigation.dart +++ b/lib/navigation.dart @@ -1,7 +1,10 @@ import 'dart:ffi'; import 'package:flutter/material.dart'; -import 'testUI.dart'; +import 'pages/testUI.dart'; +import 'pages/TodayView.dart'; +import 'pages/InboxView.dart'; +import 'pages/NotesView.dart'; class Navigation extends StatefulWidget { const Navigation({super.key}); @@ -14,9 +17,9 @@ class _NavigationState extends State { int _selectedIndex = 0; static const List _widgetOptions = [ - TestUI(), - Text("Inbox"), - Text("Notes"), + TodayView(), + InboxView(), + NotesView(), ]; void _onItemTapped(int index) { diff --git a/lib/pages/InboxView.dart b/lib/pages/InboxView.dart new file mode 100644 index 0000000..4d40bd7 --- /dev/null +++ b/lib/pages/InboxView.dart @@ -0,0 +1,19 @@ +import 'package:flutter/material.dart'; + +class InboxView extends StatefulWidget { + const InboxView({super.key}); + + @override + State createState() => _InboxViewState(); +} + +class _InboxViewState extends State { + @override + Widget build(BuildContext context) { + return const Scaffold( + body: Center( + child: Icon(Icons.inbox), + ) + ); + } +} diff --git a/lib/pages/NoteDetailView.dart b/lib/pages/NoteDetailView.dart new file mode 100644 index 0000000..a9802da --- /dev/null +++ b/lib/pages/NoteDetailView.dart @@ -0,0 +1,19 @@ +import 'package:flutter/material.dart'; + +class NoteDetailView extends StatefulWidget { + const NoteDetailView({super.key}); + + @override + State createState() => _NoteDetailViewState(); +} + +class _NoteDetailViewState extends State { + @override + Widget build(BuildContext context) { + return const Scaffold( + body: Center( + child: Text('NoteDetailView'), + ) + ); + } +} diff --git a/lib/pages/NotesView.dart b/lib/pages/NotesView.dart new file mode 100644 index 0000000..b0d7c4b --- /dev/null +++ b/lib/pages/NotesView.dart @@ -0,0 +1,19 @@ +import 'package:flutter/material.dart'; + +class NotesView extends StatefulWidget { + const NotesView({super.key}); + + @override + State createState() => _NotesViewState(); +} + +class _NotesViewState extends State { + @override + Widget build(BuildContext context) { + return const Scaffold( + body: Center( + child: Icon(Icons.note), + ) + ); + } +} diff --git a/lib/pages/TodayView.dart b/lib/pages/TodayView.dart new file mode 100644 index 0000000..cad5346 --- /dev/null +++ b/lib/pages/TodayView.dart @@ -0,0 +1,19 @@ +import 'package:flutter/material.dart'; + +class TodayView extends StatefulWidget { + const TodayView({super.key}); + + @override + State createState() => _TodayViewState(); +} + +class _TodayViewState extends State { + @override + Widget build(BuildContext context) { + return const Scaffold( + body: Center( + child: Icon(Icons.calendar_today) + ) + ); + } +} diff --git a/lib/testUI.dart b/lib/pages/testUI.dart similarity index 100% rename from lib/testUI.dart rename to lib/pages/testUI.dart From 9f26bc8595be3cacf5e8f415c2e993ba40214c4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=20Ku=CC=88ng?= Date: Wed, 27 Sep 2023 15:17:16 +0200 Subject: [PATCH 04/10] note entity --- lib/model/note.dart | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/model/note.dart b/lib/model/note.dart index ece0167..9d13582 100644 --- a/lib/model/note.dart +++ b/lib/model/note.dart @@ -1,8 +1,9 @@ import 'base_entity.dart'; -class Note extends BaseEntity{ +class Note extends BaseEntity { static String id = BaseEntity.getId; - static String Title = BaseEntity.getTitle; - static String CreationDate = BaseEntity.getCreationDate; - -} \ No newline at end of file + static String title = BaseEntity.getTitle; + static String creationDate = BaseEntity.getCreationDate; + static String lastModificationDate = "LastModificationDate"; + static String description = ""; +} From 05386ac20f434af1bc142bc8edfd8449905bea22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=20Ku=CC=88ng?= Date: Thu, 28 Sep 2023 08:47:50 +0200 Subject: [PATCH 05/10] entity --- lib/model/note.dart | 6 ++++-- lib/model/promemoria.dart | 15 ++++++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/model/note.dart b/lib/model/note.dart index 9d13582..df6bed2 100644 --- a/lib/model/note.dart +++ b/lib/model/note.dart @@ -1,9 +1,11 @@ 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 = "LastModificationDate"; - static String description = ""; + static String lastModificationDate = BaseEntity.getLastEditDate; + static String description = ''; } diff --git a/lib/model/promemoria.dart b/lib/model/promemoria.dart index 4838317..32f4ebc 100644 --- a/lib/model/promemoria.dart +++ b/lib/model/promemoria.dart @@ -4,15 +4,20 @@ import 'base_entity.dart'; import 'identifiers/enum/color.dart'; import 'identifiers/enum/priority.dart'; -class Note extends BaseEntity{ +const String promemoriaTable = 'promemoria'; + +class Promemoria extends BaseEntity{ static String id = BaseEntity.getId; static String title = BaseEntity.getTitle; static String creationDate = BaseEntity.getCreationDate; + static String lastModificationDate = BaseEntity.getLastEditDate; + static String expirationDate = ''; + static String description = ''; + + static Priority priority = Priority.none; + static Color color = Color.none; + - static String expirationDate = 'expirationDate'; - static String description = 'description'; - Priority priority = Priority.none; - Color color = Color.none; } From 782bbebce9ee8a1cea4d9ba8a64e6f3302ef1cbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=20Ku=CC=88ng?= Date: Thu, 28 Sep 2023 11:38:19 +0200 Subject: [PATCH 06/10] database init --- lib/database/database.dart | 182 +++++++++++++++++++++++++++++++++---- lib/model/promemoria.dart | 1 - lib/navigation.dart | 5 +- lib/pages/test.dart | 45 +++++++++ pubspec.yaml | 3 + 5 files changed, 215 insertions(+), 21 deletions(-) create mode 100644 lib/pages/test.dart diff --git a/lib/database/database.dart b/lib/database/database.dart index 10474b8..8d9aea3 100644 --- a/lib/database/database.dart +++ b/lib/database/database.dart @@ -1,37 +1,181 @@ +import 'package:path/path.dart'; +import 'package:sqflite/sqflite.dart'; +// Models import 'package:progetto_m335_flutter/model/note.dart'; +import 'package:progetto_m335_flutter/model/promemoria.dart'; -class Database{ - static final Database _instance = Database._init(); +class NoteDatabase { + static final NoteDatabase instance = NoteDatabase._init(); static Database? _database; - Database._init(); + // Zero args constructor needed to extend this class + NoteDatabase(); + + NoteDatabase._init(); Future get database async { if (_database != null) return _database!; - _database = await _initDB('database.db'); + _database = await _initDB('note.db'); return _database!; } - Future _createDB(Database database) async{ - const integerPrimaryKeyAutoincrement = 'INTEGER PRIMARY KEY AUTOINCREMENT'; - const textNotNull = 'TEXT NOT NULL'; - const integerNotNull = 'INTEGER NOT NULL'; - const integer = 'INTEGER'; - const real = 'REAL'; - const text = 'TEXT'; + 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) { + print("Database already created"); + + }else{ + print("demo data inserting"); + await database.execute('''CREATE TABLE promemoria ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + title TEXT NOT NULL, + creationDate TEXT NOT NULL, + lastModificationDate TEXT, + expirationDate 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, + description TEXT +); + '''); + + print("database created"); + } + + + await fillDemoData(database, version); + } + + Future fillDemoData(Database database, int version) async { + + print("boh speriamo funzioni"); + // Add some fake accounts + + + // Add fake categories + await database.execute(''' + INSERT INTO note ( + title, + creationDate, + lastModificationDate, + description + ) VALUES ( + 'Nota 2', + '2023-09-28', + '2023-09-28', + 'Questo è un esempio di nota 2.' + ) +'''); await database.execute(''' - CREATE TABLE $Note ( - ${Note.id} $integerPrimaryKeyAutoincrement, - - ) - '''); + INSERT INTO note ( + title, + creationDate, + lastModificationDate, + description + ) VALUES ( + 'Nota 2', + '2023-09-28', + '2023-09-28', + 'Questo è un esempio di nota 2.' + ) +'''); + + // Add currencies + await database.execute(''' + INSERT INTO promemoria ( + title, + creationDate, + lastModificationDate, + expirationDate, + description, + priority, + color + ) VALUES ( + 'Promemoria 1', + '2023-09-27', + '2023-09-27', + '2023-10-05', + 'Questo è un esempio di promemoria 1.', + 'Alta', + 'Rosso' + ) +'''); + + // Add fake budgets + await database.execute(''' + INSERT INTO promemoria ( + title, + creationDate, + lastModificationDate, + expirationDate, + description, + priority, + color + ) VALUES ( + 'Promemoria 2', + '2023-09-28', + '2023-09-28', + '2023-10-10', + 'Questo è un esempio di promemoria 2.', + 'Media', + 'Verde' + ) +'''); + print("Demo data inserted"); } - execute(String s) { - + Future clearDatabase() async { + try { + await _database?.transaction((txn) async { + var batch = txn.batch(); + batch.delete(noteTable); + batch.delete(promemoriaTable); + await batch.commit(); + }); + } catch (error) { + throw Exception('DbBase.cleanDatabase: $error'); + } } -} \ No newline at end of file + + Future> selectAllPromemoria() async { + final db = await database; + + final List> maps = await db.query(promemoriaTable); + + return maps; + } + + Future close() async { + final database = await instance.database; + database.close(); + } + + // WARNING: FOR DEV/TEST PURPOSES ONLY!! + Future deleteDatabase() async { + final databasePath = await getDatabasesPath(); + final path = join(databasePath, 'note.db'); + databaseFactory.deleteDatabase(path); + } +} diff --git a/lib/model/promemoria.dart b/lib/model/promemoria.dart index 32f4ebc..1d33319 100644 --- a/lib/model/promemoria.dart +++ b/lib/model/promemoria.dart @@ -18,6 +18,5 @@ class Promemoria extends BaseEntity{ static Color color = Color.none; - } diff --git a/lib/navigation.dart b/lib/navigation.dart index 180a383..e06a378 100644 --- a/lib/navigation.dart +++ b/lib/navigation.dart @@ -5,6 +5,7 @@ import 'pages/testUI.dart'; import 'pages/TodayView.dart'; import 'pages/InboxView.dart'; import 'pages/NotesView.dart'; +import 'pages/test.dart'; class Navigation extends StatefulWidget { const Navigation({super.key}); @@ -15,11 +16,12 @@ class Navigation extends StatefulWidget { class _NavigationState extends State { - int _selectedIndex = 0; + int _selectedIndex = 3; static const List _widgetOptions = [ TodayView(), InboxView(), NotesView(), + Test() ]; void _onItemTapped(int index) { @@ -41,6 +43,7 @@ class _NavigationState extends State { icon: Icon(Icons.calendar_today), label: "today"), BottomNavigationBarItem(icon: Icon(Icons.inbox), label: "Inbox"), BottomNavigationBarItem(icon: Icon(Icons.note), label: "Notes"), + BottomNavigationBarItem(icon: Icon(Icons.settings), label: "Settings") ], currentIndex: _selectedIndex, onTap: _onItemTapped, diff --git a/lib/pages/test.dart b/lib/pages/test.dart new file mode 100644 index 0000000..7cda250 --- /dev/null +++ b/lib/pages/test.dart @@ -0,0 +1,45 @@ +import 'package:flutter/material.dart'; +import 'package:progetto_m335_flutter/database/database.dart'; +import 'package:progetto_m335_flutter/model/note.dart'; + +import '../database/database.dart'; + +class Test extends StatefulWidget { + const Test({super.key}); + + @override + State createState() => _TestState(); +} + +class _TestState extends State { + NoteDatabase noteDatabase = NoteDatabase.instance; + + Future _pressed() async { + print("Inserting demo data"); + final db = await noteDatabase.database; + + + } + + Future _printdata() async { + final db = await noteDatabase.database; + + print("Printing data"); + print(await db.query(noteTable)); + print("Data printed"); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + body: Center( + child: Column( + children: [ + FloatingActionButton(onPressed: _pressed), + FloatingActionButton(onPressed: _printdata) + ], + ) + ) + ); + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 84d5e35..f735786 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -36,6 +36,9 @@ dependencies: # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2 firebase_core: ^2.16.0 + sqflite: ^2.3.0 + path: ^1.8.3 + sqflite_common_ffi: ^2.3.0+2 dev_dependencies: flutter_test: From 52d7defb75fb1bd8482e05b6728e3c353b9b526a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=20Ku=CC=88ng?= Date: Thu, 28 Sep 2023 11:47:01 +0200 Subject: [PATCH 07/10] entity upgrade --- lib/model/note.dart | 1 + lib/model/promemoria.dart | 10 +++------- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/model/note.dart b/lib/model/note.dart index df6bed2..d542561 100644 --- a/lib/model/note.dart +++ b/lib/model/note.dart @@ -7,5 +7,6 @@ class Note extends BaseEntity { static String title = BaseEntity.getTitle; static String creationDate = BaseEntity.getCreationDate; static String lastModificationDate = BaseEntity.getLastEditDate; + static String arrayPromemoria = ''; static String description = ''; } diff --git a/lib/model/promemoria.dart b/lib/model/promemoria.dart index 1d33319..5326c1d 100644 --- a/lib/model/promemoria.dart +++ b/lib/model/promemoria.dart @@ -1,22 +1,18 @@ -import 'dart:ui'; - import 'base_entity.dart'; import 'identifiers/enum/color.dart'; import 'identifiers/enum/priority.dart'; const String promemoriaTable = 'promemoria'; -class Promemoria extends BaseEntity{ +class Promemoria extends BaseEntity { static String id = BaseEntity.getId; static String title = BaseEntity.getTitle; static String creationDate = BaseEntity.getCreationDate; static String lastModificationDate = BaseEntity.getLastEditDate; static String expirationDate = ''; + static String arrayPromemoria = ''; static String description = ''; - static Priority priority = Priority.none; + static Color color = Color.none; - - } - From 64b4f64f8c358dc8921e4afa503da12ed4930652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=20Ku=CC=88ng?= Date: Thu, 28 Sep 2023 13:38:09 +0200 Subject: [PATCH 08/10] work in progress database --- lib/database/database.dart | 145 ++++++++++++++++++++++++++++++------- lib/main.dart | 1 + lib/myApp.dart | 1 + 3 files changed, 121 insertions(+), 26 deletions(-) diff --git a/lib/database/database.dart b/lib/database/database.dart index 8d9aea3..098ea74 100644 --- a/lib/database/database.dart +++ b/lib/database/database.dart @@ -22,62 +22,55 @@ 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 - - // Add fake categories await database.execute(''' INSERT INTO note ( title, creationDate, lastModificationDate, + arrayPromemoria, description ) VALUES ( 'Nota 2', @@ -93,6 +86,7 @@ class NoteDatabase { title, creationDate, lastModificationDate, + arrayPromemoria, description ) VALUES ( 'Nota 2', @@ -109,6 +103,7 @@ class NoteDatabase { creationDate, lastModificationDate, expirationDate, + arrayPromemoria, description, priority, color @@ -130,6 +125,7 @@ class NoteDatabase { creationDate, lastModificationDate, expirationDate, + arrayPromemoria, description, priority, color @@ -159,6 +155,50 @@ 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 createNote(Database database, Note note) async { + await database.execute(''' + INSERT INTO note ( + title, + creationDate, + lastModificationDate, + arrayPromemoria, + description, + + ) VALUES ( + '$note.title}', + '$note.creationDate', + '$note.lastModificationDate', + '$note.arrayPromemoria.toString()', + '$note.description', + ) + '''); + + print('note $note.title inserted'); + } + Future> selectAllPromemoria() async { final db = await database; @@ -167,15 +207,68 @@ 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 createPromemoria(Database database, + 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.toString()', + '$promemoria.description', + '$promemoria.priority', + '$promemoria.color' + ) + '''); + + 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, where: 'id = ?', + // Pass the Dog's id as a whereArg to prevent SQL injection. +// whereArgs: [dog.id],) +//} + + } + diff --git a/lib/main.dart b/lib/main.dart index cbaaa02..9fac54f 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -3,4 +3,5 @@ import 'myApp.dart'; void main() { runApp(MyApp()); + print("App started"); } diff --git a/lib/myApp.dart b/lib/myApp.dart index 2f9d223..fd337d4 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( From a58721a6f4bd8abf626f3790743429da3d2f21b9 Mon Sep 17 00:00:00 2001 From: grata Date: Thu, 28 Sep 2023 13:57:21 +0200 Subject: [PATCH 09/10] crud --- lib/database/database.dart | 103 ++++++++++++++++--------------------- lib/model/note.dart | 59 +++++++++++++++++++++ lib/model/promemoria.dart | 81 ++++++++++++++++++++++++++++- 3 files changed, 183 insertions(+), 60 deletions(-) diff --git a/lib/database/database.dart b/lib/database/database.dart index 098ea74..84d67ed 100644 --- a/lib/database/database.dart +++ b/lib/database/database.dart @@ -62,8 +62,24 @@ class NoteDatabase { await fillDemoData(database, version); } - Future fillDemoData(Database database, int version) async { + + 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(''' INSERT INTO note ( @@ -80,7 +96,6 @@ class NoteDatabase { ) '''); - await database.execute(''' INSERT INTO note ( title, @@ -160,7 +175,6 @@ class NoteDatabase { database.close(); } - Future deleteDatabase() async { final databasePath = await getDatabasesPath(); final path = join(databasePath, 'note.db'); @@ -177,28 +191,6 @@ class NoteDatabase { return database.isOpen; } - - Future createNote(Database database, Note note) async { - await database.execute(''' - INSERT INTO note ( - title, - creationDate, - lastModificationDate, - arrayPromemoria, - description, - - ) VALUES ( - '$note.title}', - '$note.creationDate', - '$note.lastModificationDate', - '$note.arrayPromemoria.toString()', - '$note.description', - ) - '''); - - print('note $note.title inserted'); - } - Future> selectAllPromemoria() async { final db = await database; @@ -207,7 +199,6 @@ class NoteDatabase { return maps; } - Future> selectAllNotes() async { final db = await database; @@ -216,35 +207,27 @@ class NoteDatabase { return maps; } + Future createNote(Database database, Note note) async { + await database.insert( + 'note', + note.toMap(), + conflictAlgorithm: ConflictAlgorithm.replace, + ); - Future createPromemoria(Database database, - 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.toString()', - '$promemoria.description', - '$promemoria.priority', - '$promemoria.color' - ) - '''); + 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; @@ -252,23 +235,25 @@ class NoteDatabase { return results.first; } - Future> readNote(int id) async { final db = await database; - final results= await db.query('SELECT * FROM promemoria where id=$id'); + final results = await db.query('SELECT * FROM promemoria where id=$id'); return results.first; } -// Future updatePromemoria(Promemoria promemoria) async { - // final db = await database; + Future updatePromemoria(Promemoria promemoria) async { + final db = await database; - //await db.update('promemoria', promemoria, where: 'id = ?', - // Pass the Dog's id as a whereArg to prevent SQL injection. -// whereArgs: [dog.id],) -//} + 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/model/note.dart b/lib/model/note.dart index d542561..7172796 100644 --- a/lib/model/note.dart +++ b/lib/model/note.dart @@ -9,4 +9,63 @@ class Note extends BaseEntity { static String lastModificationDate = BaseEntity.getLastEditDate; static String arrayPromemoria = ''; static String description = ''; + + Map toMap() { + return { + 'id': id, + 'title': title, + 'creationDate': creationDate, + 'lastModificationDate': lastModificationDate, + 'arrayPromemoria': arrayPromemoria, + 'description': description + }; + } + + String getId() { + return id; + } + + void setId(String id) { + id = id; + } + + String getTitle() { + return title; + } + + void setTitle(String title) { + title = title; + } + + String getCreationDate() { + return creationDate; + } + + void setCreationDate(String creationDate) { + creationDate = creationDate; + } + + String getLastModificationDate() { + return lastModificationDate; + } + + void setLastModificationDate(String lastModificationDate) { + lastModificationDate = lastModificationDate; + } + + String getArrayPromemoria() { + return arrayPromemoria; + } + + void setArrayPromemoria(String arrayPromemoria) { + arrayPromemoria = arrayPromemoria; + } + + String getDescription() { + return description; + } + + void setDescription(String description) { + description = description; + } } diff --git a/lib/model/promemoria.dart b/lib/model/promemoria.dart index 5326c1d..d57a9fc 100644 --- a/lib/model/promemoria.dart +++ b/lib/model/promemoria.dart @@ -13,6 +13,85 @@ class Promemoria extends BaseEntity { static String arrayPromemoria = ''; static String description = ''; static Priority priority = Priority.none; - static Color color = Color.none; + + 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 id) { + id = id; + } + + String getTitle() { + return title; + } + + void setTitle(String title) { + title = title; + } + + String getCreationDate() { + return creationDate; + } + + void setCreationDate(String creationDate) { + creationDate = creationDate; + } + + String getLastModificationDate() { + return lastModificationDate; + } + + void setLastModificationDate(String lastModificationDate) { + lastModificationDate = lastModificationDate; + } + + String getExpirationDate() { + return expirationDate; + } + + void setExpirationDate(String expirationDate) { + expirationDate = expirationDate; + } + + String getArrayPromemoria() { + return arrayPromemoria; + } + + void setArrayPromemoria(String arrayPromemoria) { + arrayPromemoria = arrayPromemoria; + } + + String getDescription() { + return description; + } + + void setDescription(String description) { + description = description; + } + + Priority getPriority() { + return priority; + } + + void setPriority(Priority priority) { + priority = priority; + } + + } From 7aa8c8dcbce934556f249fc2ec4601a376fe0b64 Mon Sep 17 00:00:00 2001 From: grata Date: Fri, 29 Sep 2023 09:40:35 +0200 Subject: [PATCH 10/10] Added Firebase --- android/app/build.gradle | 6 +- lib/database/FireDb.dart | 167 ++++++++++++++++++++++++++++++++++++++ lib/main.dart | 5 +- lib/model/note.dart | 55 ++++++++----- lib/model/promemoria.dart | 100 ++++++++++++++++------- pubspec.yaml | 1 + 6 files changed, 285 insertions(+), 49 deletions(-) create mode 100644 lib/database/FireDb.dart 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/main.dart b/lib/main.dart index 9fac54f..f743340 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,7 +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 7172796..f6c8b12 100644 --- a/lib/model/note.dart +++ b/lib/model/note.dart @@ -2,13 +2,13 @@ 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 { @@ -21,51 +21,68 @@ class Note extends BaseEntity { }; } + Note( + this.id, + this.title, + this.creationDate, + this.lastModificationDate, + this.arrayPromemoria, + this.description, + ); + String getId() { return id; } - void setId(String id) { - id = id; + void setId(String id1) { + id = id1; } String getTitle() { return title; } - void setTitle(String title) { - title = title; + void setTitle(String title1) { + title = title1; } String getCreationDate() { return creationDate; } - void setCreationDate(String creationDate) { - creationDate = creationDate; + void setCreationDate(String creationDate1) { + creationDate = creationDate1; } String getLastModificationDate() { return lastModificationDate; } - void setLastModificationDate(String lastModificationDate) { - lastModificationDate = lastModificationDate; + void setLastModificationDate(String lastModificationDate1) { + lastModificationDate = lastModificationDate1; } String getArrayPromemoria() { return arrayPromemoria; } - void setArrayPromemoria(String arrayPromemoria) { - arrayPromemoria = arrayPromemoria; + void setArrayPromemoria(String arrayPromemoria1) { + arrayPromemoria = arrayPromemoria1; } String getDescription() { return description; } - void setDescription(String description) { - description = 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 d57a9fc..40a5f2f 100644 --- a/lib/model/promemoria.dart +++ b/lib/model/promemoria.dart @@ -4,16 +4,37 @@ import 'identifiers/enum/priority.dart'; const String promemoriaTable = 'promemoria'; -class Promemoria extends BaseEntity { - static String id = BaseEntity.getId; - static String title = BaseEntity.getTitle; - static String creationDate = BaseEntity.getCreationDate; - static String lastModificationDate = BaseEntity.getLastEditDate; - static String expirationDate = ''; - static String arrayPromemoria = ''; - static String description = ''; - static Priority priority = Priority.none; - static Color color = Color.none; +class Promemoria { + String id = ''; + String title; + String creationDate; + String lastModificationDate; + String expirationDate; + String arrayPromemoria; + String description; + String priority; + String color; + + Promemoria( + this.id, + this.title, + this.creationDate, + this.lastModificationDate, + this.expirationDate, + this.arrayPromemoria, + this.description, + this.priority, + this.color); + + Promemoria.newConstructor( + this.title, + this.creationDate, + this.lastModificationDate, + this.expirationDate, + this.arrayPromemoria, + this.description, + this.priority, + this.color); Map toMap() { return { @@ -29,69 +50,92 @@ class Promemoria extends BaseEntity { }; } - String getId(){ + String getId() { return id; } - void setId(String id) { - id = id; + void setId(String id1) { + id = id1; } String getTitle() { return title; } - void setTitle(String title) { - title = title; + void setTitle(String title1) { + title = title1; } String getCreationDate() { return creationDate; } - void setCreationDate(String creationDate) { - creationDate = creationDate; + void setCreationDate(String creationDate1) { + creationDate = creationDate1; } String getLastModificationDate() { return lastModificationDate; } - void setLastModificationDate(String lastModificationDate) { - lastModificationDate = lastModificationDate; + void setLastModificationDate(String lastModificationDate1) { + lastModificationDate = lastModificationDate1; } String getExpirationDate() { return expirationDate; } - void setExpirationDate(String expirationDate) { - expirationDate = expirationDate; + void setExpirationDate(String expirationDate1) { + expirationDate = expirationDate1; } String getArrayPromemoria() { return arrayPromemoria; } - void setArrayPromemoria(String arrayPromemoria) { - arrayPromemoria = arrayPromemoria; + void setArrayPromemoria(String arrayPromemoria1) { + arrayPromemoria = arrayPromemoria1; } String getDescription() { return description; } - void setDescription(String description) { - description = description; + void setDescription(String description1) { + description = description1; } - Priority getPriority() { + String getPriority() { return priority; } - void setPriority(Priority priority) { - priority = 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/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: