@@ -37,6 +37,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
3737 on < AccountSaveHeadlineToggled > (_onAccountSaveHeadlineToggled);
3838 on < AccountFollowTopicToggled > (_onAccountFollowTopicToggled);
3939 on < AccountFollowSourceToggled > (_onAccountFollowSourceToggled);
40+ on < AccountFollowCountryToggled > (_onAccountFollowCountryToggled);
4041 on < AccountClearUserPreferences > (_onAccountClearUserPreferences);
4142 }
4243
@@ -190,6 +191,64 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
190191 }
191192 }
192193
194+ Future <void > _onAccountFollowCountryToggled (
195+ AccountFollowCountryToggled event,
196+ Emitter <AccountState > emit,
197+ ) async {
198+ if (state.user == null || state.preferences == null ) return ;
199+ emit (state.copyWith (status: AccountStatus .loading));
200+
201+ final currentPrefs = state.preferences! ;
202+ final isCurrentlyFollowed = currentPrefs.followedCountries.any (
203+ (c) => c.id == event.country.id,
204+ );
205+ final List <Country > updatedFollowedCountries;
206+
207+ updatedFollowedCountries = isCurrentlyFollowed
208+ ? (List .from (currentPrefs.followedCountries)
209+ ..removeWhere ((c) => c.id == event.country.id))
210+ : (List .from (currentPrefs.followedCountries)..add (event.country));
211+
212+ final updatedPrefs = currentPrefs.copyWith (
213+ followedCountries: updatedFollowedCountries,
214+ );
215+
216+ try {
217+ final sortedPrefs = _sortPreferences (updatedPrefs);
218+ await _userContentPreferencesRepository.update (
219+ id: state.user! .id,
220+ item: sortedPrefs,
221+ userId: state.user! .id,
222+ );
223+ emit (
224+ state.copyWith (
225+ status: AccountStatus .success,
226+ preferences: sortedPrefs,
227+ clearError: true ,
228+ ),
229+ );
230+ } on HttpException catch (e) {
231+ _logger.severe (
232+ 'AccountFollowCountryToggled failed with HttpException: $e ' ,
233+ );
234+ emit (state.copyWith (status: AccountStatus .failure, error: e));
235+ } catch (e, st) {
236+ _logger.severe (
237+ 'AccountFollowCountryToggled failed with unexpected error: $e ' ,
238+ e,
239+ st,
240+ );
241+ emit (
242+ state.copyWith (
243+ status: AccountStatus .failure,
244+ error: OperationFailedException (
245+ 'Failed to update followed countries: $e ' ,
246+ ),
247+ ),
248+ );
249+ }
250+ }
251+
193252 Future <void > _onAccountSaveHeadlineToggled (
194253 AccountSaveHeadlineToggled event,
195254 Emitter <AccountState > emit,
@@ -434,11 +493,16 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
434493 // Sort followed sources by name ascending
435494 final sortedSources = List <Source >.from (preferences.followedSources)
436495 ..sort ((a, b) => a.name.compareTo (b.name));
496+
497+ // Sort followed countries by name ascending
498+ final sortedCountries = List <Country >.from (preferences.followedCountries)
499+ ..sort ((a, b) => a.name.compareTo (b.name));
437500
438501 return preferences.copyWith (
439502 savedHeadlines: sortedHeadlines,
440503 followedTopics: sortedTopics,
441504 followedSources: sortedSources,
505+ followedCountries: sortedCountries,
442506 );
443507 }
444508
0 commit comments