import 'dart:ffi'; 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 { //drawer final GlobalKey scaffoldKey = GlobalKey(); final Repository productoRepository; final Repository categoriaRepository; HomeViewModel({ required this.productoRepository, required this.categoriaRepository, }); String _busqueda = ""; String get busqueda => _busqueda; List _categorias = []; bool _isLoading = false; CategoriaProducto? _selectedCategoria; List get categorias => _categorias; bool get isLoading => _isLoading; CategoriaProducto? get selectedCategoria => _selectedCategoria; List _productos = []; Producto? _selectedProducto; List 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 fetchLocalCategorias() async { _isLoading = true; notifyListeners(); final resultado = await categoriaRepository.consultarConOrden( orderBy: 'nombre ASC', where: 'eliminado IS NULL'); _categorias = resultado; _isLoading = false; notifyListeners(); } Future 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 openDrawer() { print(scaffoldKey.currentState); scaffoldKey.currentState?.openDrawer(); } void closeDrawer() { scaffoldKey.currentState?.closeDrawer(); } bool isDrawerOpen() { return scaffoldKey.currentState?.isDrawerOpen ?? false; } void toggleDrawer() { if (isDrawerOpen()) { closeDrawer(); } else { openDrawer(); } } // void nextPage() { // if (_currentPage < totalPages) { // fetchLocal(page: _currentPage + 1); // } // } // void previousPage() { // if (_currentPage > 1) { // fetchLocal(page: _currentPage - 1); // } // } }