Aidra Connect 10.0.2+16
Aidra Connect Mobile Application
Loading...
Searching...
No Matches
elearning_v2_cubit.dart
Go to the documentation of this file.
1import 'package:flutter_bloc/flutter_bloc.dart';
2import 'package:equatable/equatable.dart';
3
4import '../../../domain/repository/elearning_v2_repository.dart';
5import '../../../domain/entities/course_entity.dart';
6import '../../../domain/entities/qcm_entity.dart';
7
8part 'elearning_v2_state.dart';
9
10class ElearningV2Cubit extends Cubit<ElearningV2State> {
12
13 ElearningV2Cubit(this._repository) : super(ElearningV2Initial());
14
15 Future<void> loadCourses() async {
16 try {
17 emit(LoadingCoursesState());
18
19 // Load courses from repository
20 final coursesResult = await _repository.getCourses();
21
22 coursesResult.fold(
23 (failure) => emit(CoursesLoadingFailureState(failure.message)),
25 } catch (e) {
26 emit(CoursesLoadingFailureState(e.toString()));
27 }
28 }
29
30 Future<void> loadCourseDetails(int courseId) async {
31 try {
32 emit(LoadingCourseDetailsState());
33
34 // Get course details
35 final courseResult = await _repository.getCourseDetails(courseId);
36 final pdfPathResult = await _repository.getPdfPathForCourse(courseId);
37 final qcmListResult = await _repository.getQcmForCourse(courseId);
38
39 // Check if all requests were successful
40 if (courseResult.isRight() &&
41 pdfPathResult.isRight() &&
42 qcmListResult.isRight()) {
43 final course = courseResult
44 .getOrElse(() => throw Exception("Failed to get course"));
45 final pdfPath = pdfPathResult
46 .getOrElse(() => throw Exception("Failed to get PDF path"));
47 final qcmList = qcmListResult
48 .getOrElse(() => throw Exception("Failed to get QCM list"));
49
51 } else {
52 // Handle failures
53 String errorMessage = "Failed to load course details";
54 if (courseResult.isLeft()) {
55 errorMessage =
56 courseResult.fold((l) => l.message, (r) => errorMessage);
57 } else if (pdfPathResult.isLeft()) {
58 errorMessage =
59 pdfPathResult.fold((l) => l.message, (r) => errorMessage);
60 } else if (qcmListResult.isLeft()) {
61 errorMessage =
62 qcmListResult.fold((l) => l.message, (r) => errorMessage);
63 }
64
65 emit(CourseDetailsLoadingFailureState(errorMessage));
66 }
67 } catch (e) {
68 emit(CourseDetailsLoadingFailureState(e.toString()));
69 }
70 }
71
72 Future<void> updateProgress(int courseId, double progress) async {
73 try {
74 // Update repository
75 final result = await _repository.updateCourseProgress(courseId, progress);
76
77 result.fold(
78 (failure) => print('Failed to update progress: ${failure.message}'),
79 (success) {
80 // If we're on the course details screen, update the state
81 if (state is CourseDetailsLoadedState) {
82 final currentState = state as CourseDetailsLoadedState;
83 final updatedCourse =
84 currentState.course.copyWith(progress: progress);
85
87 updatedCourse,
88 currentState.pdfPath,
89 currentState.qcmList,
90 ));
91 }
92
93 // Also update the courses list if we're on that screen
94 if (state is CoursesLoadedState) {
95 final currentState = state as CoursesLoadedState;
96 final updatedCourses = currentState.courses.map((course) {
97 if (course.id == courseId) {
98 return course.copyWith(progress: progress);
99 }
100 return course;
101 }).toList();
102
103 emit(CoursesLoadedState(updatedCourses));
104 }
105 });
106 } catch (e) {
107 // Handle error (could emit a failure state if needed)
108 print('Failed to update progress: $e');
109 }
110 }
111}
Future< void > updateProgress(int courseId, double progress) async
Future< void > loadCourseDetails(int courseId) async
final ElearningV2Repository _repository
ElearningV2Cubit(this._repository) Future< void > loadCourses() async
sealed class DocumentState extends Equatable failure
final String pdfPath
class CoursesLoadingFailureState extends ElearningV2State course
abstract class ElearningV2State extends Equatable courses
final List< QcmEntity > qcmList
const CourseDetailsLoadedState(this.course, this.pdfPath, this.qcmList)
const CoursesLoadedState(this.courses)
class GetPdfPathUseCase implements UseCase< String, PdfParams > courseId
final double progress