|
@@ -1,6 +1,7 @@
|
|
|
|
|
+import 'package:drift/drift.dart';
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
|
+import 'package:ristoris/core/constants.dart';
|
|
|
import 'package:ristoris/database/database.dart';
|
|
import 'package:ristoris/database/database.dart';
|
|
|
-import 'package:ristoris/database/tables/agenti.dart';
|
|
|
|
|
import '../services/api_service.dart';
|
|
import '../services/api_service.dart';
|
|
|
import '../services/sync/agente_sync_service.dart';
|
|
import '../services/sync/agente_sync_service.dart';
|
|
|
import '../services/sync/cliente_sync_service.dart';
|
|
import '../services/sync/cliente_sync_service.dart';
|
|
@@ -72,33 +73,201 @@ final referenceSyncServiceProvider = Provider<ReferenceSyncService>((ref) {
|
|
|
);
|
|
);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+enum SyncPhaseStatus { waiting, downloading, processing, completed }
|
|
|
|
|
+
|
|
|
|
|
+class SyncPhaseInfo {
|
|
|
|
|
+ final String name;
|
|
|
|
|
+ final SyncPhaseStatus status;
|
|
|
|
|
+ final int? currentRecord;
|
|
|
|
|
+ final int? totalRecords;
|
|
|
|
|
+
|
|
|
|
|
+ const SyncPhaseInfo({
|
|
|
|
|
+ required this.name,
|
|
|
|
|
+ this.status = SyncPhaseStatus.waiting,
|
|
|
|
|
+ this.currentRecord,
|
|
|
|
|
+ this.totalRecords,
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ SyncPhaseInfo copyWith({
|
|
|
|
|
+ SyncPhaseStatus? status,
|
|
|
|
|
+ int? currentRecord,
|
|
|
|
|
+ int? totalRecords,
|
|
|
|
|
+ }) {
|
|
|
|
|
+ return SyncPhaseInfo(
|
|
|
|
|
+ name: name,
|
|
|
|
|
+ status: status ?? this.status,
|
|
|
|
|
+ currentRecord: currentRecord ?? this.currentRecord,
|
|
|
|
|
+ totalRecords: totalRecords ?? this.totalRecords,
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String get statusText {
|
|
|
|
|
+ switch (status) {
|
|
|
|
|
+ case SyncPhaseStatus.waiting:
|
|
|
|
|
+ return 'IN ATTESA';
|
|
|
|
|
+ case SyncPhaseStatus.downloading:
|
|
|
|
|
+ return 'IN SCARICAMENTO';
|
|
|
|
|
+ case SyncPhaseStatus.processing:
|
|
|
|
|
+ if (currentRecord != null && totalRecords != null) {
|
|
|
|
|
+ return 'Elaborazione $currentRecord di $totalRecords';
|
|
|
|
|
+ }
|
|
|
|
|
+ return 'IN ELABORAZIONE';
|
|
|
|
|
+ case SyncPhaseStatus.completed:
|
|
|
|
|
+ return 'COMPLETATO';
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const List<String> syncPhaseNames = [
|
|
|
|
|
+ 'Invio nuovi clienti',
|
|
|
|
|
+ 'Invio nuove filiali',
|
|
|
|
|
+ 'Invio nuovi ordini',
|
|
|
|
|
+ 'Scaricamento tabelle',
|
|
|
|
|
+ 'Scaricamento pagamenti',
|
|
|
|
|
+ 'Scaricamento sconti',
|
|
|
|
|
+ 'Scaricamento articoli',
|
|
|
|
|
+ 'Scaricamento listini',
|
|
|
|
|
+ 'Scaricamento listini articolo',
|
|
|
|
|
+ 'Scaricamento promozioni',
|
|
|
|
|
+ 'Scaricamento clienti',
|
|
|
|
|
+ 'Scaricamento filiali',
|
|
|
|
|
+ 'Scaricamento scadenze',
|
|
|
|
|
+ 'Scaricamento listini cliente',
|
|
|
|
|
+ 'Scaricamento sconti cliente',
|
|
|
|
|
+ 'Scaricamento statistiche',
|
|
|
|
|
+];
|
|
|
|
|
+
|
|
|
class SyncState {
|
|
class SyncState {
|
|
|
final bool isSyncing;
|
|
final bool isSyncing;
|
|
|
final double progress;
|
|
final double progress;
|
|
|
final String message;
|
|
final String message;
|
|
|
final String? error;
|
|
final String? error;
|
|
|
|
|
+ final List<SyncPhaseInfo> phases;
|
|
|
|
|
+ final DateTime? lastSyncTime;
|
|
|
|
|
+ final String? currentAgentCode;
|
|
|
|
|
+ final List<String> agentiCodici;
|
|
|
|
|
|
|
|
const SyncState({
|
|
const SyncState({
|
|
|
this.isSyncing = false,
|
|
this.isSyncing = false,
|
|
|
this.progress = 0,
|
|
this.progress = 0,
|
|
|
this.message = '',
|
|
this.message = '',
|
|
|
this.error,
|
|
this.error,
|
|
|
|
|
+ this.phases = const [],
|
|
|
|
|
+ this.lastSyncTime,
|
|
|
|
|
+ this.currentAgentCode,
|
|
|
|
|
+ this.agentiCodici = const [],
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
class SyncNotifier extends Notifier<SyncState> {
|
|
class SyncNotifier extends Notifier<SyncState> {
|
|
|
|
|
+ DateTime _lastProgressUpdate = DateTime.fromMillisecondsSinceEpoch(0);
|
|
|
|
|
+
|
|
|
@override
|
|
@override
|
|
|
- SyncState build() => const SyncState();
|
|
|
|
|
|
|
+ SyncState build() {
|
|
|
|
|
+ Future.microtask(() async {
|
|
|
|
|
+ try {
|
|
|
|
|
+ final parametriDao = ref.read(parametriDaoProvider);
|
|
|
|
|
+ final lastSyncTimeStr = await parametriDao.read(AppConstants.kLastUpdateParam);
|
|
|
|
|
+ if (lastSyncTimeStr != null) {
|
|
|
|
|
+ final lastSyncTime = DateTime.tryParse(lastSyncTimeStr);
|
|
|
|
|
+ if (lastSyncTime != null) {
|
|
|
|
|
+ state = SyncState(
|
|
|
|
|
+ phases: state.phases,
|
|
|
|
|
+ lastSyncTime: lastSyncTime,
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (_) {}
|
|
|
|
|
+ });
|
|
|
|
|
+ return const SyncState();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ List<SyncPhaseInfo> _initPhases() {
|
|
|
|
|
+ return syncPhaseNames.map((name) => SyncPhaseInfo(name: name)).toList();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ void _setPhase(
|
|
|
|
|
+ int index,
|
|
|
|
|
+ SyncPhaseStatus status, {
|
|
|
|
|
+ int? currentRecord,
|
|
|
|
|
+ int? totalRecords,
|
|
|
|
|
+ }) {
|
|
|
|
|
+ final phases = List<SyncPhaseInfo>.from(state.phases);
|
|
|
|
|
+ phases[index] = phases[index].copyWith(
|
|
|
|
|
+ status: status,
|
|
|
|
|
+ currentRecord: currentRecord,
|
|
|
|
|
+ totalRecords: totalRecords,
|
|
|
|
|
+ );
|
|
|
|
|
+ final progress = (index + 1) / syncPhaseNames.length;
|
|
|
|
|
+ state = SyncState(
|
|
|
|
|
+ isSyncing: true,
|
|
|
|
|
+ progress: progress,
|
|
|
|
|
+ phases: phases,
|
|
|
|
|
+ lastSyncTime: state.lastSyncTime,
|
|
|
|
|
+ currentAgentCode: state.currentAgentCode,
|
|
|
|
|
+ agentiCodici: state.agentiCodici,
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ void _resetAgentPhases() {
|
|
|
|
|
+ final phases = List<SyncPhaseInfo>.from(state.phases);
|
|
|
|
|
+ for (int i = 10; i <= 14; i++) {
|
|
|
|
|
+ phases[i] = SyncPhaseInfo(name: syncPhaseNames[i]);
|
|
|
|
|
+ }
|
|
|
|
|
+ state = SyncState(
|
|
|
|
|
+ isSyncing: true,
|
|
|
|
|
+ progress: state.progress,
|
|
|
|
|
+ phases: phases,
|
|
|
|
|
+ lastSyncTime: state.lastSyncTime,
|
|
|
|
|
+ currentAgentCode: state.currentAgentCode,
|
|
|
|
|
+ agentiCodici: state.agentiCodici,
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ void _setCurrentAgent(String codAgente) {
|
|
|
|
|
+ state = SyncState(
|
|
|
|
|
+ isSyncing: true,
|
|
|
|
|
+ progress: state.progress,
|
|
|
|
|
+ phases: state.phases,
|
|
|
|
|
+ lastSyncTime: state.lastSyncTime,
|
|
|
|
|
+ currentAgentCode: codAgente,
|
|
|
|
|
+ agentiCodici: state.agentiCodici,
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ void Function(int current, int total) _throttledProgress(int phaseIndex) {
|
|
|
|
|
+ return (int current, int total) {
|
|
|
|
|
+ final now = DateTime.now();
|
|
|
|
|
+ if (now.difference(_lastProgressUpdate).inMilliseconds >= 500) {
|
|
|
|
|
+ _lastProgressUpdate = now;
|
|
|
|
|
+ _setPhase(
|
|
|
|
|
+ phaseIndex,
|
|
|
|
|
+ SyncPhaseStatus.processing,
|
|
|
|
|
+ currentRecord: current,
|
|
|
|
|
+ totalRecords: total,
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
Future<void> startFullSync(
|
|
Future<void> startFullSync(
|
|
|
int idAgente,
|
|
int idAgente,
|
|
|
String codAgente, {
|
|
String codAgente, {
|
|
|
bool forceFullSync = false,
|
|
bool forceFullSync = false,
|
|
|
}) async {
|
|
}) async {
|
|
|
- state = const SyncState(
|
|
|
|
|
|
|
+ _lastProgressUpdate = DateTime.fromMillisecondsSinceEpoch(0);
|
|
|
|
|
+
|
|
|
|
|
+ final parametriDao = ref.read(parametriDaoProvider);
|
|
|
|
|
+ final lastSyncTimeStr = await parametriDao.read(AppConstants.kLastUpdateParam);
|
|
|
|
|
+ final lastSyncTime = lastSyncTimeStr != null ? DateTime.tryParse(lastSyncTimeStr) : null;
|
|
|
|
|
+
|
|
|
|
|
+ state = SyncState(
|
|
|
isSyncing: true,
|
|
isSyncing: true,
|
|
|
|
|
+ phases: _initPhases(),
|
|
|
message: 'Avvio sincronizzazione...',
|
|
message: 'Avvio sincronizzazione...',
|
|
|
|
|
+ lastSyncTime: lastSyncTime,
|
|
|
);
|
|
);
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
final agentiDao = ref.read(agentiDaoProvider);
|
|
final agentiDao = ref.read(agentiDaoProvider);
|
|
|
final agente = await agentiDao.findByCode(codAgente);
|
|
final agente = await agentiDao.findByCode(codAgente);
|
|
@@ -107,77 +276,195 @@ class SyncNotifier extends Notifier<SyncState> {
|
|
|
final token = await authService.loginRemote(codAgente, agente!.pwd!);
|
|
final token = await authService.loginRemote(codAgente, agente!.pwd!);
|
|
|
ref.read(apiServiceProvider).token = token;
|
|
ref.read(apiServiceProvider).token = token;
|
|
|
}
|
|
}
|
|
|
- _update('Autenticazione riuscita', 0.02);
|
|
|
|
|
|
|
|
|
|
- final parametriDao = ref.read(parametriDaoProvider);
|
|
|
|
|
final agenteSync = ref.read(agenteSyncServiceProvider);
|
|
final agenteSync = ref.read(agenteSyncServiceProvider);
|
|
|
final ordineSync = ref.read(ordineSyncServiceProvider);
|
|
final ordineSync = ref.read(ordineSyncServiceProvider);
|
|
|
final filialeSync = ref.read(filialeSyncServiceProvider);
|
|
final filialeSync = ref.read(filialeSyncServiceProvider);
|
|
|
final clienteSync = ref.read(clienteSyncServiceProvider);
|
|
final clienteSync = ref.read(clienteSyncServiceProvider);
|
|
|
final refSync = ref.read(referenceSyncServiceProvider);
|
|
final refSync = ref.read(referenceSyncServiceProvider);
|
|
|
|
|
|
|
|
- final lastUpdateStr = forceFullSync ? '200001010000' : (await parametriDao.read('lastUpdate') ?? '200001010000');
|
|
|
|
|
|
|
+ final lastUpdateStr = forceFullSync ? '200001010000' : (await parametriDao.read(AppConstants.kLastUpdateParam) ?? '200001010000');
|
|
|
|
|
|
|
|
await agenteSync.downloadAgenti(idAgente, codAgente, lastUpdateStr);
|
|
await agenteSync.downloadAgenti(idAgente, codAgente, lastUpdateStr);
|
|
|
|
|
|
|
|
- await clienteSync.uploadNewCustomers(idAgente, codAgente);
|
|
|
|
|
- _update('Clienti inviati', 0.12);
|
|
|
|
|
|
|
+ // Phase 0: Invio nuovi clienti
|
|
|
|
|
+ _setPhase(0, SyncPhaseStatus.processing);
|
|
|
|
|
+ await clienteSync.uploadNewCustomers(
|
|
|
|
|
+ idAgente,
|
|
|
|
|
+ codAgente,
|
|
|
|
|
+ onProgress: _throttledProgress(0),
|
|
|
|
|
+ );
|
|
|
|
|
+ _setPhase(0, SyncPhaseStatus.completed);
|
|
|
|
|
+
|
|
|
|
|
+ // Phase 1: Invio nuove filiali
|
|
|
|
|
+ _setPhase(1, SyncPhaseStatus.processing);
|
|
|
|
|
+ await filialeSync.uploadNewDestinations(
|
|
|
|
|
+ idAgente,
|
|
|
|
|
+ codAgente,
|
|
|
|
|
+ onProgress: _throttledProgress(1),
|
|
|
|
|
+ );
|
|
|
|
|
+ _setPhase(1, SyncPhaseStatus.completed);
|
|
|
|
|
+
|
|
|
|
|
+ // Phase 2: Invio nuovi ordini
|
|
|
|
|
+ _setPhase(2, SyncPhaseStatus.processing);
|
|
|
|
|
+ await ordineSync.uploadNewOrders(
|
|
|
|
|
+ idAgente,
|
|
|
|
|
+ codAgente,
|
|
|
|
|
+ onProgress: _throttledProgress(2),
|
|
|
|
|
+ );
|
|
|
|
|
+ _setPhase(2, SyncPhaseStatus.completed);
|
|
|
|
|
|
|
|
- // cicla per tutti gli agenti a db
|
|
|
|
|
- List<Agente> agenti = await agentiDao.listAll();
|
|
|
|
|
|
|
+ // Phases 3-9: Reference downloads
|
|
|
|
|
+ _setPhase(3, SyncPhaseStatus.downloading);
|
|
|
|
|
+ await refSync.downloadTabelle(
|
|
|
|
|
+ lastUpdateStr,
|
|
|
|
|
+ onProgress: _throttledProgress(3),
|
|
|
|
|
+ );
|
|
|
|
|
+ _setPhase(3, SyncPhaseStatus.completed);
|
|
|
|
|
|
|
|
- await filialeSync.uploadNewDestinations(idAgente, codAgente);
|
|
|
|
|
- _update('Destinazioni inviate', 0.18);
|
|
|
|
|
- await ordineSync.uploadNewOrders(idAgente, codAgente);
|
|
|
|
|
- _update('Ordini inviati', 0.22);
|
|
|
|
|
|
|
+ _setPhase(4, SyncPhaseStatus.downloading);
|
|
|
|
|
+ await refSync.downloadPagamenti(
|
|
|
|
|
+ lastUpdateStr,
|
|
|
|
|
+ onProgress: _throttledProgress(4),
|
|
|
|
|
+ );
|
|
|
|
|
+ _setPhase(4, SyncPhaseStatus.completed);
|
|
|
|
|
|
|
|
- await Future.wait([
|
|
|
|
|
- refSync.downloadTabelle(lastUpdateStr),
|
|
|
|
|
- refSync.downloadPagamenti(lastUpdateStr),
|
|
|
|
|
- refSync.downloadSconti(lastUpdateStr),
|
|
|
|
|
- refSync.downloadArticoli(lastUpdateStr),
|
|
|
|
|
- refSync.downloadListini(lastUpdateStr),
|
|
|
|
|
- refSync.downloadListiniArticolo(lastUpdateStr),
|
|
|
|
|
- refSync.downloadPromozioni(lastUpdateStr),
|
|
|
|
|
- refSync.downloadStatistiche(idAgente, codAgente, lastUpdateStr),
|
|
|
|
|
- ]);
|
|
|
|
|
- _update('Dati di riferimento scaricati', 0.60);
|
|
|
|
|
|
|
+ _setPhase(5, SyncPhaseStatus.downloading);
|
|
|
|
|
+ await refSync.downloadSconti(
|
|
|
|
|
+ lastUpdateStr,
|
|
|
|
|
+ onProgress: _throttledProgress(5),
|
|
|
|
|
+ );
|
|
|
|
|
+ _setPhase(5, SyncPhaseStatus.completed);
|
|
|
|
|
+
|
|
|
|
|
+ _setPhase(6, SyncPhaseStatus.downloading);
|
|
|
|
|
+ await refSync.downloadArticoli(
|
|
|
|
|
+ lastUpdateStr,
|
|
|
|
|
+ onProgress: _throttledProgress(6),
|
|
|
|
|
+ );
|
|
|
|
|
+ _setPhase(6, SyncPhaseStatus.completed);
|
|
|
|
|
|
|
|
- for (int i = 0; i < agenti.length; i++) {
|
|
|
|
|
- final agente = agenti[i];
|
|
|
|
|
|
|
+ _setPhase(7, SyncPhaseStatus.downloading);
|
|
|
|
|
+ await refSync.downloadListini(
|
|
|
|
|
+ lastUpdateStr,
|
|
|
|
|
+ onProgress: _throttledProgress(7),
|
|
|
|
|
+ );
|
|
|
|
|
+ _setPhase(7, SyncPhaseStatus.completed);
|
|
|
|
|
+
|
|
|
|
|
+ _setPhase(8, SyncPhaseStatus.downloading);
|
|
|
|
|
+ await refSync.downloadListiniArticolo(
|
|
|
|
|
+ lastUpdateStr,
|
|
|
|
|
+ onProgress: _throttledProgress(8),
|
|
|
|
|
+ );
|
|
|
|
|
+ _setPhase(8, SyncPhaseStatus.completed);
|
|
|
|
|
+
|
|
|
|
|
+ _setPhase(9, SyncPhaseStatus.downloading);
|
|
|
|
|
+ await refSync.downloadPromozioni(
|
|
|
|
|
+ lastUpdateStr,
|
|
|
|
|
+ onProgress: _throttledProgress(9),
|
|
|
|
|
+ );
|
|
|
|
|
+ _setPhase(9, SyncPhaseStatus.completed);
|
|
|
|
|
+
|
|
|
|
|
+ // Per-agent downloads
|
|
|
|
|
+ final List<Agente> agenti = await agentiDao.listAll();
|
|
|
|
|
+ final agentiCodici = agenti.map((a) => a.codAgente!).toList();
|
|
|
|
|
+
|
|
|
|
|
+ state = SyncState(
|
|
|
|
|
+ isSyncing: true,
|
|
|
|
|
+ progress: state.progress,
|
|
|
|
|
+ phases: state.phases,
|
|
|
|
|
+ lastSyncTime: state.lastSyncTime,
|
|
|
|
|
+ agentiCodici: agentiCodici,
|
|
|
|
|
+ );
|
|
|
|
|
|
|
|
- _update('Aggiornamento per codice ${agente.codAgente} ', 0.16);
|
|
|
|
|
|
|
+ for (final a in agenti) {
|
|
|
|
|
+ _resetAgentPhases();
|
|
|
|
|
+ _setCurrentAgent(a.codAgente!);
|
|
|
|
|
|
|
|
- await clienteSync.downloadCustomers(agente.idAgente, agente.codAgente!, lastUpdateStr);
|
|
|
|
|
- _update('(${agente.codAgente}) Clienti attivi scaricati', 0.16);
|
|
|
|
|
|
|
+ // Phase 10: Scaricamento clienti
|
|
|
|
|
+ _setPhase(10, SyncPhaseStatus.downloading);
|
|
|
|
|
+ await clienteSync.downloadCustomers(
|
|
|
|
|
+ a.idAgente,
|
|
|
|
|
+ a.codAgente!,
|
|
|
|
|
+ lastUpdateStr,
|
|
|
|
|
+ onProgress: _throttledProgress(10),
|
|
|
|
|
+ );
|
|
|
|
|
+ _setPhase(10, SyncPhaseStatus.completed);
|
|
|
|
|
|
|
|
|
|
+ // Phase 11: Scaricamento filiali
|
|
|
|
|
+ _setPhase(11, SyncPhaseStatus.downloading);
|
|
|
await filialeSync.downloadDestinations(
|
|
await filialeSync.downloadDestinations(
|
|
|
- agente.idAgente,
|
|
|
|
|
- agente.codAgente!,
|
|
|
|
|
|
|
+ a.idAgente,
|
|
|
|
|
+ a.codAgente!,
|
|
|
|
|
+ lastUpdateStr,
|
|
|
|
|
+ onProgress: _throttledProgress(11),
|
|
|
|
|
+ );
|
|
|
|
|
+ _setPhase(11, SyncPhaseStatus.completed);
|
|
|
|
|
+
|
|
|
|
|
+ // Phase 12: Scaricamento scadenze
|
|
|
|
|
+ _setPhase(12, SyncPhaseStatus.downloading);
|
|
|
|
|
+ await refSync.downloadScadenze(
|
|
|
|
|
+ a.idAgente,
|
|
|
|
|
+ a.codAgente!,
|
|
|
lastUpdateStr,
|
|
lastUpdateStr,
|
|
|
|
|
+ onProgress: _throttledProgress(12),
|
|
|
);
|
|
);
|
|
|
- _update('(${agente.codAgente}) Destinazioni scaricate', 0.20);
|
|
|
|
|
-
|
|
|
|
|
- await Future.wait([
|
|
|
|
|
- refSync.downloadScadenze(agente.idAgente, agente.codAgente!, lastUpdateStr),
|
|
|
|
|
- refSync.downloadListiniCliente(agente.idAgente, agente.codAgente!, lastUpdateStr),
|
|
|
|
|
- refSync.downloadScontiCliente(agente.idAgente, agente.codAgente!, lastUpdateStr),
|
|
|
|
|
- ordineSync.downloadRemoteOrders(agente.idAgente, agente.codAgente!, lastUpdateStr),
|
|
|
|
|
- ]);
|
|
|
|
|
|
|
+ _setPhase(12, SyncPhaseStatus.completed);
|
|
|
|
|
+
|
|
|
|
|
+ // Phase 13: Scaricamento listini cliente
|
|
|
|
|
+ _setPhase(13, SyncPhaseStatus.downloading);
|
|
|
|
|
+ await refSync.downloadListiniCliente(
|
|
|
|
|
+ a.idAgente,
|
|
|
|
|
+ a.codAgente!,
|
|
|
|
|
+ lastUpdateStr,
|
|
|
|
|
+ onProgress: _throttledProgress(13),
|
|
|
|
|
+ );
|
|
|
|
|
+ _setPhase(13, SyncPhaseStatus.completed);
|
|
|
|
|
+
|
|
|
|
|
+ // Phase 14: Scaricamento sconti cliente
|
|
|
|
|
+ _setPhase(14, SyncPhaseStatus.downloading);
|
|
|
|
|
+ await refSync.downloadScontiCliente(
|
|
|
|
|
+ a.idAgente,
|
|
|
|
|
+ a.codAgente!,
|
|
|
|
|
+ lastUpdateStr,
|
|
|
|
|
+ onProgress: _throttledProgress(14),
|
|
|
|
|
+ );
|
|
|
|
|
+ _setPhase(14, SyncPhaseStatus.completed);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- _update('Dati per agente scaricati', 0.92);
|
|
|
|
|
|
|
+ // Phase 15: Scaricamento statistiche
|
|
|
|
|
+ _setPhase(15, SyncPhaseStatus.downloading);
|
|
|
|
|
+ await refSync.downloadStatistiche(
|
|
|
|
|
+ idAgente,
|
|
|
|
|
+ codAgente,
|
|
|
|
|
+ lastUpdateStr,
|
|
|
|
|
+ onProgress: _throttledProgress(15),
|
|
|
|
|
+ );
|
|
|
|
|
+ _setPhase(15, SyncPhaseStatus.completed);
|
|
|
|
|
|
|
|
|
|
+ // Download ordini remoti (no visual phase)
|
|
|
|
|
+ for (final a in agenti) {
|
|
|
|
|
+ await ordineSync.downloadRemoteOrders(
|
|
|
|
|
+ a.idAgente,
|
|
|
|
|
+ a.codAgente!,
|
|
|
|
|
+ lastUpdateStr,
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Download parametro
|
|
|
await refSync.downloadParametro('PALLET_PERC');
|
|
await refSync.downloadParametro('PALLET_PERC');
|
|
|
- _update('Parametri verificati', 0.98);
|
|
|
|
|
|
|
|
|
|
|
|
+ // Save timestamps
|
|
|
final now = DateTime.now();
|
|
final now = DateTime.now();
|
|
|
final newLastUpdate = '${now.year}${_pad(now.month)}${_pad(now.day)}${_pad(now.hour)}${_pad(now.minute)}';
|
|
final newLastUpdate = '${now.year}${_pad(now.month)}${_pad(now.day)}${_pad(now.hour)}${_pad(now.minute)}';
|
|
|
- await parametriDao.set('lastUpdate', newLastUpdate);
|
|
|
|
|
|
|
+ await parametriDao.set(AppConstants.kLastUpdateParam, newLastUpdate);
|
|
|
|
|
+ // await parametriDao.set(AppConstants.kLastUpdateParam, now.toIso8601String());
|
|
|
|
|
|
|
|
- state = const SyncState(
|
|
|
|
|
|
|
+ state = SyncState(
|
|
|
message: 'Sincronizzazione completata!',
|
|
message: 'Sincronizzazione completata!',
|
|
|
progress: 1.0,
|
|
progress: 1.0,
|
|
|
|
|
+ phases: state.phases,
|
|
|
|
|
+ lastSyncTime: now,
|
|
|
|
|
+ agentiCodici: state.agentiCodici,
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
ref.invalidate(articleSearchProvider);
|
|
ref.invalidate(articleSearchProvider);
|
|
@@ -202,10 +489,6 @@ class SyncNotifier extends Notifier<SyncState> {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
String _pad(int n) => n.toString().padLeft(2, '0');
|
|
String _pad(int n) => n.toString().padLeft(2, '0');
|
|
|
-
|
|
|
|
|
- void _update(String msg, double progress) {
|
|
|
|
|
- state = SyncState(isSyncing: true, message: msg, progress: progress);
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
final syncProvider = NotifierProvider<SyncNotifier, SyncState>(
|
|
final syncProvider = NotifierProvider<SyncNotifier, SyncState>(
|