1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import 'dart:convert';
- import 'package:flutter/material.dart';
- import 'package:sqflite/sqflite.dart';
- import '../../core/services/services.dart';
- import '../../core/models/models.dart';
- class UsuarioViewModel extends ChangeNotifier {
- List<Usuario> _usuarios = [];
- bool _isLoading = false;
- List<Usuario> get usuarios => _usuarios;
- bool get isLoading => _isLoading;
- int _currentPage = 1;
- int _totalProducts = 0;
- int _limit = 10;
- int get currentPage => _currentPage;
- int get totalProducts => _totalProducts;
- int get totalPages => (_totalProducts / _limit).ceil();
- Future<void> fetchLocalAll({int page = 1}) async {
- _currentPage = page;
- var db = await DatabaseService().db;
- int? count = Sqflite.firstIntValue(
- await db!.rawQuery('SELECT COUNT(*) FROM Usuario'));
- _totalProducts = count ?? 0;
- int offset = (_limit * (page - 1));
- var query = await db.query('Usuario',
- where: 'eliminado IS NULL',
- orderBy: 'id asc',
- limit: _limit,
- offset: offset);
- _usuarios = query.map((element) => Usuario.fromJson(element)).toList();
- notifyListeners();
- }
- Future<bool> sincronizarUsuarios() async {
- try {
- Map<String, String> parametros = {"expand": 'permisos'};
- final response = ApiResponse(
- await BaseService().get('/pos/usuario', queryParameters: parametros));
- if (response.isOk && response.resultados != null) {
- List<Usuario> usuariosApi =
- response.resultados!.map((json) => Usuario.fromApi(json)).toList();
- if (usuariosApi.isNotEmpty) {
- await DatabaseService().sincronizarUsuarios(usuariosApi);
- notifyListeners();
- return true;
- }
- }
- return false;
- } catch (e) {
- print('Error al sincronizar usuarios: $e');
- return false;
- }
- }
- }
|