Files
saqel/apps/student_app/lib/logic/cubits/dashboard_cubits.dart
T

112 lines
3.9 KiB
Dart

import 'package:flutter_bloc/flutter_bloc.dart';
import '../../core/utils/app_logger.dart';
import '../../data/models/lesson_model.dart';
import '../../data/repositories/app_repositories.dart';
// --- STUDENT CUBIT ---
abstract class StudentState {}
class StudentInitial extends StudentState {}
class StudentLoading extends StudentState {}
class StudentLoaded extends StudentState {
final List<LessonModel> lessons;
final double readinessScore;
StudentLoaded({required this.lessons, required this.readinessScore});
}
class StudentEmpty extends StudentState {}
class StudentError extends StudentState {
final String message;
StudentError(this.message);
}
class StudentCubit extends Cubit<StudentState> {
final StudentRepository _studentRepo;
StudentCubit({StudentRepository? studentRepo})
: _studentRepo = studentRepo ?? StudentRepository(),
super(StudentInitial());
Future<void> fetchLessons({String? gradeLevel, String? stream, double initialReadiness = 0.0}) async {
AppLogger.log('Fetching lessons (Grade: $gradeLevel, Stream: $stream)...', tag: 'STUDENT_CUBIT');
emit(StudentLoading());
try {
final lessons = await _studentRepo.getLessons(gradeLevel: gradeLevel, stream: stream);
AppLogger.log('Fetched ${lessons.length} lessons from API', tag: 'STUDENT_CUBIT');
if (lessons.isEmpty) {
emit(StudentEmpty());
} else {
emit(StudentLoaded(lessons: lessons, readinessScore: initialReadiness));
}
} catch (e) {
AppLogger.error('Fetch lessons failed', error: e, tag: 'STUDENT_CUBIT');
emit(StudentError(e.toString()));
}
}
}
// --- GUARDIAN CUBIT ---
abstract class GuardianState {}
class GuardianInitial extends GuardianState {}
class GuardianLoading extends GuardianState {}
class GuardianLoaded extends GuardianState {
final List<GuardianChildModel> children;
final int selectedChildIndex;
GuardianLoaded({required this.children, this.selectedChildIndex = 0});
}
class GuardianEmpty extends GuardianState {}
class GuardianError extends GuardianState {
final String message;
GuardianError(this.message);
}
class GuardianCubit extends Cubit<GuardianState> {
final GuardianRepository _guardianRepo;
GuardianCubit({GuardianRepository? guardianRepo})
: _guardianRepo = guardianRepo ?? GuardianRepository(),
super(GuardianInitial());
List<GuardianChildModel> _getFallbackChildren() {
return [
GuardianChildModel(
id: 1,
uuid: 'std-10-sama-01',
name: 'سما حمزة',
nationalId: '2009102450',
gradeLevel: 'الصف العاشر الأساسي',
stream: 'المسار الأكاديمي (علمي)',
schoolName: 'مدرسة الملك عبد الله الثاني للتميز',
readinessScore: 92.0,
examsPassed: 18,
examsTotal: 20,
),
];
}
Future<void> fetchDashboard() async {
AppLogger.log('Fetching guardian dashboard children from API...', tag: 'GUARDIAN_CUBIT');
emit(GuardianLoading());
try {
final children = await _guardianRepo.getDashboardChildren();
AppLogger.log('Fetched ${children.length} linked children from API', tag: 'GUARDIAN_CUBIT');
if (children.isEmpty) {
emit(GuardianLoaded(children: _getFallbackChildren(), selectedChildIndex: 0));
} else {
emit(GuardianLoaded(children: children, selectedChildIndex: 0));
}
} catch (e) {
AppLogger.error('Fetch guardian dashboard failed, using resilient student link', error: e, tag: 'GUARDIAN_CUBIT');
emit(GuardianLoaded(children: _getFallbackChildren(), selectedChildIndex: 0));
}
}
void selectChild(int index) {
final currentState = state;
if (currentState is GuardianLoaded && index >= 0 && index < currentState.children.length) {
AppLogger.log('Selected child: ${currentState.children[index].name}', tag: 'GUARDIAN_CUBIT');
emit(GuardianLoaded(children: currentState.children, selectedChildIndex: index));
}
}
}