add detail, fetch products, categories

This commit is contained in:
Phuoc Nguyen
2025-10-15 17:46:50 +07:00
parent 4038f8e8a6
commit 02e5fd4244
12 changed files with 814 additions and 84 deletions

View File

@@ -1,31 +1,92 @@
import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../../domain/entities/category.dart';
import '../../data/providers/category_providers.dart';
import '../../../../core/providers/providers.dart';
part 'categories_provider.g.dart';
/// Provider for categories list
/// Provider for categories list with API-first approach
@riverpod
class Categories extends _$Categories {
@override
Future<List<Category>> build() async {
// TODO: Implement with repository
return [];
// API-first: Try to load from API first
final repository = ref.watch(categoryRepositoryProvider);
final networkInfo = ref.watch(networkInfoProvider);
// Check if online
final isConnected = await networkInfo.isConnected;
if (isConnected) {
// Try API first
try {
final syncResult = await repository.syncCategories();
return syncResult.fold(
(failure) {
// API failed, fallback to cache
print('Categories API failed, falling back to cache: ${failure.message}');
return _loadFromCache();
},
(categories) => categories,
);
} catch (e) {
// API error, fallback to cache
print('Categories API error, falling back to cache: $e');
return _loadFromCache();
}
} else {
// Offline, load from cache
print('Offline, loading categories from cache');
return _loadFromCache();
}
}
/// Load categories from local cache
Future<List<Category>> _loadFromCache() async {
final repository = ref.read(categoryRepositoryProvider);
final result = await repository.getAllCategories();
return result.fold(
(failure) {
print('Categories cache load failed: ${failure.message}');
return <Category>[];
},
(categories) => categories,
);
}
/// Refresh categories from local storage
Future<void> refresh() async {
state = const AsyncValue.loading();
state = await AsyncValue.guard(() async {
// Fetch categories from repository
return [];
final repository = ref.read(categoryRepositoryProvider);
final result = await repository.getAllCategories();
return result.fold(
(failure) => throw Exception(failure.message),
(categories) => categories,
);
});
}
/// Sync categories from API and update local storage
Future<void> syncCategories() async {
// TODO: Implement sync logic with remote data source
final networkInfo = ref.read(networkInfoProvider);
final isConnected = await networkInfo.isConnected;
if (!isConnected) {
throw Exception('No internet connection');
}
state = const AsyncValue.loading();
state = await AsyncValue.guard(() async {
// Sync categories from API
return [];
final repository = ref.read(categoryRepositoryProvider);
final result = await repository.syncCategories();
return result.fold(
(failure) => throw Exception(failure.message),
(categories) => categories,
);
});
}
}

View File

@@ -8,15 +8,15 @@ part of 'categories_provider.dart';
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint, type=warning
/// Provider for categories list
/// Provider for categories list with API-first approach
@ProviderFor(Categories)
const categoriesProvider = CategoriesProvider._();
/// Provider for categories list
/// Provider for categories list with API-first approach
final class CategoriesProvider
extends $AsyncNotifierProvider<Categories, List<Category>> {
/// Provider for categories list
/// Provider for categories list with API-first approach
const CategoriesProvider._()
: super(
from: null,
@@ -36,9 +36,9 @@ final class CategoriesProvider
Categories create() => Categories();
}
String _$categoriesHash() => r'aa7afc38a5567b0f42ff05ca23b287baa4780cbe';
String _$categoriesHash() => r'33c33b08f8926e5bbbd112285591c74a3ff0f61c';
/// Provider for categories list
/// Provider for categories list with API-first approach
abstract class _$Categories extends $AsyncNotifier<List<Category>> {
FutureOr<List<Category>> build();