bloc.dart 1.63 KB
import 'package:bloc/bloc.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:wow_english/models/app_config_entity.dart';

import '../../common/core/app_config_helper.dart';
import '../../common/request/dao/system_dao.dart';
import '../../models/app_version_entity.dart';
import '../../utils/log_util.dart';
import 'event.dart';
import 'state.dart';

class ModuleSelectBloc extends Bloc<ModuleSelectEvent, ModuleSelectState> {
  ModuleSelectBloc() : super(ModuleSelectState().init()) {
    on<InitEvent>(_init);
  }

  void _init(InitEvent event, Emitter<ModuleSelectState> emit) async {
    await _checkUpdate(emit);
    debugPrint('WQF ModuleSelectBloc _init');
  }

  Future<void> _checkUpdate(Emitter<ModuleSelectState> emit) async {
    if (AppConfigHelper.checkedUpdate) {
      return;
    }
    int localVersion = int.parse(await AppConfigHelper.getAppVersion());
    AppVersionEntity? appVersionEntity = await SystemDao.getVersionInfo();
    AppConfigHelper.checkedUpdate = true;
    if (appVersionEntity == null) {
      return;
    }
    Log.d("WQF _checkUpdate appVersionEntity: $appVersionEntity localVersion=$localVersion");
    if (defaultTargetPlatform == TargetPlatform.iOS) {
      if (localVersion < int.parse(appVersionEntity.version ?? '0')) {
        emit(UpdateDialogState(
            appVersionEntity.volType == UpdateStrategy.FORCE.name, appVersionEntity));
      }
    } else {
      if (localVersion < int.parse(appVersionEntity.version ?? '0')) {
        emit(UpdateDialogState(
            appVersionEntity.volType == UpdateStrategy.FORCE.name, appVersionEntity));
      }
    }
  }
}