10 Commits
Tito ... Joe

Author SHA1 Message Date
cd5803e6fe work in progress database 2023-09-28 14:23:23 +02:00
64b4f64f8c work in progress database 2023-09-28 13:38:09 +02:00
52d7defb75 entity upgrade 2023-09-28 11:47:01 +02:00
782bbebce9 database init 2023-09-28 11:38:19 +02:00
05386ac20f entity 2023-09-28 08:47:50 +02:00
9f26bc8595 note entity 2023-09-27 15:17:16 +02:00
Tito Arrigo
c35684c8f1 inizzializazione pagine 2023-09-27 14:17:52 +02:00
205f575db5 Merge remote-tracking branch 'origin/dev' into Joe 2023-09-27 13:52:34 +02:00
ecf7011302 new directory 2023-09-27 13:50:56 +02:00
58e013a709 database 2023-09-27 13:43:04 +02:00
20 changed files with 431 additions and 25 deletions

254
lib/database/database.dart Normal file
View File

@@ -0,0 +1,254 @@
import 'package:path/path.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;
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 {
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 {
// 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('''
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");
}
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 {
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 {
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> createPromemoria(
Database database, Promemoria promemoria) async {
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');
}
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],)
//}
}

View File

@@ -1,8 +0,0 @@
import 'base_entity.dart';
class Note extends BaseEntity{
static String id = BaseEntity.getId;
static String Title = BaseEntity.getTitle;
static String CreationDate = BaseEntity.getCreationDate;
}

View File

@@ -1,11 +0,0 @@
import 'base_entity.dart';
import 'identifiers/enum/priority.dart';
class Note extends BaseEntity{
static String id = BaseEntity.getId;
static String title = BaseEntity.getTitle;
static String creationDate = BaseEntity.getCreationDate;
static String expirationDate = 'expirationDate';
Priority priority = Priority.low;
}

View File

@@ -3,4 +3,5 @@ import 'myApp.dart';
void main() { void main() {
runApp(MyApp()); runApp(MyApp());
print("App started");
} }

12
lib/model/note.dart Normal file
View File

@@ -0,0 +1,12 @@
import 'base_entity.dart';
const String noteTable = 'note';
class Note extends BaseEntity {
static String id = BaseEntity.getId;
static String title = BaseEntity.getTitle;
static String creationDate = BaseEntity.getCreationDate;
static String lastModificationDate = BaseEntity.getLastEditDate;
static String arrayPromemoria = '';
static String description = '';
}

18
lib/model/promemoria.dart Normal file
View File

@@ -0,0 +1,18 @@
import 'base_entity.dart';
import 'identifiers/enum/color.dart';
import 'identifiers/enum/priority.dart';
const String promemoriaTable = 'promemoria';
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;
}

View File

@@ -4,6 +4,7 @@ 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(

View File

@@ -1,7 +1,11 @@
import 'dart:ffi'; import 'dart:ffi';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'testUI.dart'; import 'pages/testUI.dart';
import 'pages/TodayView.dart';
import 'pages/InboxView.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});
@@ -12,11 +16,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>[
TestUI(), TodayView(),
Text("Inbox"), InboxView(),
Text("Notes"), NotesView(),
Test()
]; ];
void _onItemTapped(int index) { void _onItemTapped(int index) {
@@ -38,6 +43,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,

19
lib/pages/InboxView.dart Normal file
View File

@@ -0,0 +1,19 @@
import 'package:flutter/material.dart';
class InboxView extends StatefulWidget {
const InboxView({super.key});
@override
State<InboxView> createState() => _InboxViewState();
}
class _InboxViewState extends State<InboxView> {
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Icon(Icons.inbox),
)
);
}
}

View File

@@ -0,0 +1,19 @@
import 'package:flutter/material.dart';
class NoteDetailView extends StatefulWidget {
const NoteDetailView({super.key});
@override
State<NoteDetailView> createState() => _NoteDetailViewState();
}
class _NoteDetailViewState extends State<NoteDetailView> {
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Text('NoteDetailView'),
)
);
}
}

19
lib/pages/NotesView.dart Normal file
View File

@@ -0,0 +1,19 @@
import 'package:flutter/material.dart';
class NotesView extends StatefulWidget {
const NotesView({super.key});
@override
State<NotesView> createState() => _NotesViewState();
}
class _NotesViewState extends State<NotesView> {
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Icon(Icons.note),
)
);
}
}

19
lib/pages/TodayView.dart Normal file
View File

@@ -0,0 +1,19 @@
import 'package:flutter/material.dart';
class TodayView extends StatefulWidget {
const TodayView({super.key});
@override
State<TodayView> createState() => _TodayViewState();
}
class _TodayViewState extends State<TodayView> {
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Icon(Icons.calendar_today)
)
);
}
}

54
lib/pages/test.dart Normal file
View File

@@ -0,0 +1,54 @@
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");
}
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(
body: Center(
child: Column(
children: [
FloatingActionButton(onPressed: _pressed),
FloatingActionButton(onPressed: _printdata),
FloatingActionButton(onPressed: _deletedatabase)
],
)
)
);
}
}

View File

@@ -36,6 +36,9 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2 cupertino_icons: ^1.0.2
firebase_core: ^2.16.0 firebase_core: ^2.16.0
sqflite: ^2.3.0
path: ^1.8.3
sqflite_common_ffi: ^2.3.0+2
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:

View File

@@ -8,7 +8,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:progetto_m335_flutter/main.dart'; import 'package:progetto_m335_flutter/myApp.dart';
void main() { void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async { testWidgets('Counter increments smoke test', (WidgetTester tester) async {