@@ -75,7 +75,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
75
75
emit (
76
76
state.copyWith (
77
77
status: AccountStatus .success,
78
- preferences: preferences,
78
+ preferences: _sortPreferences ( preferences) ,
79
79
clearError: true ,
80
80
),
81
81
);
@@ -98,7 +98,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
98
98
emit (
99
99
state.copyWith (
100
100
status: AccountStatus .success,
101
- preferences: migratedPreferences,
101
+ preferences: _sortPreferences ( migratedPreferences) ,
102
102
clearError: true ,
103
103
),
104
104
);
@@ -127,7 +127,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
127
127
);
128
128
emit (
129
129
state.copyWith (
130
- preferences: defaultPreferences,
130
+ preferences: _sortPreferences ( defaultPreferences) ,
131
131
clearError: true ,
132
132
status: AccountStatus .success,
133
133
),
@@ -146,7 +146,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
146
146
emit (
147
147
state.copyWith (
148
148
status: AccountStatus .success,
149
- preferences: existingPreferences,
149
+ preferences: _sortPreferences ( existingPreferences) ,
150
150
clearError: true ,
151
151
),
152
152
);
@@ -216,15 +216,16 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
216
216
);
217
217
218
218
try {
219
+ final sortedPrefs = _sortPreferences (updatedPrefs);
219
220
await _userContentPreferencesRepository.update (
220
221
id: state.user! .id,
221
- item: updatedPrefs ,
222
+ item: sortedPrefs ,
222
223
userId: state.user! .id,
223
224
);
224
225
emit (
225
226
state.copyWith (
226
227
status: AccountStatus .success,
227
- preferences: updatedPrefs ,
228
+ preferences: sortedPrefs ,
228
229
clearError: true ,
229
230
),
230
231
);
@@ -273,15 +274,16 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
273
274
);
274
275
275
276
try {
277
+ final sortedPrefs = _sortPreferences (updatedPrefs);
276
278
await _userContentPreferencesRepository.update (
277
279
id: state.user! .id,
278
- item: updatedPrefs ,
280
+ item: sortedPrefs ,
279
281
userId: state.user! .id,
280
282
);
281
283
emit (
282
284
state.copyWith (
283
285
status: AccountStatus .success,
284
- preferences: updatedPrefs ,
286
+ preferences: sortedPrefs ,
285
287
clearError: true ,
286
288
),
287
289
);
@@ -331,15 +333,16 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
331
333
);
332
334
333
335
try {
336
+ final sortedPrefs = _sortPreferences (updatedPrefs);
334
337
await _userContentPreferencesRepository.update (
335
338
id: state.user! .id,
336
- item: updatedPrefs ,
339
+ item: sortedPrefs ,
337
340
userId: state.user! .id,
338
341
);
339
342
emit (
340
343
state.copyWith (
341
344
status: AccountStatus .success,
342
- preferences: updatedPrefs ,
345
+ preferences: sortedPrefs ,
343
346
clearError: true ,
344
347
),
345
348
);
@@ -413,6 +416,32 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
413
416
}
414
417
}
415
418
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
+
416
445
@override
417
446
Future <void > close () {
418
447
_userSubscription.cancel ();
0 commit comments