pedido_sync.dart 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import '../../viewmodels/viewmodels.dart';
  4. class PedidoSync {
  5. static final PedidoSync _instance = PedidoSync._internal();
  6. Timer? _syncTimer;
  7. factory PedidoSync() {
  8. return _instance;
  9. }
  10. PedidoSync._internal();
  11. void startSync(
  12. PedidoViewModel pedidoViewModel, CorteCajaViewModel corteCajaViewModel) {
  13. if (_syncTimer != null && _syncTimer!.isActive) return;
  14. _syncTimer = Timer.periodic(Duration(seconds: 5), (timer) async {
  15. // Primero sincronizamos pedidos
  16. bool hasMorePedidosToSync = await pedidoViewModel.sincronizarPedidos();
  17. // Luego sincronizamos corte de caja
  18. bool hasMoreCortesToSync =
  19. await corteCajaViewModel.sincronizarCorteCajas();
  20. // Opcionalmente, podrías detener el timer si ya no hay nada más que sincronizar.
  21. // if (!hasMorePedidosToSync && !hasMoreCortesToSync) {
  22. // timer.cancel();
  23. // _syncTimer = null;
  24. // print('Sincronización completa, no hay más datos por sincronizar.');
  25. // }
  26. });
  27. }
  28. void stopSync() {
  29. _syncTimer?.cancel();
  30. _syncTimer = null;
  31. }
  32. bool isSyncing() {
  33. return _syncTimer != null && _syncTimer!.isActive;
  34. }
  35. }