comment.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import 'dart:math';
  2. import 'package:sport/bean/post_user.dart';
  3. import 'package:sport/services/Converter.dart';
  4. class Comment {
  5. String? id;
  6. String? subjectId;
  7. String? userId;
  8. String? toCommentId;
  9. String? parentCommentId;
  10. String? toUserId;
  11. String? content;
  12. int? likeCount;
  13. int? commentCount;
  14. String? createdAt;
  15. PostUser? socialInfo;
  16. String? time;
  17. int? createTime;
  18. List<Comment>? subList;
  19. bool? isLiked = false;
  20. String? userName;
  21. String? userAvatar;
  22. String? toUserName;
  23. String? toUserAvatar;
  24. bool? isExpansion;
  25. Comment({
  26. this.id,
  27. this.subjectId,
  28. this.userId,
  29. this.toCommentId,
  30. this.parentCommentId,
  31. this.toUserId,
  32. this.content,
  33. this.likeCount,
  34. this.commentCount,
  35. this.createdAt,
  36. this.socialInfo,
  37. this.time,
  38. this.createTime,
  39. this.subList,
  40. this.isLiked = false,
  41. this.userName,
  42. this.userAvatar,
  43. this.toUserName,
  44. this.toUserAvatar,
  45. });
  46. Comment.fromJson(Map<String, dynamic> json) {
  47. id = json['id'];
  48. subjectId = json['subject_id'];
  49. userId = json['user_id'];
  50. toCommentId = json['to_comment_id'];
  51. parentCommentId = json['parent_comment_id'];
  52. toUserId = json['to_user_id'];
  53. content = json['content'];
  54. likeCount = Converter.toInt(json['like_count']);
  55. commentCount = Converter.toInt(json['comment_count']);
  56. createdAt = json['created_at'];
  57. socialInfo = json['socialInfo'] != null
  58. ? new PostUser.fromJson(json['socialInfo'])
  59. : null;
  60. time = json['time'];
  61. createTime = json['create_time'];
  62. if (json['subList'] != null) {
  63. subList = <Comment>[];
  64. json['subList'].forEach((v) {
  65. subList!.add(new Comment.fromJson(v));
  66. });
  67. }
  68. isLiked = json['is_liked'];
  69. userName = json['user_name'];
  70. userAvatar = json['user_avatar'];
  71. toUserName = json['to_user_name'];
  72. toUserAvatar = json['to_user_avatar'];
  73. }
  74. Map<String, dynamic> toJson() {
  75. final Map<String, dynamic> data = new Map<String, dynamic>();
  76. data['id'] = this.id;
  77. data['subject_id'] = this.subjectId;
  78. data['user_id'] = this.userId;
  79. data['to_comment_id'] = this.toCommentId;
  80. data['parent_comment_id'] = this.parentCommentId;
  81. data['to_user_id'] = this.toUserId;
  82. data['content'] = this.content;
  83. data['like_count'] = this.likeCount;
  84. data['comment_count'] = this.commentCount;
  85. data['created_at'] = this.createdAt;
  86. if (this.socialInfo != null) {
  87. data['socialInfo'] = this.socialInfo!.toJson();
  88. }
  89. data['time'] = this.time;
  90. data['create_time'] = this.createTime;
  91. if (this.subList != null) {
  92. data['subList'] = this.subList!.map((v) => v.toJson()).toList();
  93. }
  94. data['user_name'] = this.userName;
  95. data['user_avatar'] = this.userAvatar;
  96. data['to_user_name'] = this.toUserName;
  97. data['to_user_avatar'] = this.toUserAvatar;
  98. return data;
  99. }
  100. void toggleLike() {
  101. isLiked = !(isLiked ?? false);
  102. likeCount = incLikeCount();
  103. }
  104. int? incLikeCount() {
  105. return max(0, likeCount ?? 0 + (isLiked ?? false ? 1 : -1));
  106. }
  107. }