// 搜索 BLoC — 标签+心情筛选日记 // // 状态机: SearchInitial → SearchLoading → SearchLoaded/SearchError // Phase 1 使用简单的标签+心情筛选,后续可扩展全文搜索。 import 'package:flutter_bloc/flutter_bloc.dart'; import '../../../data/models/journal_entry.dart'; import '../../../data/repositories/journal_repository.dart'; part 'search_event.dart'; part 'search_state.dart'; /// 搜索 BLoC — 处理标签和心情筛选日记的状态转换 class SearchBloc extends Bloc { final JournalRepository _journalRepo; SearchBloc({required JournalRepository journalRepository}) : _journalRepo = journalRepository, super(const SearchInitial()) { on(_onSearchByMood); on(_onSearchByTag); on(_onSearchClear); } /// 按心情筛选日记 Future _onSearchByMood( SearchByMood event, Emitter emit, ) async { emit(const SearchLoading()); try { if (event.mood == null) { emit(const SearchLoaded()); return; } final results = await _journalRepo.getJournals( mood: event.mood!.value, page: 1, pageSize: 50, ); emit(SearchLoaded( results: results, activeMood: event.mood!.value, )); } catch (e) { emit(const SearchError('搜索失败,请重试')); } } /// 按标签筛选日记 Future _onSearchByTag( SearchByTag event, Emitter emit, ) async { emit(const SearchLoading()); try { final results = await _journalRepo.getJournals( tag: event.tag, page: 1, pageSize: 50, ); emit(SearchLoaded( results: results, activeTag: event.tag, )); } catch (e) { emit(const SearchError('搜索失败,请重试')); } } /// 清除搜索结果 void _onSearchClear( SearchClear event, Emitter emit, ) { emit(const SearchLoaded()); } }