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

class GetActionTypeBloc {
  final _repository = Repository();
  final _getActionTypesFetcher = PublishSubject<dynamic>();

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

  void getActionTypes(Function(dynamic) onSuccess, Function(String) onError) async {
    try {
      _repository.getActionTypes().then((value) {
        onSuccess(value);
        _getActionTypesFetcher.sink.add(value);
      }).catchError((onError) {
        _getActionTypesFetcher.addError(onError);
        onError(onError);
      });
    } catch (e) {
      _getActionTypesFetcher.addError(e);
      onError('error');
    }
  }

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

final getActionTypeBloc = GetActionTypeBloc();
