123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- 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<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
- final Repository<Producto> productoRepository;
- final Repository<CategoriaProducto> categoriaRepository;
- HomeViewModel({
- required this.productoRepository,
- required this.categoriaRepository,
- });
- bool _isOpen = false;
- bool get isopen => _isOpen;
- 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 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 isHorarioServicio() {
- DateTime now = DateTime.now();
- int hora = now.hour;
- int minuto = now.minute;
- int weekday = now.weekday;
- // 1 = Lunes, 7 = Domingo
- bool isOpen = false;
- if (weekday >= 1 && weekday <= 6) {
- // Lunes a Sábado: 8:00 a.m. - 10:00 p.m.
- isOpen = (hora >= 8 && hora < 22) || (hora == 22 && minuto == 0);
- } else if (weekday == 7) {
- // Domingo: 4:00 p.m. - 10:00 p.m.
- isOpen = (hora >= 16 && hora < 22) || (hora == 22 && minuto == 0);
- }
- _isOpen = isOpen;
- notifyListeners();
- }
- // void nextPage() {
- // if (_currentPage < totalPages) {
- // fetchLocal(page: _currentPage + 1);
- // }
- // }
- // void previousPage() {
- // if (_currentPage > 1) {
- // fetchLocal(page: _currentPage - 1);
- // }
- // }
- }
|