Blame view

lib/pages/section/bloc/section_bloc.dart 7.27 KB
d35a4e87   liangchengyou   feat:磨耳朵功能UI
1
  import 'package:flutter/cupertino.dart';
8988aa69   liangchengyou   feat:首页+课程列表数据获取
2
  import 'package:flutter/foundation.dart';
d35a4e87   liangchengyou   feat:磨耳朵功能UI
3
  import 'package:flutter_bloc/flutter_bloc.dart';
2187c85f   吴启风   feat:课程结构调整
4
  import 'package:wow_english/common/request/dao/lesson_dao.dart';
13e6d11d   liangchengyou   feat:首页课程模块接口
5
  import 'package:wow_english/common/request/exception.dart';
3c1d5c64   liangchengyou   feat:练习功能完成
6
7
  import 'package:wow_english/common/request/dao/listen_dao.dart';
  import 'package:wow_english/models/course_process_entity.dart';
13e6d11d   liangchengyou   feat:首页课程模块接口
8
  import 'package:wow_english/utils/loading.dart';
c61b3c1a   Key   feat: toast_util....
9
  import 'package:wow_english/utils/toast_util.dart';
60e47f7c   liangchengyou   feat:课程选择功能
10
  
2187c85f   吴启风   feat:课程结构调整
11
12
  import '../../../models/course_section_entity.dart';
  import '../../../models/course_unit_entity.dart';
3ba925a9   吴启风   feat:环节页增加翻页切换单元效果
13
  import '../../../utils/list_ext.dart';
60e47f7c   liangchengyou   feat:课程选择功能
14
  
2187c85f   吴启风   feat:课程结构调整
15
16
  part 'section_event.dart';
  part 'section_state.dart';
8988aa69   liangchengyou   feat:首页+课程列表数据获取
17
  
2187c85f   吴启风   feat:课程结构调整
18
  class SectionBloc extends Bloc<SectionEvent, SectionState> {
c61b3c1a   Key   feat: toast_util....
19
  
3ba925a9   吴启风   feat:环节页增加翻页切换单元效果
20
21
22
23
24
25
26
27
28
29
30
31
32
    PageController _pageController;
  
    PageController get pageController => _pageController;
  
    ///当前页索引
    int _currentPage = 0;
  
    int get currentPage => _currentPage;
  
    ScrollController _listController;
  
    ScrollController get listController => _listController;
  
2187c85f   吴启风   feat:课程结构调整
33
34
35
36
    CourseUnitEntity _courseUnitEntity;
  
    CourseUnitEntity get courseUnitEntity => _courseUnitEntity;
  
66a7e3e7   吴启风   feat:退出课堂和结束课堂优化
37
38
    ///courseUnitId与课程环节列表的映射
    final Map<int, List<CourseSectionEntity>?> _courseSectionDatasMap = {};
2187c85f   吴启风   feat:课程结构调整
39
  
66a7e3e7   吴启风   feat:退出课堂和结束课堂优化
40
    Map<int, List<CourseSectionEntity>?> get courseSectionDatasMap => _courseSectionDatasMap;
8988aa69   liangchengyou   feat:首页+课程列表数据获取
41
  
3c1d5c64   liangchengyou   feat:练习功能完成
42
43
44
45
    CourseProcessEntity? _processEntity;
  
    CourseProcessEntity? get processEntity => _processEntity;
  
66a7e3e7   吴启风   feat:退出课堂和结束课堂优化
46
    SectionBloc(this._courseUnitEntity, this._currentPage,
22f36232   吴启风   feat:过渡页-练习环节
47
48
        this._pageController, this._listController)
        : super(LessonInitial()) {
66a7e3e7   吴启风   feat:退出课堂和结束课堂优化
49
      on<RequestDataEvent>(_requestSectionsData);
46675a89   吴启风   feat:过渡页-视频环节
50
      on<RequestEndClassEvent>(_requestEndClass);
934e2b47   liangchengyou   feat:权限调整+课程进度接口对接
51
      on<RequestEnterClassEvent>(_requestEnterClass);
3c1d5c64   liangchengyou   feat:练习功能完成
52
      on<RequestVideoLessonEvent>(_requestVideoLesson);
3ba925a9   吴启风   feat:环节页增加翻页切换单元效果
53
      on<CurrentUnitIndexChangeEvent>(_pageControllerChange);
60e47f7c   liangchengyou   feat:课程选择功能
54
    }
13e6d11d   liangchengyou   feat:首页课程模块接口
55
  
66a7e3e7   吴启风   feat:退出课堂和结束课堂优化
56
    void _requestSectionsData(
22f36232   吴启风   feat:过渡页-练习环节
57
        RequestDataEvent event, Emitter<SectionState> emitter) async {
993c1a04   liangchengyou   feat:添加数据模型
58
59
      try {
        await loading(() async {
66a7e3e7   吴启风   feat:退出课堂和结束课堂优化
60
61
          _courseSectionDatasMap[event.courseUnitId] =
              await LessonDao.courseSection(courseUnitId: event.courseUnitId);
2187c85f   吴启风   feat:课程结构调整
62
          emitter(LessonDataLoadState());
993c1a04   liangchengyou   feat:添加数据模型
63
64
65
        });
      } catch (e) {
        if (e is ApiException) {
c61b3c1a   Key   feat: toast_util....
66
          showToast(e.message.toString());
13e6d11d   liangchengyou   feat:首页课程模块接口
67
68
69
        }
      }
    }
3c1d5c64   liangchengyou   feat:练习功能完成
70
  
22f36232   吴启风   feat:过渡页-练习环节
71
72
    void _requestVideoLesson(
        RequestVideoLessonEvent event, Emitter<SectionState> emitter) async {
3c1d5c64   liangchengyou   feat:练习功能完成
73
74
75
      try {
        await loading(() async {
          _processEntity = await ListenDao.process(event.courseLessonId);
22f36232   吴启风   feat:过渡页-练习环节
76
77
          emitter(
              RequestVideoLessonState(event.courseLessonId, event.courseType));
3c1d5c64   liangchengyou   feat:练习功能完成
78
79
80
        });
      } catch (e) {
        if (e is ApiException) {
22f36232   吴启风   feat:过渡页-练习环节
81
          showToast(e.message ?? '请求失败,请检查网络连接');
3c1d5c64   liangchengyou   feat:练习功能完成
82
83
84
        }
      }
    }
934e2b47   liangchengyou   feat:权限调整+课程进度接口对接
85
  
22f36232   吴启风   feat:过渡页-练习环节
86
87
    void _requestEnterClass(
        RequestEnterClassEvent event, Emitter<SectionState> emitter) async {
934e2b47   liangchengyou   feat:权限调整+课程进度接口对接
88
89
      try {
        await loading(() async {
22f36232   吴启风   feat:过渡页-练习环节
90
91
          await ListenDao.enterClass(event.courseLessonId);
          emitter(RequestEnterClassState(event.courseLessonId, event.courseType));
934e2b47   liangchengyou   feat:权限调整+课程进度接口对接
92
93
94
        });
      } catch (e) {
        if (e is ApiException) {
22f36232   吴启风   feat:过渡页-练习环节
95
          showToast(e.message ?? '请求失败,请检查网络连接');
934e2b47   liangchengyou   feat:权限调整+课程进度接口对接
96
97
98
99
        }
      }
    }
  
22f36232   吴启风   feat:过渡页-练习环节
100
101
    void _requestEndClass(
        RequestEndClassEvent event, Emitter<SectionState> emitter) async {
66a7e3e7   吴启风   feat:退出课堂和结束课堂优化
102
103
104
      if (event.isCompleted) {
        await await ListenDao.endClass(event.courseLessonId,
            currentStep: event.currentStep,
22f36232   吴启风   feat:过渡页-练习环节
105
106
            currentTime: event.currentTime);
      } else {
66a7e3e7   吴启风   feat:退出课堂和结束课堂优化
107
108
        await await ListenDao.exitClass(event.courseLessonId,
            currentStep: event.currentStep,
22f36232   吴启风   feat:过渡页-练习环节
109
110
            currentTime: event.currentTime);
      }
46675a89   吴启风   feat:过渡页-视频环节
111
      if (event.autoNextSection) {
22f36232   吴启风   feat:过渡页-练习环节
112
        final nextCourseSection =
336074ab   吴启风   feat:过渡页-绘本环节跨单元处理
113
114
115
116
117
118
            await getNextCourseSection(int.parse(event.courseLessonId));
        if (nextCourseSection != null) {
          ///进入课堂
          add(RequestEnterClassEvent(nextCourseSection.id.toString(),
              nextCourseSection.courseType));
        }
46675a89   吴启风   feat:过渡页-视频环节
119
      }
934e2b47   liangchengyou   feat:权限调整+课程进度接口对接
120
    }
3ba925a9   吴启风   feat:环节页增加翻页切换单元效果
121
  
22f36232   吴启风   feat:过渡页-练习环节
122
123
    void _pageControllerChange(
        CurrentUnitIndexChangeEvent event, Emitter<SectionState> emitter) async {
3ba925a9   吴启风   feat:环节页增加翻页切换单元效果
124
125
126
127
      _currentPage = event.unitIndex;
      emitter(CurrentPageIndexState());
    }
  
336074ab   吴启风   feat:过渡页-绘本环节跨单元处理
128
    ///未锁定的页(单元)数
3ba925a9   吴启风   feat:环节页增加翻页切换单元效果
129
    int unlockPageCount() {
22f36232   吴启风   feat:过渡页-练习环节
130
131
132
      return _courseUnitEntity.courseUnitVOList
              ?.indexWhereOrNull((element) => element.lock == true) ??
          1;
3ba925a9   吴启风   feat:环节页增加翻页切换单元效果
133
    }
46675a89   吴启风   feat:过渡页-视频环节
134
  
336074ab   吴启风   feat:过渡页-绘本环节跨单元处理
135
    ///当前页的课程详情
66a7e3e7   吴启风   feat:退出课堂和结束课堂优化
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
    CourseUnitDetail getCourseUnitDetail({int? pageIndex}) {
      return _courseUnitEntity.courseUnitVOList![pageIndex ?? _currentPage];
    }
  
    ///根据courseLessonId查找对应的courseSection
    CourseSectionEntity? findCourseSectionById(int courseLessonId) {
      for (var entry in courseSectionDatasMap.entries) {
        var sectionList = entry.value;
        if (sectionList != null) {
          for (var section in sectionList) {
            if (section.id == courseLessonId) {
              return section;
            }
          }
        }
      }
      return null;
    }
  
    ///根据sortOrder查找对应的courseSection
    CourseSectionEntity? findCourseSectionBySort(int sortOrder) {
      for (var entry in courseSectionDatasMap.entries) {
        var sectionList = entry.value;
        if (sectionList != null) {
          for (var section in sectionList) {
            if (section.sortOrder == sortOrder) {
              return section;
            }
          }
        }
      }
      return null;
    }
  
    ///根据courseLessonId查找对应的CourseUnitDetail
    CourseUnitDetail? findCourseUnitDetailById(int courseLessonId) {
      final curCourseSectionEntity = findCourseSectionById(courseLessonId);
      if (curCourseSectionEntity != null) {
        final curCourseUnitDetail = _courseUnitEntity.courseUnitVOList?.firstWhere((element) =>
          element.id == curCourseSectionEntity.courseUnitId);
        return curCourseUnitDetail;
      }
      return null;
    }
  
336074ab   吴启风   feat:过渡页-绘本环节跨单元处理
181
182
    ///根据courseLessonId查找下一个courseSection
    Future<CourseSectionEntity?> getNextCourseSection(int courseLessonId) async {
66a7e3e7   吴启风   feat:退出课堂和结束课堂优化
183
184
185
      final curCourseSectionEntity = findCourseSectionById(courseLessonId);
      final curSectionSort = curCourseSectionEntity?.sortOrder ?? 0;
      final nextCourseSectionEntity = findCourseSectionBySort(curSectionSort + 1);
46675a89   吴启风   feat:过渡页-视频环节
186
187
188
189
      if (nextCourseSectionEntity != null) {
        return nextCourseSectionEntity;
      } else {
        ///跨unit选lesson
66a7e3e7   吴启风   feat:退出课堂和结束课堂优化
190
        final curCourseUnitDetail = findCourseUnitDetailById(courseLessonId);
46675a89   吴启风   feat:过渡页-视频环节
191
        if (curCourseUnitDetail != null) {
22f36232   吴启风   feat:过渡页-练习环节
192
          final nextCourseUnitDetail = _courseUnitEntity.courseUnitVOList
66a7e3e7   吴启风   feat:退出课堂和结束课堂优化
193
              ?.firstWhere((element) => element.sortOrder == (curCourseUnitDetail.sortOrder! + 1));
46675a89   吴启风   feat:过渡页-视频环节
194
          if (nextCourseUnitDetail != null) {
336074ab   吴启风   feat:过渡页-绘本环节跨单元处理
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
            final courseUnitId = nextCourseUnitDetail.id!;
            try {
              await loading(() async {
                _courseSectionDatasMap[courseUnitId] =
                await LessonDao.courseSection(courseUnitId: courseUnitId);
                emit(LessonDataLoadState());
              });
              _pageController.nextPage(
                duration: const Duration(milliseconds: 500),
                curve: Curves.ease,
              );
              return _courseSectionDatasMap[courseUnitId]!.first;
            } catch (e) {
              if (e is ApiException) {
                showToast(e.message.toString());
              }
              return null;
            }
46675a89   吴启风   feat:过渡页-视频环节
213
214
215
216
217
218
219
220
221
222
          } else {
            ///最后一个unit了
            return null;
          }
        } else {
          ///找不到对应的unitDetail,理论上不可能
          return null;
        }
      }
    }
60e47f7c   liangchengyou   feat:课程选择功能
223
  }