Selaa lähdekoodia

Home view model con fetch de categorias y productos por categoria

OscarGil03 2 kuukautta sitten
vanhempi
commit
f51cccea1c

+ 26 - 0
lib/core/models/categoria_producto_model.dart

@@ -40,6 +40,32 @@ class CategoriaProducto extends Basico {
     }..addAll(super.toJson());
   }
 
+  Map<String, dynamic> toMap() {
+    return {
+      'id': id,
+      'nombre': nombre ?? '',
+      'descripcion': descripcion ?? '',
+      'esToping': esToping ?? 0,
+      'maximo': maximo ?? 0,
+      'minimo': minimo ?? 0,
+      'creado': creado?.toIso8601String(),
+      'modificado': modificado?.toIso8601String(),
+      'eliminado': eliminado?.toIso8601String(),
+    };
+  }
+
+  static CategoriaProducto fromMap(Map<String, dynamic> map) {
+    final mesa = CategoriaProducto(
+      id: map['id'] as int,
+      nombre: map['nombre'] as String?,
+      descripcion: map['descripcion'] as String?,
+      esToping: map['esToping'] as int,
+      maximo: map['maximo'] as int,
+      minimo: map['minimo'] as int,
+    );
+    return mesa;
+  }
+
   CategoriaProducto.fromJson(Map<String, dynamic> json) {
     super.parseJson(json);
     nombre = Basico.parseString(json['nombre']);

+ 19 - 0
lib/core/models/producto_model.dart

@@ -96,6 +96,25 @@ class Producto extends Basico {
     };
   }
 
+  static Producto fromMap(Map<String, dynamic> map) {
+    final mesa = Producto(
+      id: map['id'] as int,
+      idCategoria: map['id'] as int,
+      nombre: map['nombre'] as String?,
+      descripcion: map['descripcion'] as String?,
+      imagen: map['imagen'] as String?,
+      venta: map['venta'] as int,
+      existencia: map['existencia'] as int,
+      precio: map['precio'] as double,
+      verMenu: map['verMenu'] as int,
+      codigo: map['codigo'] as String?,
+      descuento: map['descuento'] as String?,
+      toping: map['toping'] as int,
+      activo: map['activo'] as int,
+    );
+    return mesa;
+  }
+
   Producto.fromJson(Map<String, dynamic> json) {
     super.parseJson(json);
     idCategoria = Basico.parseInt(json['idCategoria']);

+ 17 - 0
lib/main.dart

@@ -33,6 +33,19 @@ void main() async {
     toMap: (m) => m.toMap(),
   );
 
+  final categoriaRepository =
+      repositoryFactory.getRepository<CategoriaProducto>(
+    tableName: 'CategoriaProducto',
+    fromMap: CategoriaProducto.fromMap,
+    toMap: (m) => m.toMap(),
+  );
+
+  final productoRepository = repositoryFactory.getRepository<Producto>(
+    tableName: 'Producto',
+    fromMap: Producto.fromMap,
+    toMap: (m) => m.toMap(),
+  );
+
   SystemChrome.setPreferredOrientations([
     DeviceOrientation.landscapeRight,
     DeviceOrientation.landscapeLeft,
@@ -44,6 +57,10 @@ void main() async {
       ChangeNotifierProvider(create: (_) => UsuarioViewModel()),
       ChangeNotifierProvider(create: (_) => ProductoViewModel()),
       ChangeNotifierProvider(
+          create: (_) => HomeViewModel(
+              categoriaRepository: categoriaRepository,
+              productoRepository: productoRepository)),
+      ChangeNotifierProvider(
           create: (_) => MesaViewModel(mesaRepository: mesaRepository)),
       ChangeNotifierProvider(create: (_) => CategoriaProductoViewModel()),
       // Agrega aquí cualquier otro provider que necesites

+ 93 - 0
lib/mvvm/viewmodels/home_view_model.dart

@@ -0,0 +1,93 @@
+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 {
+  final Repository<Producto> productoRepository;
+  final Repository<CategoriaProducto> categoriaRepository;
+
+  HomeViewModel({
+    required this.productoRepository,
+    required this.categoriaRepository,
+  });
+
+  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 nextPage() {
+  //   if (_currentPage < totalPages) {
+  //     fetchLocal(page: _currentPage + 1);
+  //   }
+  // }
+
+  // void previousPage() {
+  //   if (_currentPage > 1) {
+  //     fetchLocal(page: _currentPage - 1);
+  //   }
+  // }
+}

+ 1 - 0
lib/mvvm/viewmodels/viewmodels.dart

@@ -5,3 +5,4 @@ export '../viewmodels/usuarios_view_model.dart';
 export '../viewmodels/mesa_view_model.dart';
 export '../viewmodels/producto_view_model.dart';
 export '../viewmodels/categoria_producto_view_model.dart';
+export '../viewmodels/home_view_model.dart';