post.dart 4.3 KB

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