Blame view

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