Compare commits
4 Commits
Zina
...
controller
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a58721a6f4 | ||
| 64b4f64f8c | |||
| 52d7defb75 | |||
| 782bbebce9 |
@@ -1,37 +1,259 @@
|
||||
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<Database> 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<Database> _initDB(String filePath) async {
|
||||
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,
|
||||
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 (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
title TEXT NOT NULL,
|
||||
creationDate TEXT NOT NULL,
|
||||
lastModificationDate TEXT,
|
||||
arrayPromemoria TEXT,
|
||||
description TEXT
|
||||
);
|
||||
''');
|
||||
|
||||
print("database created");
|
||||
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 (
|
||||
title,
|
||||
creationDate,
|
||||
lastModificationDate,
|
||||
arrayPromemoria,
|
||||
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,
|
||||
arrayPromemoria,
|
||||
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,
|
||||
arrayPromemoria,
|
||||
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,
|
||||
arrayPromemoria,
|
||||
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 close() async {
|
||||
final database = await instance.database;
|
||||
database.close();
|
||||
}
|
||||
|
||||
Future<void> deleteDatabase() async {
|
||||
final databasePath = await getDatabasesPath();
|
||||
final path = join(databasePath, 'note.db');
|
||||
databaseFactory.deleteDatabase(path);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// Restituisce true se il database è stato aperto correttamente, altrimenti false.
|
||||
return database.isOpen;
|
||||
}
|
||||
|
||||
Future<List<Map>> selectAllPromemoria() async {
|
||||
final db = await database;
|
||||
|
||||
final List<Map<String, dynamic>> maps = await db.query(promemoriaTable);
|
||||
|
||||
return maps;
|
||||
}
|
||||
|
||||
Future<List<Map>> selectAllNotes() async {
|
||||
final db = await database;
|
||||
|
||||
final List<Map<String, dynamic>> maps = await db.query(noteTable);
|
||||
|
||||
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,
|
||||
);
|
||||
|
||||
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.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()]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,4 +3,5 @@ import 'myApp.dart';
|
||||
|
||||
void main() {
|
||||
runApp(MyApp());
|
||||
print("App started");
|
||||
}
|
||||
|
||||
@@ -7,5 +7,65 @@ 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 = '';
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,97 @@
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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<Navigation> {
|
||||
|
||||
int _selectedIndex = 0;
|
||||
int _selectedIndex = 3;
|
||||
static const List<Widget> _widgetOptions = <Widget>[
|
||||
TodayView(),
|
||||
InboxView(),
|
||||
NotesView(),
|
||||
Test()
|
||||
];
|
||||
|
||||
void _onItemTapped(int index) {
|
||||
@@ -41,6 +43,7 @@ class _NavigationState extends State<Navigation> {
|
||||
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,
|
||||
|
||||
45
lib/pages/test.dart
Normal file
45
lib/pages/test.dart
Normal file
@@ -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<Test> createState() => _TestState();
|
||||
}
|
||||
|
||||
class _TestState extends State<Test> {
|
||||
NoteDatabase noteDatabase = NoteDatabase.instance;
|
||||
|
||||
Future<void> _pressed() async {
|
||||
print("Inserting demo data");
|
||||
final db = await noteDatabase.database;
|
||||
|
||||
|
||||
}
|
||||
|
||||
Future<void> _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)
|
||||
],
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user