import 'package:farm_tpf/data/repository/repository.dart';
import 'package:rxdart/rxdart.dart';

class GetHarvestBloc {
  final _repository = Repository();
  final _getHarvestFetcher = PublishSubject<dynamic>();

  Stream<dynamic> get actions => _getHarvestFetcher.stream;

  void getHarvests(Function(dynamic) onSuccess, Function(String) onError) async {
    try {
      _repository.getHarvests().then((value) {
        onSuccess(value);
        _getHarvestFetcher.sink.add(value);
      }).catchError((onError) {
        _getHarvestFetcher.addError(onError);
        onError(onError);
      });
    } catch (e) {
      _getHarvestFetcher.addError(e);
      onError(e.toString());
    }
  }

  void dispose() async {
    await _getHarvestFetcher.drain();
    _getHarvestFetcher.close();
  }
}

final getHarvestBloc = GetHarvestBloc();
