Merge remote-tracking branch 'origin/firebase' into dev

# Conflicts:
#	lib/database/database.dart
#	lib/model/note.dart
#	lib/model/promemoria.dart
#	lib/navigation.dart
#	lib/pages/InboxView.dart
#	lib/pages/TodayView.dart
#	pubspec.yaml
This commit is contained in:
Tito Arrigo
2023-09-29 10:27:17 +02:00
8 changed files with 495 additions and 42 deletions

View File

@@ -2,6 +2,7 @@ plugins {
id "com.android.application"
id "kotlin-android"
id "dev.flutter.flutter-gradle-plugin"
id 'com.google.gms.google-services'
}
def localProperties = new Properties()
@@ -64,4 +65,7 @@ flutter {
source '../..'
}
dependencies {}
dependencies {
implementation platform('com.google.firebase:firebase-bom:32.3.1')
implementation 'com.google.firebase:firebase-analytics-ktx'
}

167
lib/database/FireDb.dart Normal file
View File

@@ -0,0 +1,167 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:progetto_m335_flutter/model/promemoria.dart';
import 'package:progetto_m335_flutter/model/note.dart';
class FireDb {
Future createPromemoria(Promemoria promemoria) async {
final docPromemoria =
FirebaseFirestore.instance.collection('promemoria').doc();
final json = {
'id': docPromemoria.id,
'title': promemoria.getTitle(),
'creationDate': promemoria.getCreationDate(),
'lastModificationDate': promemoria.getLastModificationDate(),
'expirationDate': promemoria.getExpirationDate(),
'arrayPromemoria': promemoria.getArrayPromemoria(),
'description': promemoria.getDescription(),
'priority': promemoria.getPriority(),
'color': promemoria.getColor(),
};
await docPromemoria.set(json);
}
Future createAllPromemoria(List<Promemoria> promemorias) async {
for (var promemoria in promemorias) {
await createPromemoria(promemoria);
}
}
Future createNote(Note note) async {
final docNote = FirebaseFirestore.instance.collection('note').doc();
final json = {
'id': docNote.id,
'title': note.getTitle(),
'creationDate': note.getCreationDate(),
'lastModificationDate': note.getLastModificationDate(),
'arrayPromemoria': note.getArrayPromemoria(),
'description': note.getDescription(),
};
await docNote.set(json);
}
Future createAllNotes(List<Note> notes) async {
for (var note in notes) {
await createNote(note);
}
}
Future<List<Promemoria>> readAllPromemoria() async {
var promemorias =
await FirebaseFirestore.instance.collection('promemoria').get();
List<Promemoria> promemoriaList = [];
for (var promemoria in promemorias.docs) {
promemoriaList.add(Promemoria.fromJson(promemoria.data()));
}
return promemoriaList;
}
Future<List<Note>> readAllNotes() async {
var notes = await FirebaseFirestore.instance.collection('note').get();
List<Note> noteList = [];
for (var note in notes.docs) {
noteList.add(Note.fromJson(note.data()));
}
return noteList;
}
Future<Note?> readNoteById(String id) async {
var docNote = await FirebaseFirestore.instance.collection('note').doc(id);
final snapshot = await docNote.get();
if (snapshot.exists) {
return Note.fromJson(snapshot.data()!);
} else {
return null;
}
}
Future<Promemoria?> readPromemoriaById(String id) async {
final docPromemoria =
await FirebaseFirestore.instance.collection('promemoria').doc(id);
final snapshot = await docPromemoria.get();
if (snapshot.exists) {
return Promemoria.fromJson(snapshot.data()!);
} else {
return null;
}
}
/*
Future updateNote(Note note) async {
final docNote = FirebaseFirestore.instance.collection('note').doc(note.getId());
final json = {
'id': note.getId(),
'title': note.getTitle(),
'creationDate': note.getCreationDate(),
'lastModificationDate': note.getLastModificationDate(),
'arrayPromemoria': note.getArrayPromemoria(),
'description': note.getDescription(),
};
await docNote.update(json);
}
Future updatePromemoria(Promemoria promemoria) async {
final docPromemoria = FirebaseFirestore.instance.collection('promemoria').doc(promemoria.getId());
final json = {
'id': docPromemoria.id,
'title': promemoria.getTitle(),
'creationDate': promemoria.getCreationDate(),
'lastModificationDate': promemoria.getLastModificationDate(),
'expirationDate': promemoria.getExpirationDate(),
'arrayPromemoria': promemoria.getArrayPromemoria(),
'description': promemoria.getDescription(),
'priority': promemoria.getPriority(),
'color': promemoria.getColor(),
};
await docPromemoria.update(json);
}
*/
Future deleteNoteById(String id) async {
final docNote = FirebaseFirestore.instance.collection('note').doc(id);
await docNote.delete();
}
Future deletePromemoriaById(String id) async {
final docPromemoria = FirebaseFirestore.instance.collection('promemoria').doc(id);
await docPromemoria.delete();
}
Future deleteAllNotes() async {
var notes = await FirebaseFirestore.instance.collection('note').get();
for (var note in notes.docs) {
await deleteNoteById(note.id);
}
}
Future deleteAllPromemoria() async {
var promemorias = await FirebaseFirestore.instance.collection('promemoria').get();
for (var promemoria in promemorias.docs) {
await deletePromemoriaById(promemoria.id);
}
}
}

View File

