news page
This commit is contained in:
101
lib/features/news/presentation/providers/news_provider.dart
Normal file
101
lib/features/news/presentation/providers/news_provider.dart
Normal file
@@ -0,0 +1,101 @@
|
||||
/// News Providers
|
||||
///
|
||||
/// State management for news articles using Riverpod.
|
||||
/// Provides access to news data and filtering capabilities.
|
||||
library;
|
||||
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:worker/features/news/data/datasources/news_local_datasource.dart';
|
||||
import 'package:worker/features/news/data/repositories/news_repository_impl.dart';
|
||||
import 'package:worker/features/news/domain/entities/news_article.dart';
|
||||
import 'package:worker/features/news/domain/repositories/news_repository.dart';
|
||||
|
||||
part 'news_provider.g.dart';
|
||||
|
||||
/// News Local DataSource Provider
|
||||
///
|
||||
/// Provides instance of NewsLocalDataSource.
|
||||
@riverpod
|
||||
NewsLocalDataSource newsLocalDataSource(Ref ref) {
|
||||
return NewsLocalDataSource();
|
||||
}
|
||||
|
||||
/// News Repository Provider
|
||||
///
|
||||
/// Provides instance of NewsRepository implementation.
|
||||
@riverpod
|
||||
NewsRepository newsRepository(Ref ref) {
|
||||
final localDataSource = ref.watch(newsLocalDataSourceProvider);
|
||||
return NewsRepositoryImpl(localDataSource: localDataSource);
|
||||
}
|
||||
|
||||
/// News Articles Provider
|
||||
///
|
||||
/// Fetches all news articles sorted by published date.
|
||||
/// Returns AsyncValue<List<NewsArticle>> for proper loading/error handling.
|
||||
@riverpod
|
||||
Future<List<NewsArticle>> newsArticles(Ref ref) async {
|
||||
final repository = ref.watch(newsRepositoryProvider);
|
||||
return repository.getAllArticles();
|
||||
}
|
||||
|
||||
/// Featured Article Provider
|
||||
///
|
||||
/// Fetches the featured article for the top section.
|
||||
/// Returns AsyncValue<NewsArticle?> (null if no featured article).
|
||||
@riverpod
|
||||
Future<NewsArticle?> featuredArticle(Ref ref) async {
|
||||
final repository = ref.watch(newsRepositoryProvider);
|
||||
return repository.getFeaturedArticle();
|
||||
}
|
||||
|
||||
/// Selected News Category Provider
|
||||
///
|
||||
/// Manages the currently selected category filter.
|
||||
/// null means "All" is selected (show all categories).
|
||||
@riverpod
|
||||
class SelectedNewsCategory extends _$SelectedNewsCategory {
|
||||
@override
|
||||
NewsCategory? build() {
|
||||
// Default: show all categories
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Set selected category
|
||||
void setCategory(NewsCategory? category) {
|
||||
state = category;
|
||||
}
|
||||
|
||||
/// Clear selection (show all)
|
||||
void clearSelection() {
|
||||
state = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// Filtered News Articles Provider
|
||||
///
|
||||
/// Returns news articles filtered by selected category.
|
||||
/// If no category is selected, returns all articles.
|
||||
@riverpod
|
||||
Future<List<NewsArticle>> filteredNewsArticles(Ref ref) async {
|
||||
final selectedCategory = ref.watch(selectedNewsCategoryProvider);
|
||||
final repository = ref.watch(newsRepositoryProvider);
|
||||
|
||||
// If no category selected, return all articles
|
||||
if (selectedCategory == null) {
|
||||
return repository.getAllArticles();
|
||||
}
|
||||
|
||||
// Filter by selected category
|
||||
return repository.getArticlesByCategory(selectedCategory);
|
||||
}
|
||||
|
||||
/// News Article by ID Provider
|
||||
///
|
||||
/// Fetches a specific article by ID.
|
||||
/// Used for article detail page.
|
||||
@riverpod
|
||||
Future<NewsArticle?> newsArticleById(Ref ref, String articleId) async {
|
||||
final repository = ref.watch(newsRepositoryProvider);
|
||||
return repository.getArticleById(articleId);
|
||||
}
|
||||
Reference in New Issue
Block a user