前端改动: - 新建设置页面 (主题切换/关于/隐私政策/用户协议/儿童隐私保护) - SettingsBloc 注册到 MultiRepositoryProvider 全局可访问 - MoodBloc 修复编译错误 + 接入 /diary/stats/mood API - MoodPage 添加错误状态展示和重试按钮 - AchievementBloc + 页面改造接入 /diary/achievements API - StickerBloc + 页面改造接入 /diary/sticker-packs API - TemplateBloc + 页面改造接入 /diary/templates API - ProfilePage 设置入口改为跳转 /settings - 添加 /settings 路由 后端改动: - 扩展 mood_stats_service 测试 (连续天数算法/心情计数/边界场景) - 新增 class_service 测试 (班级码生成/唯一性/错误映射) - 新增 achievement_service 测试 (DTO 结构/序列化/map 构建) - 新增 sticker_service 测试 (DTO 序列化/错误处理) - 扩展 dto.rs 测试 (achievement/mood_stats/sticker/template/notification) - 清理 2 个 unused import warning 验证: - cargo check 0 error 0 warning - flutter analyze 0 error
149 lines
3.7 KiB
Dart
149 lines
3.7 KiB
Dart
// 心情 BLoC — 通过 API 加载心情统计数据
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:nuanji_app/data/models/journal_entry.dart';
|
|
import 'package:nuanji_app/data/remote/api_client.dart';
|
|
|
|
// ===== 模型 =====
|
|
|
|
/// 心情统计数据
|
|
class MoodStats {
|
|
final List<MoodCount> moodCounts;
|
|
final int streakDays;
|
|
final int totalJournals;
|
|
final Mood? dominantMood;
|
|
|
|
const MoodStats({
|
|
this.moodCounts = const [],
|
|
this.streakDays = 0,
|
|
this.totalJournals = 0,
|
|
this.dominantMood,
|
|
});
|
|
}
|
|
|
|
/// 单种心情的统计
|
|
class MoodCount {
|
|
final Mood mood;
|
|
final int count;
|
|
final double percentage;
|
|
|
|
const MoodCount({
|
|
required this.mood,
|
|
required this.count,
|
|
required this.percentage,
|
|
});
|
|
}
|
|
|
|
/// 统计周期
|
|
enum StatsPeriod { week, month, quarter }
|
|
|
|
// ===== State =====
|
|
|
|
/// 心情页面状态
|
|
class MoodState {
|
|
final MoodStats stats;
|
|
final StatsPeriod selectedPeriod;
|
|
final bool isLoading;
|
|
final String? errorMessage;
|
|
|
|
const MoodState({
|
|
this.stats = const MoodStats(),
|
|
this.selectedPeriod = StatsPeriod.week,
|
|
this.isLoading = false,
|
|
this.errorMessage,
|
|
});
|
|
|
|
MoodState copyWith({
|
|
MoodStats? stats,
|
|
StatsPeriod? selectedPeriod,
|
|
bool? isLoading,
|
|
String? errorMessage,
|
|
}) =>
|
|
MoodState(
|
|
stats: stats ?? this.stats,
|
|
selectedPeriod: selectedPeriod ?? this.selectedPeriod,
|
|
isLoading: isLoading ?? this.isLoading,
|
|
errorMessage: errorMessage,
|
|
);
|
|
}
|
|
|
|
// ===== BLoC =====
|
|
|
|
/// 心情统计 BLoC — ChangeNotifier 模式
|
|
class MoodBloc extends ChangeNotifier {
|
|
final ApiClient _api;
|
|
MoodState _state = const MoodState();
|
|
MoodState get state => _state;
|
|
|
|
MoodBloc({required ApiClient api}) : _api = api;
|
|
|
|
/// 初始加载
|
|
void load() {
|
|
_state = _state.copyWith(isLoading: true);
|
|
notifyListeners();
|
|
_loadStats();
|
|
}
|
|
|
|
/// 切换统计周期
|
|
void changePeriod(StatsPeriod period) {
|
|
if (period == _state.selectedPeriod) return;
|
|
_state = _state.copyWith(selectedPeriod: period, isLoading: true);
|
|
notifyListeners();
|
|
_loadStats();
|
|
}
|
|
|
|
Future<void> _loadStats() async {
|
|
try {
|
|
final periodStr = switch (_state.selectedPeriod) {
|
|
StatsPeriod.week => 'week',
|
|
StatsPeriod.month => 'month',
|
|
StatsPeriod.quarter => 'quarter',
|
|
};
|
|
|
|
final response = await _api.get(
|
|
'/diary/stats/mood',
|
|
queryParams: {'period': periodStr},
|
|
);
|
|
final body = response.data as Map<String, dynamic>;
|
|
final data = body['data'] as Map<String, dynamic>;
|
|
|
|
final counts = (data['mood_counts'] as List? ?? [])
|
|
.map((item) {
|
|
final m = item as Map<String, dynamic>;
|
|
return MoodCount(
|
|
mood: Mood.values.firstWhere(
|
|
(v) => v.value == m['mood'],
|
|
orElse: () => Mood.happy,
|
|
),
|
|
count: (m['count'] as num).toInt(),
|
|
percentage: (m['percentage'] as num).toDouble(),
|
|
);
|
|
})
|
|
.toList();
|
|
|
|
final dominant = data['dominant_mood'] != null
|
|
? Mood.values.firstWhere(
|
|
(v) => v.value == data['dominant_mood'],
|
|
orElse: () => Mood.happy,
|
|
)
|
|
: null;
|
|
|
|
_state = _state.copyWith(
|
|
isLoading: false,
|
|
stats: MoodStats(
|
|
moodCounts: counts,
|
|
streakDays: (data['streak_days'] as num?)?.toInt() ?? 0,
|
|
totalJournals: (data['total_journals'] as num?)?.toInt() ?? 0,
|
|
dominantMood: dominant,
|
|
),
|
|
);
|
|
} catch (e) {
|
|
_state = _state.copyWith(
|
|
isLoading: false,
|
|
errorMessage: '加载统计数据失败',
|
|
);
|
|
}
|
|
notifyListeners();
|
|
}
|
|
}
|