Skip to content

Commit 22cf06c

Browse files
committed
dio: Update each page to dio request and adjust body accordingly
1 parent 5f2d3db commit 22cf06c

File tree

10 files changed

+72
-98
lines changed

10 files changed

+72
-98
lines changed

lib/pages/Attendance.dart

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import 'package:flutter/material.dart';
2+
import 'package:mujslcm/session_manager.dart';
23
import 'redirects.dart';
3-
import 'dart:convert';
4-
import 'package:http/http.dart' as http;
54
import 'package:fl_chart/fl_chart.dart';
5+
import 'package:mujslcm/utils/util.dart';
66

77
class AttendancePage extends StatefulWidget {
8-
final String newCookies;
9-
10-
const AttendancePage({Key? key, required this.newCookies}) : super(key: key);
8+
const AttendancePage({super.key});
119

1210
@override
1311
_AttendancePageState createState() => _AttendancePageState();
@@ -19,30 +17,27 @@ class _AttendancePageState extends State<AttendancePage> {
1917
@override
2018
void initState() {
2119
super.initState();
22-
_attendanceData = fetchAttendance(widget.newCookies);
20+
_attendanceData = fetchAttendance(SessionManager.sessionCookie ?? "");
2321
}
2422

2523
Future<List<Map<String, dynamic>>?> fetchAttendance(String newCookies) async {
26-
final Map<String, String> headers = {
27-
"User-Agent":
28-
"Mozilla/5.0 (X11; Linux x86_64; rv:132.0) Gecko/20100101 Firefox/132.0",
24+
final Map<String, String> header = {
25+
...headers,
2926
"Cookie": newCookies,
3027
};
3128

3229
final Map<String, String> body = {"StudentCode": ""};
3330

3431
final attendanceUrl = Attendance;
3532

36-
final session = http.Client();
37-
final response = await session.post(Uri.parse(attendanceUrl),
38-
headers: headers, body: body);
33+
final response = await post(attendanceUrl, header, body);
3934

4035
if (response.statusCode != 200) {
4136
print('Failed to load attendance data');
4237
return null;
4338
}
4439

45-
final decoded = jsonDecode(response.body);
40+
final decoded = response.data;
4641
final List<dynamic> attendanceList = decoded["AttendanceSummaryList"];
4742

4843
List<Map<String, dynamic>> attendanceData = [];
@@ -68,7 +63,7 @@ class _AttendancePageState extends State<AttendancePage> {
6863
int? classesNeeded;
6964
if (newpercentage < 75) {
7065
classesNeeded =
71-
((0.75 * totalClasses - attendedClasses) / (1 - 0.75)).ceil();
66+
((0.75 * totalClasses - attendedClasses) / (1 - 0.75)).floor();
7267
statusMessage = "$classesNeeded more classes needed to reach 75%.";
7368
} else {
7469
statusMessage = "Good! Your attendance is above or equal to 75%.";
@@ -87,7 +82,6 @@ class _AttendancePageState extends State<AttendancePage> {
8782
});
8883
}
8984

90-
session.close();
9185
return attendanceData;
9286
}
9387

@@ -113,7 +107,7 @@ class _AttendancePageState extends State<AttendancePage> {
113107
child: Container(
114108
width: double.infinity,
115109
height: double.infinity,
116-
decoration: BoxDecoration(
110+
decoration: const BoxDecoration(
117111
color: Color(0xFF212121),
118112
borderRadius: const BorderRadius.only(
119113
topLeft: Radius.circular(40),

lib/pages/about.dart

Lines changed: 0 additions & 11 deletions
This file was deleted.

lib/pages/cgpa.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
import 'package:mujslcm/session_manager.dart';
2+
13
import 'redirects.dart';
24
import 'package:flutter/material.dart';
35
import 'package:fl_chart/fl_chart.dart';
46
import 'app_colors.dart';
57
import 'package:mujslcm/utils/util.dart';
68

79
class CGPA extends StatefulWidget {
8-
final String newCookies;
9-
const CGPA({super.key, required this.newCookies});
10+
const CGPA({super.key});
1011

1112
@override
1213
_CGPAState createState() => _CGPAState();
@@ -30,7 +31,7 @@ class _CGPAState extends State<CGPA> {
3031

3132
final Map<String, String> header = {
3233
...headers,
33-
"Cookie": widget.newCookies,
34+
"Cookie": SessionManager.sessionCookie ?? "",
3435
};
3536

3637
final Map<String, String> body = {

lib/pages/grades.dart

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import 'dart:convert';
21
import 'redirects.dart';
32
import 'package:flutter/material.dart';
4-
import 'package:http/http.dart' as http;
3+
import 'package:mujslcm/session_manager.dart';
4+
import 'package:mujslcm/utils/util.dart';
55

66
class Grades extends StatefulWidget {
7-
final String newCookies;
8-
9-
const Grades({super.key, required this.newCookies});
7+
const Grades({super.key});
108

119
@override
1210
_GradesState createState() => _GradesState();
@@ -27,10 +25,9 @@ class _GradesState extends State<Grades> {
2725

2826
final url = GradesURL;
2927

30-
final Map<String, String> headers = {
31-
"Cookie": widget.newCookies,
32-
"User-Agent":
33-
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
28+
final Map<String, String> header = {
29+
"Cookie": SessionManager.sessionCookie ?? "",
30+
...headers,
3431
};
3532

3633
final Map<String, String> body = {
@@ -39,14 +36,11 @@ class _GradesState extends State<Grades> {
3936
};
4037

4138
try {
42-
final session = http.Client();
43-
var response =
44-
await session.post(Uri.parse(url), headers: headers, body: body);
45-
session.close();
39+
var response = await post(url, header, body);
4640

4741
if (response.statusCode == 200) {
4842
setState(() {
49-
gradesData = jsonDecode(response.body);
43+
gradesData = response.data;
5044
isLoading = false;
5145
});
5246
} else {
@@ -89,7 +83,7 @@ class _GradesState extends State<Grades> {
8983
return Scaffold(
9084
backgroundColor: const Color(0xFF121316),
9185
appBar: AppBar(
92-
title: Text(
86+
title: const Text(
9387
"Grades",
9488
style: TextStyle(color: Colors.white),
9589
),

lib/pages/home_page.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ class _HomePageState extends State<HomePage> {
3636
void initState() {
3737
super.initState();
3838
_pages = [
39-
Information(newCookies: widget.newCookies),
39+
Information(),
4040
HomeScreen(name: widget.name, newCookies: widget.newCookies),
41-
Settings(newCookies: widget.newCookies),
41+
Settings(),
4242
];
4343
}
4444

@@ -202,7 +202,7 @@ class HomeScreen extends StatelessWidget {
202202
),
203203
),
204204
onPressed: () {
205-
_navigateTo(context, Timetable(newCookies: newCookies));
205+
_navigateTo(context, Timetable());
206206
},
207207
child: Stack(
208208
alignment: Alignment.center,
@@ -255,7 +255,7 @@ class HomeScreen extends StatelessWidget {
255255

256256
Widget _attendanceCard(BuildContext context, double screenWidth) {
257257
return InkWell(
258-
onTap: () => _navigateTo(context, AttendancePage(newCookies: newCookies)),
258+
onTap: () => _navigateTo(context, AttendancePage()),
259259
child: Container(
260260
height: screenWidth * 0.6,
261261
width: screenWidth * 0.9,
@@ -294,7 +294,7 @@ class HomeScreen extends StatelessWidget {
294294

295295
Widget _marksCard(BuildContext context, double screenWidth) {
296296
return InkWell(
297-
onTap: () => _navigateTo(context, Marks(newCookies: newCookies)),
297+
onTap: () => _navigateTo(context, Marks()),
298298
child: ClipRRect(
299299
// Ensures border radius applies to children too
300300
borderRadius: BorderRadius.circular(25),
@@ -340,7 +340,7 @@ class HomeScreen extends StatelessWidget {
340340

341341
Widget _cgpaCard(BuildContext context, double screenWidth) {
342342
return InkWell(
343-
onTap: () => _navigateTo(context, CGPA(newCookies: newCookies)),
343+
onTap: () => _navigateTo(context, CGPA()),
344344
child: Container(
345345
height: screenWidth * 0.6,
346346
width: screenWidth * 0.9,
@@ -382,7 +382,7 @@ class HomeScreen extends StatelessWidget {
382382

383383
Widget _gradesCard(BuildContext context, double screenWidth) {
384384
return InkWell(
385-
onTap: () => _navigateTo(context, Grades(newCookies: newCookies)),
385+
onTap: () => _navigateTo(context, Grades()),
386386
child: ClipRRect(
387387
borderRadius: BorderRadius.circular(25),
388388
child: Container(

lib/pages/information.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import 'package:mujslcm/utils/util.dart';
66
import 'package:mujslcm/session_manager.dart';
77

88
class Information extends StatefulWidget {
9-
final String newCookies;
10-
11-
const Information({Key? key, required this.newCookies}) : super(key: key);
9+
const Information({super.key});
1210

1311
@override
1412
State<Information> createState() => _InformationState();

lib/pages/login.dart

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,13 @@ class _MyLoginState extends State<MyLogin> {
662662
document.querySelector('input[name="__RequestVerificationToken"]');
663663
final token = tokenElement?.attributes['value'];
664664

665-
final response = await post(otpValidateURL, headers, payload);
665+
final response = await dio.post(otpValidateURL,
666+
data: payload,
667+
options: Options(
668+
headers: headers,
669+
contentType: Headers.formUrlEncodedContentType,
670+
validateStatus: (status) => status! < 500,
671+
));
666672

667673
final newpayload = {
668674
"__RequestVerificationToken": token ?? "",
@@ -726,11 +732,13 @@ class _MyLoginState extends State<MyLogin> {
726732
'Cookie': cleanedCookies,
727733
};
728734

729-
final loginResponse = await post(
730-
url,
731-
headersForLogin,
732-
payload,
733-
);
735+
final loginResponse = await dio.post(url,
736+
data: payload,
737+
options: Options(
738+
headers: headersForLogin,
739+
contentType: Headers.formUrlEncodedContentType,
740+
validateStatus: (status) => status! < 500,
741+
));
734742

735743
if (loginResponse.statusCode == 302) {
736744
final API = loginResponse.headers['set-cookie'];

lib/pages/marks.dart

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import 'dart:convert';
1+
import 'package:mujslcm/session_manager.dart';
2+
import 'package:mujslcm/utils/util.dart';
3+
24
import 'redirects.dart';
35
import 'package:flutter/material.dart';
4-
import 'package:http/http.dart' as http;
56
import 'package:fl_chart/fl_chart.dart';
67

78
class Marks extends StatefulWidget {
8-
final String newCookies;
9-
10-
const Marks({super.key, required this.newCookies});
9+
const Marks({super.key});
1110

1211
@override
1312
_MarksState createState() => _MarksState();
@@ -26,13 +25,9 @@ class _MarksState extends State<Marks> {
2625
isLoading = true;
2726
});
2827

29-
final url =
30-
"https://mujslcm.jaipur.manipal.edu/Student/Academic/GetInternalMarkForFaculty";
31-
32-
final Map<String, String> headers = {
33-
"Cookie": widget.newCookies,
34-
"User-Agent":
35-
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
28+
final Map<String, String> header = {
29+
"Cookie": SessionManager.sessionCookie ?? "",
30+
...headers,
3631
};
3732

3833
final Map<String, String> body = {
@@ -41,15 +36,12 @@ class _MarksState extends State<Marks> {
4136
};
4237

4338
try {
44-
final session = http.Client();
45-
var response =
46-
await session.post(Uri.parse(url), headers: headers, body: body);
47-
session.close();
39+
var response = await post(MarksURL, header, body);
4840

4941
if (response.statusCode == 200) {
5042
setState(() {
5143
marksData = List<Map<String, dynamic>>.from(
52-
jsonDecode(response.body)["InternalMarksList"]);
44+
response.data["InternalMarksList"]);
5345
isLoading = false;
5446
});
5547
} else {
@@ -74,7 +66,7 @@ class _MarksState extends State<Marks> {
7466
return Scaffold(
7567
backgroundColor: const Color(0xFF121316),
7668
appBar: AppBar(
77-
title: Text(
69+
title: const Text(
7870
"Marks Details",
7971
style: TextStyle(color: Colors.white),
8072
),

lib/pages/settings.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import 'package:flutter/material.dart';
22
import 'package:url_launcher/url_launcher.dart';
33

44
class Settings extends StatelessWidget {
5-
final String newCookies;
6-
7-
const Settings({super.key, required this.newCookies});
5+
const Settings({super.key});
86

97
void _launchURL(String url) async {
108
if (await canLaunchUrl(Uri.parse(url))) {

0 commit comments

Comments
 (0)