Files
firka/firka_common/lib/ui/components/grade_helpers.dart
Armand 32936c2aa5 firka: extract firka_common package with shared widgets (Isar kept separate)
- Create firka_common package with core helpers (debug, json, icon), theme,
  and shared widgets (FirkaCard, FirkaShadow, GradeWidget, GradeSmallCard,
  ClassIconWidget, FirkaIconWidget, DelayedSpinnerWidget, CounterDigitWidget)
- Keep Isar models (GenericCacheModel, TimetableCacheModel, HomeworkCacheModel,
  DatedCacheEntry, util) in firka and firka_wear - not moved to firka_common
- Update firka and firka_wear to depend on firka_common for shared UI only
- Add configurable roundGrade thresholds for firka settings
- Add package param to FirkaIconWidget for app asset paths
2026-03-03 15:33:11 +01:00

101 lines
2.0 KiB
Dart

import 'dart:ui';
import 'package:firka_common/ui/theme/style.dart';
import 'package:kreta_api/kreta_api.dart';
int roundGrade(
double grade, {
double t1 = 1,
double t2 = 0.5,
double t3 = 0.5,
double t4 = 0.5,
}) {
if (grade < 1 + t1) {
return 1;
}
if (grade < 2 + t2) {
return 2;
}
if (grade < 3 + t3) {
return 3;
}
if (grade < 4 + t4) {
return 4;
}
return 5;
}
int percentageToGrade(int grade) {
if (grade < 50) {
return 1;
}
if (grade < 60) {
return 2;
}
if (grade < 70) {
return 3;
}
if (grade < 80) {
return 4;
}
return 5;
}
Color getGradeColor(
double grade, {
double t1 = 1,
double t2 = 0.5,
double t3 = 0.5,
double t4 = 0.5,
}) {
switch (roundGrade(grade, t1: t1, t2: t2, t3: t3, t4: t4)) {
case 2:
return appStyle.colors.grade2;
case 3:
return appStyle.colors.grade3;
case 4:
return appStyle.colors.grade4;
case 5:
return appStyle.colors.grade5;
default:
return appStyle.colors.grade1;
}
}
(int total, List<int> countsByGrade) getGradeDistribution(List<Grade> grades) {
final filtered = grades
.where((g) => g.type.name != "felevi_jegy_ertekeles")
.toList();
final counts = [0, 0, 0, 0, 0];
for (final g in filtered) {
if (g.numericValue == null) continue;
final value = g.valueType.name == "Szazalekos"
? percentageToGrade(g.numericValue!.round())
: g.numericValue!.round().clamp(1, 5);
counts[value - 1]++;
}
return (filtered.length, counts);
}
extension GradeListExtension on List<Grade> {
double getAverageBySubject(Subject subject) {
var weightTotal = 0.00;
var sum = 0.00;
for (var grade in this) {
if (grade.subject.uid == subject.uid) {
if (grade.numericValue != null) {
var weight = (grade.weightPercentage ?? 100) / 100.0;
weightTotal += weight;
sum += grade.numericValue! * weight;
}
}
}
return sum / weightTotal;
}
}