12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- import 'package:flutter/material.dart';
- import 'package:sqflite/sqflite.dart';
- import '../../core/services/reposit_factory.dart';
- import '../../core/services/services.dart';
- import '../../core/models/models.dart';
- class HomeViewModel extends ChangeNotifier {
- final Repository<Producto> productoRepository;
- final Repository<CategoriaProducto> categoriaRepository;
- HomeViewModel({
- required this.productoRepository,
- required this.categoriaRepository,
- });
- String _busqueda = "";
- String get busqueda => _busqueda;
- List<CategoriaProducto> _categorias = [];
- bool _isLoading = false;
- CategoriaProducto? _selectedCategoria;
- List<CategoriaProducto> get categorias => _categorias;
- bool get isLoading => _isLoading;
- CategoriaProducto? get selectedCategoria => _selectedCategoria;
- List<Producto> _productos = [];
- Producto? _selectedProducto;
- List<Producto> get productos => _productos;
- Producto? get selectedProducto => _selectedProducto;
- int _currentPage = 1;
- int _totalCategorias = 0;
- int _limit = 10;
- int get currentPage => _currentPage;
- int get totalCategorias => _totalCategorias;
- int get totalPages => (_totalCategorias / _limit).ceil();
- setBusqueda(String value) {
- _busqueda = value;
- notifyListeners();
- }
- void selectCategoria(CategoriaProducto categoria) {
- _selectedCategoria = categoria;
- notifyListeners();
- }
- void selectProducto(Producto producto) {
- _selectedProducto = producto;
- notifyListeners();
- }
- Future<void> fetchLocalCategorias() async {
- _isLoading = true;
- notifyListeners();
- final resultado = await categoriaRepository.consultarConOrden(
- orderBy: 'nombre ASC', where: 'eliminado IS NULL');
- _categorias = resultado;
- _isLoading = false;
- notifyListeners();
- }
- Future<void> fetchLocalProductosPorCategoria(
- CategoriaProducto categoria) async {
- _isLoading = true;
- notifyListeners();
- final resultado = await productoRepository.consultarConOrden(
- orderBy: 'nombre ASC',
- where: 'eliminado IS NULL AND idCategoria = ${categoria.id}');
- _productos = resultado;
- _isLoading = false;
- notifyListeners();
- }
- // void nextPage() {
- // if (_currentPage < totalPages) {
- // fetchLocal(page: _currentPage + 1);
- // }
- // }
- // void previousPage() {
- // if (_currentPage > 1) {
- // fetchLocal(page: _currentPage - 1);
- // }
- // }
- }
|