home_view_model.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. bool _isOpen = false;
  17. bool get isopen => _isOpen;
  18. String _busqueda = "";
  19. String get busqueda => _busqueda;
  20. List<CategoriaProducto> _categorias = [];
  21. bool _isLoading = false;
  22. CategoriaProducto? _selectedCategoria;
  23. List<CategoriaProducto> get categorias => _categorias;
  24. bool get isLoading => _isLoading;
  25. CategoriaProducto? get selectedCategoria => _selectedCategoria;
  26. List<Producto> _productos = [];
  27. Producto? _selectedProducto;
  28. List<Producto> get productos => _productos;
  29. Producto? get selectedProducto => _selectedProducto;
  30. int _currentPage = 1;
  31. int _totalCategorias = 0;
  32. int _limit = 10;
  33. int get currentPage => _currentPage;
  34. int get totalCategorias => _totalCategorias;
  35. int get totalPages => (_totalCategorias / _limit).ceil();
  36. setBusqueda(String value) {
  37. _busqueda = value;
  38. notifyListeners();
  39. }
  40. void selectCategoria(CategoriaProducto categoria) {
  41. _selectedCategoria = categoria;
  42. notifyListeners();
  43. }
  44. void selectProducto(Producto producto) {
  45. _selectedProducto = producto;
  46. notifyListeners();
  47. }
  48. Future<void> fetchLocalCategorias() async {
  49. _isLoading = true;
  50. notifyListeners();
  51. final resultado = await categoriaRepository.consultarConOrden(
  52. orderBy: 'nombre ASC', where: 'eliminado IS NULL');
  53. _categorias = resultado;
  54. _isLoading = false;
  55. notifyListeners();
  56. }
  57. Future<void> fetchLocalProductosPorCategoria(
  58. CategoriaProducto categoria) async {
  59. _isLoading = true;
  60. notifyListeners();
  61. final resultado = await productoRepository.consultarConOrden(
  62. orderBy: 'nombre ASC',
  63. where: 'eliminado IS NULL AND idCategoria = ${categoria.id}');
  64. _productos = resultado;
  65. _isLoading = false;
  66. notifyListeners();
  67. }
  68. void openDrawer() {
  69. print(scaffoldKey.currentState);
  70. scaffoldKey.currentState?.openDrawer();
  71. }
  72. void closeDrawer() {
  73. scaffoldKey.currentState?.closeDrawer();
  74. }
  75. bool isDrawerOpen() {
  76. return scaffoldKey.currentState?.isDrawerOpen ?? false;
  77. }
  78. void toggleDrawer() {
  79. if (isDrawerOpen()) {
  80. closeDrawer();
  81. } else {
  82. openDrawer();
  83. }
  84. }
  85. void isHorarioServicio() {
  86. DateTime now = DateTime.now();
  87. int hora = now.hour;
  88. int minuto = now.minute;
  89. int weekday = now.weekday;
  90. // 1 = Lunes, 7 = Domingo
  91. bool isOpen = false;
  92. if (weekday >= 1 && weekday <= 6) {
  93. // Lunes a Sábado: 8:00 a.m. - 10:00 p.m.
  94. isOpen = (hora >= 8 && hora < 22) || (hora == 22 && minuto == 0);
  95. } else if (weekday == 7) {
  96. // Domingo: 4:00 p.m. - 10:00 p.m.
  97. isOpen = (hora >= 16 && hora < 22) || (hora == 22 && minuto == 0);
  98. }
  99. _isOpen = isOpen;
  100. notifyListeners();
  101. }
  102. // void nextPage() {
  103. // if (_currentPage < totalPages) {
  104. // fetchLocal(page: _currentPage + 1);
  105. // }
  106. // }
  107. // void previousPage() {
  108. // if (_currentPage > 1) {
  109. // fetchLocal(page: _currentPage - 1);
  110. // }
  111. // }
  112. }