// 心情 BLoC — 管理心情统计和趋势数据 import 'package:flutter/material.dart'; import 'package:nuanji_app/data/models/journal_entry.dart'; // ===== 心情统计模型 ===== /// 心情统计数据 class MoodStats { final List 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, }); } /// 心情趋势数据点 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 } // ===== 心情页面状态 ===== /// 心情页面状态 class MoodState { final MoodStats stats; final List 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, }); MoodState copyWith({ MoodStats? stats, List? 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 ===== class MoodBloc extends ChangeNotifier { MoodState _state = const MoodState(); MoodState get state => _state; /// 切换统计周期 void changePeriod(StatsPeriod period) { _state = _state.copyWith(selectedPeriod: period, isLoading: true); notifyListeners(); _loadStats(); } /// 加载统计数据 Future _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(); } /// 初始加载 void load() { _state = _state.copyWith(isLoading: true); notifyListeners(); _loadStats(); } }