165 lines
5.2 KiB
Dart
165 lines
5.2 KiB
Dart
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'dart:typed_data';
|
|
import '../../data/models/teacher_models.dart';
|
|
import '../../data/repositories/teacher_repository.dart';
|
|
|
|
/**
|
|
* ==============================================================================
|
|
* SAQEL TEACHER STUDIO - STUDIO UPLOAD CUBIT
|
|
* ==============================================================================
|
|
*
|
|
* إدارة حالة استوديو رفع الحصص وفحص الجودة الإدراكي:
|
|
* - التحقق من معيار معهد ماساتشوستس (20 - 25 دقيقة)
|
|
* - استدعاء التدقيق الآلي للدرس ومخرجات المنهاج الوزاري.
|
|
*/
|
|
|
|
class TeacherStudioState {
|
|
final String lessonTitle;
|
|
final double durationMinutes;
|
|
final String gradeLevel;
|
|
final String? selectedFileName;
|
|
final double? selectedFileSizeMb;
|
|
final String? selectedFilePath;
|
|
final Uint8List? selectedFileBytes;
|
|
final bool isAuditing;
|
|
final TeacherLessonAuditModel? auditResult;
|
|
final bool isUploading;
|
|
final bool isUploaded;
|
|
final String? uploadSuccessMessage;
|
|
|
|
const TeacherStudioState({
|
|
required this.lessonTitle,
|
|
required this.durationMinutes,
|
|
this.gradeLevel = 'الصف العاشر الأساسي',
|
|
this.selectedFileName,
|
|
this.selectedFileSizeMb,
|
|
this.selectedFilePath,
|
|
this.selectedFileBytes,
|
|
required this.isAuditing,
|
|
this.auditResult,
|
|
this.isUploading = false,
|
|
this.isUploaded = false,
|
|
this.uploadSuccessMessage,
|
|
});
|
|
|
|
TeacherStudioState copyWith({
|
|
String? lessonTitle,
|
|
double? durationMinutes,
|
|
String? gradeLevel,
|
|
String? selectedFileName,
|
|
double? selectedFileSizeMb,
|
|
String? selectedFilePath,
|
|
Uint8List? selectedFileBytes,
|
|
bool? isAuditing,
|
|
TeacherLessonAuditModel? auditResult,
|
|
bool? isUploading,
|
|
bool? isUploaded,
|
|
String? uploadSuccessMessage,
|
|
}) {
|
|
return TeacherStudioState(
|
|
lessonTitle: lessonTitle ?? this.lessonTitle,
|
|
durationMinutes: durationMinutes ?? this.durationMinutes,
|
|
gradeLevel: gradeLevel ?? this.gradeLevel,
|
|
selectedFileName: selectedFileName ?? this.selectedFileName,
|
|
selectedFileSizeMb: selectedFileSizeMb ?? this.selectedFileSizeMb,
|
|
selectedFilePath: selectedFilePath ?? this.selectedFilePath,
|
|
selectedFileBytes: selectedFileBytes ?? this.selectedFileBytes,
|
|
isAuditing: isAuditing ?? this.isAuditing,
|
|
auditResult: auditResult ?? this.auditResult,
|
|
isUploading: isUploading ?? this.isUploading,
|
|
isUploaded: isUploaded ?? this.isUploaded,
|
|
uploadSuccessMessage: uploadSuccessMessage ?? this.uploadSuccessMessage,
|
|
);
|
|
}
|
|
}
|
|
|
|
class TeacherStudioCubit extends Cubit<TeacherStudioState> {
|
|
final TeacherRepository repository;
|
|
|
|
TeacherStudioCubit({required this.repository})
|
|
: super(const TeacherStudioState(
|
|
lessonTitle: 'شرح قاعدة لنتز والحث الكهرومغناطيسي — فيزياء 2008',
|
|
durationMinutes: 13.0,
|
|
isAuditing: false,
|
|
auditResult: null,
|
|
));
|
|
|
|
void updateLessonTitle(String title) {
|
|
emit(state.copyWith(lessonTitle: title));
|
|
}
|
|
|
|
void updateDuration(double duration) {
|
|
emit(state.copyWith(durationMinutes: duration));
|
|
}
|
|
|
|
void updateGradeLevel(String grade) {
|
|
emit(state.copyWith(gradeLevel: grade));
|
|
}
|
|
|
|
void selectVideoFile(
|
|
String fileName,
|
|
double sizeMb,
|
|
double durationMinutes, {
|
|
String? path,
|
|
Uint8List? bytes,
|
|
}) {
|
|
emit(state.copyWith(
|
|
selectedFileName: fileName,
|
|
selectedFileSizeMb: sizeMb,
|
|
selectedFilePath: path,
|
|
selectedFileBytes: bytes,
|
|
durationMinutes: durationMinutes,
|
|
isUploaded: false,
|
|
uploadSuccessMessage: null,
|
|
));
|
|
}
|
|
|
|
Future<void> runQualityGate() async {
|
|
emit(state.copyWith(isAuditing: true));
|
|
try {
|
|
final result = await repository.auditStudioVideo(
|
|
title: state.lessonTitle,
|
|
durationMinutes: state.durationMinutes,
|
|
subject: 'الفيزياء',
|
|
);
|
|
emit(state.copyWith(
|
|
isAuditing: false,
|
|
auditResult: result,
|
|
));
|
|
} catch (_) {
|
|
emit(state.copyWith(isAuditing: false));
|
|
}
|
|
}
|
|
|
|
Future<bool> uploadAndPublishLesson() async {
|
|
if (state.selectedFileName == null ||
|
|
(state.selectedFilePath == null && state.selectedFileBytes == null)) {
|
|
return false;
|
|
}
|
|
emit(state.copyWith(isUploading: true));
|
|
try {
|
|
final res = await repository.uploadLesson(
|
|
title: state.lessonTitle,
|
|
durationMinutes: state.durationMinutes,
|
|
gradeLevel: state.gradeLevel,
|
|
subject: 'الفيزياء والعلوم التطبيقية',
|
|
fileName: state.selectedFileName ?? 'lentz_law_physics_2008.mp4',
|
|
fileSizeMb: state.selectedFileSizeMb ?? 48.2,
|
|
filePath: state.selectedFilePath,
|
|
fileBytes: state.selectedFileBytes,
|
|
);
|
|
|
|
final msg = res['message']?.toString() ?? 'تم رفع ونشر الحصة بنجاح في المنهاج الوزاري وسوق صَقِل! 🚀';
|
|
emit(state.copyWith(
|
|
isUploading: false,
|
|
isUploaded: true,
|
|
uploadSuccessMessage: msg,
|
|
));
|
|
return true;
|
|
} catch (e) {
|
|
emit(state.copyWith(isUploading: false));
|
|
return false;
|
|
}
|
|
}
|
|
}
|