/// 时间: 2026-03-29 /// 功能: 首页自动刷新管理 /// 介绍: 管理首页诗句自动刷新功能,包括定时器、状态管理等 /// 最新变化: 新建文件 import 'dart:async'; import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../../../utils/http/poetry_api.dart'; class AutoRefreshManager { static const String _autoRefreshKey = 'auto_refresh_enabled'; static const Duration _refreshInterval = Duration(seconds: 5); static AutoRefreshManager? _instance; Timer? _refreshTimer; bool _isEnabled = false; VoidCallback? _onRefresh; AutoRefreshManager._internal(); factory AutoRefreshManager() { _instance ??= AutoRefreshManager._internal(); return _instance!; } Future init() async { final prefs = await SharedPreferences.getInstance(); _isEnabled = prefs.getBool(_autoRefreshKey) ?? false; _debugLog('自动刷新初始化,状态: $_isEnabled'); } bool get isEnabled => _isEnabled; Future setEnabled(bool enabled) async { _isEnabled = enabled; final prefs = await SharedPreferences.getInstance(); await prefs.setBool(_autoRefreshKey, enabled); _debugLog('自动刷新状态已设置: $enabled'); if (enabled) { _startTimer(); } else { _stopTimer(); } } void setOnRefresh(VoidCallback? callback) { _onRefresh = callback; _debugLog('设置刷新回调'); } void _startTimer() { _stopTimer(); _refreshTimer = Timer.periodic(_refreshInterval, (timer) { _debugLog('自动刷新触发'); if (_onRefresh != null) { _onRefresh!(); } }); _debugLog('自动刷新定时器已启动'); } void _stopTimer() { if (_refreshTimer != null) { _refreshTimer!.cancel(); _refreshTimer = null; _debugLog('自动刷新定时器已停止'); } } void stopTimer() { _stopTimer(); } void dispose() { _stopTimer(); _onRefresh = null; _debugLog('自动刷新管理器已释放'); } void _debugLog(String message) { if (kDebugMode) {} } } class DebugInfoManager { static const String _debugInfoKey = 'debug_info_enabled'; static DebugInfoManager? _instance; bool _isEnabled = false; final ValueNotifier _messageNotifier = ValueNotifier(''); Timer? _messageTimer; DebugInfoManager._internal(); factory DebugInfoManager() { _instance ??= DebugInfoManager._internal(); return _instance!; } Future init() async { final prefs = await SharedPreferences.getInstance(); _isEnabled = prefs.getBool(_debugInfoKey) ?? false; _debugLog('调试信息初始化,状态: $_isEnabled'); } bool get isEnabled => _isEnabled; ValueNotifier get messageNotifier => _messageNotifier; Future setEnabled(bool enabled) async { _isEnabled = enabled; final prefs = await SharedPreferences.getInstance(); await prefs.setBool(_debugInfoKey, enabled); _debugLog('调试信息状态已设置: $enabled'); if (!enabled) { _messageNotifier.value = ''; } } void showMessage(String message) { if (!_isEnabled) return; _messageNotifier.value = message; _debugLog('显示调试信息: $message'); _messageTimer?.cancel(); _messageTimer = Timer(const Duration(seconds: 2), () { _messageNotifier.value = ''; _debugLog('调试信息已隐藏'); }); } void showRefreshSuccess() { showMessage('刷新成功'); } void showRefreshFailed() { showMessage('刷新失败'); } void showNextSuccess() { showMessage('下一条成功'); } void showNextFailed() { showMessage('下一条失败'); } void showPreviousSuccess() { showMessage('上一条成功'); } void showPreviousFailed() { showMessage('上一条失败'); } void showLiked() { showMessage('已点赞'); } void showUnliked() { showMessage('已取消点赞'); } void showCopySuccess() { showMessage('复制成功'); } void showCopyFailed() { showMessage('复制失败'); } void dispose() { _messageTimer?.cancel(); _messageNotifier.value = ''; _debugLog('调试信息管理器已清理'); } void _debugLog(String message) { if (kDebugMode) {} } } class OfflineDataManager { static const String _offlineDataKey = 'offline_poetry_data'; static const String _onlineStatusKey = 'personal_card_online'; static OfflineDataManager? _instance; List> _cachedPoetryList = []; int _currentIndex = 0; OfflineDataManager._internal(); factory OfflineDataManager() { _instance ??= OfflineDataManager._internal(); return _instance!; } Future init() async { await _loadCachedData(); _debugLog('离线数据管理器初始化,缓存数量: ${_cachedPoetryList.length}'); } Future isOnline() async { final prefs = await SharedPreferences.getInstance(); return prefs.getBool(_onlineStatusKey) ?? true; } Future hasCachedData() async { await _loadCachedData(); return _cachedPoetryList.isNotEmpty; } Future getNextPoetry() async { await _loadCachedData(); if (_cachedPoetryList.isEmpty) { return null; } final poetryData = _cachedPoetryList[_currentIndex]; _currentIndex = (_currentIndex + 1) % _cachedPoetryList.length; return _convertToPoetryData(poetryData); } Future getPreviousPoetry() async { await _loadCachedData(); if (_cachedPoetryList.isEmpty) { return null; } _currentIndex = (_currentIndex - 1 + _cachedPoetryList.length) % _cachedPoetryList.length; final poetryData = _cachedPoetryList[_currentIndex]; return _convertToPoetryData(poetryData); } Future _loadCachedData() async { final prefs = await SharedPreferences.getInstance(); final cachedData = prefs.getStringList(_offlineDataKey) ?? []; _cachedPoetryList = []; for (final item in cachedData) { try { // 移除 Map 两边的大括号,然后分割键值对 final cleanItem = item.substring(1, item.length - 1); final keyValuePairs = cleanItem.split(', '); final map = {}; for (final pair in keyValuePairs) { final parts = pair.split(': '); if (parts.length == 2) { final key = parts[0].replaceAll('"', ''); var value = parts[1].replaceAll('"', ''); // 尝试转换数字 if (int.tryParse(value) != null) { map[key] = int.parse(value); } else if (double.tryParse(value) != null) { map[key] = double.parse(value); } else { map[key] = value; } } } _cachedPoetryList.add(map); } catch (e) { _debugLog('解析缓存数据失败: $e'); } } } PoetryData? _convertToPoetryData(Map data) { try { final id = data['id'] ?? 0; final name = data['name'] ?? ''; final alias = data['alias'] ?? ''; final keywords = data['keywords'] ?? ''; final introduce = data['introduce'] ?? ''; final drtime = data['drtime'] ?? ''; final like = data['like'] ?? 0; final url = data['url'] ?? ''; final tui = data['tui'] ?? 0; final star = data['star'] ?? 5; final hitsTotal = data['hitsTotal'] ?? 0; final hitsMonth = data['hitsMonth'] ?? 0; final hitsDay = data['hitsDay'] ?? 0; final date = data['date'] ?? ''; final datem = data['datem'] ?? ''; final time = data['time'] ?? ''; final createTime = data['createTime'] ?? ''; final updateTime = data['updateTime'] ?? ''; return PoetryData( id: id, name: name, alias: alias, keywords: keywords, introduce: introduce, drtime: drtime, like: like, url: url, tui: tui, star: star, hitsTotal: hitsTotal, hitsMonth: hitsMonth, hitsDay: hitsDay, date: date, datem: datem, time: time, createTime: createTime, updateTime: updateTime, ); } catch (e) { _debugLog('转换为PoetryData失败: $e'); return null; } } void _debugLog(String message) { if (kDebugMode) {} } }