Files
HiddingselAppOriginal/lib/appflow/model/articles.dart
2026-02-13 15:53:22 +01:00

86 lines
3.0 KiB
Dart

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<RepresentedOrganisationModel> categories;
String get content => WebViewHelper.getHtmlContentWithoutMedia(htmlContent);
List<ImageModel> 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<RepresentedOrganisationModel>.empty();
factory ArticleModel.fromWordpressJson(Map<String, dynamic> 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<dynamic>)
.cast<int>()
.map((id) => RepresentedOrganisationModel.fromWordpressId(id))
.where((e) => e != null)
.cast<RepresentedOrganisationModel>()
.toList(),
);
factory ArticleModel.fromJson(Map<String, dynamic> 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<dynamic>)
.cast<String>()
.map((id) => EventOrganisationModel.fromId(id))
.where((e) => e != null)
.cast<RepresentedOrganisationModel>()
.toList(),
otherImages: (json['otherImages'] as List<dynamic>)
.cast<String>()
.map((resouce) => ImageModel(resouce))
.toList(),
);
Map<String, dynamic> 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;
}