import 'dart:developer';

import 'package:mnemo_cards/features/packs/packs_api.dart';

import 'preview_pack_holder.dart';

class PreviewPacksPoller {
  PacksApi _api;
  PreviewPackHolder _previewPackHolder;
  bool polling = false;

  PreviewPacksPoller(this._api, this._previewPackHolder);

  Future<void> init() async {
    Stream.periodic(Duration(seconds: 60), (_) async {
      poll();
    });
  }

  Future<void> poll() async {
    if (!polling) {
      try {
        polling = true;
        final r = await _api.getPacksPreviews(null);
        _previewPackHolder.setPreviewPacks(r, save: true);
      } catch (e) {
        log(e.toString());
      }
      polling = false;
    }
  }
}
