fix(profile): 修复我的收藏与观看历史列表不显示
This commit is contained in:
1 parent
f2ae0c70b6
commit
a48433f01d
6 files changed
+202
-12
No files matched your search
@@ -3,6 +3,8 @@
|
|||||||
import 'json_numbers.dart';
|
import 'json_numbers.dart';
|
||||||
|
|
||||||
/// 服务端标准分页响应 `{total, list}`。
|
/// 服务端标准分页响应 `{total, list}`。
|
||||||
|
///
|
||||||
|
/// 兼容旧版「data 直接是数组」的响应:按数组长度作为 total,避免列表空白。
|
||||||
class PageResult<T> {
|
class PageResult<T> {
|
||||||
const PageResult({this.total = 0, this.list = const []});
|
const PageResult({this.total = 0, this.list = const []});
|
||||||
|
|
||||||
@@ -14,10 +16,32 @@ class PageResult<T> {
|
|||||||
Map<String, dynamic> json,
|
Map<String, dynamic> json,
|
||||||
T Function(Map<String, dynamic>) parseItem,
|
T Function(Map<String, dynamic>) parseItem,
|
||||||
) {
|
) {
|
||||||
final items = (json['list'] as List? ?? const [])
|
final items = _parseItemMaps(json['list']).map(parseItem).toList(
|
||||||
.whereType<Map<String, dynamic>>()
|
growable: false,
|
||||||
.map(parseItem)
|
);
|
||||||
.toList(growable: false);
|
|
||||||
return PageResult(total: jsonToInt(json['total']), list: items);
|
return PageResult(total: jsonToInt(json['total']), list: items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 兼容分页 Map 与旧版纯 List;其它类型视为空页。
|
||||||
|
factory PageResult.parse(
|
||||||
|
Object? data,
|
||||||
|
T Function(Map<String, dynamic>) parseItem,
|
||||||
|
) {
|
||||||
|
if (data is Map) {
|
||||||
|
return PageResult.fromJson(Map<String, dynamic>.from(data), parseItem);
|
||||||
|
}
|
||||||
|
if (data is List) {
|
||||||
|
final items = _parseItemMaps(data).map(parseItem).toList(growable: false);
|
||||||
|
return PageResult(total: items.length, list: items);
|
||||||
|
}
|
||||||
|
return PageResult<T>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Map<String, dynamic>> _parseItemMaps(Object? raw) {
|
||||||
|
if (raw is! List) return const [];
|
||||||
|
return [
|
||||||
|
for (final item in raw)
|
||||||
|
if (item is Map) Map<String, dynamic>.from(item),
|
||||||
|
];
|
||||||
}
|
}
|
||||||
@@ -29,10 +29,8 @@ class LibraryApi {
|
|||||||
'/app/favorites',
|
'/app/favorites',
|
||||||
query: {'pageIndex': '$pageIndex', 'pageSize': '$pageSize'},
|
query: {'pageIndex': '$pageIndex', 'pageSize': '$pageSize'},
|
||||||
);
|
);
|
||||||
if (data is Map<String, dynamic>) {
|
// 线上可能仍返回 List;分页壳与纯数组都要能渲染。
|
||||||
return PageResult.fromJson(data, Drama.fromCardJson);
|
return PageResult.parse(data, Drama.fromCardJson);
|
||||||
}
|
|
||||||
return const PageResult();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> removeFavorite(String dramaId) async {
|
Future<void> removeFavorite(String dramaId) async {
|
||||||
|
|||||||
@@ -49,10 +49,8 @@ class WatchApi {
|
|||||||
'/app/watch/recent',
|
'/app/watch/recent',
|
||||||
query: {'pageIndex': '$pageIndex', 'pageSize': '$pageSize'},
|
query: {'pageIndex': '$pageIndex', 'pageSize': '$pageSize'},
|
||||||
);
|
);
|
||||||
if (data is Map<String, dynamic>) {
|
// 线上可能仍返回 List;分页壳与纯数组都要能渲染。
|
||||||
return PageResult.fromJson(data, WatchProgress.fromJson);
|
return PageResult.parse(data, WatchProgress.fromJson);
|
||||||
}
|
|
||||||
return const PageResult();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 删除某部剧的观看记录(记录页清单条用)。
|
/// 删除某部剧的观看记录(记录页清单条用)。
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
// Author: fruit.com.cn
|
||||||
|
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:fruitshorts/core/data/models/page_result.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
group('PageResult.parse', () {
|
||||||
|
test('should_parse_page_map_when_total_and_list_present', () {
|
||||||
|
final page = PageResult.parse({
|
||||||
|
'total': '3',
|
||||||
|
'list': [
|
||||||
|
{'id': '1', 'title': 'A'},
|
||||||
|
{'id': '2', 'title': 'B'},
|
||||||
|
],
|
||||||
|
}, _item);
|
||||||
|
|
||||||
|
expect(page.total, 3);
|
||||||
|
expect(page.list.map((e) => e['id']), ['1', '2']);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should_parse_legacy_list_when_data_is_array', () {
|
||||||
|
final page = PageResult.parse([
|
||||||
|
{'id': '9', 'title': '收藏剧'},
|
||||||
|
{'id': '8', 'title': '另一部'},
|
||||||
|
], _item);
|
||||||
|
|
||||||
|
expect(page.total, 2);
|
||||||
|
expect(page.list, hasLength(2));
|
||||||
|
expect(page.list.first['title'], '收藏剧');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should_return_empty_page_when_data_is_null_or_empty_list', () {
|
||||||
|
expect(PageResult.parse(null, _item).total, 0);
|
||||||
|
expect(PageResult.parse(null, _item).list, isEmpty);
|
||||||
|
expect(PageResult.parse(const <Object>[], _item).total, 0);
|
||||||
|
expect(PageResult.parse(const <Object>[], _item).list, isEmpty);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should_return_empty_page_when_payload_is_invalid', () {
|
||||||
|
final page = PageResult.parse('broken', _item);
|
||||||
|
expect(page.total, 0);
|
||||||
|
expect(page.list, isEmpty);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should_skip_non_map_entries_in_legacy_list', () {
|
||||||
|
final page = PageResult.parse([
|
||||||
|
{'id': '1'},
|
||||||
|
'noise',
|
||||||
|
42,
|
||||||
|
{'id': '2'},
|
||||||
|
], _item);
|
||||||
|
|
||||||
|
expect(page.total, 2);
|
||||||
|
expect(page.list.map((e) => e['id']), ['1', '2']);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, dynamic> _item(Map<String, dynamic> json) => json;
|
||||||
@@ -38,4 +38,67 @@ void main() {
|
|||||||
expect(page.list.single.id, '9');
|
expect(page.list.single.id, '9');
|
||||||
expect(page.list.single.title, '收藏剧');
|
expect(page.list.single.title, '收藏剧');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should_parse_favorites_when_server_returns_legacy_list', () async {
|
||||||
|
final adapter = FakeHttpAdapter(
|
||||||
|
(_) async => FakeHttpResponse(
|
||||||
|
jsonEncode({
|
||||||
|
'code': 0,
|
||||||
|
'message': 'ok',
|
||||||
|
'data': [
|
||||||
|
{'id': '5', 'title': '闪婚后', 'cover': '/files/5.png'},
|
||||||
|
{'id': '4', 'title': '另一部', 'cover': '/files/4.png'},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
final api = LibraryApi(
|
||||||
|
ApiClient(baseUrl: 'http://127.0.0.1:8081', adapter: adapter),
|
||||||
|
);
|
||||||
|
|
||||||
|
final page = await api.favorites();
|
||||||
|
|
||||||
|
expect(page.total, 2);
|
||||||
|
expect(page.list.map((drama) => drama.id), ['5', '4']);
|
||||||
|
expect(page.list.first.title, '闪婚后');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should_return_empty_favorites_when_data_is_null', () async {
|
||||||
|
final adapter = FakeHttpAdapter(
|
||||||
|
(_) async => FakeHttpResponse(
|
||||||
|
jsonEncode({'code': 0, 'message': 'ok', 'data': null}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
final api = LibraryApi(
|
||||||
|
ApiClient(baseUrl: 'http://127.0.0.1:8081', adapter: adapter),
|
||||||
|
);
|
||||||
|
|
||||||
|
final page = await api.favorites();
|
||||||
|
|
||||||
|
expect(page.total, 0);
|
||||||
|
expect(page.list, isEmpty);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should_throw_api_exception_when_favorites_business_code_failed', () async {
|
||||||
|
final adapter = FakeHttpAdapter(
|
||||||
|
(_) async => FakeHttpResponse(
|
||||||
|
jsonEncode({'code': 401, 'message': '未登录或登录已过期', 'data': null}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
final api = LibraryApi(
|
||||||
|
ApiClient(
|
||||||
|
baseUrl: 'http://127.0.0.1:8081',
|
||||||
|
adapter: adapter,
|
||||||
|
// 无刷新回调时,401 直接抛业务异常,避免重试逻辑。
|
||||||
|
tokenRefresher: null,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await expectLater(
|
||||||
|
() => api.favorites(),
|
||||||
|
throwsA(
|
||||||
|
isA<ApiException>().having((e) => e.code, 'code', 401),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
@@ -50,4 +50,52 @@ void main() {
|
|||||||
expect(page.list.single.episodeNumber, 6);
|
expect(page.list.single.episodeNumber, 6);
|
||||||
expect(page.list.single.totalEpisodes, 68);
|
expect(page.list.single.totalEpisodes, 68);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should_parse_recent_when_server_returns_legacy_list', () async {
|
||||||
|
final adapter = FakeHttpAdapter(
|
||||||
|
(_) async => FakeHttpResponse(
|
||||||
|
jsonEncode({
|
||||||
|
'code': 0,
|
||||||
|
'message': 'ok',
|
||||||
|
'data': [
|
||||||
|
{
|
||||||
|
'dramaId': '5',
|
||||||
|
'episodeId': '7',
|
||||||
|
'positionMs': '203904',
|
||||||
|
'durationMs': '227882',
|
||||||
|
'finished': false,
|
||||||
|
'playCount': 8,
|
||||||
|
'updatedAt': '2026-10-21T15:17:05.419',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
final api = WatchApi(
|
||||||
|
ApiClient(baseUrl: 'http://127.0.0.1:8081', adapter: adapter),
|
||||||
|
);
|
||||||
|
|
||||||
|
final page = await api.recent();
|
||||||
|
|
||||||
|
expect(page.total, 1);
|
||||||
|
expect(page.list.single.dramaId, '5');
|
||||||
|
expect(page.list.single.episodeId, '7');
|
||||||
|
expect(page.list.single.position.inMilliseconds, 203904);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should_return_empty_recent_when_data_is_null', () async {
|
||||||
|
final adapter = FakeHttpAdapter(
|
||||||
|
(_) async => FakeHttpResponse(
|
||||||
|
jsonEncode({'code': 0, 'message': 'ok', 'data': null}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
final api = WatchApi(
|
||||||
|
ApiClient(baseUrl: 'http://127.0.0.1:8081', adapter: adapter),
|
||||||
|
);
|
||||||
|
|
||||||
|
final page = await api.recent();
|
||||||
|
|
||||||
|
expect(page.total, 0);
|
||||||
|
expect(page.list, isEmpty);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user