database init
This commit is contained in:
@@ -1,32 +1,181 @@
|
|||||||
import 'package:path/path.dart';
|
import 'package:path/path.dart';
|
||||||
import 'package:progetto_m335_flutter/model/note.dart';
|
|
||||||
import 'package:sqflite/sqflite.dart';
|
import 'package:sqflite/sqflite.dart';
|
||||||
|
|
||||||
class Database{
|
// Models
|
||||||
static final Database _instance = Database._init();
|
import 'package:progetto_m335_flutter/model/note.dart';
|
||||||
|
import 'package:progetto_m335_flutter/model/promemoria.dart';
|
||||||
|
|
||||||
|
class NoteDatabase {
|
||||||
|
static final NoteDatabase instance = NoteDatabase._init();
|
||||||
static Database? _database;
|
static Database? _database;
|
||||||
|
|
||||||
Database._init();
|
// Zero args constructor needed to extend this class
|
||||||
|
NoteDatabase();
|
||||||
|
|
||||||
Future _createDB(Database database) async{
|
NoteDatabase._init();
|
||||||
const integerPrimaryKeyAutoincrement = 'INTEGER PRIMARY KEY AUTOINCREMENT';
|
|
||||||
const textNotNull = 'TEXT NOT NULL';
|
Future<Database> get database async {
|
||||||
const integerNotNull = 'INTEGER NOT NULL';
|
if (_database != null) return _database!;
|
||||||
const integer = 'INTEGER';
|
|
||||||
const real = 'REAL';
|
_database = await _initDB('note.db');
|
||||||
const text = 'TEXT';
|
return _database!;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Database> _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('''
|
await database.execute('''
|
||||||
CREATE TABLE $Note (
|
INSERT INTO note (
|
||||||
${Note.id} $integerPrimaryKeyAutoincrement,
|
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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<List<Map>> selectAllPromemoria() async {
|
||||||
|
final db = await database;
|
||||||
|
|
||||||
|
final List<Map<String, dynamic>> maps = await db.query(promemoriaTable);
|
||||||
|
|
||||||
|
return maps;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future close() async {
|
||||||
|
final database = await instance.database;
|
||||||
|
database.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
// WARNING: FOR DEV/TEST PURPOSES ONLY!!
|
||||||
|
Future<void> deleteDatabase() async {
|
||||||
|
final databasePath = await getDatabasesPath();
|
||||||
|
final path = join(databasePath, 'note.db');
|
||||||
|
databaseFactory.deleteDatabase(path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,5 @@ class Promemoria extends BaseEntity{
|
|||||||
static Color color = Color.none;
|
static Color color = Color.none;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import 'pages/testUI.dart';
|
|||||||
import 'pages/TodayView.dart';
|
import 'pages/TodayView.dart';
|
||||||
import 'pages/InboxView.dart';
|
import 'pages/InboxView.dart';
|
||||||
import 'pages/NotesView.dart';
|
import 'pages/NotesView.dart';
|
||||||
|
import 'pages/test.dart';
|
||||||
|
|
||||||
class Navigation extends StatefulWidget {
|
class Navigation extends StatefulWidget {
|
||||||
const Navigation({super.key});
|
const Navigation({super.key});
|
||||||
@@ -13,11 +14,12 @@ class Navigation extends StatefulWidget {
|
|||||||
|
|
||||||
class _NavigationState extends State<Navigation> {
|
class _NavigationState extends State<Navigation> {
|
||||||
|
|
||||||
int _selectedIndex = 0;
|
int _selectedIndex = 3;
|
||||||
static const List<Widget> _widgetOptions = <Widget>[
|
static const List<Widget> _widgetOptions = <Widget>[
|
||||||
TodayView(),
|
TodayView(),
|
||||||
InboxView(),
|
InboxView(),
|
||||||
NotesView(),
|
NotesView(),
|
||||||
|
Test()
|
||||||
];
|
];
|
||||||
|
|
||||||
void _onItemTapped(int index) {
|
void _onItemTapped(int index) {
|
||||||
@@ -36,6 +38,7 @@ class _NavigationState extends State<Navigation> {
|
|||||||
icon: Icon(Icons.calendar_today), label: "today"),
|
icon: Icon(Icons.calendar_today), label: "today"),
|
||||||
BottomNavigationBarItem(icon: Icon(Icons.inbox), label: "Inbox"),
|
BottomNavigationBarItem(icon: Icon(Icons.inbox), label: "Inbox"),
|
||||||
BottomNavigationBarItem(icon: Icon(Icons.note), label: "Notes"),
|
BottomNavigationBarItem(icon: Icon(Icons.note), label: "Notes"),
|
||||||
|
BottomNavigationBarItem(icon: Icon(Icons.settings), label: "Settings")
|
||||||
],
|
],
|
||||||
currentIndex: _selectedIndex,
|
currentIndex: _selectedIndex,
|
||||||
onTap: _onItemTapped,
|
onTap: _onItemTapped,
|
||||||
|
|||||||
Reference in New Issue
Block a user