Skip to content

Commit

Permalink
Merge pull request #42 from 98021Varundeep/feature/homepage-loading-i…
Browse files Browse the repository at this point in the history
…con-#41

feat: Add loading animation while fetching course data
  • Loading branch information
angrezichatterbox authored Nov 11, 2024
2 parents 8a4a3ea + 2193104 commit 02cd38b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 20 deletions.
57 changes: 38 additions & 19 deletions lib/controllers/homepage/course_summary_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,62 @@ import 'package:bunk_mate/screens/OnBoardView.dart';

class CourseSummaryController extends GetxController {
var courseSummary = <CourseSummary>[].obs;
var isLoading = true.obs;
final storage = const FlutterSecureStorage();

@override
void onInit() {
super.onInit();
fetchCourseSummary();
}

Future<String?> getToken() async {
return await storage.read(key: 'token');
}

Future<void> fetchCourseSummary() async {
final token = await getToken();
if (token == null) {
Get.snackbar('Error', 'No token found.');
return;
}
var headers = {
HttpHeaders.authorizationHeader: "Token $token",
};

try {
isLoading.value = true;
final token = await getToken();

if (token == null) {
Get.snackbar('Error', 'No token found.');
return;
}

var headers = {
HttpHeaders.authorizationHeader: "Token $token",
};

var url = Uri.parse(
ApiEndPoints.baseUrl + ApiEndPoints.homeEndPoints.courseSummary);

http.Response response = await http.get(url, headers: headers);

print(response.statusCode);

if (response.statusCode == 200) {
List<dynamic> jsonData = json.decode(response.body);
var fetchedSummary =
jsonData.map((data) => CourseSummary.fromJson(data)).toList();
print(response.body);
print(jsonData[0]);
List<dynamic> jsonData = json.decode(response.body);
var fetchedSummary =
jsonData.map((data) => CourseSummary.fromJson(data)).toList();
print(response.body);
print(jsonData[0]);

courseSummary.assignAll(fetchedSummary);
print("Task Done");
}
courseSummary.assignAll(fetchedSummary);
print("Task Done");
}
else if (response.statusCode == 404) {
Get.offAll(const TimetableView());
}
} catch (error) {
print(error);
print('Error fetching course summary: $error');
Get.snackbar(
'Error',
'Failed to fetch course data. Please try again.',
snackPosition: SnackPosition.BOTTOM,
);
} finally {
isLoading.value = false;
}
}
}
}
11 changes: 10 additions & 1 deletion lib/screens/homepage/homepage_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:bunk_mate/controllers/homepage/course_summary_controller.dart';
import 'package:bunk_mate/controllers/auth/login_controller.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

class HomePage extends StatefulWidget {
const HomePage({super.key});
Expand Down Expand Up @@ -56,6 +57,14 @@ class HomePageState extends State<HomePage> {
padding: const EdgeInsets.symmetric(horizontal: 20.0),
child: SafeArea(
child: Obx(() {
if (courseSummaryController.isLoading.value) {
return Center(
child: SpinKitPulse(
color: accentColor,
size: 50.0,
),
);
}
if (courseSummaryController.courseSummary.isEmpty) {
return _buildEmptyState();
}
Expand Down Expand Up @@ -255,4 +264,4 @@ class HomePageState extends State<HomePage> {
await _handleLogout();
}
}
}
}
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.1.2"
flutter_spinkit:
dependency: "direct main"
description:
name: flutter_spinkit
sha256: d2696eed13732831414595b98863260e33e8882fc069ee80ec35d4ac9ddb0472
url: "https://pub.dev"
source: hosted
version: "5.2.1"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies:
syncfusion_flutter_charts: 21.1.39
# date_field: ^5.2.1
tenor: ^1.0.9
flutter_spinkit: ^5.2.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 02cd38b

Please sign in to comment.