Skip to content

Commit

Permalink
✨ List of session now generated on runtime
Browse files Browse the repository at this point in the history
No need to hardcoded the session manually and publish a new update everytime
  • Loading branch information
iqfareez committed Sep 17, 2023
1 parent 9fa40da commit 6e5156f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 26 deletions.
9 changes: 1 addition & 8 deletions lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@ import 'dart:io';

import 'package:flutter/foundation.dart';

/// Sessions (2-3 entries, I think 3 is enough)
/// Eg: current semester & upcoming semester
/// or: previous semester & current semester
/// https://albiruni.iium.edu.my/myapps/StudentOnline/schedule1.php
const List<String> kSessions = ['2022/2023', '2023/2024'];

/// default session & semester (current academic seesion)
/// Values must exist in [kSessions]
/// default session/current academic seesion
const String kDefaultSession = '2023/2024';

/// Values must be between 1 and 3 (inclusive)
Expand Down
14 changes: 14 additions & 0 deletions lib/util/academic_session.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class AcademicSession {
/// Returns a list of academic sessions. Replace previous implementation
/// using hardcoded constants [kSessions]
///
/// Eg: ['2023/2024', '2024/2025']
static List<String> get session {
var yearNow = DateTime.now().year;

return [
'${yearNow - 1}/$yearNow',
'$yearNow/${yearNow + 1}',
];
}
}
31 changes: 22 additions & 9 deletions lib/views/course browser/browser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../../constants.dart' as constants;
import '../../util/academic_session.dart';
import '../../util/kulliyyahs.dart';
import '../favourites_page.dart';
import 'browser_view.dart';
Expand All @@ -17,11 +18,23 @@ class Browser extends StatefulWidget {
class _BrowserState extends State<Browser> {
final GlobalKey dropdownKey = GlobalKey();
final TextEditingController _searchController = TextEditingController();
String _session = constants.kDefaultSession;
int _semester = constants.kDefaultSemester;
late String _selectedSession;
int _selectedSemester = constants.kDefaultSemester;
String? _selectedKulliyah;
StudyGrad _selectedStudyGrad = StudyGrad.ug;

@override
void initState() {
super.initState();
// check if [constants.kDefaultSession] is in the list of sessions
// if not, set the first session in the list as the default session
if (!AcademicSession.session.contains(constants.kDefaultSession)) {
_selectedSession = AcademicSession.session.first;
} else {
_selectedSession = constants.kDefaultSession;
}
}

@override
Widget build(BuildContext context) {
return GestureDetector(
Expand Down Expand Up @@ -66,26 +79,26 @@ class _BrowserState extends State<Browser> {
MouseRegion(
cursor: SystemMouseCursors.click,
child: CupertinoSegmentedControl(
groupValue: _session,
groupValue: _selectedSession,
children: {
for (var session in constants.kSessions)
for (var session in AcademicSession.session)
session: Text(session)
},
onValueChanged: (String value) {
setState(() => _session = value);
setState(() => _selectedSession = value);
}),
),
const SizedBox(height: 10),
MouseRegion(
cursor: SystemMouseCursors.click,
child: CupertinoSegmentedControl(
groupValue: _semester - 1,
groupValue: _selectedSemester - 1,
children: List.generate(
3,
(index) => Text("Sem ${index + 1}"),
).asMap(),
onValueChanged: (int value) {
setState(() => _semester = value + 1);
setState(() => _selectedSemester = value + 1);
}),
),
const SizedBox(height: 10),
Expand Down Expand Up @@ -162,8 +175,8 @@ class _BrowserState extends State<Browser> {
}

Albiruni albiruni = Albiruni(
semester: _semester,
session: _session,
semester: _selectedSemester,
session: _selectedSession,
studyGrade: _selectedStudyGrad);
Navigator.push(
context,
Expand Down
32 changes: 23 additions & 9 deletions lib/views/scheduler/input_scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:provider/provider.dart';

import '../../constants.dart' as constants;
import '../../providers/schedule_maker_provider.dart';
import '../../util/academic_session.dart';
import '../../util/kulliyyahs.dart';
import 'schedule_steps.dart';

Expand All @@ -18,11 +19,24 @@ class InputScope extends StatefulWidget {
class _InputScopeState extends State<InputScope>
with AutomaticKeepAliveClientMixin<InputScope> {
final GlobalKey dropdownKey = GlobalKey();
String _session = constants.kDefaultSession;
int _semester = constants.kDefaultSemester;
late String _selectedSession;
int _selectedSemester = constants.kDefaultSemester;
String? _selectedKulliyah;
StudyGrad _selectedStudyGrad = StudyGrad.ug;

@override
void initState() {
super.initState();

// check if [constants.kDefaultSession] is in the list of sessions
// if not, set the first session in the list as the default session
if (!AcademicSession.session.contains(constants.kDefaultSession)) {
_selectedSession = AcademicSession.session.first;
} else {
_selectedSession = constants.kDefaultSession;
}
}

@override
Widget build(BuildContext context) {
super.build(context);
Expand Down Expand Up @@ -71,13 +85,13 @@ class _InputScopeState extends State<InputScope>
Theme.of(context).colorScheme.background,
borderColor:
Theme.of(context).colorScheme.secondaryContainer,
groupValue: _session,
groupValue: _selectedSession,
children: {
for (var session in constants.kSessions)
for (var session in AcademicSession.session)
session: Text(session)
},
onValueChanged: (String value) {
setState(() => _session = value);
setState(() => _selectedSession = value);
}),
),
const SizedBox(height: 10),
Expand All @@ -88,13 +102,13 @@ class _InputScopeState extends State<InputScope>
Theme.of(context).colorScheme.background,
borderColor:
Theme.of(context).colorScheme.secondaryContainer,
groupValue: _semester - 1,
groupValue: _selectedSemester - 1,
children: List.generate(
3,
(index) => Text("Sem ${index + 1}"),
).asMap(),
onValueChanged: (int value) {
setState(() => _semester = value + 1);
setState(() => _selectedSemester = value + 1);
}),
),
const SizedBox(height: 10),
Expand Down Expand Up @@ -152,8 +166,8 @@ class _InputScopeState extends State<InputScope>
curve: Curves.bounceInOut);

Albiruni albiruni = Albiruni(
semester: _semester,
session: _session,
semester: _selectedSemester,
session: _selectedSession,
studyGrade: _selectedStudyGrad);

Provider.of<ScheduleMakerProvider>(context,
Expand Down

0 comments on commit 6e5156f

Please sign in to comment.