import 'package:flutter/scheduler.dart';
import 'package:sport/provider/lib/view_state.dart';
import 'package:sport/provider/lib/view_state_model.dart';
import 'package:sport/provider/lib/view_state_refresh_list_model.dart';
import 'package:sport/services/api/inject_api.dart';
import 'package:sport/services/api/resp.dart';

typedef FutureCreate = Future<List> Function(int pageNum);

class SimpleModel extends ViewStateRefreshListModel with InjectApi {
  final FutureCreate create;

  SimpleModel(this.create);

  @override
  Future<List> loadData({int pageNum}) async {
    var result;
    try {
      result = create.call(pageNum);
    } catch (e) {
      print("SimpleModel error: $e");
    }
    return result;
  }
}

typedef FutureData = Future Function(String code);

class SimpleDataModel<T> extends ViewStateModel with InjectApi {
  final FutureData create;
  RespData data;
  SimpleDataModel(this.create);

  /// 第一次进入页面loading skeleton
//  initData() {
//    SchedulerBinding.instance.addPostFrameCallback((Duration timestamp) {
//      setBusy();
////      refresh(init: true); // 不需要refresh
//    });
//  }

  Future<T> loadData(String code) async {
    T result;
    try {
      result = await create.call(code);
    } catch (e) {
      print(e);
    }
    return result;
  }
}
