update perf
This commit is contained in:
@@ -1,23 +1,25 @@
|
||||
/// Provider: Promotions Provider
|
||||
///
|
||||
/// Manages the state of promotions data using Riverpod.
|
||||
/// Provides access to active promotions throughout the app.
|
||||
/// Uses the same data source as news articles (single API call).
|
||||
///
|
||||
/// Uses AsyncNotifierProvider for automatic loading, error, and data states.
|
||||
library;
|
||||
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:worker/features/home/data/datasources/home_local_datasource.dart';
|
||||
import 'package:worker/features/home/data/repositories/home_repository_impl.dart';
|
||||
import 'package:worker/features/home/domain/entities/promotion.dart';
|
||||
import 'package:worker/features/home/domain/usecases/get_promotions.dart';
|
||||
import 'package:worker/features/news/presentation/providers/news_provider.dart';
|
||||
|
||||
part 'promotions_provider.g.dart';
|
||||
|
||||
/// Max number of promotions to display on home page
|
||||
const int _maxPromotions = 5;
|
||||
|
||||
/// Promotions Provider
|
||||
///
|
||||
/// Fetches and caches the list of active promotions.
|
||||
/// Automatically handles loading, error, and data states.
|
||||
/// Uses the same data source as news articles to avoid duplicate API calls.
|
||||
/// Converts NewsArticle to Promotion entity for display in PromotionSlider.
|
||||
/// Limited to 5 items max.
|
||||
///
|
||||
/// Usage:
|
||||
/// ```dart
|
||||
@@ -31,33 +33,22 @@ part 'promotions_provider.g.dart';
|
||||
/// );
|
||||
/// ```
|
||||
@riverpod
|
||||
class PromotionsNotifier extends _$PromotionsNotifier {
|
||||
@override
|
||||
Future<List<Promotion>> build() async {
|
||||
// Initialize dependencies
|
||||
final localDataSource = const HomeLocalDataSourceImpl();
|
||||
final repository = HomeRepositoryImpl(localDataSource: localDataSource);
|
||||
final useCase = GetPromotions(repository);
|
||||
Future<List<Promotion>> promotions(Ref ref) async {
|
||||
// Use newsArticles provider (same API call, no duplicate request)
|
||||
final articles = await ref.watch(newsArticlesProvider.future);
|
||||
|
||||
// Fetch promotions (only active ones)
|
||||
return await useCase();
|
||||
}
|
||||
// Take max 5 articles and convert to Promotion
|
||||
final limitedArticles = articles.take(_maxPromotions).toList();
|
||||
|
||||
/// Refresh promotions data
|
||||
///
|
||||
/// Forces a refresh from the server (when API is available).
|
||||
/// Updates the cached state with fresh data.
|
||||
Future<void> refresh() async {
|
||||
// Set loading state
|
||||
state = const AsyncValue.loading();
|
||||
|
||||
// Fetch fresh data
|
||||
state = await AsyncValue.guard(() async {
|
||||
final localDataSource = const HomeLocalDataSourceImpl();
|
||||
final repository = HomeRepositoryImpl(localDataSource: localDataSource);
|
||||
final useCase = GetPromotions(repository);
|
||||
|
||||
return await useCase.refresh();
|
||||
});
|
||||
}
|
||||
return limitedArticles.map((article) {
|
||||
final now = DateTime.now();
|
||||
return Promotion(
|
||||
id: article.id,
|
||||
title: article.title,
|
||||
description: article.excerpt,
|
||||
imageUrl: article.imageUrl,
|
||||
startDate: article.publishedDate,
|
||||
endDate: now.add(const Duration(days: 365)), // Always active
|
||||
);
|
||||
}).toList();
|
||||
}
|
||||
|
||||
@@ -10,8 +10,9 @@ part of 'promotions_provider.dart';
|
||||
// ignore_for_file: type=lint, type=warning
|
||||
/// Promotions Provider
|
||||
///
|
||||
/// Fetches and caches the list of active promotions.
|
||||
/// Automatically handles loading, error, and data states.
|
||||
/// Uses the same data source as news articles to avoid duplicate API calls.
|
||||
/// Converts NewsArticle to Promotion entity for display in PromotionSlider.
|
||||
/// Limited to 5 items max.
|
||||
///
|
||||
/// Usage:
|
||||
/// ```dart
|
||||
@@ -25,13 +26,14 @@ part of 'promotions_provider.dart';
|
||||
/// );
|
||||
/// ```
|
||||
|
||||
@ProviderFor(PromotionsNotifier)
|
||||
const promotionsProvider = PromotionsNotifierProvider._();
|
||||
@ProviderFor(promotions)
|
||||
const promotionsProvider = PromotionsProvider._();
|
||||
|
||||
/// Promotions Provider
|
||||
///
|
||||
/// Fetches and caches the list of active promotions.
|
||||
/// Automatically handles loading, error, and data states.
|
||||
/// Uses the same data source as news articles to avoid duplicate API calls.
|
||||
/// Converts NewsArticle to Promotion entity for display in PromotionSlider.
|
||||
/// Limited to 5 items max.
|
||||
///
|
||||
/// Usage:
|
||||
/// ```dart
|
||||
@@ -44,12 +46,20 @@ const promotionsProvider = PromotionsNotifierProvider._();
|
||||
/// error: (error, stack) => ErrorWidget(error),
|
||||
/// );
|
||||
/// ```
|
||||
final class PromotionsNotifierProvider
|
||||
extends $AsyncNotifierProvider<PromotionsNotifier, List<Promotion>> {
|
||||
|
||||
final class PromotionsProvider
|
||||
extends
|
||||
$FunctionalProvider<
|
||||
AsyncValue<List<Promotion>>,
|
||||
List<Promotion>,
|
||||
FutureOr<List<Promotion>>
|
||||
>
|
||||
with $FutureModifier<List<Promotion>>, $FutureProvider<List<Promotion>> {
|
||||
/// Promotions Provider
|
||||
///
|
||||
/// Fetches and caches the list of active promotions.
|
||||
/// Automatically handles loading, error, and data states.
|
||||
/// Uses the same data source as news articles to avoid duplicate API calls.
|
||||
/// Converts NewsArticle to Promotion entity for display in PromotionSlider.
|
||||
/// Limited to 5 items max.
|
||||
///
|
||||
/// Usage:
|
||||
/// ```dart
|
||||
@@ -62,7 +72,7 @@ final class PromotionsNotifierProvider
|
||||
/// error: (error, stack) => ErrorWidget(error),
|
||||
/// );
|
||||
/// ```
|
||||
const PromotionsNotifierProvider._()
|
||||
const PromotionsProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
@@ -74,48 +84,18 @@ final class PromotionsNotifierProvider
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$promotionsNotifierHash();
|
||||
String debugGetCreateSourceHash() => _$promotionsHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
PromotionsNotifier create() => PromotionsNotifier();
|
||||
}
|
||||
$FutureProviderElement<List<Promotion>> $createElement(
|
||||
$ProviderPointer pointer,
|
||||
) => $FutureProviderElement(pointer);
|
||||
|
||||
String _$promotionsNotifierHash() =>
|
||||
r'3cd866c74ba11c6519e9b63521e1757ef117c7a9';
|
||||
|
||||
/// Promotions Provider
|
||||
///
|
||||
/// Fetches and caches the list of active promotions.
|
||||
/// Automatically handles loading, error, and data states.
|
||||
///
|
||||
/// Usage:
|
||||
/// ```dart
|
||||
/// // In a ConsumerWidget
|
||||
/// final promotionsAsync = ref.watch(promotionsProvider);
|
||||
///
|
||||
/// promotionsAsync.when(
|
||||
/// data: (promotions) => PromotionSlider(promotions: promotions),
|
||||
/// loading: () => CircularProgressIndicator(),
|
||||
/// error: (error, stack) => ErrorWidget(error),
|
||||
/// );
|
||||
/// ```
|
||||
|
||||
abstract class _$PromotionsNotifier extends $AsyncNotifier<List<Promotion>> {
|
||||
FutureOr<List<Promotion>> build();
|
||||
@$mustCallSuper
|
||||
@override
|
||||
void runBuild() {
|
||||
final created = build();
|
||||
final ref = this.ref as $Ref<AsyncValue<List<Promotion>>, List<Promotion>>;
|
||||
final element =
|
||||
ref.element
|
||||
as $ClassProviderElement<
|
||||
AnyNotifier<AsyncValue<List<Promotion>>, List<Promotion>>,
|
||||
AsyncValue<List<Promotion>>,
|
||||
Object?,
|
||||
Object?
|
||||
>;
|
||||
element.handleValue(ref, created);
|
||||
FutureOr<List<Promotion>> create(Ref ref) {
|
||||
return promotions(ref);
|
||||
}
|
||||
}
|
||||
|
||||
String _$promotionsHash() => r'2eac0298d2b84ad5cc50faa6b8a015dbf7b7a1d3';
|
||||
|
||||
Reference in New Issue
Block a user