import '../../../infrastructure/utility_widgets/loadable_hydrated_bloc/loadable_hydrated_bloc.dart';
import '../../../infrastructure/utility_widgets/loadable_hydrated_bloc/loadable_hydrated_bloc_event.dart';
import '../repository/ticker_repository.dart';
import 'ticker_event.dart';
import 'ticker_state.dart';

class TickerBloc
    extends LoadableHydratedBloc<TickerEvent, TickerState, TickerRepository> {
  @override
  Future<void> gatherData() async {
    final (ticker, nav) = await (repo.getTicker(), repo.getNav()).wait;
    add(DataGathered((state) => state.copyWith(ticker: ticker, nav: nav)));
  }

  @override
  TickerRepository repository() => TickerRepository();

  @override
  TickerState fromNothing() => const TickerState();

  @override
  TickerState fromStorage(Map<String, dynamic> json) =>
      TickerState.fromJson(json);

  @override
  Map<String, dynamic>? toStorage(TickerState state) => state.toJson();
}
