import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:zhiying_base_widget/widgets/goods_details/store/bloc/store_repository.dart';
import 'package:zhiying_comm/util/empty_util.dart'
    '';

import 'bloc.dart';

class StoreBloc extends Bloc<StoreEvent, StoreState> {
  StoreRepository repository;

  StoreBloc({this.repository});

  @override
  StoreState get initialState => StoreInitial();

  @override
  Stream<StoreState> mapEventToState(
    StoreEvent event,
  ) async* {

    /// 初始化
    if(event is StoreInitEvent){
      yield* _mapInitEventToState(event);
    }
  }


  /// 获取数据
  Stream<StoreState> _mapInitEventToState(StoreInitEvent event) async*{
      var result  = await repository.fetchParentData(event);
      if(!EmptyUtil.isEmpty(result)){
        yield StoreLoadedState(model: result);
        return;
      }
      var net = await repository.fetchNetData(event);
      if(!EmptyUtil.isEmpty(net))
        yield StoreLoadedState(model: result);
      else
        yield StoreErrorState();
  }

}
