2024-12-10 13:04:11 +04:00
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
|
import 'events.dart';
|
|
|
|
|
import 'state.dart';
|
2024-12-10 13:06:37 +04:00
|
|
|
|
import '../utils/character_service.dart';
|
|
|
|
|
import '../models/character.dart';
|
2024-12-10 13:04:11 +04:00
|
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
|
|
class DebouncedSearchCubit extends Cubit<String> {
|
|
|
|
|
DebouncedSearchCubit() : super('');
|
|
|
|
|
|
|
|
|
|
Timer? _debounce;
|
|
|
|
|
|
|
|
|
|
// Метод для обновления поиска с задержкой
|
|
|
|
|
void search(String query) {
|
|
|
|
|
if (_debounce?.isActive ?? false) _debounce?.cancel();
|
|
|
|
|
_debounce = Timer(const Duration(milliseconds: 500), () {
|
|
|
|
|
emit(query);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<void> close() {
|
|
|
|
|
_debounce?.cancel();
|
|
|
|
|
return super.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-17 14:28:23 +04:00
|
|
|
|
|
2024-12-10 13:04:11 +04:00
|
|
|
|
class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
|
|
|
|
final CharacterService characterService;
|
|
|
|
|
|
|
|
|
|
HomeBloc(this.characterService) : super(const HomeState()) {
|
|
|
|
|
on<HomeLoadDataEvent>(_onLoadData);
|
2024-12-17 14:28:23 +04:00
|
|
|
|
on<HomeSearchEvent>(_onSearchCharacters); // Добавлен новый обработчик для поиска
|
2024-12-10 13:04:11 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-17 14:28:23 +04:00
|
|
|
|
// Загрузка данных
|
2024-12-10 13:04:11 +04:00
|
|
|
|
Future<void> _onLoadData(HomeLoadDataEvent event, Emitter<HomeState> emit) async {
|
|
|
|
|
emit(state.copyWith(status: HomeStatus.loading));
|
|
|
|
|
|
|
|
|
|
try {
|
2024-12-10 13:41:28 +04:00
|
|
|
|
// Получаем список объектов CharacterDTO
|
|
|
|
|
final charactersDTO = await characterService.getCharacters(search: event.searchQuery);
|
|
|
|
|
|
|
|
|
|
// Преобразуем список Character в список CharacterDTO
|
|
|
|
|
final charactersAsDTO = charactersDTO.map((character) => character.toDTO()).toList();
|
|
|
|
|
|
|
|
|
|
emit(state.copyWith(status: HomeStatus.loaded, characters: charactersAsDTO)); // Передаем в emit
|
2024-12-10 13:04:11 +04:00
|
|
|
|
} catch (e) {
|
|
|
|
|
print('Error: $e');
|
|
|
|
|
emit(state.copyWith(status: HomeStatus.error, errorMessage: e.toString()));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-10 13:41:28 +04:00
|
|
|
|
|
2024-12-17 14:28:23 +04:00
|
|
|
|
// Логика поиска
|
|
|
|
|
void _onSearchCharacters(HomeSearchEvent event, Emitter<HomeState> emit) {
|
|
|
|
|
final query = event.query.toLowerCase();
|
2024-12-10 13:41:28 +04:00
|
|
|
|
|
2024-12-17 14:28:23 +04:00
|
|
|
|
// Фильтруем данные по запросу
|
|
|
|
|
final filteredCharacters = state.characters
|
|
|
|
|
.where((character) => character.name.toLowerCase().contains(query))
|
|
|
|
|
.toList();
|
2024-12-10 13:04:11 +04:00
|
|
|
|
|
2024-12-17 14:28:23 +04:00
|
|
|
|
// Обновляем состояние с отфильтрованными данными
|
|
|
|
|
emit(state.copyWith(status: HomeStatus.loaded, characters: filteredCharacters));
|
|
|
|
|
}
|
|
|
|
|
}
|