forked from eatvio/eatvio_app
166 lines
4.4 KiB
Dart
166 lines
4.4 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:eatvio_app/features/blog_post_create/repositories/blog_post_repository.dart';
|
|
import 'package:eatvio_app/features/blog_post_create/services/blog_post_service.dart';
|
|
import 'package:flutter_image_compress/flutter_image_compress.dart';
|
|
|
|
class FixtureBlogPostRepository extends BlogPostRepository {
|
|
FixtureBlogPostRepository() : super(blogPostService: _NoopBlogPostService());
|
|
|
|
final List<BlogPostCreatePayload> createPayloads = <BlogPostCreatePayload>[];
|
|
final List<BlogPostUpdatePayload> updatePayloads = <BlogPostUpdatePayload>[];
|
|
final List<int> deleteCalls = <int>[];
|
|
final List<({int postId, int mediaId})> deleteMediaCalls = <({int postId, int mediaId})>[];
|
|
|
|
Duration createDelay = Duration.zero;
|
|
Duration updateDelay = Duration.zero;
|
|
Duration deleteDelay = Duration.zero;
|
|
|
|
Object? createError;
|
|
Object? updateError;
|
|
Object? deleteError;
|
|
Object? deleteMediaError;
|
|
|
|
@override
|
|
Future<void> createBlogPost({
|
|
String? title,
|
|
String? videoUrl,
|
|
String? description,
|
|
List<XFile> images = const [],
|
|
}) async {
|
|
createPayloads.add(
|
|
BlogPostCreatePayload(
|
|
title: title,
|
|
description: description,
|
|
videoUrl: videoUrl,
|
|
imageName: images.isEmpty ? null : images.first.name,
|
|
),
|
|
);
|
|
if (createDelay > Duration.zero) {
|
|
await Future.delayed(createDelay);
|
|
}
|
|
if (createError != null) {
|
|
throw createError!;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<void> updateBlogPost({
|
|
required int id,
|
|
String? title,
|
|
String? videoUrl,
|
|
String? description,
|
|
List<int> existingMediaIds = const [],
|
|
List<XFile> newImages = const [],
|
|
List<int> mediaIdsToDelete = const [],
|
|
}) async {
|
|
for (final mediaId in mediaIdsToDelete) {
|
|
deleteMediaCalls.add((postId: id, mediaId: mediaId));
|
|
}
|
|
updatePayloads.add(
|
|
BlogPostUpdatePayload(
|
|
id: id,
|
|
title: title,
|
|
description: description,
|
|
videoUrl: videoUrl,
|
|
imageName: newImages.isEmpty ? null : newImages.first.name,
|
|
existingMediaIds: existingMediaIds,
|
|
),
|
|
);
|
|
if (updateDelay > Duration.zero) {
|
|
await Future.delayed(updateDelay);
|
|
}
|
|
if (updateError != null) {
|
|
throw updateError!;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<void> deleteBlogPost(int id) async {
|
|
deleteCalls.add(id);
|
|
if (deleteDelay > Duration.zero) {
|
|
await Future.delayed(deleteDelay);
|
|
}
|
|
if (deleteError != null) {
|
|
throw deleteError!;
|
|
}
|
|
}
|
|
}
|
|
|
|
class BlogPostCreatePayload {
|
|
BlogPostCreatePayload({
|
|
required this.title,
|
|
required this.description,
|
|
required this.videoUrl,
|
|
required this.imageName,
|
|
});
|
|
|
|
final String? title;
|
|
final String? description;
|
|
final String? videoUrl;
|
|
final String? imageName;
|
|
|
|
Map<String, dynamic> toJson() => <String, dynamic>{
|
|
'title': title,
|
|
'description_md': description,
|
|
'video_url': videoUrl,
|
|
'image_name': imageName,
|
|
};
|
|
}
|
|
|
|
class BlogPostUpdatePayload {
|
|
BlogPostUpdatePayload({
|
|
required this.id,
|
|
required this.title,
|
|
required this.description,
|
|
required this.videoUrl,
|
|
required this.imageName,
|
|
required this.existingMediaIds,
|
|
});
|
|
|
|
final int id;
|
|
final String? title;
|
|
final String? description;
|
|
final String? videoUrl;
|
|
final String? imageName;
|
|
final List<int> existingMediaIds;
|
|
|
|
Map<String, dynamic> toJson() => <String, dynamic>{
|
|
'id': id,
|
|
'title': title,
|
|
'description_md': description,
|
|
'video_url': videoUrl,
|
|
'existing_media_ids': existingMediaIds,
|
|
'image_name': imageName,
|
|
};
|
|
}
|
|
|
|
class _NoopBlogPostService implements BlogPostService {
|
|
@override
|
|
Future<void> createBlogPost({required FormData formData}) {
|
|
throw UnimplementedError('Fixture overrides repository methods directly.');
|
|
}
|
|
|
|
@override
|
|
Future<void> deleteBlogPost({required int id}) {
|
|
throw UnimplementedError('Fixture overrides repository methods directly.');
|
|
}
|
|
|
|
@override
|
|
Future<void> deleteBlogPostMedia({required int postId, required int mediaId}) {
|
|
throw UnimplementedError('Fixture overrides repository methods directly.');
|
|
}
|
|
|
|
@override
|
|
Future<void> updateBlogPost({required int id, required FormData formData}) {
|
|
throw UnimplementedError('Fixture overrides repository methods directly.');
|
|
}
|
|
}
|
|
|
|
class FeedReloadProbe {
|
|
int loadRequests = 0;
|
|
|
|
Future<void> loadFeedBlogPostList() async {
|
|
loadRequests += 1;
|
|
}
|
|
}
|