Fix feed tag search and prepare 2.0.1+22
This commit is contained in:
@@ -3,33 +3,57 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../../../backend/backend.dart';
|
||||
import '../../../core/utils/result.dart';
|
||||
|
||||
// Меняем FutureProvider на StreamProvider
|
||||
final postDetailsControllerProvider =
|
||||
FutureProvider.family<Post?, PostDetailsArgs>((ref, args) async {
|
||||
StreamProvider.family<Post?, PostDetailsArgs>((ref, args) async* {
|
||||
// Правильно собираем зависимости на самом верхнем уровне (фиксим антипаттерн)
|
||||
final providerManager = ref.watch(providerManagerProvider);
|
||||
final cacheService = ref.watch(cacheServiceProvider);
|
||||
final postRepository = ref.watch(postRepositoryProvider);
|
||||
|
||||
// Вспомогательная функция для загрузки тегов
|
||||
Future<Post?> enrich(Post? post) async {
|
||||
if (post == null) return null;
|
||||
final shouldEnrich = post.tagGroups.isEmpty ||
|
||||
(post.tagGroups.length == 1 && post.tagGroups.containsKey('general'));
|
||||
if (!shouldEnrich) return post;
|
||||
final enriched =
|
||||
await ref.watch(providerManagerProvider).enrichPostTags(post);
|
||||
|
||||
final enriched = await providerManager.enrichPostTags(post);
|
||||
if (enriched is Success<Post>) {
|
||||
await ref.watch(cacheServiceProvider).cachePosts([enriched.data]);
|
||||
await cacheService.cachePosts([enriched.data]);
|
||||
return enriched.data;
|
||||
}
|
||||
return post;
|
||||
}
|
||||
|
||||
if (args.initialPost != null) return enrich(args.initialPost);
|
||||
final cached = await ref
|
||||
.watch(postRepositoryProvider)
|
||||
.getCachedPost(args.postId, args.providerId);
|
||||
// СЦЕНАРИЙ 1: Пост уже передан из ленты (Самый частый случай)
|
||||
if (args.initialPost != null) {
|
||||
yield args
|
||||
.initialPost; // МГНОВЕННО отдаем пост в UI, экран сразу рендерится!
|
||||
final enriched = await enrich(args.initialPost);
|
||||
yield enriched; // Чуть позже отдаем версию с загруженными тегами
|
||||
return;
|
||||
}
|
||||
|
||||
// СЦЕНАРИЙ 2: Прямой переход (например, по ссылке), проверяем локальный кэш
|
||||
final cached =
|
||||
await postRepository.getCachedPost(args.postId, args.providerId);
|
||||
if (cached is Success<Post?> && cached.data != null) {
|
||||
return enrich(cached.data);
|
||||
yield cached.data; // Нашли в кэше — сразу показываем контент
|
||||
final enriched = await enrich(cached.data);
|
||||
yield enriched; // Догружаем теги на фоне
|
||||
return;
|
||||
}
|
||||
|
||||
// СЦЕНАРИЙ 3: Полный фоллбэк, если вообще ничего нет — идем в сеть за самим постом
|
||||
final remote = await providerManager.getPost(args.providerId, args.postId);
|
||||
if (remote is Success<Post?> && remote.data != null) {
|
||||
yield remote.data;
|
||||
final enriched = await enrich(remote.data);
|
||||
yield enriched;
|
||||
} else {
|
||||
yield null;
|
||||
}
|
||||
final remote = await ref
|
||||
.watch(providerManagerProvider)
|
||||
.getPost(args.providerId, args.postId);
|
||||
return remote is Success<Post?> ? enrich(remote.data) : null;
|
||||
});
|
||||
|
||||
class PostDetailsArgs {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user