joinClass method

Future<void> joinClass()

Implementation

Future<void> joinClass() async {
  TCICLog.info(
    "joinClass: joinClass start",
    actionModule: ActionModule.tcicController.name,
    actionName: ActionName.joinClass.name,
  );
  final smallWindowMode = isInSmallWindowMode();
  if (smallWindowMode) {
    TCICLog.info(
      "joinClass: smallWindowMode is true",
      actionModule: ActionModule.tcicController.name,
      actionName: ActionName.joinClass.name,
    );
    _eventbus.fire(MainEvent(type: EventTypeEnum.joinClassSuccess));
    return;
  }
  if (isGroupRoom() &&
      isStudent() &&
      (_config.groupLiveCode == null || _config.groupLiveCode!.isEmpty)) {
    _eventbus.fire(MainEvent(type: EventTypeEnum.joinClassFailed));
    throw StateError('groupLiveCode is required for student in group room');
  }
  var body = JoinClassBody(
    classId: _config.classId,
    role: getRole().index,
    groupLiveCode: isGroupRoom() ? _config.groupLiveCode : null,
  );
  var data = await networkService.joinClass(body);
  if (data != null) {
    if (data.errorCode == 0) {
      joinClassSuccess = true;
      joinClassTimeStamp = data.timestamp * 1000;
      _membersInfoObs.updateJoinClassTime(data.nanoTimestamp);
      _liveSteamInfoObs.updateLiveStreamInfo(data.streams);
      _stateDataObs.updateIsJoinClassSuccess(true);
      _membersInfoObs.updateIsSelfOnline(true, "joinClassSuccess");
      _membersInfoObs.updateOnlineStudentCount(data.studentOnlineNumber);
      final Map<String, Member> formatedMembers = {};
      for (var item in data.members) {
        formatedMembers[item.userId] = item;
      }
      _ctrlBusinessLogic.initMembers(formatedMembers);

      startHeartBeat();
      userSig = data.userSig;
      // 这里可能还没初始化好,需要等待IM初始化完成。暂时这么解决
      if (!imInitSuccess) {
        await Future.delayed(const Duration(seconds: 1));
      }
      var loginResult = await TIM.login(
        userId: _config.userId,
        userSig: data.userSig,
      );
      if (loginResult) {
        imLoginSuccess = true;
        _eventbus.fire(MainEvent(type: EventTypeEnum.imLoginSuccess));
        var joinResult = await _joinImGroups(getImJoinTargets());
        if (!joinResult) {
          _eventbus.fire(MainEvent(type: EventTypeEnum.someThingErrorInIm));
          TCICLog.error(
            'join im group failed',
            actionModule: ActionModule.tcicController.name,
            actionName: ActionName.joinClass.name,
          );
          return;
        }
        _eventbus.fire(MainEvent(type: EventTypeEnum.joinClassSuccess));

        // 加入课堂成功后,异步获取全量成员列表(不阻塞主流程)
        _fetchAllMembersInBackground();
      }
    } else {
      _eventbus.fire(MainEvent(type: EventTypeEnum.joinClassFailed));
      fireMainBreakEvent(
        MainProcessBreakenEventEnum.joinClassFailed,
        data.errorCode,
        requestId: data.requestId,
      );
    }
  } else {
    _eventbus.fire(MainEvent(type: EventTypeEnum.joinClassFailed));
    fireMainBreakEvent(
      MainProcessBreakenEventEnum.joinClassFailed,
      data?.errorCode ?? 0,
      requestId: data?.requestId ?? "",
    );
  }
  TCICLog.info(
    'userId: ${_config.userId}',
    actionModule: ActionModule.tcicController.name,
    actionName: ActionName.joinClass.name,
  );
}