post_detail_model.dart 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import 'dart:math';
  2. import 'package:sport/bean/comment.dart';
  3. import 'package:sport/bean/post.dart';
  4. import 'package:sport/provider/lib/view_state_refresh_list_model.dart';
  5. import 'package:sport/services/api/inject_api.dart';
  6. class PostDetailModel extends ViewStateRefreshListModel with InjectApi {
  7. final Post _post;
  8. String _sortBy = "created_at";
  9. PostDetailModel(this._post);
  10. @override
  11. Future<List> loadData({int pageNum = 1}) async {
  12. if (pageNum == 1) {
  13. return handlePage(await api.getPostComments(_post.id, page: max(1, pageNum), sortBy: _sortBy));
  14. } else {
  15. if (list.isEmpty) {
  16. return handlePage(await api.getPostComments(_post.id, page: max(1, pageNum), sortBy: _sortBy));
  17. } else {
  18. Comment comment = list.last;
  19. return (await api.getPostCommentsFromId(comment.id, sortBy: _sortBy)).results;
  20. }
  21. }
  22. }
  23. sortBy(String name) {
  24. if (name == "点赞最多") {
  25. _sortBy = "like_count";
  26. } else if (name == "最新评论") {
  27. _sortBy = "created_at";
  28. }
  29. initData();
  30. }
  31. }