@@ -75,7 +75,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
7575 emit (
7676 state.copyWith (
7777 status: AccountStatus .success,
78- preferences: preferences,
78+ preferences: _sortPreferences ( preferences) ,
7979 clearError: true ,
8080 ),
8181 );
@@ -98,7 +98,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
9898 emit (
9999 state.copyWith (
100100 status: AccountStatus .success,
101- preferences: migratedPreferences,
101+ preferences: _sortPreferences ( migratedPreferences) ,
102102 clearError: true ,
103103 ),
104104 );
@@ -127,7 +127,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
127127 );
128128 emit (
129129 state.copyWith (
130- preferences: defaultPreferences,
130+ preferences: _sortPreferences ( defaultPreferences) ,
131131 clearError: true ,
132132 status: AccountStatus .success,
133133 ),
@@ -146,7 +146,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
146146 emit (
147147 state.copyWith (
148148 status: AccountStatus .success,
149- preferences: existingPreferences,
149+ preferences: _sortPreferences ( existingPreferences) ,
150150 clearError: true ,
151151 ),
152152 );
@@ -216,15 +216,16 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
216216 );
217217
218218 try {
219+ final sortedPrefs = _sortPreferences (updatedPrefs);
219220 await _userContentPreferencesRepository.update (
220221 id: state.user! .id,
221- item: updatedPrefs ,
222+ item: sortedPrefs ,
222223 userId: state.user! .id,
223224 );
224225 emit (
225226 state.copyWith (
226227 status: AccountStatus .success,
227- preferences: updatedPrefs ,
228+ preferences: sortedPrefs ,
228229 clearError: true ,
229230 ),
230231 );
@@ -273,15 +274,16 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
273274 );
274275
275276 try {
277+ final sortedPrefs = _sortPreferences (updatedPrefs);
276278 await _userContentPreferencesRepository.update (
277279 id: state.user! .id,
278- item: updatedPrefs ,
280+ item: sortedPrefs ,
279281 userId: state.user! .id,
280282 );
281283 emit (
282284 state.copyWith (
283285 status: AccountStatus .success,
284- preferences: updatedPrefs ,
286+ preferences: sortedPrefs ,
285287 clearError: true ,
286288 ),
287289 );
@@ -331,15 +333,16 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
331333 );
332334
333335 try {
336+ final sortedPrefs = _sortPreferences (updatedPrefs);
334337 await _userContentPreferencesRepository.update (
335338 id: state.user! .id,
336- item: updatedPrefs ,
339+ item: sortedPrefs ,
337340 userId: state.user! .id,
338341 );
339342 emit (
340343 state.copyWith (
341344 status: AccountStatus .success,
342- preferences: updatedPrefs ,
345+ preferences: sortedPrefs ,
343346 clearError: true ,
344347 ),
345348 );
@@ -413,6 +416,32 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
413416 }
414417 }
415418
419+ /// Sorts the lists within UserContentPreferences locally.
420+ ///
421+ /// This client-side sorting is necessary due to a backend limitation that
422+ /// does not support sorting for saved or followed content lists. This
423+ /// approach remains efficient as these lists are fetched all at once and
424+ /// are kept small by user account-type limits.
425+ UserContentPreferences _sortPreferences (UserContentPreferences preferences) {
426+ // Sort saved headlines by updatedAt descending (newest first)
427+ final sortedHeadlines = List <Headline >.from (preferences.savedHeadlines)
428+ ..sort ((a, b) => b.updatedAt.compareTo (a.updatedAt));
429+
430+ // Sort followed topics by name ascending
431+ final sortedTopics = List <Topic >.from (preferences.followedTopics)
432+ ..sort ((a, b) => a.name.compareTo (b.name));
433+
434+ // Sort followed sources by name ascending
435+ final sortedSources = List <Source >.from (preferences.followedSources)
436+ ..sort ((a, b) => a.name.compareTo (b.name));
437+
438+ return preferences.copyWith (
439+ savedHeadlines: sortedHeadlines,
440+ followedTopics: sortedTopics,
441+ followedSources: sortedSources,
442+ );
443+ }
444+
416445 @override
417446 Future <void > close () {
418447 _userSubscription.cancel ();
0 commit comments