feat(app): 设置页 UI + Mood/成就/贴纸 BLoC 接入 API + B7 测试扩展
前端改动: - 新建设置页面 (主题切换/关于/隐私政策/用户协议/儿童隐私保护) - 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
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
// 心情 BLoC — 管理心情统计和趋势数据
|
||||
// 心情 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 {
|
||||
@@ -33,35 +34,20 @@ class MoodCount {
|
||||
});
|
||||
}
|
||||
|
||||
/// 心情趋势数据点
|
||||
class MoodTrendPoint {
|
||||
final DateTime date;
|
||||
final Mood mood;
|
||||
final int journalCount;
|
||||
|
||||
const MoodTrendPoint({
|
||||
required this.date,
|
||||
required this.mood,
|
||||
required this.journalCount,
|
||||
});
|
||||
}
|
||||
|
||||
/// 统计周期
|
||||
enum StatsPeriod { week, month, quarter }
|
||||
|
||||
// ===== 心情页面状态 =====
|
||||
// ===== State =====
|
||||
|
||||
/// 心情页面状态
|
||||
class MoodState {
|
||||
final MoodStats stats;
|
||||
final List<MoodTrendPoint> trendData;
|
||||
final StatsPeriod selectedPeriod;
|
||||
final bool isLoading;
|
||||
final String? errorMessage;
|
||||
|
||||
const MoodState({
|
||||
this.stats = const MoodStats(),
|
||||
this.trendData = const [],
|
||||
this.selectedPeriod = StatsPeriod.week,
|
||||
this.isLoading = false,
|
||||
this.errorMessage,
|
||||
@@ -69,55 +55,27 @@ class MoodState {
|
||||
|
||||
MoodState copyWith({
|
||||
MoodStats? stats,
|
||||
List<MoodTrendPoint>? trendData,
|
||||
StatsPeriod? selectedPeriod,
|
||||
bool? isLoading,
|
||||
String? errorMessage,
|
||||
}) =>
|
||||
MoodState(
|
||||
stats: stats ?? this.stats,
|
||||
trendData: trendData ?? this.trendData,
|
||||
selectedPeriod: selectedPeriod ?? this.selectedPeriod,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
errorMessage: errorMessage,
|
||||
);
|
||||
}
|
||||
|
||||
// ===== 心情 BLoC =====
|
||||
// ===== BLoC =====
|
||||
|
||||
/// 心情统计 BLoC — ChangeNotifier 模式
|
||||
class MoodBloc extends ChangeNotifier {
|
||||
final ApiClient _api;
|
||||
MoodState _state = const MoodState();
|
||||
MoodState get state => _state;
|
||||
|
||||
/// 切换统计周期
|
||||
void changePeriod(StatsPeriod period) {
|
||||
_state = _state.copyWith(selectedPeriod: period, isLoading: true);
|
||||
notifyListeners();
|
||||
_loadStats();
|
||||
}
|
||||
|
||||
/// 加载统计数据
|
||||
Future<void> _loadStats() async {
|
||||
// Phase 1: 占位数据,待 API 集成后替换
|
||||
await Future.delayed(const Duration(milliseconds: 300));
|
||||
|
||||
_state = _state.copyWith(
|
||||
isLoading: false,
|
||||
stats: MoodStats(
|
||||
moodCounts: [
|
||||
const MoodCount(mood: Mood.happy, count: 12, percentage: 40.0),
|
||||
const MoodCount(mood: Mood.calm, count: 8, percentage: 26.7),
|
||||
const MoodCount(mood: Mood.thinking, count: 5, percentage: 16.7),
|
||||
const MoodCount(mood: Mood.sad, count: 3, percentage: 10.0),
|
||||
const MoodCount(mood: Mood.angry, count: 2, percentage: 6.6),
|
||||
],
|
||||
streakDays: 7,
|
||||
totalJournals: 30,
|
||||
dominantMood: Mood.happy,
|
||||
),
|
||||
);
|
||||
notifyListeners();
|
||||
}
|
||||
MoodBloc({required ApiClient api}) : _api = api;
|
||||
|
||||
/// 初始加载
|
||||
void load() {
|
||||
@@ -125,4 +83,66 @@ class MoodBloc extends ChangeNotifier {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user