post_detail_model.dart 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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!,
  14. page: max(1, pageNum), sortBy: _sortBy));
  15. } else {
  16. if (list.isEmpty) {
  17. return handlePage(await api.getPostComments(_post.id!,
  18. page: max(1, pageNum), sortBy: _sortBy));
  19. } else {
  20. Comment comment = list.last;
  21. return (await api.getPostCommentsFromId(comment.id!, sortBy: _sortBy))
  22. .results;
  23. }
  24. }
  25. }
  26. sortBy(String name) {
  27. if (name == "点赞最多") {
  28. _sortBy = "like_count";
  29. } else if (name == "最新评论") {
  30. _sortBy = "created_at";
  31. }
  32. initData();
  33. }
  34. }