home_bloc.dart
2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wow_english/common/request/dao/home_dao.dart';
import 'package:wow_english/common/request/exception.dart';
import 'package:wow_english/models/course_entity.dart';
import 'package:wow_english/common/request/dao/listen_dao.dart';
import 'package:wow_english/models/course_process_entity.dart';
import 'package:wow_english/utils/loading.dart';
import 'package:wow_english/utils/toast_util.dart';
part 'home_event.dart';
part 'home_state.dart';
class HomeBloc extends Bloc<HomeEvent, HomeState> {
final String? moduleId;
CourseEntity? _modelData;
CourseEntity? get modelData => _modelData;
CourseProcessEntity? _processEntity;
CourseProcessEntity? get processEntity => _processEntity;
HomeBloc(this.moduleId) : super(HomeInitial()) {
on<RequestDataEvent>(_requestData);
on<RequestExitClassEvent>(_requestExitClass);
on<RequestEnterClassEvent>(_requestEnterClass);
on<RequestVideoLessonEvent>(_requestVideoLesson);
}
void _requestData(RequestDataEvent event, Emitter<HomeState> emitter) async {
try {
await loading(() async {
_modelData = await HomeDao.courseLesson(moduleId: moduleId ?? '');
emitter(HomeDataLoadState());
});
} catch (e) {
if (e is ApiException) {
showToast(e.message.toString());
}
}
}
void _requestVideoLesson(RequestVideoLessonEvent event, Emitter<HomeState> emitter) async {
try {
await loading(() async {
_processEntity = await ListenDao.process(event.courseLessonId);
emitter(RequestVideoLessonState(event.courseLessonId,event.courseType));
});
} catch (e) {
if (e is ApiException) {
showToast(e.message??'请求失败,请检查网络连接');
}
}
}
void _requestEnterClass(RequestEnterClassEvent event,Emitter<HomeState> emitter) async {
try {
await loading(() async {
await ListenDao.enterClass(event.courseLessonId);
emitter(RequestEnterClassState(event.courseLessonId,event.courseType));
});
} catch (e) {
if (e is ApiException) {
showToast(e.message??'请求失败,请检查网络连接');
}
}
}
void _requestExitClass(RequestExitClassEvent event,Emitter<HomeState> emitter) async {
await ListenDao.exitClass(event.courseLessonId,event.currentStep,event.currentTime);
}
}