@@ -22,55 +22,63 @@ class NoteDatabase {
}
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) {
if (await isDatabaseExists()) {
print("Database already created");
deleteDatabase();
print("Database deleted");
}
}else{
print("demo data inserting");
await database.execute('''CREATE TABLE promemoria (
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 (
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");
}
print("database created");
await fillDemoData(database, version);
}
Future fillDemoData(Database database, int version) async {
print("boh speriamo funzioni");
// Add some fake accounts
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('''
@@ -78,6 +86,7 @@ class NoteDatabase {
title,
creationDate,
lastModificationDate,
arrayPromemoria,
description
) VALUES (
'Nota 2',
@@ -87,12 +96,12 @@ class NoteDatabase {
)
''');
await database.execute('''
INSERT INTO note (
title,
creationDate,
lastModificationDate,
arrayPromemoria,
description
) VALUES (
'Nota 2',
@@ -109,6 +118,7 @@ class NoteDatabase {
creationDate,
lastModificationDate,
expirationDate,
arrayPromemoria,
description,
priority,
color
@@ -130,6 +140,7 @@ class NoteDatabase {
creationDate,
lastModificationDate,
expirationDate,
arrayPromemoria,
description,
priority,
color
@@ -159,6 +170,27 @@ class NoteDatabase {
}
}
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;
@@ -167,15 +199,61 @@ class NoteDatabase {
return maps;
}
Future close() async {
final database = await instance.database;
database.close();
Future<List<Map>> selectAllNotes() async {
final db = await database;
final List<Map<String, dynamic>> maps = await db.query(noteTable);
return maps;
}
// WARNING: FOR DEV/TEST PURPOSES ONLY!!
Future<void> deleteDatabase() async {
final databasePath = await getDatabasesPath();
final path = join(databasePath, 'note.db');
databaseFactory.deleteDatabase(path);
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()]);
}
}

View File

@@ -1,6 +1,10 @@
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'myApp.dart';
void main() {
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
print("App started");
}

View File

@@ -2,11 +2,87 @@ 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 = '';
class Note{
String id;
String title;
String creationDate;
String lastModificationDate;
String arrayPromemoria;
String description;
Map<String, dynamic> toMap() {
return {
'id': id,
'title': title,
'creationDate': creationDate,
'lastModificationDate': lastModificationDate,
'arrayPromemoria': arrayPromemoria,
'description': description
};
}
Note(
this.id,
this.title,
this.creationDate,
this.lastModificationDate,
this.arrayPromemoria,
this.description,
);
String getId() {
return id;
}
void setId(String id1) {
id = id1;
}
String getTitle() {
return title;
}
void setTitle(String title1) {
title = title1;
}
String getCreationDate() {
return creationDate;
}
void setCreationDate(String creationDate1) {
creationDate = creationDate1;
}
String getLastModificationDate() {
return lastModificationDate;
}
void setLastModificationDate(String lastModificationDate1) {
lastModificationDate = lastModificationDate1;
}
String getArrayPromemoria() {
return arrayPromemoria;
}
void setArrayPromemoria(String arrayPromemoria1) {
arrayPromemoria = arrayPromemoria1;
}
String getDescription() {
return description;
}
void setDescription(String description1) {
description = description1;
}
static Note fromJson(Map<String, dynamic> data) => Note(
data['id'],
data['title'],
data['creationDate'],
data['lastModificationDate'],
data['arrayPromemoria'],
data['description']);
}

View File

@@ -4,16 +4,138 @@ import 'identifiers/enum/priority.dart';
const String promemoriaTable = 'promemoria';
class Promemoria extends BaseEntity {
static String expirationDate = '';
static String arrayPromemoria = '';
String description = '';
static Priority priority = Priority.none;
class Promemoria {
String id = '';
String title;
String creationDate;
String lastModificationDate;
String expirationDate;
String arrayPromemoria;
String description;
String priority;
String color;
static Color color = Color.none;
Promemoria(
this.id,
this.title,
this.creationDate,
this.lastModificationDate,
this.expirationDate,
this.arrayPromemoria,
this.description,
this.priority,
this.color);
Promemoria(String description){
this.description = description;
Promemoria.newConstructor(
this.title,
this.creationDate,
this.lastModificationDate,
this.expirationDate,
this.arrayPromemoria,
this.description,
this.priority,
this.color);
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 id1) {
id = id1;
}
String getTitle() {
return title;
}
void setTitle(String title1) {
title = title1;
}
String getCreationDate() {
return creationDate;
}
void setCreationDate(String creationDate1) {
creationDate = creationDate1;
}
String getLastModificationDate() {
return lastModificationDate;
}
void setLastModificationDate(String lastModificationDate1) {
lastModificationDate = lastModificationDate1;
}
String getExpirationDate() {
return expirationDate;
}
void setExpirationDate(String expirationDate1) {
expirationDate = expirationDate1;
}
String getArrayPromemoria() {
return arrayPromemoria;
}
void setArrayPromemoria(String arrayPromemoria1) {
arrayPromemoria = arrayPromemoria1;
}
String getDescription() {
return description;
}
void setDescription(String description1) {
description = description1;
}
String getPriority() {
return priority;
}
void setPriority(Priority priority1) {
priority = priority1.toString();
}
String getColor() {
return color;
}
void setColor(Color color1) {
color = color1.toString();
}
static Promemoria fromJson(Map<String, dynamic> data) {
Promemoria promemoria = Promemoria(
data['id'],
data['title'],
data['creationDate'],
data['lastModificationDate'],
data['expirationDate'],
data['arrayPromemoria'],
data['description'],
data['priority'],
data['color']);
print(promemoria.getId().toString());
return promemoria;
}
}

View File

@@ -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(

View File

@@ -39,6 +39,7 @@ dependencies:
sqflite: ^2.3.0
path: ^1.8.3
sqflite_common_ffi: ^2.3.0+2
cloud_firestore: ^4.9.2
dev_dependencies:
flutter_test: