Blame view

lib/pages/section/bloc/section_bloc.dart 5.39 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
37
38
39
40
41
42
43
    CourseUnitEntity _courseUnitEntity;
  
    CourseUnitEntity get courseUnitEntity => _courseUnitEntity;
  
    CourseUnitDetail _courseUnitDetail;
  
    CourseUnitDetail get courseUnitDetail => _courseUnitDetail;
  
    List<CourseSectionEntity>? _courseSectionDatas;
  
    List<CourseSectionEntity>? get courseSectionDatas => _courseSectionDatas;
8988aa69   liangchengyou   feat:首页+课程列表数据获取
44
  
3c1d5c64   liangchengyou   feat:练习功能完成
45
46
47
48
    CourseProcessEntity? _processEntity;
  
    CourseProcessEntity? get processEntity => _processEntity;
  
22f36232   吴启风   feat:过渡页-练习环节
49
50
51
    SectionBloc(this._courseUnitEntity, this._courseUnitDetail,
        this._pageController, this._listController)
        : super(LessonInitial()) {
993c1a04   liangchengyou   feat:添加数据模型
52
      on<RequestDataEvent>(_requestData);
46675a89   吴启风   feat:过渡页-视频环节
53
      on<RequestEndClassEvent>(_requestEndClass);
934e2b47   liangchengyou   feat:权限调整+课程进度接口对接
54
      on<RequestEnterClassEvent>(_requestEnterClass);
3c1d5c64   liangchengyou   feat:练习功能完成
55
      on<RequestVideoLessonEvent>(_requestVideoLesson);
3ba925a9   吴启风   feat:环节页增加翻页切换单元效果
56
      on<CurrentUnitIndexChangeEvent>(_pageControllerChange);
60e47f7c   liangchengyou   feat:课程选择功能
57
    }
13e6d11d   liangchengyou   feat:首页课程模块接口
58
  
22f36232   吴启风   feat:过渡页-练习环节
59
60
    void _requestData(
        RequestDataEvent event, Emitter<SectionState> emitter) async {
993c1a04   liangchengyou   feat:添加数据模型
61
62
      try {
        await loading(() async {
22f36232   吴启风   feat:过渡页-练习环节
63
64
          _courseSectionDatas =
              await LessonDao.courseSection(courseUnitId: _courseUnitDetail.id!);
2187c85f   吴启风   feat:课程结构调整
65
          emitter(LessonDataLoadState());
993c1a04   liangchengyou   feat:添加数据模型
66
67
68
        });
      } catch (e) {
        if (e is ApiException) {
c61b3c1a   Key   feat: toast_util....
69
          showToast(e.message.toString());
13e6d11d   liangchengyou   feat:首页课程模块接口
70
71
72
        }
      }
    }
3c1d5c64   liangchengyou   feat:练习功能完成
73
  
22f36232   吴启风   feat:过渡页-练习环节
74
75
    void _requestVideoLesson(
        RequestVideoLessonEvent event, Emitter<SectionState> emitter) async {
3c1d5c64   liangchengyou   feat:练习功能完成
76
77
78
      try {
        await loading(() async {
          _processEntity = await ListenDao.process(event.courseLessonId);
22f36232   吴启风   feat:过渡页-练习环节
79
80
          emitter(
              RequestVideoLessonState(event.courseLessonId, event.courseType));
3c1d5c64   liangchengyou   feat:练习功能完成
81
82
83
        });
      } catch (e) {
        if (e is ApiException) {
22f36232   吴启风   feat:过渡页-练习环节
84
          showToast(e.message ?? '请求失败,请检查网络连接');
3c1d5c64   liangchengyou   feat:练习功能完成
85
86
87
        }
      }
    }
934e2b47   liangchengyou   feat:权限调整+课程进度接口对接
88
  
22f36232   吴启风   feat:过渡页-练习环节
89
90
    void _requestEnterClass(
        RequestEnterClassEvent event, Emitter<SectionState> emitter) async {
934e2b47   liangchengyou   feat:权限调整+课程进度接口对接
91
92
      try {
        await loading(() async {
22f36232   吴启风   feat:过渡页-练习环节
93
94
          await ListenDao.enterClass(event.courseLessonId);
          emitter(RequestEnterClassState(event.courseLessonId, event.courseType));
934e2b47   liangchengyou   feat:权限调整+课程进度接口对接
95
96
97
        });
      } catch (e) {
        if (e is ApiException) {
22f36232   吴启风   feat:过渡页-练习环节
98
          showToast(e.message ?? '请求失败,请检查网络连接');
934e2b47   liangchengyou   feat:权限调整+课程进度接口对接
99
100
101
102
        }
      }
    }
  
22f36232   吴启风   feat:过渡页-练习环节
103
104
105
106
107
108
109
110
111
112
    void _requestEndClass(
        RequestEndClassEvent event, Emitter<SectionState> emitter) async {
      if (event.isLastPage) {
        await await ListenDao.endClass(event.courseLessonId, event.currentStep,
            currentTime: event.currentTime);
      } else {
        await await ListenDao.exitClass(event.courseLessonId, event.currentStep,
            currentTime: event.currentTime);
      }
      debugPrint("WQF _requestEndClass autoNextSection=${event.autoNextSection}");
46675a89   吴启风   feat:过渡页-视频环节
113
      if (event.autoNextSection) {
22f36232   吴启风   feat:过渡页-练习环节
114
115
116
        final nextCourseSection =
            getNextCourseSectionBySort(int.parse(event.courseLessonId));
        debugPrint("WQF nextCourseSection = $nextCourseSection");
46675a89   吴启风   feat:过渡页-视频环节
117
        ///进入课堂
22f36232   吴启风   feat:过渡页-练习环节
118
119
        add(RequestEnterClassEvent(nextCourseSection!.id.toString(),
            nextCourseSection.courseType));
46675a89   吴启风   feat:过渡页-视频环节
120
      }
934e2b47   liangchengyou   feat:权限调整+课程进度接口对接
121
    }
3ba925a9   吴启风   feat:环节页增加翻页切换单元效果
122
  
22f36232   吴启风   feat:过渡页-练习环节
123
124
    void _pageControllerChange(
        CurrentUnitIndexChangeEvent event, Emitter<SectionState> emitter) async {
3ba925a9   吴启风   feat:环节页增加翻页切换单元效果
125
126
127
128
129
130
      _currentPage = event.unitIndex;
      emitter(CurrentPageIndexState());
    }
  
    // 未锁定的页数
    int unlockPageCount() {
22f36232   吴启风   feat:过渡页-练习环节
131
132
133
      return _courseUnitEntity.courseUnitVOList
              ?.indexWhereOrNull((element) => element.lock == true) ??
          1;
3ba925a9   吴启风   feat:环节页增加翻页切换单元效果
134
    }
46675a89   吴启风   feat:过渡页-视频环节
135
136
  
    CourseSectionEntity? getNextCourseSectionBySort(int courseLessonId) {
22f36232   吴启风   feat:过渡页-练习环节
137
138
      final curCourseSectionEntity = _courseSectionDatas
          ?.firstWhere((element) => element.id == courseLessonId);
46675a89   吴启风   feat:过渡页-视频环节
139
      final curSort = curCourseSectionEntity?.sortOrder ?? 0;
22f36232   吴启风   feat:过渡页-练习环节
140
141
      CourseSectionEntity? nextCourseSectionEntity = _courseSectionDatas
          ?.firstWhere((element) => element.sortOrder == curSort + 1);
46675a89   吴启风   feat:过渡页-视频环节
142
143
144
145
      if (nextCourseSectionEntity != null) {
        return nextCourseSectionEntity;
      } else {
        ///跨unit选lesson
22f36232   吴启风   feat:过渡页-练习环节
146
147
148
        final curCourseUnitDetail = _courseUnitEntity.courseUnitVOList
            ?.firstWhere(
                (element) => element.id == curCourseSectionEntity?.courseUnitId);
46675a89   吴启风   feat:过渡页-视频环节
149
        if (curCourseUnitDetail != null) {
22f36232   吴启风   feat:过渡页-练习环节
150
151
          final nextCourseUnitDetail = _courseUnitEntity.courseUnitVOList
              ?.firstWhere((element) => element.sortOrder == 0);
46675a89   吴启风   feat:过渡页-视频环节
152
153
154
155
156
157
158
159
160
161
162
163
164
          if (nextCourseUnitDetail != null) {
            ///pageView翻页了,可能需要预加载 todo
            return null;
          } else {
            ///最后一个unit了
            return null;
          }
        } else {
          ///找不到对应的unitDetail,理论上不可能
          return null;
        }
      }
    }
60e47f7c   liangchengyou   feat:课程选择功能
165
  }