import 'package:flutter/material.dart';
import 'package:gmalpha_flutter/NewMessageModel/service/remote/entity/AttentionEntity.dart';
import 'package:gmalpha_flutter/NewMessageModel/service/MessageRepository.dart';
import 'package:gmalpha_flutter/commonModel/live/BaseModel.dart';
import 'package:gmalpha_flutter/commonModel/live/LiveData.dart';
import 'package:gmalpha_flutter/commonModel/toast/toast.dart';

class AttentionModel extends BaseModel {
  LiveData<AttentionEntity> zzgLive = new LiveData();
  List<Data> _newList = [];

  int page;
  final int count = 10;
  final String fromPage;

  AttentionModel(this.page, this.fromPage);

  init(BuildContext context, {Function callback}) {
    // 初始化或者刷新的时候增加一项无用项，用来渲染列表title
    if (page == 1) {
      Data firstData = Data();
      _newList.add(firstData);
    }
    MessageRepository.getInstance().getAttentionList(page, count).listen((value) {
      if (value != null) {
        // 增加一个字段，用来判断是否还有数据
        if (value.data.length == 0) {
          value.noData = true;
        } else {
          value.noData = false;
        }
        _newList.addAll(value.data);
        value.data = _newList;
        zzgLive.notifyView(value);
        if (callback is Function) {
          callback();
        }
      }
    }).onError((error) {
      Toast.show(context, "${error.toString()}");
      print(error);
    });
  }

  refresh(BuildContext context) {
    page = 1;
    _newList = [];
    init(context);
  }

  more(BuildContext context, Function fun) {
    page++;
    init(context, callback: fun);
  }

  @override
  void dispose() {
    zzgLive.dispost();
  }
}
