Skip to content

Commit

Permalink
Merge pull request #77 from NewtonMutugi/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
IamMuuo authored Jul 17, 2024
2 parents eefae61 + 80ed200 commit e466eaa
Show file tree
Hide file tree
Showing 8 changed files with 584 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/constants/tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ final List<Map<String, dynamic>> allTools = [
"name": "GPA Calculator",
"action": "Calculate GPA",
"image": "assets/images/calculator.png",
"ontap": () {},
"ontap": () {
Get.to(GpaCalculator());
},
"description": "Wanna calculate your GPA? try it here"
},
{
Expand Down
134 changes: 134 additions & 0 deletions lib/controllers/gpacalculator_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import 'package:academia/models/core/course/course_model.dart';
import 'package:academia/exports/barrel.dart';
import 'package:academia/tools/gpa_calculator/models/unit.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';

class GPACalculatorController extends GetxController {
RxList<Course> _registeredCourses = <Course>[].obs;
final RxList<Unit> _courses = <Unit>[].obs;

List<Unit> get courseList => _courses;

List<Course> get registeredCoursesList => _registeredCourses;
double get gpa => _calculateGPA();

set registeredCourses(List<Course> courses) {
_registeredCourses = courses.obs;
}

void addCourse(String name, String creditHours, String grade) {
double tcreditHours = 0;
try {
tcreditHours = double.parse(creditHours);
} catch (e) {
Get.defaultDialog(
title: "Incorrect credit Hours",
content: const Text("Credit hours must be a number"));
throw Exception('Credit hours must be a number');
}
final course = Unit(name: name, creditHours: tcreditHours, grade: grade);
_courses.add(course);
}

void removeCourse(int index) {
_courses.removeAt(index);
_courses.refresh();
}

void updateCourse(
{required int index,
required String newName,
// required String newCode,
required String newCreditHours,
required String newGrade}) {
_courses[index].name = newName;
// _courses[index].code = newCode;
try {
_courses[index].creditHours = double.parse(newCreditHours);
} catch (e) {
Get.defaultDialog(
title: "Incorrect credit Hours",
content: const Text("Credit hours must be a number"));
throw Exception('Credit hours must be a number');
}
_courses[index].grade = newGrade;
_courses.refresh();
}

double _calculateGPA() {
double totalPoints = 0;
double totalCreditHours = 0;
for (var course in _courses) {
totalPoints += getGradePoints(course.grade) * (course.creditHours);
totalCreditHours += course.creditHours;
}
return (totalPoints / totalCreditHours).toPrecision(2);
}

static double getGradePoints(String grade) {
switch (grade) {
case 'A':
return 4.0;
case 'A-':
return 3.7;
case 'B+':
return 3.3;
case 'B':
return 3.0;
case 'B-':
return 2.7;
case 'C+':
return 2.3;
case 'C':
return 2.0;
case 'C-':
return 1.7;
case 'D+':
return 1.3;
case 'D':
return 1.0;
case 'D-':
return 0.7;
case 'E':
return 0.0;
case 'F':
return 0.0;
default:
return 0.0;
}
}

static bool testValidGrade(var grade) {
switch (grade) {
case 'A':
return true;
case 'A-':
return true;
case 'B+':
return true;
case 'B':
return true;
case 'B-':
return true;
case 'C+':
return true;
case 'C':
return true;
case 'C-':
return true;
case 'D+':
return true;
case 'D':
return true;
case 'D-':
return true;
case 'E':
return true;
case 'F':
return true;
default:
return false;
}
}
}
1 change: 1 addition & 0 deletions lib/pages/auth/login_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class _LoginPageState extends State<LoginPage> {
TextFormField(
controller: admnoEditingController,
maxLength: 7,
keyboardType: TextInputType.number,
inputFormatters: [
AdmissionNumberFormatter(),
],
Expand Down
25 changes: 25 additions & 0 deletions lib/pages/courses/courses_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,31 @@ class _CoursesPageState extends State<CoursesPage> {
},
icon: const Icon(Ionicons.information_circle_outline),
),
IconButton(
onPressed: () async {
final result = await coursesController.fetchUserCourses();
result.fold(
(l) => showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text("Oops!"),
content: Text(l),
actions: [
FilledButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text("Ooh, ok"),
)
],
),
), (r) {
setState(
() {},
);
});
},
icon: Icon(Ionicons.refresh)),
],
),
SliverPadding(
Expand Down
1 change: 1 addition & 0 deletions lib/tools/gpa_calculator/gpacalculator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'pages/gpacalculator_page.dart';
13 changes: 13 additions & 0 deletions lib/tools/gpa_calculator/models/unit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Unit {
String name;
// String code;
double creditHours;
String grade;

Unit({
required this.name,
// required this.code,
required this.creditHours,
required this.grade,
});
}
Loading

0 comments on commit e466eaa

Please sign in to comment.