import 'dart:async'; import 'package:equatable/equatable.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import '../data/variety_repository.dart'; /// Inventory list state: all items from the DB plus the current search query. /// [visibleItems] applies the query; grouping by category is done in the UI. class InventoryState extends Equatable { const InventoryState({ this.items = const [], this.query = '', this.loading = true, }); final List items; final String query; final bool loading; List get visibleItems { if (query.trim().isEmpty) return items; final q = query.toLowerCase(); return items.where((i) => i.label.toLowerCase().contains(q)).toList(); } InventoryState copyWith({ List? items, String? query, bool? loading, }) { return InventoryState( items: items ?? this.items, query: query ?? this.query, loading: loading ?? this.loading, ); } @override List get props => [items, query, loading]; } /// Subscribes to the repository's reactive inventory stream. The list updates /// automatically after a quick-add — no manual refresh. class InventoryCubit extends Cubit { InventoryCubit(this._repo) : super(const InventoryState()) { _sub = _repo.watchInventory().listen( (items) => emit(state.copyWith(items: items, loading: false)), ); } final VarietyRepository _repo; late final StreamSubscription> _sub; void search(String query) => emit(state.copyWith(query: query)); @override Future close() async { await _sub.cancel(); return super.close(); } }