post.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import 'dart:math';
  2. import 'package:sport/services/Converter.dart';
  3. import 'image.dart';
  4. class Post {
  5. String id;
  6. String userId;
  7. String forumId;
  8. String title;
  9. String content;
  10. List<Image> images;
  11. String createdAt;
  12. int shareCount;
  13. int likeCount;
  14. int commentCount;
  15. String isGood;
  16. String isTop;
  17. String isUserTop;
  18. String isOfficial;
  19. String nickname;
  20. String avatar;
  21. int createTime;
  22. bool isLiked;
  23. bool isExpansion;
  24. bool isDelete;
  25. String quoteSubjectId, quoteCommentId;
  26. String forumName, forumCover;
  27. Post quoteSubject;
  28. List<String> tags;
  29. String followStatus;
  30. String quoteData;
  31. String gameName;
  32. Post({
  33. this.id,
  34. this.userId,
  35. this.forumId,
  36. this.title,
  37. this.content,
  38. this.images,
  39. this.createdAt,
  40. this.shareCount,
  41. this.likeCount,
  42. this.commentCount,
  43. this.isGood,
  44. this.nickname,
  45. this.avatar,
  46. this.createTime,
  47. this.isLiked,
  48. this.quoteData,
  49. this.gameName,
  50. });
  51. Post.fromJson(Map<String, dynamic> json) {
  52. id = json['id'];
  53. userId = json['user_id'];
  54. forumId = json['forum_id'];
  55. title = json['title'];
  56. content = json['content'];
  57. if (json['images'] != null) {
  58. images = new List();
  59. if (json['images'] is List) {
  60. json['images'].forEach((v) {
  61. if (v is String) {
  62. images.add(Image(src: v));
  63. } else {
  64. images.add(new Image.fromJson(v));
  65. }
  66. });
  67. }
  68. if (json['images'] is String) {
  69. if (json['images'] != "") images.add(new Image(src: json['images']));
  70. }
  71. }
  72. createdAt = json['created_at'];
  73. shareCount = Converter.toInt(json['share_count']);
  74. likeCount = Converter.toInt(json['like_count']);
  75. commentCount = Converter.toInt(json['comment_count']);
  76. isGood = json['is_good'];
  77. isTop = json['is_top'];
  78. isOfficial = json['is_official'];
  79. nickname = json['nickname'];
  80. avatar = json['avatar'];
  81. createTime = json['create_time'];
  82. isLiked = json['is_liked'];
  83. content = content ?? content.trim();
  84. quoteSubjectId = json['quote_subject_id'];
  85. quoteCommentId = json['quote_comment_id'];
  86. forumName = json['forum_name'];
  87. forumCover = json['forum_cover'];
  88. quoteSubject = json['quoteSubject'] != null
  89. ? new Post.fromJson(json['quoteSubject'])
  90. : null;
  91. tags = [];
  92. if (json['tags'] != null) {
  93. if (json['tags'] is List) {
  94. json['tags'].forEach((v) {
  95. if (v is String) {
  96. tags.add(v);
  97. }
  98. });
  99. } else {
  100. tags.add(json['tags']);
  101. }
  102. }
  103. followStatus = json['follow_status'];
  104. quoteData = json['quote_data'];
  105. gameName = json['game_name'];
  106. }
  107. Map<String, dynamic> toJson() {
  108. final Map<String, dynamic> data = new Map<String, dynamic>();
  109. data['id'] = this.id;
  110. data['user_id'] = this.userId;
  111. data['forum_id'] = this.forumId;
  112. data['title'] = this.title;
  113. data['content'] = this.content;
  114. if (this.images != null) {
  115. data['images'] = this.images.map((v) => v.toJson()).toList();
  116. }
  117. data['created_at'] = this.createdAt;
  118. data['share_count'] = this.shareCount;
  119. data['like_count'] = this.likeCount;
  120. data['comment_count'] = this.commentCount;
  121. data['is_good'] = this.isGood;
  122. data['is_top'] = this.isTop;
  123. data['is_official'] = this.isOfficial;
  124. data['nickname'] = this.nickname;
  125. data['avatar'] = this.avatar;
  126. data['create_time'] = this.createTime;
  127. data['is_liked'] = this.isLiked;
  128. data['follow_status'] = this.followStatus;
  129. if (this.quoteSubject != null) {
  130. data['quoteSubject'] = this.quoteSubject.toJson();
  131. }
  132. data['quote_data'] = this.quoteData;
  133. data['game_name'] = this.gameName;
  134. return data;
  135. }
  136. void toggleLike() {
  137. isLiked = !(isLiked ?? false);
  138. likeCount = incLikeCount();
  139. }
  140. int incLikeCount() {
  141. return max(0, likeCount + (isLiked ? 1 : -1));
  142. }
  143. bool isFriend() {
  144. return followStatus == "friends" || followStatus == "followed";
  145. }
  146. bool isMe(int userId) {
  147. return int.parse(this.userId) == userId;
  148. }
  149. }