217 lines
7.6 KiB
Dart
217 lines
7.6 KiB
Dart
/// News Local DataSource
|
|
///
|
|
/// Handles all local data operations for news articles.
|
|
/// Currently provides mock data for development and testing.
|
|
/// Will be extended to use Hive cache when backend API is available.
|
|
library;
|
|
|
|
import 'package:worker/features/news/data/models/news_article_model.dart';
|
|
|
|
/// News Local Data Source
|
|
///
|
|
/// Provides mock data for news articles.
|
|
/// In production, this will cache data from the remote API.
|
|
class NewsLocalDataSource {
|
|
/// Get all news articles
|
|
///
|
|
/// Returns a list of all articles from mock data.
|
|
/// In production, this will fetch from Hive cache.
|
|
Future<List<NewsArticleModel>> getAllArticles() async {
|
|
// Simulate network delay
|
|
await Future<void>.delayed(const Duration(milliseconds: 300));
|
|
|
|
return _mockArticles;
|
|
}
|
|
|
|
/// Get featured article
|
|
///
|
|
/// Returns the main featured article for the top section.
|
|
Future<NewsArticleModel?> getFeaturedArticle() async {
|
|
// Simulate network delay
|
|
await Future<void>.delayed(const Duration(milliseconds: 200));
|
|
|
|
try {
|
|
return _mockArticles.firstWhere((article) => article.isFeatured);
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// Get articles by category
|
|
///
|
|
/// Returns filtered list of articles matching the [category].
|
|
Future<List<NewsArticleModel>> getArticlesByCategory(String category) async {
|
|
// Simulate network delay
|
|
await Future<void>.delayed(const Duration(milliseconds: 200));
|
|
|
|
if (category == 'all') {
|
|
return _mockArticles;
|
|
}
|
|
|
|
return _mockArticles
|
|
.where(
|
|
(article) => article.category.toLowerCase() == category.toLowerCase(),
|
|
)
|
|
.toList();
|
|
}
|
|
|
|
/// Get a specific article by ID
|
|
///
|
|
/// Returns the article if found, null otherwise.
|
|
Future<NewsArticleModel?> getArticleById(String articleId) async {
|
|
// Simulate network delay
|
|
await Future<void>.delayed(const Duration(milliseconds: 100));
|
|
|
|
try {
|
|
return _mockArticles.firstWhere((article) => article.id == articleId);
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// Check if cache is valid
|
|
///
|
|
/// Returns true if cached data is still valid.
|
|
/// Currently always returns false since we're using mock data.
|
|
Future<bool> isCacheValid() async {
|
|
// TODO: Implement cache validation when using Hive
|
|
return false;
|
|
}
|
|
|
|
/// Cache articles locally
|
|
///
|
|
/// Saves articles to Hive for offline access.
|
|
/// Currently not implemented (using mock data).
|
|
Future<void> cacheArticles(List<NewsArticleModel> articles) async {
|
|
// TODO: Implement Hive caching when backend API is ready
|
|
}
|
|
|
|
/// Clear cached articles
|
|
///
|
|
/// Removes all cached articles from Hive.
|
|
/// Currently not implemented (using mock data).
|
|
Future<void> clearCache() async {
|
|
// TODO: Implement cache clearing when using Hive
|
|
}
|
|
|
|
/// Mock articles matching HTML design
|
|
///
|
|
/// This data will be replaced with real API data in production.
|
|
static final List<NewsArticleModel> _mockArticles = [
|
|
// Featured article
|
|
const NewsArticleModel(
|
|
id: 'featured-1',
|
|
title: '5 xu hướng gạch men phòng tắm được ưa chuộng năm 2024',
|
|
excerpt:
|
|
'Khám phá những mẫu gạch men hiện đại, sang trọng cho không gian phòng tắm. Từ những tone màu trung tính đến các họa tiết độc đáo, cùng tìm hiểu các xu hướng đang được yêu thích nhất.',
|
|
imageUrl:
|
|
'https://images.unsplash.com/photo-1503387762-592deb58ef4e?w=400&h=200&fit=crop',
|
|
category: 'news',
|
|
publishedDate: '2024-11-15T00:00:00.000Z',
|
|
viewCount: 2300,
|
|
readingTimeMinutes: 5,
|
|
isFeatured: true,
|
|
),
|
|
|
|
// Latest articles
|
|
const NewsArticleModel(
|
|
id: 'news-1',
|
|
title: 'Hướng dẫn thi công gạch granite 60x60 chuyên nghiệp',
|
|
excerpt:
|
|
'Quy trình thi công chi tiết từ A-Z cho thầy thợ xây dựng. Các bước chuẩn bị, kỹ thuật thi công và kinh nghiệm thực tế.',
|
|
imageUrl:
|
|
'https://images.unsplash.com/photo-1586023492125-27b2c045efd7?w=80&h=80&fit=crop',
|
|
category: 'professional',
|
|
publishedDate: '2024-11-12T00:00:00.000Z',
|
|
viewCount: 1800,
|
|
readingTimeMinutes: 8,
|
|
isFeatured: false,
|
|
),
|
|
|
|
const NewsArticleModel(
|
|
id: 'news-2',
|
|
title: 'Bảng giá gạch men cao cấp mới nhất tháng 11/2024',
|
|
excerpt:
|
|
'Cập nhật bảng giá chi tiết các dòng sản phẩm gạch men nhập khẩu. So sánh giá các thương hiệu hàng đầu.',
|
|
imageUrl:
|
|
'https://images.unsplash.com/photo-1560448204-e02f11c3d0e2?w=80&h=80&fit=crop',
|
|
category: 'news',
|
|
publishedDate: '2024-11-10T00:00:00.000Z',
|
|
viewCount: 3100,
|
|
readingTimeMinutes: 4,
|
|
isFeatured: false,
|
|
),
|
|
|
|
const NewsArticleModel(
|
|
id: 'news-3',
|
|
title: 'Mẹo chọn gạch ốp tường phòng bếp đẹp và bền',
|
|
excerpt:
|
|
'Những lưu ý quan trọng khi chọn gạch ốp tường cho khu vực bếp. Tư vấn về chất liệu, màu sắc và kích thước phù hợp.',
|
|
imageUrl:
|
|
'https://images.unsplash.com/photo-1545558014-8692077e9b5c?w=80&h=80&fit=crop',
|
|
category: 'professional',
|
|
publishedDate: '2024-11-08T00:00:00.000Z',
|
|
viewCount: 1500,
|
|
readingTimeMinutes: 6,
|
|
isFeatured: false,
|
|
),
|
|
|
|
const NewsArticleModel(
|
|
id: 'news-4',
|
|
title: 'Dự án biệt thự Quận 2: Ứng dụng gạch men cao cấp',
|
|
excerpt:
|
|
'Case study về việc sử dụng gạch men trong dự án biệt thự 300m². Chia sẻ kinh nghiệm và bài học từ thầu thợ.',
|
|
imageUrl:
|
|
'https://images.unsplash.com/photo-1484101403633-562f891dc89a?w=80&h=80&fit=crop',
|
|
category: 'projects',
|
|
publishedDate: '2024-11-05T00:00:00.000Z',
|
|
viewCount: 2700,
|
|
readingTimeMinutes: 10,
|
|
isFeatured: false,
|
|
),
|
|
|
|
const NewsArticleModel(
|
|
id: 'news-5',
|
|
title: 'Công cụ hỗ trợ tính toán diện tích gạch chính xác',
|
|
excerpt:
|
|
'Hướng dẫn sử dụng các công cụ và ứng dụng giúp tính toán diện tích gạch cần thiết cho công trình một cách chính xác nhất.',
|
|
imageUrl:
|
|
'https://images.unsplash.com/photo-1558618666-fcd25c85cd64?w=80&h=80&fit=crop',
|
|
category: 'professional',
|
|
publishedDate: '2024-11-03T00:00:00.000Z',
|
|
viewCount: 1200,
|
|
readingTimeMinutes: 7,
|
|
isFeatured: false,
|
|
),
|
|
|
|
// Additional articles for different categories
|
|
const NewsArticleModel(
|
|
id: 'event-1',
|
|
title: 'Hội nghị thầu thợ miền Nam 2024',
|
|
excerpt:
|
|
'Tham gia sự kiện kết nối, chia sẻ kinh nghiệm và cập nhật xu hướng mới nhất trong ngành xây dựng.',
|
|
imageUrl:
|
|
'https://images.unsplash.com/photo-1540575467063-178a50c2df87?w=80&h=80&fit=crop',
|
|
category: 'events',
|
|
publishedDate: '2024-11-01T00:00:00.000Z',
|
|
viewCount: 950,
|
|
readingTimeMinutes: 3,
|
|
isFeatured: false,
|
|
),
|
|
|
|
const NewsArticleModel(
|
|
id: 'promo-1',
|
|
title: 'Khuyến mãi mua 10 tặng 1 - Gạch Granite 60x60',
|
|
excerpt:
|
|
'Chương trình ưu đãi đặc biệt dành cho thầu thợ và đại lý. Áp dụng cho đơn hàng từ 500m² trở lên.',
|
|
imageUrl:
|
|
'https://images.unsplash.com/photo-1607400201889-565b1ee75f8e?w=80&h=80&fit=crop',
|
|
category: 'promotions',
|
|
publishedDate: '2024-10-28T00:00:00.000Z',
|
|
viewCount: 4200,
|
|
readingTimeMinutes: 2,
|
|
isFeatured: false,
|
|
),
|
|
];
|
|
}
|