Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
28e5fc4f7a |
@@ -1,90 +1,103 @@
|
|||||||
import 'package:path/path.dart';
|
import 'package:path/path.dart';
|
||||||
|
import 'package:sqflite/sqflite.dart';
|
||||||
|
|
||||||
// Models
|
// Models
|
||||||
import 'package:progetto_m335_flutter/model/note.dart';
|
import 'package:progetto_m335_flutter/model/note.dart';
|
||||||
import 'package:progetto_m335_flutter/model/promemoria.dart';
|
import 'package:progetto_m335_flutter/model/promemoria.dart';
|
||||||
import 'package:sqflite/sqflite.dart';
|
|
||||||
|
|
||||||
class NoteDatabase {
|
class NoteDatabase {
|
||||||
static final NoteDatabase instance = NoteDatabase._init();
|
static final NoteDatabase instance = NoteDatabase._init();
|
||||||
static Database? _database;
|
static Database? _database;
|
||||||
|
|
||||||
|
// Zero args constructor needed to extend this class
|
||||||
|
NoteDatabase();
|
||||||
|
|
||||||
NoteDatabase._init();
|
NoteDatabase._init();
|
||||||
|
|
||||||
Future<Database> get database async {
|
Future<Database> get database async {
|
||||||
if (_database != null) return _database!;
|
if (_database != null) return _database!;
|
||||||
|
|
||||||
_database = await _initDB('note.db');
|
_database = await _initDB('note.db');
|
||||||
return _database!;
|
return _database!;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Database> _initDB(String filePath) async {
|
Future<Database> _initDB(String filePath) async {
|
||||||
print("Initializing database");
|
// On Android, it is typically data/data//databases.
|
||||||
|
// On iOS and MacOS, it is the Documents directory.
|
||||||
final databasePath = await getDatabasesPath();
|
final databasePath = await getDatabasesPath();
|
||||||
|
// Directory databasePath = await getApplicationDocumentsDirectory();
|
||||||
|
|
||||||
final path = join(databasePath, filePath);
|
final path = join(databasePath, filePath);
|
||||||
|
|
||||||
return await openDatabase(path, version: 1, onCreate: _createDB);
|
return await openDatabase(path, version: 1, onCreate: _createDB);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future _createDB(Database database, int version) async {
|
Future _createDB(Database database, int version) async {
|
||||||
|
//check if the database is created
|
||||||
|
if (database.query(noteTable) != null) {
|
||||||
|
print("Database already created");
|
||||||
|
|
||||||
await database.execute('''CREATE TABLE promemoria (
|
}else{
|
||||||
|
print("demo data inserting");
|
||||||
|
await database.execute('''CREATE TABLE promemoria (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
title TEXT NOT NULL,
|
title TEXT NOT NULL,
|
||||||
creationDate TEXT NOT NULL,
|
creationDate TEXT NOT NULL,
|
||||||
lastModificationDate TEXT,
|
lastModificationDate TEXT,
|
||||||
expirationDate TEXT,
|
expirationDate TEXT,
|
||||||
arrayPromemoria TEXT,
|
|
||||||
description TEXT,
|
description TEXT,
|
||||||
priority TEXT,
|
priority TEXT,
|
||||||
color TEXT
|
color TEXT
|
||||||
);
|
);
|
||||||
''');
|
''');
|
||||||
|
|
||||||
await database.execute('''CREATE TABLE note (
|
await database.execute('''CREATE TABLE note (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
title TEXT NOT NULL,
|
title TEXT NOT NULL,
|
||||||
creationDate TEXT NOT NULL,
|
creationDate TEXT NOT NULL,
|
||||||
lastModificationDate TEXT,
|
lastModificationDate TEXT,
|
||||||
arrayPromemoria TEXT,
|
|
||||||
description TEXT
|
description TEXT
|
||||||
);
|
);
|
||||||
''');
|
''');
|
||||||
|
|
||||||
print("database created");
|
print("database created");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
await fillDemoData(database, version);
|
await fillDemoData(database, version);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future fillDemoData(Database database, int version) async {
|
Future fillDemoData(Database database, int version) async {
|
||||||
|
|
||||||
|
print("boh speriamo funzioni");
|
||||||
|
// Add some fake accounts
|
||||||
|
|
||||||
|
|
||||||
// Add fake categories
|
// Add fake categories
|
||||||
await database.execute('''
|
await database.execute('''
|
||||||
INSERT INTO note (
|
INSERT INTO note (
|
||||||
title,
|
title,
|
||||||
creationDate,
|
creationDate,
|
||||||
lastModificationDate,
|
lastModificationDate,
|
||||||
arrayPromemoria,
|
|
||||||
description
|
description
|
||||||
) VALUES (
|
) VALUES (
|
||||||
'Nota 2',
|
'Nota 2',
|
||||||
'2023-09-28',
|
'2023-09-28',
|
||||||
'2023-09-28',
|
'2023-09-28',
|
||||||
'',
|
|
||||||
'Questo è un esempio di nota 2.'
|
'Questo è un esempio di nota 2.'
|
||||||
)
|
)
|
||||||
''');
|
''');
|
||||||
|
|
||||||
|
|
||||||
await database.execute('''
|
await database.execute('''
|
||||||
INSERT INTO note (
|
INSERT INTO note (
|
||||||
title,
|
title,
|
||||||
creationDate,
|
creationDate,
|
||||||
lastModificationDate,
|
lastModificationDate,
|
||||||
arrayPromemoria,
|
|
||||||
description
|
description
|
||||||
) VALUES (
|
) VALUES (
|
||||||
'Nota 2',
|
'Nota 2',
|
||||||
'2023-09-28',
|
'2023-09-28',
|
||||||
'2023-09-28',
|
'2023-09-28',
|
||||||
'',
|
|
||||||
'Questo è un esempio di nota 2.'
|
'Questo è un esempio di nota 2.'
|
||||||
)
|
)
|
||||||
''');
|
''');
|
||||||
@@ -96,7 +109,6 @@ class NoteDatabase {
|
|||||||
creationDate,
|
creationDate,
|
||||||
lastModificationDate,
|
lastModificationDate,
|
||||||
expirationDate,
|
expirationDate,
|
||||||
arrayPromemoria,
|
|
||||||
description,
|
description,
|
||||||
priority,
|
priority,
|
||||||
color
|
color
|
||||||
@@ -105,7 +117,6 @@ class NoteDatabase {
|
|||||||
'2023-09-27',
|
'2023-09-27',
|
||||||
'2023-09-27',
|
'2023-09-27',
|
||||||
'2023-10-05',
|
'2023-10-05',
|
||||||
'',
|
|
||||||
'Questo è un esempio di promemoria 1.',
|
'Questo è un esempio di promemoria 1.',
|
||||||
'Alta',
|
'Alta',
|
||||||
'Rosso'
|
'Rosso'
|
||||||
@@ -119,7 +130,6 @@ class NoteDatabase {
|
|||||||
creationDate,
|
creationDate,
|
||||||
lastModificationDate,
|
lastModificationDate,
|
||||||
expirationDate,
|
expirationDate,
|
||||||
arrayPromemoria,
|
|
||||||
description,
|
description,
|
||||||
priority,
|
priority,
|
||||||
color
|
color
|
||||||
@@ -128,7 +138,6 @@ class NoteDatabase {
|
|||||||
'2023-09-28',
|
'2023-09-28',
|
||||||
'2023-09-28',
|
'2023-09-28',
|
||||||
'2023-10-10',
|
'2023-10-10',
|
||||||
'',
|
|
||||||
'Questo è un esempio di promemoria 2.',
|
'Questo è un esempio di promemoria 2.',
|
||||||
'Media',
|
'Media',
|
||||||
'Verde'
|
'Verde'
|
||||||
@@ -150,42 +159,6 @@ class NoteDatabase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future close() async {
|
|
||||||
final database = await instance.database;
|
|
||||||
database.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
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<void> 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<List<Map>> selectAllPromemoria() async {
|
Future<List<Map>> selectAllPromemoria() async {
|
||||||
final db = await database;
|
final db = await database;
|
||||||
|
|
||||||
@@ -194,61 +167,15 @@ class NoteDatabase {
|
|||||||
return maps;
|
return maps;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<Map>> selectAllNotes() async {
|
Future close() async {
|
||||||
final db = await database;
|
final database = await instance.database;
|
||||||
|
database.close();
|
||||||
final List<Map<String, dynamic>> maps = await db.query(noteTable);
|
|
||||||
|
|
||||||
return maps;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> createPromemoria(
|
// WARNING: FOR DEV/TEST PURPOSES ONLY!!
|
||||||
Database database, Promemoria promemoria) async {
|
Future<void> deleteDatabase() async {
|
||||||
await database.execute('''
|
final databasePath = await getDatabasesPath();
|
||||||
INSERT INTO promemoria (
|
final path = join(databasePath, 'note.db');
|
||||||
title,
|
databaseFactory.deleteDatabase(path);
|
||||||
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<Map<String, Object?>> readPromemoria(int id) async {
|
|
||||||
final db = await database;
|
|
||||||
|
|
||||||
final results = await db.query('SELECT * FROM note where id=$id');
|
|
||||||
return results.first;
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<Map<String, Object?>> readNote(int id) async {
|
|
||||||
final db = await database;
|
|
||||||
|
|
||||||
final results = await db.query('SELECT * FROM promemoria where id=$id');
|
|
||||||
|
|
||||||
return results.first;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Future<void> 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],)
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,5 +3,4 @@ import 'myApp.dart';
|
|||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(MyApp());
|
runApp(MyApp());
|
||||||
print("App started");
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import 'navigation.dart';
|
|||||||
class MyApp extends StatelessWidget {
|
class MyApp extends StatelessWidget {
|
||||||
const MyApp({ Key? key }) : super(key: key);
|
const MyApp({ Key? key }) : super(key: key);
|
||||||
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ class _NavigationState extends State<Navigation> {
|
|||||||
],
|
],
|
||||||
currentIndex: _selectedIndex,
|
currentIndex: _selectedIndex,
|
||||||
onTap: _onItemTapped,
|
onTap: _onItemTapped,
|
||||||
|
type: BottomNavigationBarType.fixed,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,14 +29,6 @@ class _TestState extends State<Test> {
|
|||||||
print("Data printed");
|
print("Data printed");
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _deletedatabase() async {
|
|
||||||
final db = await noteDatabase.database;
|
|
||||||
|
|
||||||
print("Deleting database");
|
|
||||||
await db.delete(noteTable);
|
|
||||||
print("Database deleted");
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
@@ -44,8 +36,7 @@ class _TestState extends State<Test> {
|
|||||||
child: Column(
|
child: Column(
|
||||||
children: [
|
children: [
|
||||||
FloatingActionButton(onPressed: _pressed),
|
FloatingActionButton(onPressed: _pressed),
|
||||||
FloatingActionButton(onPressed: _printdata),
|
FloatingActionButton(onPressed: _printdata)
|
||||||
FloatingActionButton(onPressed: _deletedatabase)
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user