home_view_model.dart 2.9 KB

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