Merge branch 'main' of https://git.renolation.com/renolation/retail
# Conflicts: # docs/API_RESPONSE_FIX.md # docs/AUTH_UI_SUMMARY.md # docs/AUTO_LOGIN_DEBUG.md # docs/AUTO_LOGIN_FIXED.md # docs/BUILD_STATUS.md # docs/CLEANUP_COMPLETE.md # docs/EXPORT_FILES_SUMMARY.md # docs/RIVERPOD_DI_MIGRATION.md # docs/TEST_AUTO_LOGIN.md # lib/features/categories/data/datasources/category_remote_datasource.dart # lib/features/categories/presentation/providers/categories_provider.dart # lib/features/categories/presentation/providers/categories_provider.g.dart # lib/features/products/data/datasources/product_remote_datasource.dart # lib/features/products/data/models/product_model.dart # lib/features/products/presentation/pages/products_page.dart # lib/features/products/presentation/providers/products_provider.dart # lib/features/products/presentation/providers/products_provider.g.dart
This commit is contained in:
@@ -1,193 +1,101 @@
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import '../../domain/entities/category.dart';
|
||||
import '../../data/models/category_model.dart';
|
||||
import '../../../products/data/models/product_model.dart';
|
||||
import '../../../products/domain/entities/product.dart';
|
||||
import 'category_remote_datasource_provider.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 {
|
||||
return await _fetchCategories();
|
||||
}
|
||||
// API-first: Try to load from API first
|
||||
final repository = ref.watch(categoryRepositoryProvider);
|
||||
final networkInfo = ref.watch(networkInfoProvider);
|
||||
|
||||
Future<List<Category>> _fetchCategories() async {
|
||||
final datasource = ref.read(categoryRemoteDataSourceProvider);
|
||||
final categoryModels = await datasource.getAllCategories();
|
||||
return categoryModels.map((model) => model.toEntity()).toList();
|
||||
}
|
||||
// Check if online
|
||||
final isConnected = await networkInfo.isConnected;
|
||||
|
||||
Future<void> refresh() async {
|
||||
state = const AsyncValue.loading();
|
||||
state = await AsyncValue.guard(() async {
|
||||
return await _fetchCategories();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Provider for single category by ID
|
||||
@riverpod
|
||||
Future<Category> category(Ref ref, String id) async {
|
||||
final datasource = ref.read(categoryRemoteDataSourceProvider);
|
||||
final categoryModel = await datasource.getCategoryById(id);
|
||||
return categoryModel.toEntity();
|
||||
}
|
||||
|
||||
/// Pagination state for category products
|
||||
class CategoryProductsState {
|
||||
final Category category;
|
||||
final List<Product> products;
|
||||
final int currentPage;
|
||||
final int totalPages;
|
||||
final int totalItems;
|
||||
final bool hasMore;
|
||||
final bool isLoadingMore;
|
||||
|
||||
const CategoryProductsState({
|
||||
required this.category,
|
||||
required this.products,
|
||||
required this.currentPage,
|
||||
required this.totalPages,
|
||||
required this.totalItems,
|
||||
required this.hasMore,
|
||||
this.isLoadingMore = false,
|
||||
});
|
||||
|
||||
CategoryProductsState copyWith({
|
||||
Category? category,
|
||||
List<Product>? products,
|
||||
int? currentPage,
|
||||
int? totalPages,
|
||||
int? totalItems,
|
||||
bool? hasMore,
|
||||
bool? isLoadingMore,
|
||||
}) {
|
||||
return CategoryProductsState(
|
||||
category: category ?? this.category,
|
||||
products: products ?? this.products,
|
||||
currentPage: currentPage ?? this.currentPage,
|
||||
totalPages: totalPages ?? this.totalPages,
|
||||
totalItems: totalItems ?? this.totalItems,
|
||||
hasMore: hasMore ?? this.hasMore,
|
||||
isLoadingMore: isLoadingMore ?? this.isLoadingMore,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Provider for category with its products (with pagination)
|
||||
@riverpod
|
||||
class CategoryWithProducts extends _$CategoryWithProducts {
|
||||
static const int _limit = 20;
|
||||
|
||||
@override
|
||||
Future<CategoryProductsState> build(String categoryId) async {
|
||||
return await _fetchCategoryWithProducts(categoryId: categoryId, page: 1);
|
||||
}
|
||||
|
||||
Future<CategoryProductsState> _fetchCategoryWithProducts({
|
||||
required String categoryId,
|
||||
required int page,
|
||||
}) async {
|
||||
final datasource = ref.read(categoryRemoteDataSourceProvider);
|
||||
|
||||
final response = await datasource.getCategoryWithProducts(
|
||||
categoryId,
|
||||
page,
|
||||
_limit,
|
||||
);
|
||||
|
||||
// Extract data
|
||||
final CategoryModel categoryModel = response['category'] as CategoryModel;
|
||||
final List<dynamic> productsJson = response['products'] as List<dynamic>;
|
||||
final meta = response['meta'] as Map<String, dynamic>;
|
||||
|
||||
// Convert category to entity
|
||||
final category = categoryModel.toEntity();
|
||||
|
||||
// Convert products to entities
|
||||
final products = productsJson
|
||||
.map((json) => ProductModel.fromJson(json as Map<String, dynamic>))
|
||||
.map((model) => model.toEntity())
|
||||
.toList();
|
||||
|
||||
// Extract pagination info
|
||||
final currentPage = meta['currentPage'] as int? ?? page;
|
||||
final totalPages = meta['totalPages'] as int? ?? 1;
|
||||
final totalItems = meta['totalItems'] as int? ?? products.length;
|
||||
final hasMore = currentPage < totalPages;
|
||||
|
||||
return CategoryProductsState(
|
||||
category: category,
|
||||
products: products,
|
||||
currentPage: currentPage,
|
||||
totalPages: totalPages,
|
||||
totalItems: totalItems,
|
||||
hasMore: hasMore,
|
||||
);
|
||||
}
|
||||
|
||||
/// Load more products (next page)
|
||||
Future<void> loadMore() async {
|
||||
final currentState = state.value;
|
||||
if (currentState == null || !currentState.hasMore) return;
|
||||
|
||||
// Set loading more flag
|
||||
state = AsyncValue.data(
|
||||
currentState.copyWith(isLoadingMore: true),
|
||||
);
|
||||
|
||||
// Fetch next page
|
||||
final nextPage = currentState.currentPage + 1;
|
||||
|
||||
try {
|
||||
final newState = await _fetchCategoryWithProducts(
|
||||
categoryId: currentState.category.id,
|
||||
page: nextPage,
|
||||
);
|
||||
|
||||
// Append new products to existing ones
|
||||
state = AsyncValue.data(
|
||||
newState.copyWith(
|
||||
products: [...currentState.products, ...newState.products],
|
||||
isLoadingMore: false,
|
||||
),
|
||||
);
|
||||
} catch (error, stackTrace) {
|
||||
// Restore previous state on error
|
||||
state = AsyncValue.data(
|
||||
currentState.copyWith(isLoadingMore: false),
|
||||
);
|
||||
state = AsyncValue.error(error, stackTrace);
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/// Refresh category and products
|
||||
/// 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 {
|
||||
final currentState = state.value;
|
||||
if (currentState == null) return;
|
||||
state = const AsyncValue.loading();
|
||||
state = await AsyncValue.guard(() async {
|
||||
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 {
|
||||
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 {
|
||||
return await _fetchCategoryWithProducts(
|
||||
categoryId: currentState.category.id,
|
||||
page: 1,
|
||||
final repository = ref.read(categoryRepositoryProvider);
|
||||
final result = await repository.syncCategories();
|
||||
|
||||
return result.fold(
|
||||
(failure) => throw Exception(failure.message),
|
||||
(categories) => categories,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Provider for selected category state
|
||||
/// This is used in the products feature for filtering
|
||||
/// Provider for selected category
|
||||
@riverpod
|
||||
class SelectedCategoryInCategories extends _$SelectedCategoryInCategories {
|
||||
class SelectedCategory extends _$SelectedCategory {
|
||||
@override
|
||||
String? build() {
|
||||
return null;
|
||||
}
|
||||
String? build() => null;
|
||||
|
||||
void select(String? categoryId) {
|
||||
state = categoryId;
|
||||
@@ -196,8 +104,4 @@ class SelectedCategoryInCategories extends _$SelectedCategoryInCategories {
|
||||
void clear() {
|
||||
state = null;
|
||||
}
|
||||
|
||||
bool get hasSelection => state != null;
|
||||
|
||||
bool isSelected(String categoryId) => state == categoryId;
|
||||
}
|
||||
|
||||
@@ -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'5156d31a6d7b9457c4735b66e170b262140758e2';
|
||||
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();
|
||||
@@ -59,223 +59,32 @@ abstract class _$Categories extends $AsyncNotifier<List<Category>> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Provider for single category by ID
|
||||
/// Provider for selected category
|
||||
|
||||
@ProviderFor(category)
|
||||
const categoryProvider = CategoryFamily._();
|
||||
@ProviderFor(SelectedCategory)
|
||||
const selectedCategoryProvider = SelectedCategoryProvider._();
|
||||
|
||||
/// Provider for single category by ID
|
||||
|
||||
final class CategoryProvider
|
||||
extends
|
||||
$FunctionalProvider<AsyncValue<Category>, Category, FutureOr<Category>>
|
||||
with $FutureModifier<Category>, $FutureProvider<Category> {
|
||||
/// Provider for single category by ID
|
||||
const CategoryProvider._({
|
||||
required CategoryFamily super.from,
|
||||
required String super.argument,
|
||||
}) : super(
|
||||
retry: null,
|
||||
name: r'categoryProvider',
|
||||
isAutoDispose: true,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$categoryHash();
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return r'categoryProvider'
|
||||
''
|
||||
'($argument)';
|
||||
}
|
||||
|
||||
@$internal
|
||||
@override
|
||||
$FutureProviderElement<Category> $createElement($ProviderPointer pointer) =>
|
||||
$FutureProviderElement(pointer);
|
||||
|
||||
@override
|
||||
FutureOr<Category> create(Ref ref) {
|
||||
final argument = this.argument as String;
|
||||
return category(ref, argument);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return other is CategoryProvider && other.argument == argument;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
return argument.hashCode;
|
||||
}
|
||||
}
|
||||
|
||||
String _$categoryHash() => r'e26dd362e42a1217a774072f453a64c7a6195e73';
|
||||
|
||||
/// Provider for single category by ID
|
||||
|
||||
final class CategoryFamily extends $Family
|
||||
with $FunctionalFamilyOverride<FutureOr<Category>, String> {
|
||||
const CategoryFamily._()
|
||||
: super(
|
||||
retry: null,
|
||||
name: r'categoryProvider',
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
isAutoDispose: true,
|
||||
);
|
||||
|
||||
/// Provider for single category by ID
|
||||
|
||||
CategoryProvider call(String id) =>
|
||||
CategoryProvider._(argument: id, from: this);
|
||||
|
||||
@override
|
||||
String toString() => r'categoryProvider';
|
||||
}
|
||||
|
||||
/// Provider for category with its products (with pagination)
|
||||
|
||||
@ProviderFor(CategoryWithProducts)
|
||||
const categoryWithProductsProvider = CategoryWithProductsFamily._();
|
||||
|
||||
/// Provider for category with its products (with pagination)
|
||||
final class CategoryWithProductsProvider
|
||||
extends
|
||||
$AsyncNotifierProvider<CategoryWithProducts, CategoryProductsState> {
|
||||
/// Provider for category with its products (with pagination)
|
||||
const CategoryWithProductsProvider._({
|
||||
required CategoryWithProductsFamily super.from,
|
||||
required String super.argument,
|
||||
}) : super(
|
||||
retry: null,
|
||||
name: r'categoryWithProductsProvider',
|
||||
isAutoDispose: true,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$categoryWithProductsHash();
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return r'categoryWithProductsProvider'
|
||||
''
|
||||
'($argument)';
|
||||
}
|
||||
|
||||
@$internal
|
||||
@override
|
||||
CategoryWithProducts create() => CategoryWithProducts();
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return other is CategoryWithProductsProvider && other.argument == argument;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
return argument.hashCode;
|
||||
}
|
||||
}
|
||||
|
||||
String _$categoryWithProductsHash() =>
|
||||
r'a5ea35fad4e711ea855e4874f9135145d7d44b67';
|
||||
|
||||
/// Provider for category with its products (with pagination)
|
||||
|
||||
final class CategoryWithProductsFamily extends $Family
|
||||
with
|
||||
$ClassFamilyOverride<
|
||||
CategoryWithProducts,
|
||||
AsyncValue<CategoryProductsState>,
|
||||
CategoryProductsState,
|
||||
FutureOr<CategoryProductsState>,
|
||||
String
|
||||
> {
|
||||
const CategoryWithProductsFamily._()
|
||||
: super(
|
||||
retry: null,
|
||||
name: r'categoryWithProductsProvider',
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
isAutoDispose: true,
|
||||
);
|
||||
|
||||
/// Provider for category with its products (with pagination)
|
||||
|
||||
CategoryWithProductsProvider call(String categoryId) =>
|
||||
CategoryWithProductsProvider._(argument: categoryId, from: this);
|
||||
|
||||
@override
|
||||
String toString() => r'categoryWithProductsProvider';
|
||||
}
|
||||
|
||||
/// Provider for category with its products (with pagination)
|
||||
|
||||
abstract class _$CategoryWithProducts
|
||||
extends $AsyncNotifier<CategoryProductsState> {
|
||||
late final _$args = ref.$arg as String;
|
||||
String get categoryId => _$args;
|
||||
|
||||
FutureOr<CategoryProductsState> build(String categoryId);
|
||||
@$mustCallSuper
|
||||
@override
|
||||
void runBuild() {
|
||||
final created = build(_$args);
|
||||
final ref =
|
||||
this.ref
|
||||
as $Ref<AsyncValue<CategoryProductsState>, CategoryProductsState>;
|
||||
final element =
|
||||
ref.element
|
||||
as $ClassProviderElement<
|
||||
AnyNotifier<
|
||||
AsyncValue<CategoryProductsState>,
|
||||
CategoryProductsState
|
||||
>,
|
||||
AsyncValue<CategoryProductsState>,
|
||||
Object?,
|
||||
Object?
|
||||
>;
|
||||
element.handleValue(ref, created);
|
||||
}
|
||||
}
|
||||
|
||||
/// Provider for selected category state
|
||||
/// This is used in the products feature for filtering
|
||||
|
||||
@ProviderFor(SelectedCategoryInCategories)
|
||||
const selectedCategoryInCategoriesProvider =
|
||||
SelectedCategoryInCategoriesProvider._();
|
||||
|
||||
/// Provider for selected category state
|
||||
/// This is used in the products feature for filtering
|
||||
final class SelectedCategoryInCategoriesProvider
|
||||
extends $NotifierProvider<SelectedCategoryInCategories, String?> {
|
||||
/// Provider for selected category state
|
||||
/// This is used in the products feature for filtering
|
||||
const SelectedCategoryInCategoriesProvider._()
|
||||
/// Provider for selected category
|
||||
final class SelectedCategoryProvider
|
||||
extends $NotifierProvider<SelectedCategory, String?> {
|
||||
/// Provider for selected category
|
||||
const SelectedCategoryProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'selectedCategoryInCategoriesProvider',
|
||||
name: r'selectedCategoryProvider',
|
||||
isAutoDispose: true,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$selectedCategoryInCategoriesHash();
|
||||
String debugGetCreateSourceHash() => _$selectedCategoryHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
SelectedCategoryInCategories create() => SelectedCategoryInCategories();
|
||||
SelectedCategory create() => SelectedCategory();
|
||||
|
||||
/// {@macro riverpod.override_with_value}
|
||||
Override overrideWithValue(String? value) {
|
||||
@@ -286,13 +95,11 @@ final class SelectedCategoryInCategoriesProvider
|
||||
}
|
||||
}
|
||||
|
||||
String _$selectedCategoryInCategoriesHash() =>
|
||||
r'510d79a73dcfeba5efa886f5f95f7470dbd09a47';
|
||||
String _$selectedCategoryHash() => r'a47cd2de07ad285d4b73b2294ba954cb1cdd8e4c';
|
||||
|
||||
/// Provider for selected category state
|
||||
/// This is used in the products feature for filtering
|
||||
/// Provider for selected category
|
||||
|
||||
abstract class _$SelectedCategoryInCategories extends $Notifier<String?> {
|
||||
abstract class _$SelectedCategory extends $Notifier<String?> {
|
||||
String? build();
|
||||
@$mustCallSuper
|
||||
@override
|
||||
|
||||
Reference in New Issue
Block a user