Files
wushu/lib/views/profile/guide/tongji.dart
Developer d6ac0ed1e4 重构
2026-03-31 05:42:47 +08:00

244 lines
7.3 KiB
Dart

/// 时间: 2026-03-31
/// 功能: 统计数据管理
/// 介绍: 管理应用的各种统计数据,包括浏览次数、答题次数、点赞数等
/// 最新变化: 新建文件
import 'package:shared_preferences/shared_preferences.dart';
class StatisticsManager {
static const String _todayViewsKey = 'today_views';
static const String _weekViewsKey = 'week_views';
static const String _firstUseTimeKey = 'first_use_time';
static const String _todayLikesKey = 'today_likes';
static const String _todayQuestionsKey = 'today_questions';
static const String _lastViewDateKey = 'last_view_date';
static const String _lastWeekKey = 'last_week_start';
static const String _lastQuestionDateKey = 'last_question_date';
static StatisticsManager? _instance;
StatisticsManager._internal();
factory StatisticsManager() {
_instance ??= StatisticsManager._internal();
return _instance!;
}
/// 记录一次浏览
Future<void> recordView() async {
final prefs = await SharedPreferences.getInstance();
final now = DateTime.now();
final today = _formatDate(now);
final weekStart = _getWeekStart(now);
final lastViewDate = prefs.getString(_lastViewDateKey);
final lastWeekStart = prefs.getString(_lastWeekKey);
if (lastViewDate != today) {
await prefs.setInt(_todayViewsKey, 0);
await prefs.setString(_lastViewDateKey, today);
}
if (lastWeekStart != weekStart) {
await prefs.setInt(_weekViewsKey, 0);
await prefs.setString(_lastWeekKey, weekStart);
}
final todayViews = prefs.getInt(_todayViewsKey) ?? 0;
final weekViews = prefs.getInt(_weekViewsKey) ?? 0;
await prefs.setInt(_todayViewsKey, todayViews + 1);
await prefs.setInt(_weekViewsKey, weekViews + 1);
}
/// 获取今日浏览次数
Future<int> getTodayViews() async {
final prefs = await SharedPreferences.getInstance();
final today = _formatDate(DateTime.now());
final lastViewDate = prefs.getString(_lastViewDateKey);
if (lastViewDate != today) {
return 0;
}
return prefs.getInt(_todayViewsKey) ?? 0;
}
/// 获取本周浏览次数
Future<int> getWeekViews() async {
final prefs = await SharedPreferences.getInstance();
final now = DateTime.now();
final weekStart = _getWeekStart(now);
final lastWeekStart = prefs.getString(_lastWeekKey);
if (lastWeekStart != weekStart) {
return 0;
}
return prefs.getInt(_weekViewsKey) ?? 0;
}
/// 记录首次使用时间
Future<void> recordFirstUse() async {
final prefs = await SharedPreferences.getInstance();
if (!prefs.containsKey(_firstUseTimeKey)) {
await prefs.setString(_firstUseTimeKey, DateTime.now().toIso8601String());
}
}
/// 获取首次使用时间
Future<String> getFirstUseTime() async {
final prefs = await SharedPreferences.getInstance();
final timeStr = prefs.getString(_firstUseTimeKey);
if (timeStr == null) {
return '未记录';
}
final date = DateTime.parse(timeStr);
return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
}
/// 记录今日点赞
Future<void> recordTodayLike() async {
final prefs = await SharedPreferences.getInstance();
final now = DateTime.now();
final today = _formatDate(now);
final lastLikeDate = prefs.getString('last_like_date');
if (lastLikeDate != today) {
await prefs.setInt(_todayLikesKey, 0);
await prefs.setString('last_like_date', today);
}
final todayLikes = prefs.getInt(_todayLikesKey) ?? 0;
await prefs.setInt(_todayLikesKey, todayLikes + 1);
}
/// 获取今日点赞数
Future<int> getTodayLikes() async {
final prefs = await SharedPreferences.getInstance();
final today = _formatDate(DateTime.now());
final lastLikeDate = prefs.getString('last_like_date');
if (lastLikeDate != today) {
return 0;
}
return prefs.getInt(_todayLikesKey) ?? 0;
}
/// 获取数据占用空间
Future<String> getDataSize() async {
try {
final prefs = await SharedPreferences.getInstance();
final keys = prefs.getKeys();
int totalBytes = 0;
for (final key in keys) {
final value = prefs.get(key);
if (value != null) {
totalBytes += value.toString().length * 2;
}
}
if (totalBytes < 1024) {
return '$totalBytes B';
} else if (totalBytes < 1024 * 1024) {
return '${(totalBytes / 1024).toStringAsFixed(1)} KB';
} else {
return '${(totalBytes / (1024 * 1024)).toStringAsFixed(1)} MB';
}
} catch (e) {
return '计算失败';
}
}
/// 获取累计浏览数
Future<int> getTotalViews() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getInt('total_views') ?? 0;
}
/// 记录累计浏览
Future<void> recordTotalView() async {
final prefs = await SharedPreferences.getInstance();
final totalViews = prefs.getInt('total_views') ?? 0;
await prefs.setInt('total_views', totalViews + 1);
}
/// 获取累计点赞数
Future<int> getTotalLikes() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getInt('total_likes') ?? 0;
}
/// 记录累计点赞
Future<void> recordTotalLike() async {
final prefs = await SharedPreferences.getInstance();
final totalLikes = prefs.getInt('total_likes') ?? 0;
await prefs.setInt('total_likes', totalLikes + 1);
}
/// 获取累计答题数
Future<int> getTotalQuestions() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getInt('totalQuestions') ?? 0;
}
/// 记录今日答题
Future<void> recordTodayQuestion() async {
final prefs = await SharedPreferences.getInstance();
final now = DateTime.now();
final today = _formatDate(now);
final lastQuestionDate = prefs.getString(_lastQuestionDateKey);
if (lastQuestionDate != today) {
await prefs.setInt(_todayQuestionsKey, 0);
await prefs.setString(_lastQuestionDateKey, today);
}
final todayQuestions = prefs.getInt(_todayQuestionsKey) ?? 0;
await prefs.setInt(_todayQuestionsKey, todayQuestions + 1);
}
/// 获取今日答题数
Future<int> getTodayQuestions() async {
final prefs = await SharedPreferences.getInstance();
final today = _formatDate(DateTime.now());
final lastQuestionDate = prefs.getString(_lastQuestionDateKey);
if (lastQuestionDate != today) {
return 0;
}
return prefs.getInt(_todayQuestionsKey) ?? 0;
}
/// 获取使用天数
Future<int> getUseDays() async {
final prefs = await SharedPreferences.getInstance();
final timeStr = prefs.getString(_firstUseTimeKey);
if (timeStr == null) {
return 1;
}
try {
final firstUseDate = DateTime.parse(timeStr);
final today = DateTime.now();
final difference = today.difference(firstUseDate);
return difference.inDays + 1;
} catch (e) {
return 1;
}
}
/// 格式化日期为 YYYY-MM-DD
String _formatDate(DateTime date) {
return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
}
/// 获取本周开始日期(周一)
String _getWeekStart(DateTime date) {
final monday = date.subtract(Duration(days: date.weekday - 1));
return _formatDate(monday);
}
}