Livecardrework (#104)
This commit is contained in:
@@ -7,6 +7,24 @@ import 'package:filcnaplo/models/settings.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
|
||||
|
||||
const settingsDB = DatabaseStruct("settings", {
|
||||
"language": String, "start_page": int, "rounding": int, "theme": int, "accent_color": int, "news": int, "news_state": int, "developer_mode": int,
|
||||
"update_channel": int, "config": String, // general
|
||||
"grade_color1": int, "grade_color2": int, "grade_color3": int, "grade_color4": int, "grade_color5": int, // grade colors
|
||||
"vibration_strength": int, "ab_weeks": int, "swap_ab_weeks": int,
|
||||
"notifications": int, "notifications_bitfield": int, "notification_poll_interval": int, // notifications
|
||||
"x_filc_id": String, "graph_class_avg": int, "presentation_mode": int, "bell_delay": int, "bell_delay_enabled": int,
|
||||
});
|
||||
const usersDB = DatabaseStruct(
|
||||
"users", {"id": String, "name": String, "username": String, "password": String, "institute_code": String, "student": String, "role": int});
|
||||
const userDataDB = DatabaseStruct("user_data", {
|
||||
"id": String, "grades": String, "timetable": String, "exams": String, "homework": String, "messages": String, "notes": String,
|
||||
"events": String, "absences": String, "group_averages": String,
|
||||
// "subject_lesson_count": String, // non kreta data
|
||||
});
|
||||
|
||||
Future<void> createTable(Database db, DatabaseStruct struct) => db.execute("CREATE TABLE IF NOT EXISTS ${struct.table} ($struct)");
|
||||
|
||||
Future<Database> initDB() async {
|
||||
Database db;
|
||||
|
||||
@@ -17,13 +35,9 @@ Future<Database> initDB() async {
|
||||
db = await openDatabase("app.db");
|
||||
}
|
||||
|
||||
// Create table Users
|
||||
var usersDB = await createUsersTable(db);
|
||||
await db.execute("CREATE TABLE IF NOT EXISTS user_data ("
|
||||
"id TEXT NOT NULL, grades TEXT, timetable TEXT, exams TEXT, homework TEXT, messages TEXT, notes TEXT, events TEXT, absences TEXT)");
|
||||
|
||||
// Create table Settings
|
||||
var settingsDB = await createSettingsTable(db);
|
||||
await createTable(db, settingsDB);
|
||||
await createTable(db, usersDB);
|
||||
await createTable(db, userDataDB);
|
||||
|
||||
if ((await db.rawQuery("SELECT COUNT(*) FROM settings"))[0].values.first == 0) {
|
||||
// Set default values for table Settings
|
||||
@@ -32,8 +46,21 @@ Future<Database> initDB() async {
|
||||
|
||||
// Migrate Databases
|
||||
try {
|
||||
await migrateDB(db, "settings", settingsDB.struct.keys, SettingsProvider.defaultSettings().toMap(), createSettingsTable);
|
||||
await migrateDB(db, "users", usersDB.struct.keys, {"role": 0}, createUsersTable);
|
||||
await migrateDB(
|
||||
db,
|
||||
struct: settingsDB,
|
||||
defaultValues: SettingsProvider.defaultSettings().toMap(),
|
||||
);
|
||||
await migrateDB(
|
||||
db,
|
||||
struct: usersDB,
|
||||
defaultValues: {"role": 0},
|
||||
);
|
||||
await migrateDB(db, struct: userDataDB, defaultValues: {
|
||||
"grades": "[]", "timetable": "[]", "exams": "[]", "homework": "[]", "messages": "[]", "notes": "[]", "events": "[]", "absences": "[]",
|
||||
"group_averages": "[]",
|
||||
// "subject_lesson_count": "{}", // non kreta data
|
||||
});
|
||||
} catch (error) {
|
||||
print("ERROR: migrateDB: $error");
|
||||
}
|
||||
@@ -41,44 +68,16 @@ Future<Database> initDB() async {
|
||||
return db;
|
||||
}
|
||||
|
||||
Future<DatabaseStruct> createSettingsTable(Database db) async {
|
||||
var settingsDB = DatabaseStruct({
|
||||
"language": String, "start_page": int, "rounding": int, "theme": int, "accent_color": int, "news": int, "news_state": int, "developer_mode": int,
|
||||
"update_channel": int, "config": String, // general
|
||||
"grade_color1": int, "grade_color2": int, "grade_color3": int, "grade_color4": int, "grade_color5": int, // grade colors
|
||||
"vibration_strength": int, "ab_weeks": int, "swap_ab_weeks": int,
|
||||
"notifications": int, "notifications_bitfield": int, "notification_poll_interval": int, // notifications
|
||||
"x_filc_id": String, "graph_class_avg": int,
|
||||
});
|
||||
|
||||
// Create table Settings
|
||||
await db.execute("CREATE TABLE IF NOT EXISTS settings ($settingsDB)");
|
||||
|
||||
return settingsDB;
|
||||
}
|
||||
|
||||
Future<DatabaseStruct> createUsersTable(Database db) async {
|
||||
var usersDB = DatabaseStruct(
|
||||
{"id": String, "name": String, "username": String, "password": String, "institute_code": String, "student": String, "role": int});
|
||||
|
||||
// Create table Users
|
||||
await db.execute("CREATE TABLE IF NOT EXISTS users ($usersDB)");
|
||||
|
||||
return usersDB;
|
||||
}
|
||||
|
||||
Future<void> migrateDB(
|
||||
Database db,
|
||||
String table,
|
||||
Iterable<String> keys,
|
||||
Map<String, Object?> defaultValues,
|
||||
Future<DatabaseStruct> Function(Database) create,
|
||||
) async {
|
||||
var originalRows = await db.query(table);
|
||||
Database db, {
|
||||
required DatabaseStruct struct,
|
||||
required Map<String, Object?> defaultValues,
|
||||
}) async {
|
||||
var originalRows = await db.query(struct.table);
|
||||
|
||||
if (originalRows.isEmpty) {
|
||||
await db.execute("drop table $table");
|
||||
await create(db);
|
||||
await db.execute("drop table ${struct.table}");
|
||||
await createTable(db, struct);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -86,25 +85,28 @@ Future<void> migrateDB(
|
||||
|
||||
// go through each row and add missing keys or delete non existing keys
|
||||
await Future.forEach<Map<String, Object?>>(originalRows, (original) async {
|
||||
bool migrationRequired = keys.any((key) => !original.containsKey(key) || original[key] == null);
|
||||
bool migrationRequired = struct.struct.keys.any((key) => !original.containsKey(key) || original[key] == null) ||
|
||||
original.keys.any((key) => !struct.struct.containsKey(key));
|
||||
|
||||
if (migrationRequired) {
|
||||
print("INFO: Migrating $table");
|
||||
print("INFO: Migrating ${struct.table}");
|
||||
var copy = Map<String, Object?>.from(original);
|
||||
|
||||
// Fill missing columns
|
||||
for (var key in keys) {
|
||||
if (!keys.contains(key)) {
|
||||
print("DEBUG: dropping $key");
|
||||
copy.remove(key);
|
||||
}
|
||||
|
||||
for (var key in struct.struct.keys) {
|
||||
if (!original.containsKey(key) || original[key] == null) {
|
||||
print("DEBUG: migrating $key");
|
||||
copy[key] = defaultValues[key];
|
||||
}
|
||||
}
|
||||
|
||||
for (var key in original.keys) {
|
||||
if (!struct.struct.keys.contains(key)) {
|
||||
print("DEBUG: dropping $key");
|
||||
copy.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
migrated.add(copy);
|
||||
}
|
||||
});
|
||||
@@ -112,12 +114,12 @@ Future<void> migrateDB(
|
||||
// replace the old table with the migrated one
|
||||
if (migrated.isNotEmpty) {
|
||||
// Delete table
|
||||
await db.execute("drop table $table");
|
||||
await db.execute("drop table ${struct.table}");
|
||||
|
||||
// Recreate table
|
||||
await create(db);
|
||||
await createTable(db, struct);
|
||||
await Future.forEach(migrated, (Map<String, Object?> copy) async {
|
||||
await db.insert(table, copy);
|
||||
await db.insert(struct.table, copy);
|
||||
});
|
||||
|
||||
print("INFO: Database migrated");
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'dart:convert';
|
||||
import 'package:filcnaplo/models/subject_lesson_count.dart';
|
||||
import 'package:filcnaplo/models/user.dart';
|
||||
import 'package:sqflite_common/sqlite_api.dart';
|
||||
|
||||
@@ -13,6 +14,7 @@ import 'package:filcnaplo_kreta_api/models/message.dart';
|
||||
import 'package:filcnaplo_kreta_api/models/note.dart';
|
||||
import 'package:filcnaplo_kreta_api/models/event.dart';
|
||||
import 'package:filcnaplo_kreta_api/models/absence.dart';
|
||||
import 'package:filcnaplo_kreta_api/models/group_average.dart';
|
||||
|
||||
class DatabaseQuery {
|
||||
DatabaseQuery({required this.db});
|
||||
@@ -106,9 +108,27 @@ class UserDatabaseQuery {
|
||||
Future<List<Absence>> getAbsences({required String userId}) async {
|
||||
List<Map> userData = await db.query("user_data", where: "id = ?", whereArgs: [userId]);
|
||||
if (userData.isEmpty) return [];
|
||||
String? absebcesJson = userData.elementAt(0)["absences"] as String?;
|
||||
if (absebcesJson == null) return [];
|
||||
List<Absence> absebces = (jsonDecode(absebcesJson) as List).map((e) => Absence.fromJson(e)).toList();
|
||||
return absebces;
|
||||
String? absencesJson = userData.elementAt(0)["absences"] as String?;
|
||||
if (absencesJson == null) return [];
|
||||
List<Absence> absences = (jsonDecode(absencesJson) as List).map((e) => Absence.fromJson(e)).toList();
|
||||
return absences;
|
||||
}
|
||||
|
||||
Future<List<GroupAverage>> getGroupAverages({required String userId}) async {
|
||||
List<Map> userData = await db.query("user_data", where: "id = ?", whereArgs: [userId]);
|
||||
if (userData.isEmpty) return [];
|
||||
String? groupAveragesJson = userData.elementAt(0)["group_averages"] as String?;
|
||||
if (groupAveragesJson == null) return [];
|
||||
List<GroupAverage> groupAverages = (jsonDecode(groupAveragesJson) as List).map((e) => GroupAverage.fromJson(e)).toList();
|
||||
return groupAverages;
|
||||
}
|
||||
|
||||
Future<SubjectLessonCount> getSubjectLessonCount({required String userId}) async {
|
||||
List<Map> userData = await db.query("user_data", where: "id = ?", whereArgs: [userId]);
|
||||
if (userData.isEmpty) return SubjectLessonCount.fromMap({});
|
||||
String? lessonCountJson = userData.elementAt(0)["subject_lesson_count"] as String?;
|
||||
if (lessonCountJson == null) return SubjectLessonCount.fromMap({});
|
||||
SubjectLessonCount lessonCount = SubjectLessonCount.fromMap(jsonDecode(lessonCountJson) as Map);
|
||||
return lessonCount;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'dart:convert';
|
||||
import 'package:filcnaplo/models/subject_lesson_count.dart';
|
||||
import 'package:sqflite_common/sqlite_api.dart';
|
||||
|
||||
// Models
|
||||
@@ -12,6 +13,7 @@ import 'package:filcnaplo_kreta_api/models/message.dart';
|
||||
import 'package:filcnaplo_kreta_api/models/note.dart';
|
||||
import 'package:filcnaplo_kreta_api/models/event.dart';
|
||||
import 'package:filcnaplo_kreta_api/models/absence.dart';
|
||||
import 'package:filcnaplo_kreta_api/models/group_average.dart';
|
||||
|
||||
class DatabaseStore {
|
||||
DatabaseStore({required this.db});
|
||||
@@ -43,43 +45,54 @@ class UserDatabaseStore {
|
||||
|
||||
final Database db;
|
||||
|
||||
Future storeGrades(List<Grade> grades, {required String userId}) async {
|
||||
Future<void> storeGrades(List<Grade> grades, {required String userId}) async {
|
||||
String gradesJson = jsonEncode(grades.map((e) => e.json).toList());
|
||||
await db.update("user_data", {"grades": gradesJson}, where: "id = ?", whereArgs: [userId]);
|
||||
}
|
||||
|
||||
Future storeLessons(List<Lesson> lessons, {required String userId}) async {
|
||||
Future<void> storeLessons(List<Lesson> lessons, {required String userId}) async {
|
||||
String lessonsJson = jsonEncode(lessons.map((e) => e.json).toList());
|
||||
await db.update("user_data", {"timetable": lessonsJson}, where: "id = ?", whereArgs: [userId]);
|
||||
}
|
||||
|
||||
Future storeExams(List<Exam> exams, {required String userId}) async {
|
||||
Future<void> storeExams(List<Exam> exams, {required String userId}) async {
|
||||
String examsJson = jsonEncode(exams.map((e) => e.json).toList());
|
||||
await db.update("user_data", {"exams": examsJson}, where: "id = ?", whereArgs: [userId]);
|
||||
}
|
||||
|
||||
Future storeHomework(List<Homework> homework, {required String userId}) async {
|
||||
Future<void> storeHomework(List<Homework> homework, {required String userId}) async {
|
||||
String homeworkJson = jsonEncode(homework.map((e) => e.json).toList());
|
||||
await db.update("user_data", {"homework": homeworkJson}, where: "id = ?", whereArgs: [userId]);
|
||||
}
|
||||
|
||||
Future storeMessages(List<Message> messages, {required String userId}) async {
|
||||
Future<void> storeMessages(List<Message> messages, {required String userId}) async {
|
||||
String messagesJson = jsonEncode(messages.map((e) => e.json).toList());
|
||||
await db.update("user_data", {"messages": messagesJson}, where: "id = ?", whereArgs: [userId]);
|
||||
}
|
||||
|
||||
Future storeNotes(List<Note> notes, {required String userId}) async {
|
||||
Future<void> storeNotes(List<Note> notes, {required String userId}) async {
|
||||
String notesJson = jsonEncode(notes.map((e) => e.json).toList());
|
||||
await db.update("user_data", {"notes": notesJson}, where: "id = ?", whereArgs: [userId]);
|
||||
}
|
||||
|
||||
Future storeEvents(List<Event> events, {required String userId}) async {
|
||||
Future<void> storeEvents(List<Event> events, {required String userId}) async {
|
||||
String eventsJson = jsonEncode(events.map((e) => e.json).toList());
|
||||
await db.update("user_data", {"events": eventsJson}, where: "id = ?", whereArgs: [userId]);
|
||||
}
|
||||
|
||||
Future storeAbsences(List<Absence> absences, {required String userId}) async {
|
||||
Future<void> storeAbsences(List<Absence> absences, {required String userId}) async {
|
||||
String absencesJson = jsonEncode(absences.map((e) => e.json).toList());
|
||||
await db.update("user_data", {"absences": absencesJson}, where: "id = ?", whereArgs: [userId]);
|
||||
}
|
||||
|
||||
Future<void> storeGroupAverages(List<GroupAverage> groupAverages, {required String userId}) async {
|
||||
String groupAveragesJson = jsonEncode(groupAverages.map((e) => e.json).toList());
|
||||
await db.update("user_data", {"group_averages": groupAveragesJson}, where: "id = ?", whereArgs: [userId]);
|
||||
}
|
||||
|
||||
|
||||
Future<void> storeSubjectLessonCount(SubjectLessonCount lessonCount, {required String userId}) async {
|
||||
String lessonCountJson = jsonEncode(lessonCount.toMap());
|
||||
await db.update("user_data", {"subject_lesson_count": lessonCountJson}, where: "id = ?", whereArgs: [userId]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
class DatabaseStruct {
|
||||
final String table;
|
||||
final Map<String, dynamic> struct;
|
||||
|
||||
DatabaseStruct(this.struct);
|
||||
const DatabaseStruct(this.table, this.struct);
|
||||
|
||||
String _toDBfield(String name, dynamic type) {
|
||||
String typeName = "";
|
||||
|
||||
Reference in New Issue
Block a user