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

95 lines
3.3 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());
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(GuardianEmpty());
} else {
emit(GuardianLoaded(children: children, selectedChildIndex: 0));
}
} catch (e) {
AppLogger.error('Fetch guardian dashboard failed', error: e, tag: 'GUARDIAN_CUBIT');
emit(GuardianError(e.toString()));
}
}
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));
}
}
}