topic_detail_provider.dart 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import 'package:flutter_riverpod/flutter_riverpod.dart';
  2. import '../constant/api_const.dart';
  3. import '../http/http_util.dart';
  4. import '../http/model_parser.dart';
  5. import '../model/topic_item_model.dart';
  6. import '../util/toast_util.dart';
  7. /// @author: bo.zeng
  8. /// @email: cnhbwds@gmail.com
  9. /// @date: 2025 2025/4/20 23:08
  10. /// @description:
  11. class TopicDetailProvider extends Notifier<TopicRecordModel> {
  12. @override
  13. TopicRecordModel build() {
  14. return TopicRecordModel();
  15. }
  16. Future<void> fetchTopicDetail(String aid) async {
  17. var jsonData = await HttpUtil().get(
  18. apiTopicDetail,
  19. queryParameters: {"aid": aid}, //"665168438702149"
  20. );
  21. final response = ModelParser.parseObject<TopicRecordModel>(
  22. jsonData,
  23. TopicRecordModel.fromJson,
  24. );
  25. state = response;
  26. }
  27. Future<void> fetchTopicShare({required String? contentId}) async {
  28. if (contentId == null || contentId.isEmpty) {
  29. return;
  30. }
  31. await HttpUtil().get(
  32. apiTopicShare,
  33. queryParameters: {"contentId": contentId},
  34. );
  35. }
  36. Future<void> reportComment(String type, String resourceId) async {
  37. final jsonData = await HttpUtil().post(
  38. apiCommentReport,
  39. data: {"type": type, "resourceId": resourceId},
  40. );
  41. if (jsonData != null) {
  42. showToast("举报成功");
  43. }
  44. }
  45. Future<void> updateLike() async {
  46. state = state.copyWith(
  47. isLiked: state.isLiked == true ? false : true,
  48. likeNum: state.isLiked == true ? state.likeNum! - 1 : state.likeNum! + 1,
  49. );
  50. }
  51. Future<void> updateFavorite() async {
  52. state = state.copyWith(isFavorite: state.isFavorite == true ? false : true);
  53. }
  54. }