import 'dart:async';
import 'package:zhiying_base_widget/pages/feedback_page/bloc/feedback_repository.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'feedback_event.dart';
import 'feedback_state.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class FeedbackBloc extends Bloc<FeedbackEvent, FeedbackState> {
  // FeedbackBloc() : super(FeedbackInitial());
  @override
  FeedbackState get initialState => FeedbackInitial();

  FeedBackRepository repository;

  FeedbackBloc(this.repository);

  @override
  Stream<FeedbackState> mapEventToState(
    FeedbackEvent event,
  ) async* {
    /// 初始化
    if (event is FeedbackInitEvent) {
      yield* _mapInitToState(event);
    }
  }

  /// 初始化
  Stream<FeedbackState> _mapInitToState(FeedbackInitEvent event) async* {
    var result = await repository.fetchData(event.model);
    if (!EmptyUtil.isEmpty(result)) {
      yield FeedbackLoadedState(model: result);
    } else {
      yield FeedbackErrorState();
    }
  }
}
