65 lines
2.4 KiB
Dart
65 lines
2.4 KiB
Dart
// lib/features/recycling_guide/presentation/providers/recycling_provider.dart
|
|
// Riverpod — compatible con lo que usa Persona C en el resto de la app.
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import '../../data/repositories/recycling_repository.dart';
|
|
import '../../domain/entities/recycling_category.dart';
|
|
|
|
// ── Repositorio singleton ────────────────────────────────────────────
|
|
final recyclingRepositoryProvider = Provider<RecyclingRepository>(
|
|
(ref) => RecyclingRepository(),
|
|
);
|
|
|
|
// ── Categorías (carga inicial desde JSON) ────────────────────────────
|
|
final recyclingCategoriesProvider =
|
|
FutureProvider<List<RecyclingCategory>>((ref) {
|
|
return ref.watch(recyclingRepositoryProvider).obtenerCategorias();
|
|
});
|
|
|
|
// ── Estado del buscador ──────────────────────────────────────────────
|
|
class RecyclingSearchNotifier extends StateNotifier<RecyclingSearchState> {
|
|
final RecyclingRepository _repo;
|
|
|
|
RecyclingSearchNotifier(this._repo)
|
|
: super(const RecyclingSearchState.idle());
|
|
|
|
Future<void> buscar(String query) async {
|
|
if (query.trim().isEmpty) {
|
|
state = const RecyclingSearchState.idle();
|
|
return;
|
|
}
|
|
state = const RecyclingSearchState.loading();
|
|
final resultados = await _repo.buscar(query);
|
|
state = RecyclingSearchState.done(resultados);
|
|
}
|
|
|
|
void limpiar() => state = const RecyclingSearchState.idle();
|
|
}
|
|
|
|
final recyclingSearchProvider =
|
|
StateNotifierProvider<RecyclingSearchNotifier, RecyclingSearchState>((ref) {
|
|
return RecyclingSearchNotifier(ref.watch(recyclingRepositoryProvider));
|
|
});
|
|
|
|
// ── Estado sellado del buscador ──────────────────────────────────────
|
|
sealed class RecyclingSearchState {
|
|
const RecyclingSearchState();
|
|
|
|
const factory RecyclingSearchState.idle() = _Idle;
|
|
const factory RecyclingSearchState.loading() = _Loading;
|
|
const factory RecyclingSearchState.done(List<SearchResult> results) = _Done;
|
|
}
|
|
|
|
class _Idle extends RecyclingSearchState {
|
|
const _Idle();
|
|
}
|
|
|
|
class _Loading extends RecyclingSearchState {
|
|
const _Loading();
|
|
}
|
|
|
|
class _Done extends RecyclingSearchState {
|
|
final List<SearchResult> results;
|
|
const _Done(this.results);
|
|
}
|