diff --git a/firka/lib/services/live_activity_service.dart b/firka/lib/services/live_activity_service.dart index 0b23462..5ddd16e 100644 --- a/firka/lib/services/live_activity_service.dart +++ b/firka/lib/services/live_activity_service.dart @@ -1581,12 +1581,12 @@ class LiveActivityService { final emptyType = NameUidDesc( uid: 'placeholder', name: 'Placeholder', - description: null, + description: '', ); final emptyState = NameUidDesc( uid: 'active', name: 'Active', - description: null, + description: '', ); placeholderLesson = Lesson( diff --git a/firka/lib/ui/phone/pages/home/home_grades.dart b/firka/lib/ui/phone/pages/home/home_grades.dart index 82a3b20..331aa96 100644 --- a/firka/lib/ui/phone/pages/home/home_grades.dart +++ b/firka/lib/ui/phone/pages/home/home_grades.dart @@ -156,18 +156,7 @@ class _HomeGradesScreen extends FirkaState { if (lessons != null && lessons!.response != null) { for (var lesson in lessons!.response!) { if (subjects.where((s) => s.uid == lesson.uid).isEmpty) { - subjects.add( - Subject( - uid: lesson.uid, - name: lesson.name, - category: NameUidDesc( - uid: lesson.subjectCategoryId, - name: lesson.subjectCategoryName, - description: lesson.subjectCategoryDescription, - ), - sortIndex: lesson.sortIndex, - ), - ); + subjects.add(lesson.subject); } } } diff --git a/kreta_api/lib/src/extensions.dart b/kreta_api/lib/src/extensions.dart new file mode 100644 index 0000000..740bb09 --- /dev/null +++ b/kreta_api/lib/src/extensions.dart @@ -0,0 +1,17 @@ +extension JsonHelper on Map { + double? dbl(String key) { + final value = this[key]; + if (value == null) { + return null; + } + return (value as num).toDouble(); + } + + DateTime? localDate(String key) { + final value = this[key]; + if (value == null) { + return null; + } + return DateTime.parse(value as String).toLocal(); + } +} diff --git a/kreta_api/lib/src/model/class_group.dart b/kreta_api/lib/src/model/class_group.dart index d520c58..edfed1f 100644 --- a/kreta_api/lib/src/model/class_group.dart +++ b/kreta_api/lib/src/model/class_group.dart @@ -1,4 +1,6 @@ +import '../extensions.dart'; import 'generic.dart'; +import 'subject.dart'; class ClassGroup { final String uid; @@ -27,17 +29,11 @@ class ClassGroup { return ClassGroup( uid: json['Uid'], name: json['Nev'], - headTeacher: json['OsztalyFonok'] != null - ? UidObj.fromJson(json['OsztalyFonok']) - : null, - substituteHeadTeacher: json['OsztalyFonokHelyettes'] != null - ? UidObj.fromJson(json['OsztalyFonokHelyettes']) - : null, - studyGroup: NameUidDesc.fromJson(json['OktatasNevelesiKategoria']), + headTeacher: json.uid('OsztalyFonok'), + substituteHeadTeacher: json.uid('OsztalyFonokHelyettes'), + studyGroup: json.nameUidDesc('OktatasNevelesiKategoria')!, studyGroupSortIndex: json['OktatasNevelesiKategoriaSortIndex'], - studyTask: json['OktatasNevelesiFeladat'] != null - ? NameUidDesc.fromJson(json['OktatasNevelesiFeladat']) - : null, + studyTask: json.nameUidDesc('OktatasNevelesiFeladat'), isActive: json['IsAktiv'], type: json['Tipus'], ); @@ -59,55 +55,32 @@ class ClassGroup { } } -class SubjectAverage { - final String uid; - final String name; - final String? teacherName; - final String subjectCategoryId; - final String subjectCategoryName; - final String subjectCategoryDescription; +class SubjectAverage extends UidObj { + final Subject subject; final double? average; final double? weightedSum; final double? weightedCount; - final int sortIndex; SubjectAverage({ - required this.uid, - required this.name, - this.teacherName, - required this.subjectCategoryId, - required this.subjectCategoryName, - required this.subjectCategoryDescription, + required super.uid, + required this.subject, this.average, this.weightedSum, this.weightedCount, - required this.sortIndex, }); factory SubjectAverage.fromJson(Map json) { - final tantargy = json['Tantargy'] ?? {}; - final kategori = tantargy['Kategoria'] ?? {}; - return SubjectAverage( - uid: json['Uid'] ?? '', - name: tantargy['Nev'] ?? '', - teacherName: json['TeacherName'], - subjectCategoryId: kategori['Uid'] ?? '', - subjectCategoryName: kategori['Nev'] ?? '', - subjectCategoryDescription: kategori['Leiras'] ?? '', - average: json['Atlag'] != null ? (json['Atlag'] as num).toDouble() : null, - weightedSum: json['SulyozottOsztalyzatOsszege'] != null - ? (json['SulyozottOsztalyzatOsszege'] as num).toDouble() - : null, - weightedCount: json['SulyozottOsztalyzatSzama'] != null - ? (json['SulyozottOsztalyzatSzama'] as num).toDouble() - : null, - sortIndex: tantargy['SortIndex'] ?? 0, + uid: json['Uid'], + subject: Subject.fromJson(json['Tantargy']), + average: json.dbl('Atlag'), + weightedSum: json.dbl('SulyozottOsztalyzatOsszege'), + weightedCount: json.dbl('SulyozottOsztalyzatSzama'), ); } @override String toString() { - return 'SubjectAverage(uid: "$uid", name: "$name", category: "$subjectCategoryName", average: $average)'; + return 'SubjectAverage(uid: "$uid", name: "${subject.name}", category: "${subject.category.name}", average: $average)'; } } diff --git a/kreta_api/lib/src/model/generic.dart b/kreta_api/lib/src/model/generic.dart index cb2d276..74b0341 100644 --- a/kreta_api/lib/src/model/generic.dart +++ b/kreta_api/lib/src/model/generic.dart @@ -1,11 +1,9 @@ -class NameUidDesc { - final String uid; - final String? name; - final String? description; +class NameUidDesc extends NameUid { + final String description; NameUidDesc({ - required this.uid, - required this.name, + required super.uid, + required super.name, required this.description, }); @@ -31,11 +29,10 @@ class NameUidDesc { } } -class NameUid { - final String uid; +class NameUid extends UidObj { final String name; - NameUid({required this.uid, required this.name}); + NameUid({required super.uid, required this.name}); factory NameUid.fromJson(Map json) { return NameUid(uid: json['Uid'], name: json['Nev']); @@ -62,3 +59,29 @@ class UidObj { ')'; } } + +extension ToUidObj on Map { + UidObj? uid(String key) { + final value = this[key]; + if (value == null) { + return null; + } + return UidObj.fromJson(value); + } + + NameUid? nameUid(String key) { + final value = this[key]; + if (value == null) { + return null; + } + return NameUid.fromJson(value); + } + + NameUidDesc? nameUidDesc(String key) { + final value = this[key]; + if (value == null) { + return null; + } + return NameUidDesc.fromJson(value); + } +} diff --git a/kreta_api/lib/src/model/grade.dart b/kreta_api/lib/src/model/grade.dart index ef04aaa..7a7e00d 100644 --- a/kreta_api/lib/src/model/grade.dart +++ b/kreta_api/lib/src/model/grade.dart @@ -1,8 +1,7 @@ import 'generic.dart'; import 'subject.dart'; -class Grade { - final String uid; +class Grade extends UidObj { final DateTime recordDate; final DateTime creationDate; final DateTime? ackDate; @@ -21,7 +20,7 @@ class Grade { final int sortIndex; Grade({ - required this.uid, + required super.uid, required this.recordDate, required this.creationDate, this.ackDate, @@ -50,18 +49,16 @@ class Grade { : null, subject: Subject.fromJson(json['Tantargy']), topic: json['Tema'], - type: NameUidDesc.fromJson(json['Tipus']), - mode: json['Mod'] != null ? NameUidDesc.fromJson(json['Mod']) : null, - valueType: NameUidDesc.fromJson(json['ErtekFajta']), + type: json.nameUidDesc('Tipus')!, + mode: json.nameUidDesc('Mod'), + valueType: json.nameUidDesc('ErtekFajta')!, teacher: json['ErtekeloTanarNeve'], kind: json['Kind'], numericValue: json['SzamErtek'], strValue: json['SzovegesErtek'], weightPercentage: json['SulySzazalekErteke'], shortStrValue: json['SzovegesErtekelesRovidNev'], - classGroup: json['OsztalyCsoport'] != null - ? UidObj.fromJson(json['OsztalyCsoport']) - : null, + classGroup: json.uid('OsztalyCsoport'), sortIndex: json['SortIndex'], ); } diff --git a/kreta_api/lib/src/model/guardian.dart b/kreta_api/lib/src/model/guardian.dart index 123bf4f..22ce4f3 100644 --- a/kreta_api/lib/src/model/guardian.dart +++ b/kreta_api/lib/src/model/guardian.dart @@ -1,16 +1,16 @@ -class Guardian { +import 'generic.dart'; + +class Guardian extends NameUid { final String? email; final bool isLegalRepresentative; - final String? name; final String? phoneNumber; - final String uid; Guardian({ required this.email, required this.isLegalRepresentative, - required this.name, + required super.name, required this.phoneNumber, - required this.uid, + required super.uid, }); factory Guardian.fromJson(Map json) { diff --git a/kreta_api/lib/src/model/homework.dart b/kreta_api/lib/src/model/homework.dart index d3662a7..34c1324 100644 --- a/kreta_api/lib/src/model/homework.dart +++ b/kreta_api/lib/src/model/homework.dart @@ -1,8 +1,8 @@ +import '../extensions.dart'; import 'generic.dart'; import 'subject.dart'; -class Homework { - final String uid; +class Homework extends UidObj { final Subject subject; final String subjectName; final String teacherName; @@ -17,7 +17,7 @@ class Homework { final bool canAttach; Homework({ - required this.uid, + required super.uid, required this.subject, required this.subjectName, required this.teacherName, @@ -39,13 +39,13 @@ class Homework { subjectName: json["TantargyNeve"], teacherName: json["RogzitoTanarNeve"], description: json["Szoveg"], - startDate: DateTime.parse(json["FeladasDatuma"]).toLocal(), - dueDate: DateTime.parse(json["HataridoDatuma"]).toLocal(), - creationDate: DateTime.parse(json["RogzitesIdopontja"]).toLocal(), + startDate: json.localDate("FeladasDatuma")!, + dueDate: json.localDate("HataridoDatuma")!, + creationDate: json.localDate("RogzitesIdopontja")!, isCreatedByTeacher: json["IsTanarRogzitette"], isDone: json["IsMegoldva"], canBeSubmitted: json["IsBeadhato"], - classGroup: UidObj.fromJson(json["OsztalyCsoport"]), + classGroup: json.uid("OsztalyCsoport")!, canAttach: json["IsCsatolasEngedelyezes"], ); } diff --git a/kreta_api/lib/src/model/institution.dart b/kreta_api/lib/src/model/institution.dart index 5424854..0900d94 100644 --- a/kreta_api/lib/src/model/institution.dart +++ b/kreta_api/lib/src/model/institution.dart @@ -1,14 +1,15 @@ -class Institution { +import 'generic.dart'; + +class Institution extends UidObj { final CustomizationSettings customizationSettings; final String shortName; final List systemModuleList; - final String uid; Institution({ required this.customizationSettings, required this.shortName, required this.systemModuleList, - required this.uid, + required super.uid, }); factory Institution.fromJson(Map json) { diff --git a/kreta_api/lib/src/model/notice_board.dart b/kreta_api/lib/src/model/notice_board.dart index 52d9cc2..0fae62a 100644 --- a/kreta_api/lib/src/model/notice_board.dart +++ b/kreta_api/lib/src/model/notice_board.dart @@ -1,7 +1,7 @@ +import '../extensions.dart'; import 'generic.dart'; -class NoticeBoardItem { - final String uid; +class NoticeBoardItem extends UidObj { final String author; final DateTime validFrom; final DateTime validTo; @@ -10,7 +10,7 @@ class NoticeBoardItem { final String contentText; NoticeBoardItem({ - required this.uid, + required super.uid, required this.author, required this.validFrom, required this.validTo, @@ -23,8 +23,8 @@ class NoticeBoardItem { return NoticeBoardItem( uid: json['Uid'], author: json['RogzitoNeve'], - validFrom: DateTime.parse(json['ErvenyessegKezdete']), - validTo: DateTime.parse(json['ErvenyessegVege']), + validFrom: json.localDate('ErvenyessegKezdete')!, + validTo: json.localDate('ErvenyessegVege')!, title: json['Cim'], contentHTML: json['Tartalom'], contentText: json['TartalomText'], @@ -45,8 +45,7 @@ class NoticeBoardItem { } } -class InfoBoardItem { - final String uid; +class InfoBoardItem extends UidObj { final String title; final DateTime date; final String author; @@ -56,7 +55,7 @@ class InfoBoardItem { final NameUidDesc type; InfoBoardItem({ - required this.uid, + required super.uid, required this.title, required this.date, required this.author, @@ -70,12 +69,12 @@ class InfoBoardItem { return InfoBoardItem( uid: json['Uid'], title: json['Cim'], - date: DateTime.parse(json['Datum']), + date: json.localDate('Datum')!, author: json['KeszitoTanarNeve'], - createdAt: DateTime.parse(json['KeszitesDatuma']), + createdAt: json.localDate('KeszitesDatuma')!, contentText: json['Tartalom'], contentHTML: json['TartalomFormazott'], - type: NameUidDesc.fromJson(json['Tipus']), + type: json.nameUidDesc('Tipus')!, ); } diff --git a/kreta_api/lib/src/model/omission.dart b/kreta_api/lib/src/model/omission.dart index 9c354bc..b3ac732 100644 --- a/kreta_api/lib/src/model/omission.dart +++ b/kreta_api/lib/src/model/omission.dart @@ -1,8 +1,8 @@ +import '../extensions.dart'; import 'generic.dart'; import 'subject.dart'; -class Omission { - final String uid; +class Omission extends UidObj { final Subject subject; final Class? c; final DateTime date; @@ -16,7 +16,7 @@ class Omission { final UidObj? classGroup; Omission({ - required this.uid, + required super.uid, required this.subject, required this.c, required this.date, @@ -35,19 +35,15 @@ class Omission { uid: json['Uid'], subject: Subject.fromJson(json['Tantargy']), c: json['Osztaly'] != null ? Class.fromJson(json['Osztaly']) : null, - date: DateTime.parse(json['Datum']).toLocal(), + date: json.localDate('Datum')!, teacher: json['RogzitoTanarNeve'], - type: json['Tipus'] != null ? NameUidDesc.fromJson(json['Tipus']) : null, - mode: json['Mod'] != null ? NameUidDesc.fromJson(json['Mod']) : null, + type: json.nameUidDesc('Tipus'), + mode: json.nameUidDesc('Mod'), lateForMin: json['KesesPercben'], - createdAt: DateTime.parse(json['KeszitesDatuma']).toLocal(), + createdAt: json.localDate('KeszitesDatuma')!, state: json['IgazolasAllapota'], - proofType: json['IgazolasTipusa'] != null - ? NameUidDesc.fromJson(json['IgazolasTipusa']) - : null, - classGroup: json['OsztalyCsoport'] != null - ? UidObj.fromJson(json['OsztalyCsoport']) - : null, + proofType: json.nameUidDesc('IgazolasTipusa'), + classGroup: json.uid('OsztalyCsoport'), ); } @@ -79,8 +75,8 @@ class Class { factory Class.fromJson(Map json) { return Class( - start: DateTime.parse(json['KezdoDatum']).toLocal(), - end: DateTime.parse(json['VegDatum']).toLocal(), + start: json.localDate('KezdoDatum')!, + end: json.localDate('VegDatum')!, classNo: json['Oraszam'], ); } diff --git a/kreta_api/lib/src/model/subject.dart b/kreta_api/lib/src/model/subject.dart index 25060b4..dea12b9 100644 --- a/kreta_api/lib/src/model/subject.dart +++ b/kreta_api/lib/src/model/subject.dart @@ -1,15 +1,13 @@ import 'generic.dart'; -class Subject { - final String uid; - final String name; +class Subject extends NameUid { final NameUidDesc category; final int sortIndex; final String? teacherName; Subject({ - required this.uid, - required this.name, + required super.uid, + required super.name, required this.category, required this.sortIndex, this.teacherName, @@ -19,7 +17,7 @@ class Subject { return Subject( uid: json['Uid'], name: json['Nev'], - category: NameUidDesc.fromJson(json['Kategoria']), + category: json.nameUidDesc('Kategoria')!, sortIndex: json['SortIndex'], teacherName: json['alkalmazottNev'], ); diff --git a/kreta_api/lib/src/model/test.dart b/kreta_api/lib/src/model/test.dart index 9e685a5..f8319a4 100644 --- a/kreta_api/lib/src/model/test.dart +++ b/kreta_api/lib/src/model/test.dart @@ -1,8 +1,8 @@ +import '../extensions.dart'; import 'generic.dart'; import 'subject.dart'; -class Test { - final String uid; +class Test extends UidObj { final DateTime date; final DateTime reportDate; final String teacherName; @@ -14,7 +14,7 @@ class Test { final UidObj classGroup; Test({ - required this.uid, + required super.uid, required this.date, required this.reportDate, required this.teacherName, @@ -29,15 +29,15 @@ class Test { factory Test.fromJson(Map json) { return Test( uid: json['Uid'], - date: DateTime.parse(json['Datum']).toLocal(), - reportDate: DateTime.parse(json['BejelentesDatuma']).toLocal(), + date: json.localDate('Datum')!, + reportDate: json.localDate('BejelentesDatuma')!, teacherName: json['RogzitoTanarNeve'], lessonNumber: json['OrarendiOraOraszama'], subject: Subject.fromJson(json['Tantargy']), subjectName: json['TantargyNeve'], theme: json['Temaja'], - method: NameUidDesc.fromJson(json['Modja']), - classGroup: UidObj.fromJson(json['OsztalyCsoport']), + method: json.nameUidDesc('Modja')!, + classGroup: json.uid('OsztalyCsoport')!, ); } diff --git a/kreta_api/lib/src/model/timetable.dart b/kreta_api/lib/src/model/timetable.dart index 8fb5efc..6d4debe 100644 --- a/kreta_api/lib/src/model/timetable.dart +++ b/kreta_api/lib/src/model/timetable.dart @@ -1,12 +1,11 @@ +import '../extensions.dart'; import 'generic.dart'; import 'subject.dart'; -class Lesson { - final String uid; +class Lesson extends NameUid { final String date; final DateTime start; final DateTime end; - final String name; final int? lessonNumber; final int? lessonSeqNumber; final NameUid? classGroup; @@ -33,11 +32,11 @@ class Lesson { final DateTime lastModifiedAt; Lesson({ - required this.uid, + required super.uid, required this.date, required this.start, required this.end, - required this.name, + required super.name, this.lessonNumber, this.lessonSeqNumber, this.classGroup, @@ -74,25 +73,21 @@ class Lesson { return Lesson( uid: json['Uid'], date: json['Datum'], - start: DateTime.parse(json['KezdetIdopont']).toLocal(), - end: DateTime.parse(json['VegIdopont']).toLocal(), + start: json.localDate('KezdetIdopont')!, + end: json.localDate('VegIdopont')!, name: json['Nev'], lessonNumber: json['Oraszam'], lessonSeqNumber: json['OraEvesSorszama'], - classGroup: json['OsztalyCsoport'] != null - ? NameUid.fromJson(json['OsztalyCsoport']) - : null, + classGroup: json.nameUid('OsztalyCsoport'), teacher: json['TanarNeve'], subject: json['Tantargy'] != null ? Subject.fromJson(json['Tantargy']) : null, theme: json['Tema'], roomName: json['TeremNeve'], - type: NameUidDesc.fromJson(json['Tipus']), - studentPresence: json['TanuloJelenlet'] != null - ? NameUidDesc.fromJson(json['TanuloJelenlet']) - : null, - state: NameUidDesc.fromJson(json['Allapot']), + type: json.nameUidDesc('Tipus')!, + studentPresence: json.nameUidDesc('TanuloJelenlet'), + state: json.nameUidDesc('Allapot')!, substituteTeacher: json['HelyettesTanarNeve'], homeworkUid: json['HaziFeladatUid'], taskGroupUid: json['FeladatGroupUid'], @@ -108,8 +103,8 @@ class Lesson { json['DigitalisTamogatoEszkozTipusList'] != null ? List.from(json['DigitalisTamogatoEszkozTipusList']) : List.empty(), - createdAt: DateTime.parse(json['Letrehozas']).toLocal(), - lastModifiedAt: DateTime.parse(json['UtolsoModositas']).toLocal(), + createdAt: json.localDate('Letrehozas')!, + lastModifiedAt: json.localDate('UtolsoModositas')!, ); }