BaseBloc constructor

BaseBloc({
  1. bool hasMapKey = false,
  2. bool hasProDis = false,
  3. bool hasAddImage = false,
  4. bool hasLoadFile = false,
  5. bool hasGetAddressMap = false,
  6. BaseState init = const BaseState(),
})

Implementation

BaseBloc({bool hasMapKey = false, bool hasProDis = false, bool hasAddImage = false, bool hasLoadFile = false,
  bool hasGetAddressMap = false, BaseState init = const BaseState()}) : super(init) {
  on<LoadingEvent>((event, emit) => emit(BaseState(isShowLoading: event.value)));
  on<TrackEvent>((event, emit) {
    ApiClient().trackApp(event.path, method: event.method, urlPath: event.urlPath, clientKey: event.clientKey);
  });
  if (hasMapKey) {
    on<LoadMapKeyBaseEvent>((event, emit) async {
      emit(const BaseState(isShowLoading: true));
      final resp = await ApiClient().getAPI2('${Constants().apiVersion}base/option?key=maptitles_key', hasHeader: false);
      if (resp.isNotEmpty) {
        final json = jsonDecode(resp);
        if (Util.checkKeyFromJson(json, 'success') && Util.checkKeyFromJson(json, 'data') && json['success']) {
          final list = json['data'].toList();
          if (list.isNotEmpty) {
            emit(LoadMapKeyBaseState(list[0]['value']??''));
            return;
          }
        }
      }
      emit(const BaseState());
    });
  }
  if (hasProDis) {
    on<LoadProvinceBaseEvent>((event, emit) async {
      emit(const BaseState(isShowLoading: true));
      final resp = await ApiClient().getAPI(Constants().apiVersion + 'locations/list_provinces', ItemListModel(), hasHeader: false);
      emit(resp.checkOK() && resp.data.list.isNotEmpty ? LoadProvinceBaseState(resp.data.list) : const BaseState());
    });
    on<LoadDistrictBaseEvent>((event, emit) async {
      emit(const BaseState(isShowLoading: true));
      final resp = await ApiClient().getAPI(Constants().apiVersion + 'locations/list_districts?province_id=' + event.idProvince, ItemListModel(), hasHeader: false);
      emit(resp.checkOK() && resp.data.list.isNotEmpty ? LoadDistrictBaseState(resp.data.list) : const BaseState());
    });
  }
  if (hasLoadFile) {
    on<DownloadFilesPostItemEvent>((event, emit) async {
      emit(const BaseState(isShowLoading: true));
      List<File> files = await Util.loadFilesFromNetwork(null, event.list);
      emit(DownloadFilesPostItemState(files));
    });
  }
  if (hasAddImage) on<AddImageHomeEvent>((event, emit) => emit(AddImageHomeState()));
  if (hasGetAddressMap) {
    on<GetAddressFromMapEvent>((event, emit) async {
      if (event.showLoading) emit(const BaseState(isShowLoading: true));
      final resp = await ApiClient().getAPI2(event.linkApi??'https://maps.track-asia.com/api/v1/autocomplete?lang=vi&text=${event.text}&key=public_key', hasHeader: false, isFullPath: true);
      if (resp.isNotEmpty) {
        final json = jsonDecode(resp);
        if (Util.checkKeyFromJson(json, 'features')) {
          emit(GetAddressFromMapState(formatMapFromJson(json['features'])));
          return;
        }
      }
      if (event.showLoading) emit(GetAddressFromMapState([]));
    });
    on<HideAddressEvent>((event, emit) => emit(HideAddressState()));
  }
}