import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:zhiying_comm/util/empty_util.dart';
import 'bloc.dart';

class CustomNoticeBloc extends Bloc<CustomNoticeEvent, CustomNoticeState> {
  CustomNoticeRepository repository;

  CustomNoticeBloc({this.repository});

  @override
  CustomNoticeState get initialState => CustomNoticeInitial();

  @override
  Stream<CustomNoticeState> mapEventToState(
    CustomNoticeEvent event,
  ) async* {
    if(event is CustomNoticeInitEvent){
      yield* _mapInitEventToState(event);
    }
  }

  /// 初始化
  Stream<CustomNoticeState> _mapInitEventToState(CustomNoticeInitEvent event) async* {
    var parentData = await repository.fetchParentData(event);
    if(!EmptyUtil.isEmpty(parentData))
      yield CustomNoticeLoadedState(model: parentData);
    else
      yield CustomNoticeErrorState();
  }
}
