comment.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 ? new PostUser.fromJson(json['socialInfo']) : null;
  58. time = json['time'];
  59. createTime = json['create_time'];
  60. if (json['subList'] != null) {
  61. subList = new List<Comment>();
  62. json['subList'].forEach((v) {
  63. subList.add(new Comment.fromJson(v));
  64. });
  65. }
  66. isLiked = json['is_liked'];
  67. userName = json['user_name'];
  68. userAvatar = json['user_avatar'];
  69. toUserName = json['to_user_name'];
  70. toUserAvatar = json['to_user_avatar'];
  71. }
  72. Map<String, dynamic> toJson() {
  73. final Map<String, dynamic> data = new Map<String, dynamic>();
  74. data['id'] = this.id;
  75. data['subject_id'] = this.subjectId;
  76. data['user_id'] = this.userId;
  77. data['to_comment_id'] = this.toCommentId;
  78. data['parent_comment_id'] = this.parentCommentId;
  79. data['to_user_id'] = this.toUserId;
  80. data['content'] = this.content;
  81. data['like_count'] = this.likeCount;
  82. data['comment_count'] = this.commentCount;
  83. data['created_at'] = this.createdAt;
  84. if (this.socialInfo != null) {
  85. data['socialInfo'] = this.socialInfo.toJson();
  86. }
  87. data['time'] = this.time;
  88. data['create_time'] = this.createTime;
  89. if (this.subList != null) {
  90. data['subList'] = this.subList.map((v) => v.toJson()).toList();
  91. }
  92. data['user_name'] = this.userName;
  93. data['user_avatar'] = this.userAvatar;
  94. data['to_user_name'] = this.toUserName;
  95. data['to_user_avatar'] = this.toUserAvatar;
  96. return data;
  97. }
  98. void toggleLike() {
  99. isLiked = !(isLiked ?? false);
  100. likeCount = incLikeCount();
  101. }
  102. int incLikeCount() {
  103. return max(0, likeCount + (isLiked ? 1 : -1));
  104. }
  105. }