import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:zhiying_base_widget/widgets/home/home_notice/bloc/home_notice_repository.dart';
import 'package:zhiying_comm/util/empty_util.dart';
import 'bloc.dart';

class HomeNoticeBloc extends Bloc<HomeNoticeEvent, HomeNoticeState> {
  HomeNoticeRepository repository;

  HomeNoticeBloc({this.repository});

  @override
  HomeNoticeState get initialState => HomeNoticeInitial();

  @override
  Stream<HomeNoticeState> mapEventToState(
    HomeNoticeEvent event,
  ) async* {
    if(event is HomeNoticeInitEvent){
      yield* _mapInitEventToState(event);
    }
  }

  /// 初始化
  Stream<HomeNoticeState> _mapInitEventToState(HomeNoticeInitEvent event) async* {
    var parentData = await repository.fetchParentData(event);
    if(!EmptyUtil.isEmpty(parentData))
      yield HomeNoticeLoadedState(model: parentData);
    else
      yield HomeNoticeErrorState();
  }
}
