removed controller
This commit is contained in:
@@ -1,122 +0,0 @@
|
||||
import 'package:progetto_m335_flutter/database/FireDb.dart';
|
||||
|
||||
import '../model/note.dart';
|
||||
import '../model/promemoria.dart';
|
||||
import 'database.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
|
||||
class Controller {
|
||||
NoteDatabase database = NoteDatabase.instance;
|
||||
FireDb fireDb = FireDb();
|
||||
|
||||
|
||||
Future<List<Note>> getAllNote() async {
|
||||
final db = await database.database;
|
||||
var notes = await db.query(noteTable);
|
||||
List<Note> noteList = notes.map((e) => Note.fromJson(e)).toList();
|
||||
return noteList;
|
||||
}
|
||||
|
||||
Future<List<Promemoria>> getAllPromemoria() async {
|
||||
final db = await database.database;
|
||||
var promemorias = await db.query(promemoriaTable);
|
||||
List<Promemoria> promemoriaList =
|
||||
promemorias.map((e) => Promemoria.fromJson(e)).toList();
|
||||
return promemoriaList;
|
||||
}
|
||||
|
||||
Future<Note> getNoteById(int id) async {
|
||||
final db = await database.database;
|
||||
var note = await db.query(noteTable, where: 'id = ?', whereArgs: [id]);
|
||||
return Note.fromJson(note.first);
|
||||
}
|
||||
|
||||
Future<Promemoria> getPromemoriaById(int id) async {
|
||||
final db = await database.database;
|
||||
var promemoria =
|
||||
await db.query(promemoriaTable, where: 'id = ?', whereArgs: [id]);
|
||||
return Promemoria.fromJson(promemoria.first);
|
||||
}
|
||||
|
||||
//add note
|
||||
void addNote(Note note) async {
|
||||
final db = await database.database;
|
||||
await db.execute('''
|
||||
INSERT INTO note (
|
||||
title,
|
||||
creationDate,
|
||||
lastModificationDate,
|
||||
arrayPromemoria,
|
||||
description
|
||||
) VALUES (
|
||||
'${note.title}',
|
||||
'${note.creationDate}',
|
||||
'${note.lastModificationDate}',
|
||||
'${note.arrayPromemoria}',
|
||||
'${note.description}'
|
||||
)
|
||||
''');
|
||||
}
|
||||
|
||||
//add Promemoria
|
||||
void addPromemoria(Promemoria promemoria) async {
|
||||
final db = await database.database;
|
||||
await db.execute('''
|
||||
INSERT INTO promemoria (
|
||||
title,
|
||||
creationDate,
|
||||
lastModificationDate,
|
||||
expirationDate,
|
||||
arrayPromemoria,
|
||||
description,
|
||||
priority,
|
||||
color
|
||||
) VALUES (
|
||||
'${promemoria.title}',
|
||||
'${promemoria.creationDate}',
|
||||
'${promemoria.lastModificationDate}',
|
||||
'${promemoria.expirationDate}',
|
||||
'${promemoria.arrayPromemoria}',
|
||||
'${promemoria.description}',
|
||||
'${promemoria.priority}',
|
||||
'${promemoria.color}'
|
||||
)
|
||||
''');
|
||||
}
|
||||
|
||||
void deleteAll() async {
|
||||
final db = await database.database;
|
||||
await db.execute('''
|
||||
DELETE FROM promemoria
|
||||
''');
|
||||
|
||||
await db.execute('''
|
||||
DELETE FROM note
|
||||
''');
|
||||
}
|
||||
|
||||
|
||||
void getDataFromFirebase(Database database, int version) async {
|
||||
this.deleteAll();
|
||||
|
||||
var promemorias = await fireDb.readAllPromemoria();
|
||||
var notes = await fireDb.readAllNotes();
|
||||
|
||||
for (var promemoria in promemorias) {
|
||||
this.addPromemoria(promemoria);
|
||||
}
|
||||
|
||||
for (var note in notes) {
|
||||
this.addNote(note);
|
||||
}
|
||||
}
|
||||
|
||||
void syncData() async {
|
||||
var promemorias = await this.getAllPromemoria();
|
||||
var notes = await this.getAllNote();
|
||||
|
||||
await fireDb.createAllPromemoria(promemorias);
|
||||
await fireDb.createAllNotes(notes);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,14 +1,13 @@
|
||||
import 'package:path/path.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
import '../model/note.dart';
|
||||
import 'FireDb.dart';
|
||||
import 'package:progetto_m335_flutter/model/promemoria.dart';
|
||||
import 'controller.dart';
|
||||
|
||||
class NoteDatabase {
|
||||
static final NoteDatabase instance = NoteDatabase._init();
|
||||
static Database? _database;
|
||||
FireDb fireDb = FireDb();
|
||||
Controller controller = Controller();
|
||||
|
||||
NoteDatabase._init();
|
||||
|
||||
@@ -51,7 +50,7 @@ class NoteDatabase {
|
||||
''');
|
||||
|
||||
print("database created");
|
||||
controller.getDataFromFirebase(database, version);
|
||||
getDataFromFirebase(database, version);
|
||||
}
|
||||
|
||||
Future<List<Map>> selectAllPromemoria() async {
|
||||
@@ -61,4 +60,109 @@ class NoteDatabase {
|
||||
|
||||
return maps;
|
||||
}
|
||||
|
||||
Future<List<Note>> getAllNote() async {
|
||||
var notes = await _database?.query(noteTable);
|
||||
List<Note> noteList = notes!.map((e) => Note.fromJson(e)).toList();
|
||||
return noteList;
|
||||
}
|
||||
|
||||
Future<List<Promemoria>> getAllPromemoria() async {
|
||||
var promemorias = await _database?.query(promemoriaTable);
|
||||
List<Promemoria> promemoriaList =
|
||||
promemorias!.map((e) => Promemoria.fromJson(e)).toList();
|
||||
return promemoriaList;
|
||||
}
|
||||
|
||||
Future<Note> getNoteById(int id) async {
|
||||
var note = await _database?.query(noteTable, where: 'id = ?', whereArgs: [id]);
|
||||
return Note.fromJson(note!.first);
|
||||
}
|
||||
|
||||
Future<Promemoria> getPromemoriaById(int id) async {
|
||||
var promemoria =
|
||||
await _database?.query(promemoriaTable, where: 'id = ?', whereArgs: [id]);
|
||||
return Promemoria.fromJson(promemoria!.first);
|
||||
}
|
||||
|
||||
//add note
|
||||
void addNote(Note note) async {
|
||||
await _database?.execute('''
|
||||
INSERT INTO note (
|
||||
title,
|
||||
creationDate,
|
||||
lastModificationDate,
|
||||
arrayPromemoria,
|
||||
description
|
||||
) VALUES (
|
||||
'${note.title}',
|
||||
'${note.creationDate}',
|
||||
'${note.lastModificationDate}',
|
||||
'${note.arrayPromemoria}',
|
||||
'${note.description}'
|
||||
)
|
||||
''');
|
||||
}
|
||||
|
||||
//add Promemoria
|
||||
void addPromemoria(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}',
|
||||
'${promemoria.description}',
|
||||
'${promemoria.priority}',
|
||||
'${promemoria.color}'
|
||||
)
|
||||
''');
|
||||
}
|
||||
|
||||
void deleteAll() async {
|
||||
await _database?.execute('''
|
||||
DELETE FROM promemoria
|
||||
''');
|
||||
|
||||
await _database?.execute('''
|
||||
DELETE FROM note
|
||||
''');
|
||||
}
|
||||
|
||||
|
||||
void getDataFromFirebase(Database database, int version) async {
|
||||
this.deleteAll();
|
||||
|
||||
var promemorias = await fireDb.readAllPromemoria();
|
||||
var notes = await fireDb.readAllNotes();
|
||||
|
||||
for (var promemoria in promemorias) {
|
||||
this.addPromemoria(promemoria);
|
||||
}
|
||||
|
||||
for (var note in notes) {
|
||||
this.addNote(note);
|
||||
}
|
||||
}
|
||||
|
||||
void syncData() async {
|
||||
var promemorias = await getAllPromemoria();
|
||||
var notes = await getAllNote();
|
||||
|
||||
await fireDb.deleteAllPromemoria();
|
||||
await fireDb.deleteAllNotes();
|
||||
|
||||
await fireDb.createAllPromemoria(promemorias);
|
||||
await fireDb.createAllNotes(notes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,11 @@
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:progetto_m335_flutter/database/controller.dart';
|
||||
import 'myApp.dart';
|
||||
import 'dart:async';
|
||||
|
||||
Timer? timer;
|
||||
|
||||
Future<void> main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await Firebase.initializeApp();
|
||||
runApp(MyApp());
|
||||
print("App started");
|
||||
Controller controller = Controller();
|
||||
timer = Timer.periodic(Duration(seconds: 60), (Timer t) => (
|
||||
controller.syncData()
|
||||
));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:progetto_m335_flutter/database/controller.dart';
|
||||
import 'package:progetto_m335_flutter/database/database.dart';
|
||||
import 'package:progetto_m335_flutter/model/note.dart';
|
||||
import 'package:progetto_m335_flutter/model/promemoria.dart';
|
||||
@@ -15,7 +14,6 @@ class Test extends StatefulWidget {
|
||||
|
||||
class _TestState extends State<Test> {
|
||||
NoteDatabase noteDatabase = NoteDatabase.instance;
|
||||
Controller controller = Controller();
|
||||
|
||||
Future<void> _pressed() async {
|
||||
print("Inserting demo data");
|
||||
@@ -44,14 +42,14 @@ class _TestState extends State<Test> {
|
||||
"alta",
|
||||
"rosso");
|
||||
|
||||
controller.addNote(nota);
|
||||
controller.addPromemoria(promemoria);
|
||||
noteDatabase.addNote(nota);
|
||||
noteDatabase.addPromemoria(promemoria);
|
||||
|
||||
final db = await noteDatabase.database;
|
||||
|
||||
print("Printing data");
|
||||
print((await controller.getAllPromemoria()).first);
|
||||
print((await controller.getAllNote()).first);
|
||||
print((await noteDatabase.getAllPromemoria()).first);
|
||||
print((await noteDatabase.getAllNote()).first);
|
||||
print("promemoria");
|
||||
print(await db.query(promemoriaTable));
|
||||
print("note");
|
||||
@@ -59,7 +57,7 @@ class _TestState extends State<Test> {
|
||||
print("Data printed");
|
||||
|
||||
print("savedata from database to firebase");
|
||||
controller.syncData();
|
||||
noteDatabase.syncData();
|
||||
print("data saved");
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user