topic_list_provider.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import 'package:flutter_riverpod/flutter_riverpod.dart';
  2. import 'package:news_app/constant/api_const.dart';
  3. import 'package:news_app/http/http_util.dart';
  4. import 'package:news_app/model/topic_item_model.dart';
  5. import '../http/model_parser.dart';
  6. /// @author: bo.zeng
  7. /// @email: cnhbwds@gmail.com
  8. /// @date: 2025 2025/4/18 10:43
  9. /// @description:
  10. class TopicListProvider extends Notifier<TopicItemModel> {
  11. @override
  12. TopicItemModel build() {
  13. return TopicItemModel();
  14. }
  15. Future<void> fetchList({required int page,required String tid}) async {
  16. var jsonData = await HttpUtil().get(
  17. apiTopicList,
  18. queryParameters: tid.isEmpty ? {"pn": page, "ps": 10} : {"tid":tid ,"pn": page, "ps": 10},
  19. );
  20. final response = ModelParser.parseObject<TopicItemModel>(
  21. jsonData,
  22. TopicItemModel.fromJson,
  23. );
  24. final oldRecords = state.records ?? [];
  25. state = state.copyWith(
  26. total: response.total,
  27. size: response.size,
  28. current: response.current,
  29. pages: response.pages,
  30. records:
  31. page == 1 ? response.records : [...oldRecords, ...?response.records],
  32. );
  33. }
  34. Future<void> fetchTopicLike({
  35. required String? contentId,
  36. required bool current,
  37. }) async {
  38. if (contentId == null || contentId.isEmpty) {
  39. return;
  40. }
  41. final jsonData = await HttpUtil().get(
  42. current ? apiTopicCancelLike : apiTopicLike,
  43. queryParameters: {"contentId": contentId},
  44. );
  45. if (jsonData == true) {
  46. //修改成功后,把state列表中的item likeCount+1
  47. if (current) {
  48. state = state.copyWith(
  49. records:
  50. state.records?.map((topic) {
  51. if (topic.contentId == contentId) {
  52. return topic.copyWith(
  53. likeNum: (topic.likeNum ?? 1) - 1,
  54. isLiked: false,
  55. );
  56. }
  57. return topic;
  58. }).toList(),
  59. );
  60. } else {
  61. state = state.copyWith(
  62. records:
  63. state.records?.map((topic) {
  64. if (topic.contentId == contentId) {
  65. return topic.copyWith(
  66. likeNum: (topic.likeNum ?? 0) + 1,
  67. isLiked: true,
  68. );
  69. }
  70. return topic;
  71. }).toList(),
  72. );
  73. }
  74. }
  75. }
  76. Future<void> fetchTopicFavorite({
  77. required String? contentId,
  78. required bool current,
  79. }) async {
  80. if (contentId == null || contentId.isEmpty) {
  81. return;
  82. }
  83. final jsonData = await HttpUtil().get(
  84. current ? apiTopicCancelFavorite : apiTopicFavorite,
  85. queryParameters: {"contentId": contentId},
  86. );
  87. if (jsonData == true) {
  88. //修改成功后,把state列表中的item likeCount+1
  89. if (current) {
  90. state = state.copyWith(
  91. records:
  92. state.records?.map((topic) {
  93. if (topic.contentId == contentId) {
  94. return topic.copyWith(
  95. favoriteCount: (topic.favoriteCount ?? 1) - 1,
  96. isFavorite: false,
  97. );
  98. }
  99. return topic;
  100. }).toList(),
  101. );
  102. } else {
  103. state = state.copyWith(
  104. records:
  105. state.records?.map((topic) {
  106. if (topic.contentId == contentId) {
  107. return topic.copyWith(
  108. favoriteCount: (topic.favoriteCount ?? 0) + 1,
  109. isFavorite: true,
  110. );
  111. }
  112. return topic;
  113. }).toList(),
  114. );
  115. }
  116. }
  117. }
  118. }