123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- import 'attachment.dart';
- class Post {
- final String added;
- final List<Attachment> attachments;
- final Attachment? files;
- final String content;
- final String edited;
- final String id;
- final String published;
- final String service;
- final String title;
- final String user;
- final String baseUrl;
- Post({
- required this.added,
- required this.attachments,
- required this.files,
- required this.content,
- required this.edited,
- required this.id,
- required this.published,
- required this.service,
- required this.title,
- required this.user,
- required this.baseUrl,
- });
- List<Attachment> get mediaAttachments {
- final allAttachments = files == null ?
- [...attachments.where((a) => a.isImage || a.isVideo)] :
- [...attachments.where((a) => a.isImage || a.isVideo),files!];
- final ids = allAttachments.map((e) => e.path).toSet();
- allAttachments.retainWhere((x) => ids.remove(x.path));
- return allAttachments;
- }
- factory Post.fromJson(Map<String, dynamic> json, String baseUrl) => Post(
- added: _parseString(json['added']),
- attachments: _parseAttachments(json['attachments'], baseUrl),
- files: (json["file"] as Map)?.isNotEmpty == true ? Attachment.fromJson(json["file"], baseUrl) : null,
- content: _parseString(json['substring']),
- edited: _parseString(json['edited']),
- id: _parseString(json['id']),
- published: _parseString(json['published']),
- service: _parseString(json['service']),
- title: _parseString(json['title']),
- user: _parseString(json['user']),
- baseUrl: baseUrl,
- );
- static String _parseString(dynamic value) =>
- (value is String) ? value : '';
- static List<Attachment> _parseAttachments(
- dynamic jsonData,
- String baseUrl
- ) {
- if (jsonData is! List) return [];
-
- return jsonData
- .whereType<Map<String, dynamic>>()
- .map((x) => Attachment.fromJson(x, baseUrl))
- .whereType<Attachment>()
- .toList();
- }
- }
|