fix
This commit is contained in:
@@ -6,6 +6,7 @@ abstract class CategoryLocalDataSource {
|
||||
Future<List<CategoryModel>> getAllCategories();
|
||||
Future<CategoryModel?> getCategoryById(String id);
|
||||
Future<void> cacheCategories(List<CategoryModel> categories);
|
||||
Future<void> updateCategory(CategoryModel category);
|
||||
Future<void> clearCategories();
|
||||
}
|
||||
|
||||
@@ -30,6 +31,11 @@ class CategoryLocalDataSourceImpl implements CategoryLocalDataSource {
|
||||
await box.putAll(categoryMap);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateCategory(CategoryModel category) async {
|
||||
await box.put(category.id, category);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> clearCategories() async {
|
||||
await box.clear();
|
||||
|
||||
@@ -18,8 +18,27 @@ class CategoryRepositoryImpl implements CategoryRepository {
|
||||
@override
|
||||
Future<Either<Failure, List<Category>>> getAllCategories() async {
|
||||
try {
|
||||
final categories = await localDataSource.getAllCategories();
|
||||
// Try remote first (online-first)
|
||||
final categories = await remoteDataSource.getAllCategories();
|
||||
// Cache the results
|
||||
await localDataSource.cacheCategories(categories);
|
||||
return Right(categories.map((model) => model.toEntity()).toList());
|
||||
} on ServerException catch (e) {
|
||||
// Remote failed, try local cache
|
||||
try {
|
||||
final cachedCategories = await localDataSource.getAllCategories();
|
||||
return Right(cachedCategories.map((model) => model.toEntity()).toList());
|
||||
} on CacheException catch (cacheError) {
|
||||
return Left(ServerFailure('${e.message}. Cache also unavailable.'));
|
||||
}
|
||||
} on NetworkException catch (e) {
|
||||
// Network failed, try local cache
|
||||
try {
|
||||
final cachedCategories = await localDataSource.getAllCategories();
|
||||
return Right(cachedCategories.map((model) => model.toEntity()).toList());
|
||||
} on CacheException catch (cacheError) {
|
||||
return Left(NetworkFailure('${e.message}. Cache also unavailable.'));
|
||||
}
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
}
|
||||
@@ -28,11 +47,33 @@ class CategoryRepositoryImpl implements CategoryRepository {
|
||||
@override
|
||||
Future<Either<Failure, Category>> getCategoryById(String id) async {
|
||||
try {
|
||||
final category = await localDataSource.getCategoryById(id);
|
||||
if (category == null) {
|
||||
return Left(NotFoundFailure('Category not found'));
|
||||
}
|
||||
// Try remote first (online-first)
|
||||
final category = await remoteDataSource.getCategoryById(id);
|
||||
// Cache the result
|
||||
await localDataSource.updateCategory(category);
|
||||
return Right(category.toEntity());
|
||||
} on ServerException catch (e) {
|
||||
// Remote failed, try local cache
|
||||
try {
|
||||
final cachedCategory = await localDataSource.getCategoryById(id);
|
||||
if (cachedCategory == null) {
|
||||
return Left(NotFoundFailure('Category not found in cache'));
|
||||
}
|
||||
return Right(cachedCategory.toEntity());
|
||||
} on CacheException catch (cacheError) {
|
||||
return Left(ServerFailure('${e.message}. Cache also unavailable.'));
|
||||
}
|
||||
} on NetworkException catch (e) {
|
||||
// Network failed, try local cache
|
||||
try {
|
||||
final cachedCategory = await localDataSource.getCategoryById(id);
|
||||
if (cachedCategory == null) {
|
||||
return Left(NotFoundFailure('Category not found in cache'));
|
||||
}
|
||||
return Right(cachedCategory.toEntity());
|
||||
} on CacheException catch (cacheError) {
|
||||
return Left(NetworkFailure('${e.message}. Cache also unavailable.'));
|
||||
}
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user