Skip to content

Commit 602f121

Browse files
committed
feat(headlines-feed): implement apply followed countries filter
- Add new event handler for applying followed countries filter - Inject UserContentPreferencesRepository and AppBloc into CountriesFilterBloc - Implement logic to fetch user's followed countries from preferences - Handle unauthorized and empty followed countries scenarios - Update state based on success or failure of the operation
1 parent b91f5f7 commit 602f121

File tree

1 file changed

+74
-3
lines changed

1 file changed

+74
-3
lines changed

lib/headlines-feed/bloc/countries_filter_bloc.dart

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:bloc_concurrency/bloc_concurrency.dart';
55
import 'package:core/core.dart';
66
import 'package:data_repository/data_repository.dart';
77
import 'package:equatable/equatable.dart';
8+
import 'package:flutter_news_app_mobile_client_full_source_code/app/bloc/app_bloc.dart'; // Import AppBloc
89

910
part 'countries_filter_event.dart';
1011
part 'countries_filter_state.dart';
@@ -20,16 +21,28 @@ class CountriesFilterBloc
2021
/// {@macro countries_filter_bloc}
2122
///
2223
/// Requires a [DataRepository<Country>] to interact with the data layer.
23-
CountriesFilterBloc({required DataRepository<Country> countriesRepository})
24-
: _countriesRepository = countriesRepository,
25-
super(const CountriesFilterState()) {
24+
CountriesFilterBloc({
25+
required DataRepository<Country> countriesRepository,
26+
required DataRepository<UserContentPreferences>
27+
userContentPreferencesRepository, // Inject UserContentPreferencesRepository
28+
required AppBloc appBloc, // Inject AppBloc
29+
}) : _countriesRepository = countriesRepository,
30+
_userContentPreferencesRepository = userContentPreferencesRepository,
31+
_appBloc = appBloc,
32+
super(const CountriesFilterState()) {
2633
on<CountriesFilterRequested>(
2734
_onCountriesFilterRequested,
2835
transformer: restartable(),
2936
);
37+
on<CountriesFilterApplyFollowedRequested>(
38+
_onCountriesFilterApplyFollowedRequested,
39+
transformer: restartable(),
40+
); // Register new event handler
3041
}
3142

3243
final DataRepository<Country> _countriesRepository;
44+
final DataRepository<UserContentPreferences> _userContentPreferencesRepository;
45+
final AppBloc _appBloc;
3346

3447
/// Handles the request to fetch countries based on a specific usage.
3548
///
@@ -74,4 +87,62 @@ class CountriesFilterBloc
7487
emit(state.copyWith(status: CountriesFilterStatus.failure, error: e));
7588
}
7689
}
90+
91+
/// Handles the request to apply the user's followed countries as filters.
92+
Future<void> _onCountriesFilterApplyFollowedRequested(
93+
CountriesFilterApplyFollowedRequested event,
94+
Emitter<CountriesFilterState> emit,
95+
) async {
96+
emit(state.copyWith(followedCountriesStatus: CountriesFilterStatus.loading));
97+
98+
final currentUser = _appBloc.state.user;
99+
100+
if (currentUser == null) {
101+
emit(
102+
state.copyWith(
103+
followedCountriesStatus: CountriesFilterStatus.failure,
104+
error: const UnauthorizedException(
105+
'User must be logged in to apply followed countries.',
106+
),
107+
),
108+
);
109+
return;
110+
}
111+
112+
try {
113+
final preferences = await _userContentPreferencesRepository.read(
114+
id: currentUser.id,
115+
userId: currentUser.id,
116+
);
117+
118+
if (preferences.followedCountries.isEmpty) {
119+
emit(
120+
state.copyWith(
121+
followedCountriesStatus: CountriesFilterStatus.success,
122+
followedCountries: const [],
123+
error: const OperationFailedException('No followed countries found.'),
124+
clearFollowedCountriesError: true,
125+
),
126+
);
127+
return;
128+
}
129+
130+
emit(
131+
state.copyWith(
132+
followedCountriesStatus: CountriesFilterStatus.success,
133+
followedCountries: preferences.followedCountries,
134+
clearFollowedCountriesError: true,
135+
),
136+
);
137+
} on HttpException catch (e) {
138+
emit(state.copyWith(followedCountriesStatus: CountriesFilterStatus.failure, error: e));
139+
} catch (e) {
140+
emit(
141+
state.copyWith(
142+
followedCountriesStatus: CountriesFilterStatus.failure,
143+
error: UnknownException(e.toString()),
144+
),
145+
);
146+
}
147+
}
77148
}

0 commit comments

Comments
 (0)