feat(缓存): 添加本地缓存功能并优化网络状态提示

实现本地缓存管理器,支持预加载开关和排行榜数据缓存
优化网络状态提示文案和错误处理
移除调试日志打印,改进用户体验
This commit is contained in:
Developer
2026-03-30 04:07:31 +08:00
parent 71e853587c
commit 37a2c92a16
5 changed files with 142 additions and 12 deletions

View File

@@ -3,6 +3,7 @@ import '../../constants/app_constants.dart';
import '../../utils/responsive_layout.dart';
import '../../utils/http/http_client.dart';
import '../../models/poetry_model.dart';
import '../../controllers/load/locally.dart';
/// 时间: 2026-03-25
/// 功能: 热门页面
@@ -130,7 +131,7 @@ class _PopularPageState extends State<PopularPage>
}
return RefreshIndicator(
onRefresh: () async => await _loadRankList(),
onRefresh: () async => await _loadRankList(forceRefresh: true),
child: NotificationListener<ScrollNotification>(
onNotification: (scrollNotification) {
if (scrollNotification is ScrollEndNotification &&
@@ -318,7 +319,7 @@ class _PopularPageState extends State<PopularPage>
);
}
Future<void> _loadRankList() async {
Future<void> _loadRankList({bool forceRefresh = false}) async {
setState(() {
_loading = true;
_errorMessage = '';
@@ -333,6 +334,26 @@ class _PopularPageState extends State<PopularPage>
? 'day'
: 'month';
final isPreloadEnabled = await LocalCacheManager().isPreloadEnabled();
if (isPreloadEnabled && !forceRefresh) {
print('预加载模式:尝试从本地缓存加载数据');
final cachedData = await LocalCacheManager().getCachedPopularList(type);
if (cachedData != null && cachedData.isNotEmpty) {
print('从本地缓存加载数据成功');
if (mounted) {
setState(() {
_rankList = cachedData
.map((item) => PoetryModel.fromJson(item))
.toList();
_loading = false;
});
}
return;
}
print('本地缓存为空,从服务器加载');
}
print('正在请求排行榜数据: type=$type, period=$type');
final response = await HttpClient.get(
@@ -349,6 +370,12 @@ class _PopularPageState extends State<PopularPage>
if (response.isSuccess && response.code == 0) {
final data = response.data;
final rankData = data['list'] as List<dynamic>? ?? [];
final rankDataList = rankData.cast<Map<String, dynamic>>();
if (isPreloadEnabled) {
print('保存数据到本地缓存');
await LocalCacheManager().cachePopularList(type, rankDataList);
}
if (mounted) {
setState(() {
@@ -361,7 +388,9 @@ class _PopularPageState extends State<PopularPage>
} else {
if (mounted) {
setState(() {
_errorMessage = response.message ?? '获取排行榜失败';
_errorMessage = response.message.isNotEmpty == true
? response.message
: '获取排行榜失败';
_loading = false;
});
}