import 'dart:async';

import 'package:zhiying_base_widget/widgets/mine/mine_quick_entry/models/mine_quick_entry_model.dart';
import 'package:zhiying_comm/util/base_bloc.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class MineQuickEntryBloc extends BlocBase {
  List<MineQuickEntryModel> _icons = List();

  StreamController<List<MineQuickEntryModel>> _iconsController =
      StreamController<List<MineQuickEntryModel>>();

  Stream<List<MineQuickEntryModel>> get outData => _iconsController.stream;

  @override
  void dispose() {
    _iconsController.close();
    _iconsController = null;
  }

  void loadData(int id) {
    NetUtil.request('/api/v1/mod',
        method: NetMethod.POST,
        params: Map<String, dynamic>.from({
          'ids': [id]
        }),
        onCache: (data) {}, onSuccess: (data) {
      String key = id.toString();

      Map<String, dynamic> json = Map<String, dynamic>.from(data);
      if (json.containsKey(key)) {
        List<dynamic> list = json[key];
        _icons = list.map((item) {
          return MineQuickEntryModel.fromJson(Map<String, dynamic>.from(item));
        }).toList();
      }

      _iconsController.add(_icons);
    });
  }
}
