Compare commits
1 Commits
controller
...
Joe
| Author | SHA1 | Date | |
|---|---|---|---|
| cd5803e6fe |
@@ -1,39 +1,32 @@
|
||||
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';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
|
||||
class NoteDatabase {
|
||||
static final NoteDatabase instance = NoteDatabase._init();
|
||||
static Database? _database;
|
||||
|
||||
// Zero args constructor needed to extend this class
|
||||
NoteDatabase();
|
||||
|
||||
NoteDatabase._init();
|
||||
|
||||
Future<Database> get database async {
|
||||
if (_database != null) return _database!;
|
||||
|
||||
_database = await _initDB('note.db');
|
||||
return _database!;
|
||||
}
|
||||
|
||||
Future<Database> _initDB(String filePath) async {
|
||||
print("Initializing database");
|
||||
final databasePath = await getDatabasesPath();
|
||||
|
||||
final path = join(databasePath, filePath);
|
||||
|
||||
return await openDatabase(path, version: 1, onCreate: _createDB);
|
||||
}
|
||||
|
||||
Future _createDB(Database database, int version) async {
|
||||
if (await isDatabaseExists()) {
|
||||
print("Database already created");
|
||||
deleteDatabase();
|
||||
print("Database deleted");
|
||||
}
|
||||
|
||||
await database.execute('''CREATE TABLE promemoria (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
@@ -63,23 +56,6 @@ class NoteDatabase {
|
||||
}
|
||||
|
||||
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 (
|
||||
@@ -92,6 +68,7 @@ class NoteDatabase {
|
||||
'Nota 2',
|
||||
'2023-09-28',
|
||||
'2023-09-28',
|
||||
'',
|
||||
'Questo è un esempio di nota 2.'
|
||||
)
|
||||
''');
|
||||
@@ -107,6 +84,7 @@ class NoteDatabase {
|
||||
'Nota 2',
|
||||
'2023-09-28',
|
||||
'2023-09-28',
|
||||
'',
|
||||
'Questo è un esempio di nota 2.'
|
||||
)
|
||||
''');
|
||||
@@ -127,6 +105,7 @@ class NoteDatabase {
|
||||
'2023-09-27',
|
||||
'2023-09-27',
|
||||
'2023-10-05',
|
||||
'',
|
||||
'Questo è un esempio di promemoria 1.',
|
||||
'Alta',
|
||||
'Rosso'
|
||||
@@ -149,6 +128,7 @@ class NoteDatabase {
|
||||
'2023-09-28',
|
||||
'2023-09-28',
|
||||
'2023-10-10',
|
||||
'',
|
||||
'Questo è un esempio di promemoria 2.',
|
||||
'Media',
|
||||
'Verde'
|
||||
@@ -176,19 +156,34 @@ class NoteDatabase {
|
||||
}
|
||||
|
||||
Future<void> deleteDatabase() async {
|
||||
try {
|
||||
final databasePath = await getDatabasesPath();
|
||||
final path = join(databasePath, 'note.db');
|
||||
databaseFactory.deleteDatabase(path);
|
||||
} catch (error) {
|
||||
throw Exception('DbBase.deleteDatabase: $error');
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> 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);
|
||||
Future<void> createNote(Database database, Note note) async {
|
||||
await database.execute('''
|
||||
INSERT INTO note (
|
||||
title,
|
||||
creationDate,
|
||||
lastModificationDate,
|
||||
arrayPromemoria,
|
||||
description,
|
||||
|
||||
// Restituisce true se il database è stato aperto correttamente, altrimenti false.
|
||||
return database.isOpen;
|
||||
) VALUES (
|
||||
'$note.title}',
|
||||
'$note.creationDate',
|
||||
'$note.lastModificationDate',
|
||||
'$note.arrayPromemoria.toString()',
|
||||
'$note.description',
|
||||
)
|
||||
''');
|
||||
|
||||
print('note $note.title inserted');
|
||||
}
|
||||
|
||||
Future<List<Map>> selectAllPromemoria() async {
|
||||
@@ -207,23 +202,29 @@ class NoteDatabase {
|
||||
return maps;
|
||||
}
|
||||
|
||||
Future<void> createNote(Database database, Note note) async {
|
||||
await database.insert(
|
||||
'note',
|
||||
note.toMap(),
|
||||
conflictAlgorithm: ConflictAlgorithm.replace,
|
||||
);
|
||||
|
||||
print('note $note.title inserted');
|
||||
}
|
||||
|
||||
Future<void> createPromemoria(
|
||||
Database database, Promemoria promemoria) async {
|
||||
await database.insert(
|
||||
'promemoria',
|
||||
promemoria.toMap(),
|
||||
conflictAlgorithm: ConflictAlgorithm.replace,
|
||||
);
|
||||
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');
|
||||
}
|
||||
@@ -243,17 +244,11 @@ class NoteDatabase {
|
||||
return results.first;
|
||||
}
|
||||
|
||||
Future<void> updatePromemoria(Promemoria promemoria) async {
|
||||
final db = await database;
|
||||
// Future<void> updatePromemoria(Promemoria promemoria) async {
|
||||
// final db = await database;
|
||||
|
||||
await db.update('promemoria', promemoria.toMap(),
|
||||
where: 'id = ?', whereArgs: [promemoria.getId()]);
|
||||
}
|
||||
|
||||
Future<void> updateNote(Note note) async {
|
||||
final db = await database;
|
||||
|
||||
await db.update('note', note.toMap(),
|
||||
where: 'id = ?', whereArgs: [note.getId()]);
|
||||
}
|
||||
//await db.update('promemoria', promemoria, where: 'id = ?',
|
||||
// Pass the Dog's id as a whereArg to prevent SQL injection.
|
||||
// whereArgs: [dog.id],)
|
||||
//}
|
||||
}
|
||||
|
||||
@@ -9,63 +9,4 @@ class Note extends BaseEntity {
|
||||
static String lastModificationDate = BaseEntity.getLastEditDate;
|
||||
static String arrayPromemoria = '';
|
||||
static String description = '';
|
||||
|
||||
Map<String, dynamic> 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,85 +13,6 @@ class Promemoria extends BaseEntity {
|
||||
static String arrayPromemoria = '';
|
||||
static String description = '';
|
||||
static Priority priority = Priority.none;
|
||||
|
||||
static Color color = Color.none;
|
||||
|
||||
Map<String, dynamic> 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -29,6 +29,14 @@ class _TestState extends State<Test> {
|
||||
print("Data printed");
|
||||
}
|
||||
|
||||
Future<void> _deletedatabase() async {
|
||||
final db = await noteDatabase.database;
|
||||
|
||||
print("Deleting database");
|
||||
await db.delete(noteTable);
|
||||
print("Database deleted");
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
@@ -36,7 +44,8 @@ class _TestState extends State<Test> {
|
||||
child: Column(
|
||||
children: [
|
||||
FloatingActionButton(onPressed: _pressed),
|
||||
FloatingActionButton(onPressed: _printdata)
|
||||
FloatingActionButton(onPressed: _printdata),
|
||||
FloatingActionButton(onPressed: _deletedatabase)
|
||||
],
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user