home_view_model.dart 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import 'package:flutter/material.dart';
  2. import 'package:sqflite/sqflite.dart';
  3. import '../../core/services/reposit_factory.dart';
  4. import '../../core/services/services.dart';
  5. import '../../core/models/models.dart';
  6. class HomeViewModel extends ChangeNotifier {
  7. final Repository<Producto> productoRepository;
  8. final Repository<CategoriaProducto> categoriaRepository;
  9. HomeViewModel({
  10. required this.productoRepository,
  11. required this.categoriaRepository,
  12. });
  13. String _busqueda = "";
  14. String get busqueda => _busqueda;
  15. List<CategoriaProducto> _categorias = [];
  16. bool _isLoading = false;
  17. CategoriaProducto? _selectedCategoria;
  18. List<CategoriaProducto> get categorias => _categorias;
  19. bool get isLoading => _isLoading;
  20. CategoriaProducto? get selectedCategoria => _selectedCategoria;
  21. List<Producto> _productos = [];
  22. Producto? _selectedProducto;
  23. List<Producto> get productos => _productos;
  24. Producto? get selectedProducto => _selectedProducto;
  25. int _currentPage = 1;
  26. int _totalCategorias = 0;
  27. int _limit = 10;
  28. int get currentPage => _currentPage;
  29. int get totalCategorias => _totalCategorias;
  30. int get totalPages => (_totalCategorias / _limit).ceil();
  31. setBusqueda(String value) {
  32. _busqueda = value;
  33. notifyListeners();
  34. }
  35. void selectCategoria(CategoriaProducto categoria) {
  36. _selectedCategoria = categoria;
  37. notifyListeners();
  38. }
  39. void selectProducto(Producto producto) {
  40. _selectedProducto = producto;
  41. notifyListeners();
  42. }
  43. Future<void> fetchLocalCategorias() async {
  44. _isLoading = true;
  45. notifyListeners();
  46. final resultado = await categoriaRepository.consultarConOrden(
  47. orderBy: 'nombre ASC', where: 'eliminado IS NULL');
  48. _categorias = resultado;
  49. _isLoading = false;
  50. notifyListeners();
  51. }
  52. Future<void> fetchLocalProductosPorCategoria(
  53. CategoriaProducto categoria) async {
  54. _isLoading = true;
  55. notifyListeners();
  56. final resultado = await productoRepository.consultarConOrden(
  57. orderBy: 'nombre ASC',
  58. where: 'eliminado IS NULL AND idCategoria = ${categoria.id}');
  59. _productos = resultado;
  60. _isLoading = false;
  61. notifyListeners();
  62. }
  63. // void nextPage() {
  64. // if (_currentPage < totalPages) {
  65. // fetchLocal(page: _currentPage + 1);
  66. // }
  67. // }
  68. // void previousPage() {
  69. // if (_currentPage > 1) {
  70. // fetchLocal(page: _currentPage - 1);
  71. // }
  72. // }
  73. }