import 'package:hiddingsel_app/appflow/model/images.dart'; import 'package:hiddingsel_app/appflow/model/represented_organisations.dart'; import 'package:hiddingsel_app/services/storage.dart'; import 'package:html/parser.dart' show parse; import '../../todo/WebConnector.dart'; import 'event_organisations.dart'; import 'interfaces.dart'; class ArticleModel with IsShareable, JsonEncodable { final int id; final String title; final String htmlContent; final DateTime publishedAt; final ImageModel image; final Uri url; final List categories; String get content => WebViewHelper.getHtmlContentWithoutMedia(htmlContent); List get otherImages => WebViewHelper.getImageMedia(htmlContent) .map((url) => ImageModel(url)).toList(); ArticleModel.withImageModel(this.id, this.title, this.htmlContent, this.publishedAt, this.image, this.url, {categories, otherImages}) : this.categories = categories ?? []; ArticleModel(this.id, this.title, this.htmlContent, this.publishedAt, String thumbnailImageResource, this.url, {categories, otherImages}) : this.image = ImageModel(thumbnailImageResource), this.categories = categories ?? List.empty(); factory ArticleModel.fromWordpressJson(Map json) => ArticleModel( json['id'] as int, parse(json['title']['rendered'] as String).documentElement!.text, json['content']['rendered'] as String, DateTime.parse(json['date'] as String), json['_embedded']['wp:featuredmedia'][0]['source_url'] as String, Uri.parse(json['link'] as String), categories: (json['categories'] as List) .cast() .map((id) => RepresentedOrganisationModel.fromWordpressId(id)) .where((e) => e != null) .cast() .toList(), ); factory ArticleModel.fromJson(Map json) => ArticleModel( json['id'] as int, json['title'] as String, json['htmlContent'] as String, DateTime.parse(json['publishedAt'] as String), json['image'] as String, Uri.parse(json['url'] as String), categories: (json['categories'] as List) .cast() .map((id) => EventOrganisationModel.fromId(id)) .where((e) => e != null) .cast() .toList(), otherImages: (json['otherImages'] as List) .cast() .map((resouce) => ImageModel(resouce)) .toList(), ); Map toJson() => { 'id': id, 'title': title, 'htmlContent': htmlContent, 'publishedAt': publishedAt.toIso8601String(), 'image': image.resource, 'url': url.toString(), 'categories': categories.map((c) => c.id).toList(), 'otherImages': otherImages.map((i) => i.resource).toList(), }; @override bool operator ==(Object other) => other is ArticleModel && runtimeType == other.runtimeType && id == other.id; @override int get hashCode => id.hashCode; }