forked from eatvio/eatvio_app
73 lines
2.4 KiB
Dart
73 lines
2.4 KiB
Dart
import 'package:eatvio_app/core/app_config.dart';
|
|
import 'package:eatvio_app/core/locator_service.dart';
|
|
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
|
|
import 'package:eatvio_app/features/feed/models/feed_blog_post_model.dart';
|
|
import 'package:eatvio_app/features/feed/models/post_feed_pagination_model.dart';
|
|
|
|
class PostFeedMapper {
|
|
static FeedBlogPostModel fromPostFeedItem(PostFeedItemModel item, {int? currentUserId}) {
|
|
return FeedBlogPostModel(
|
|
id: item.postId,
|
|
state: item.postState,
|
|
image: resolveMediaUrl(item.postImageUrl),
|
|
description: item.postTextMd ?? item.postShortDescription ?? '',
|
|
author: Author(
|
|
authorSlug: item.postAuthorDetails?.slug ?? '',
|
|
authorName: item.postAuthorName,
|
|
authorProfileIcon: resolveMediaUrl(item.postAuthorImageUrl),
|
|
authorIsMe: currentUserId != null && item.postAuthorId == currentUserId,
|
|
isEntitySubscribedByMe: item.isEntitySubscribedByMe,
|
|
),
|
|
videoUrl: '',
|
|
createdAt: item.postCreatedAt,
|
|
updatedAt: item.updatedAt,
|
|
title: item.postTitle,
|
|
totalLikes: item.postLikesCount,
|
|
totalViews: item.postViewsCount,
|
|
totalComments: item.postCommentsCount,
|
|
wasLikedByMe: item.postIsLikedByUser,
|
|
images: item.images.where((image) => hasRenderableImage(image.url)).toList(),
|
|
videoEmbedUrl: item.videoEmbedUrl,
|
|
videoUrlThumb: item.videoUrlThumb,
|
|
);
|
|
}
|
|
|
|
static String resolveMediaUrl(String? url) {
|
|
if (url == null || url.isEmpty || isPlaceholderMediaUrl(url)) {
|
|
return '';
|
|
}
|
|
if (url.startsWith('http://') || url.startsWith('https://')) {
|
|
return url;
|
|
}
|
|
if (url.startsWith('/')) {
|
|
return '${sl<AppConfig>().baseUrl}$url';
|
|
}
|
|
return '${sl<AppConfig>().baseUrl}/storage/$url';
|
|
}
|
|
|
|
static bool isPlaceholderMediaUrl(String url) {
|
|
return url.toLowerCase().contains('element-image-placeholder');
|
|
}
|
|
|
|
static bool hasRenderableImage(String? url) {
|
|
if (url == null || url.trim().isEmpty) {
|
|
return false;
|
|
}
|
|
return !isPlaceholderMediaUrl(url);
|
|
}
|
|
|
|
static bool hasVisibleMedia({
|
|
required String image,
|
|
required List<FeedGalleryImage> images,
|
|
String? videoEmbedUrl,
|
|
}) {
|
|
if (videoEmbedUrl != null && videoEmbedUrl.trim().isNotEmpty) {
|
|
return true;
|
|
}
|
|
if (hasRenderableImage(image)) {
|
|
return true;
|
|
}
|
|
return images.any((image) => hasRenderableImage(image.url));
|
|
}
|
|
}
|