Skip to content

Commit 065c01c

Browse files
committed
refactor(categories): use generic data repo
- Use HtDataRepository for categories - Use shared Category model - Handle generic HtHttpException
1 parent 9285415 commit 065c01c

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

lib/headlines-feed/bloc/categories_filter_bloc.dart

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import 'dart:async';
33
import 'package:bloc/bloc.dart';
44
import 'package:bloc_concurrency/bloc_concurrency.dart';
55
import 'package:equatable/equatable.dart';
6-
import 'package:ht_categories_client/ht_categories_client.dart';
7-
import 'package:ht_categories_repository/ht_categories_repository.dart';
8-
// For PaginatedResponse
6+
import 'package:ht_data_repository/ht_data_repository.dart'; // Generic Data Repository
7+
import 'package:ht_shared/ht_shared.dart'
8+
show
9+
Category,
10+
HtHttpException; // Shared models, including Category and standardized exceptions
911

1012
part 'categories_filter_event.dart';
1113
part 'categories_filter_state.dart';
@@ -14,16 +16,17 @@ part 'categories_filter_state.dart';
1416
/// Manages the state for fetching and displaying categories for filtering.
1517
///
1618
/// Handles initial fetching and pagination of categories using the
17-
/// provided [HtCategoriesRepository].
19+
/// provided [HtDataRepository].
1820
/// {@endtemplate}
1921
class CategoriesFilterBloc
2022
extends Bloc<CategoriesFilterEvent, CategoriesFilterState> {
2123
/// {@macro categories_filter_bloc}
2224
///
23-
/// Requires a [HtCategoriesRepository] to interact with the data layer.
24-
CategoriesFilterBloc({required HtCategoriesRepository categoriesRepository})
25-
: _categoriesRepository = categoriesRepository,
26-
super(const CategoriesFilterState()) {
25+
/// Requires a [HtDataRepository<Category>] to interact with the data layer.
26+
CategoriesFilterBloc({
27+
required HtDataRepository<Category> categoriesRepository,
28+
}) : _categoriesRepository = categoriesRepository,
29+
super(const CategoriesFilterState()) {
2730
on<CategoriesFilterRequested>(
2831
_onCategoriesFilterRequested,
2932
transformer: restartable(), // Only process the latest request
@@ -34,7 +37,7 @@ class CategoriesFilterBloc
3437
);
3538
}
3639

37-
final HtCategoriesRepository _categoriesRepository;
40+
final HtDataRepository<Category> _categoriesRepository;
3841

3942
/// Number of categories to fetch per page.
4043
static const _categoriesLimit = 20;
@@ -54,7 +57,7 @@ class CategoriesFilterBloc
5457
emit(state.copyWith(status: CategoriesFilterStatus.loading));
5558

5659
try {
57-
final response = await _categoriesRepository.getCategories(
60+
final response = await _categoriesRepository.readAll(
5861
limit: _categoriesLimit,
5962
);
6063
emit(
@@ -66,7 +69,7 @@ class CategoriesFilterBloc
6669
clearError: true, // Clear any previous error
6770
),
6871
);
69-
} on GetCategoriesFailure catch (e) {
72+
} on HtHttpException catch (e) {
7073
emit(state.copyWith(status: CategoriesFilterStatus.failure, error: e));
7174
} catch (e) {
7275
// Catch unexpected errors
@@ -87,7 +90,7 @@ class CategoriesFilterBloc
8790
emit(state.copyWith(status: CategoriesFilterStatus.loadingMore));
8891

8992
try {
90-
final response = await _categoriesRepository.getCategories(
93+
final response = await _categoriesRepository.readAll(
9194
limit: _categoriesLimit,
9295
startAfterId: state.cursor, // Use the cursor from the current state
9396
);
@@ -100,7 +103,7 @@ class CategoriesFilterBloc
100103
cursor: response.cursor,
101104
),
102105
);
103-
} on GetCategoriesFailure catch (e) {
106+
} on HtHttpException catch (e) {
104107
// Keep existing data but indicate failure
105108
emit(
106109
state.copyWith(

0 commit comments

Comments
 (0)