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: