From 1cc885e4b71afead58f3a18b2ac849d481bc074e Mon Sep 17 00:00:00 2001
From: Kima <kimavideos97@gmail.com>
Date: Thu, 12 Oct 2023 21:06:53 +0200
Subject: [PATCH] fixed exam subject rename thing

---
 filcnaplo_kreta_api/lib/models/exam.dart      | 17 +++++---
 .../lib/providers/exam_provider.dart          | 39 +++++++++++++++++--
 .../lib/common/widgets/exam/exam_tile.dart    | 17 +++++---
 .../lib/common/widgets/exam/exam_view.dart    |  6 ++-
 4 files changed, 63 insertions(+), 16 deletions(-)

diff --git a/filcnaplo_kreta_api/lib/models/exam.dart b/filcnaplo_kreta_api/lib/models/exam.dart
index 5f7689b..31741b0 100644
--- a/filcnaplo_kreta_api/lib/models/exam.dart
+++ b/filcnaplo_kreta_api/lib/models/exam.dart
@@ -1,3 +1,5 @@
+import 'package:filcnaplo_kreta_api/models/subject.dart';
+
 import 'category.dart';
 import 'teacher.dart';
 
@@ -6,8 +8,9 @@ class Exam {
   DateTime date;
   DateTime writeDate;
   Category? mode;
-  int? subjectIndex;
-  String subjectName;
+  // int? subjectIndex;
+  // String subjectName;
+  Subject subject;
   Teacher teacher;
   String description;
   String group;
@@ -18,8 +21,9 @@ class Exam {
     required this.date,
     required this.writeDate,
     this.mode,
-    this.subjectIndex,
-    required this.subjectName,
+    // this.subjectIndex,
+    // required this.subjectName,
+    required this.subject,
     required this.teacher,
     required this.description,
     required this.group,
@@ -36,8 +40,9 @@ class Exam {
           ? DateTime.parse(json["Datum"]).toLocal()
           : DateTime(0),
       mode: json["Modja"] != null ? Category.fromJson(json["Modja"]) : null,
-      subjectIndex: json["OrarendiOraOraszama"],
-      subjectName: json["TantargyNeve"] ?? "",
+      // subjectIndex: json["OrarendiOraOraszama"],
+      // subjectName: json["TantargyNeve"] ?? "",
+      subject: Subject.fromJson(json["Tantargy"] ?? {}),
       teacher: Teacher.fromString((json["RogzitoTanarNeve"] ?? "").trim()),
       description: (json["Temaja"] ?? "").trim(),
       group: json["OsztalyCsoport"] != null
diff --git a/filcnaplo_kreta_api/lib/providers/exam_provider.dart b/filcnaplo_kreta_api/lib/providers/exam_provider.dart
index 6f69f47..8f0c1e5 100644
--- a/filcnaplo_kreta_api/lib/providers/exam_provider.dart
+++ b/filcnaplo_kreta_api/lib/providers/exam_provider.dart
@@ -27,19 +27,49 @@ class ExamProvider with ChangeNotifier {
 
     // Load exams from the database
     if (userId != null) {
-      var dbExams = await Provider.of<DatabaseProvider>(_context, listen: false).userQuery.getExams(userId: userId);
+      var dbExams = await Provider.of<DatabaseProvider>(_context, listen: false)
+          .userQuery
+          .getExams(userId: userId);
       _exams = dbExams;
       notifyListeners();
+      await convertBySettings();
     }
   }
 
+  // for renamed subjects
+  Future<void> convertBySettings() async {
+    final _database = Provider.of<DatabaseProvider>(_context, listen: false);
+    Map<String, String> renamedSubjects =
+        (await _database.query.getSettings(_database)).renamedSubjectsEnabled
+            ? await _database.userQuery.renamedSubjects(
+                userId:
+                    Provider.of<UserProvider>(_context, listen: false).user!.id)
+            : {};
+    Map<String, String> renamedTeachers =
+        (await _database.query.getSettings(_database)).renamedTeachersEnabled
+            ? await _database.userQuery.renamedTeachers(
+                userId:
+                    Provider.of<UserProvider>(_context, listen: false).user!.id)
+            : {};
+
+    for (Exam exam in _exams) {
+      exam.subject.renamedTo =
+          renamedSubjects.isNotEmpty ? renamedSubjects[exam.subject.id] : null;
+      exam.teacher.renamedTo =
+          renamedTeachers.isNotEmpty ? renamedTeachers[exam.teacher.id] : null;
+    }
+
+    notifyListeners();
+  }
+
   // Fetches Exams from the Kreta API then stores them in the database
   Future<void> fetch() async {
     User? user = Provider.of<UserProvider>(_context, listen: false).user;
     if (user == null) throw "Cannot fetch Exams for User null";
     String iss = user.instituteCode;
 
-    List? examsJson = await Provider.of<KretaClient>(_context, listen: false).getAPI(KretaAPI.exams(iss));
+    List? examsJson = await Provider.of<KretaClient>(_context, listen: false)
+        .getAPI(KretaAPI.exams(iss));
     if (examsJson == null) throw "Cannot fetch Exams for User ${user.id}";
     List<Exam> exams = examsJson.map((e) => Exam.fromJson(e)).toList();
 
@@ -52,8 +82,11 @@ class ExamProvider with ChangeNotifier {
     if (user == null) throw "Cannot store Exams for User null";
     String userId = user.id;
 
-    await Provider.of<DatabaseProvider>(_context, listen: false).userStore.storeExams(exams, userId: userId);
+    await Provider.of<DatabaseProvider>(_context, listen: false)
+        .userStore
+        .storeExams(exams, userId: userId);
     _exams = exams;
     notifyListeners();
+    await convertBySettings();
   }
 }
diff --git a/filcnaplo_mobile_ui/lib/common/widgets/exam/exam_tile.dart b/filcnaplo_mobile_ui/lib/common/widgets/exam/exam_tile.dart
index b72983b..eada7ce 100755
--- a/filcnaplo_mobile_ui/lib/common/widgets/exam/exam_tile.dart
+++ b/filcnaplo_mobile_ui/lib/common/widgets/exam/exam_tile.dart
@@ -6,7 +6,8 @@ import 'package:filcnaplo/utils/format.dart';
 import 'package:flutter_feather_icons/flutter_feather_icons.dart';
 
 class ExamTile extends StatelessWidget {
-  const ExamTile(this.exam, {Key? key, this.onTap, this.padding}) : super(key: key);
+  const ExamTile(this.exam, {Key? key, this.onTap, this.padding})
+      : super(key: key);
 
   final Exam exam;
   final void Function()? onTap;
@@ -22,26 +23,32 @@ class ExamTile extends StatelessWidget {
           visualDensity: VisualDensity.compact,
           contentPadding: const EdgeInsets.only(left: 8.0, right: 12.0),
           onTap: onTap,
-          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14.0)),
+          shape:
+              RoundedRectangleBorder(borderRadius: BorderRadius.circular(14.0)),
           leading: SizedBox(
               width: 44,
               height: 44,
               child: Padding(
                 padding: const EdgeInsets.only(top: 2.0),
                 child: Icon(
-                  SubjectIcon.resolveVariant(subjectName: exam.subjectName, context: context),
+                  SubjectIcon.resolveVariant(
+                      subjectName: exam.subject.name, context: context),
                   size: 28.0,
                   color: AppColors.of(context).text.withOpacity(.75),
                 ),
               )),
           title: Text(
-            exam.description != "" ? exam.description : (exam.mode?.description ?? "Számonkérés"),
+            exam.description != ""
+                ? exam.description
+                : (exam.mode?.description ?? "Számonkérés"),
             maxLines: 2,
             overflow: TextOverflow.ellipsis,
             style: const TextStyle(fontWeight: FontWeight.w600),
           ),
           subtitle: Text(
-            exam.subjectName.capital(),
+            exam.subject.isRenamed
+                ? exam.subject.renamedTo!
+                : exam.subject.name.capital(),
             maxLines: 1,
             overflow: TextOverflow.ellipsis,
             style: const TextStyle(fontWeight: FontWeight.w500),
diff --git a/filcnaplo_mobile_ui/lib/common/widgets/exam/exam_view.dart b/filcnaplo_mobile_ui/lib/common/widgets/exam/exam_view.dart
index 4061baa..2bdee7f 100755
--- a/filcnaplo_mobile_ui/lib/common/widgets/exam/exam_view.dart
+++ b/filcnaplo_mobile_ui/lib/common/widgets/exam/exam_view.dart
@@ -29,13 +29,15 @@ class ExamView extends StatelessWidget {
               padding: const EdgeInsets.only(left: 6.0),
               child: Icon(
                 SubjectIcon.resolveVariant(
-                    subjectName: exam.subjectName, context: context),
+                    subjectName: exam.subject.name, context: context),
                 size: 36.0,
                 color: AppColors.of(context).text.withOpacity(.75),
               ),
             ),
             title: Text(
-              exam.subjectName.capital(),
+              exam.subject.isRenamed
+                  ? exam.subject.renamedTo!
+                  : exam.subject.name.capital(),
               maxLines: 1,
               overflow: TextOverflow.ellipsis,
               style: const TextStyle(fontWeight: FontWeight.w600),