Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9374f4ab46 |
No files matched your search
+19
-12
@@ -1,7 +1,6 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flux/features/operations/data/operations_repository.dart';
|
||||
import 'package:flux/features/operations/models/operation_model.dart';
|
||||
@@ -15,6 +14,8 @@ class DashboardStoreOperationListCubit
|
||||
final String? companyId;
|
||||
final String? storeId;
|
||||
StreamSubscription<void>? _operationsSubscription;
|
||||
Timer? _pollingTimer;
|
||||
|
||||
DashboardStoreOperationListCubit({
|
||||
required this.companyId,
|
||||
required this.storeId,
|
||||
@@ -31,31 +32,37 @@ class DashboardStoreOperationListCubit
|
||||
// Primo caricamento
|
||||
_loadOperationsSilently();
|
||||
|
||||
// Inizio ascolto campanello
|
||||
try {
|
||||
_operationsSubscription = _repository
|
||||
.watchStoreOperations(storeId: storeId!, limit: 10)
|
||||
.listen((_) {
|
||||
// Quando il campanello suona (qualcosa è cambiato a DB), ricarichiamo!
|
||||
_loadOperationsSilently();
|
||||
});
|
||||
} on Exception catch (e) {
|
||||
debugPrint(e.toString());
|
||||
}
|
||||
// 1. Ascolto notifiche locali di mutazione in memoria (reattività istantanea)
|
||||
_operationsSubscription = _repository.onOperationsChanged.listen((_) {
|
||||
_loadOperationsSilently();
|
||||
});
|
||||
|
||||
// 2. Polling programmato ogni 30 secondi per sincronizzazione di fondo
|
||||
_pollingTimer = Timer.periodic(const Duration(seconds: 30), (_) {
|
||||
_loadOperationsSilently();
|
||||
});
|
||||
}
|
||||
|
||||
void stopListening() {
|
||||
_operationsSubscription?.cancel();
|
||||
_operationsSubscription = null;
|
||||
_pollingTimer?.cancel();
|
||||
_pollingTimer = null;
|
||||
}
|
||||
|
||||
void refresh() {
|
||||
_loadOperationsSilently();
|
||||
}
|
||||
|
||||
void _loadOperationsSilently() async {
|
||||
try {
|
||||
if (companyId == null || storeId == null) return;
|
||||
final paginatedData = await _repository.fetchPaginatedOperations(
|
||||
companyId: companyId!,
|
||||
storeId: storeId!,
|
||||
page: 1,
|
||||
itemsPerPage: 20,
|
||||
onlyShowInDashboard: true,
|
||||
);
|
||||
if (isClosed) return;
|
||||
emit(
|
||||
|
||||
@@ -126,11 +126,61 @@ class ProviderFormCubit extends Cubit<ProviderFormState> {
|
||||
}
|
||||
|
||||
Future<void> addLocationLocal(ProviderLocationModel location) async {
|
||||
List<ProviderLocationModel> currentLocations = List<ProviderLocationModel>.from(
|
||||
state.localLocations,
|
||||
);
|
||||
|
||||
final bool shouldBeMain = location.isMain || currentLocations.isEmpty;
|
||||
|
||||
if (shouldBeMain) {
|
||||
currentLocations = currentLocations
|
||||
.map((loc) => loc.copyWith(isMain: false))
|
||||
.toList();
|
||||
}
|
||||
|
||||
currentLocations.add(location.copyWith(isMain: shouldBeMain));
|
||||
emit(state.copyWith(localLocations: currentLocations));
|
||||
}
|
||||
|
||||
void updateLocationLocal(int index, ProviderLocationModel location) {
|
||||
final currentLocations = List<ProviderLocationModel>.from(
|
||||
state.localLocations,
|
||||
);
|
||||
currentLocations.add(location);
|
||||
emit(state.copyWith(localLocations: currentLocations));
|
||||
if (index < 0 || index >= currentLocations.length) return;
|
||||
|
||||
bool shouldBeMain = location.isMain;
|
||||
if (!shouldBeMain && currentLocations.length == 1) {
|
||||
shouldBeMain = true;
|
||||
}
|
||||
|
||||
final List<ProviderLocationModel> updatedList = [];
|
||||
for (int i = 0; i < currentLocations.length; i++) {
|
||||
if (i == index) {
|
||||
updatedList.add(location.copyWith(isMain: shouldBeMain));
|
||||
} else {
|
||||
updatedList.add(
|
||||
shouldBeMain
|
||||
? currentLocations[i].copyWith(isMain: false)
|
||||
: currentLocations[i],
|
||||
);
|
||||
}
|
||||
}
|
||||
emit(state.copyWith(localLocations: updatedList));
|
||||
}
|
||||
|
||||
void setMainLocationLocal(int index) {
|
||||
final currentLocations = List<ProviderLocationModel>.from(
|
||||
state.localLocations,
|
||||
);
|
||||
if (index < 0 || index >= currentLocations.length) return;
|
||||
|
||||
final updatedList = currentLocations.asMap().entries.map((entry) {
|
||||
final idx = entry.key;
|
||||
final loc = entry.value;
|
||||
return loc.copyWith(isMain: idx == index);
|
||||
}).toList();
|
||||
|
||||
emit(state.copyWith(localLocations: updatedList));
|
||||
}
|
||||
|
||||
void removeLocationLocal(int index) {
|
||||
@@ -138,7 +188,13 @@ class ProviderFormCubit extends Cubit<ProviderFormState> {
|
||||
state.localLocations,
|
||||
);
|
||||
if (index >= 0 && index < currentLocations.length) {
|
||||
currentLocations.removeAt(index);
|
||||
final removedLoc = currentLocations.removeAt(index);
|
||||
if (removedLoc.id != null && removedLoc.id!.isNotEmpty) {
|
||||
_repository.deleteLocation(removedLoc.id!);
|
||||
}
|
||||
if (removedLoc.isMain && currentLocations.isNotEmpty) {
|
||||
currentLocations[0] = currentLocations[0].copyWith(isMain: true);
|
||||
}
|
||||
emit(state.copyWith(localLocations: currentLocations));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,6 +76,12 @@ class ProviderRepository {
|
||||
|
||||
// 4. Gestione Sedi (Locations)
|
||||
Future<void> saveLocation(ProviderLocationModel location) async {
|
||||
if (location.isMain && location.providerId.isNotEmpty) {
|
||||
await _supabase
|
||||
.from(Tables.providerLocations)
|
||||
.update({'is_main': false})
|
||||
.eq('provider_id', location.providerId);
|
||||
}
|
||||
await _supabase.from(Tables.providerLocations).upsert(location.toMap());
|
||||
}
|
||||
|
||||
|
||||
@@ -383,11 +383,47 @@ class _ProviderFormScreenState extends State<ProviderFormScreen> {
|
||||
itemBuilder: (context, index) {
|
||||
final loc = state.localLocations[index];
|
||||
return ListTile(
|
||||
leading: Icon(
|
||||
loc.isMain ? Icons.star : Icons.location_on,
|
||||
color: loc.isMain ? Colors.amber : null,
|
||||
onTap: () async {
|
||||
final providerFormCubit = context.read<ProviderFormCubit>();
|
||||
final res = await showDialog<ProviderLocationModel?>(
|
||||
context: context,
|
||||
builder: (context) => ProviderLocationDialog(initialLocation: loc),
|
||||
);
|
||||
if (res != null) {
|
||||
providerFormCubit.updateLocationLocal(index, res);
|
||||
}
|
||||
},
|
||||
leading: IconButton(
|
||||
tooltip: loc.isMain ? 'Sede predefinita' : 'Imposta come predefinita',
|
||||
icon: Icon(
|
||||
loc.isMain ? Icons.star : Icons.star_border,
|
||||
color: loc.isMain ? Colors.amber : Colors.grey,
|
||||
),
|
||||
onPressed: () => context
|
||||
.read<ProviderFormCubit>()
|
||||
.setMainLocationLocal(index),
|
||||
),
|
||||
title: Row(
|
||||
children: [
|
||||
Expanded(child: Text(loc.name)),
|
||||
if (loc.isMain)
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.amber.withValues(alpha: 0.2),
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
),
|
||||
child: const Text(
|
||||
'Predefinita',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.amber,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
title: Text(loc.name),
|
||||
subtitle: Text(
|
||||
'${loc.address}, ${loc.city} (${loc.province})',
|
||||
),
|
||||
|
||||
@@ -104,6 +104,7 @@ class OperationFormCubit extends Cubit<OperationFormState> {
|
||||
try {
|
||||
OperationModel operationToSave = state.operation.copyWith(
|
||||
status: targetStatus,
|
||||
showInDashboard: true,
|
||||
);
|
||||
if (operationToSave.reference.isEmpty) {
|
||||
if (operationToSave.customer != null &&
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
@@ -12,10 +14,19 @@ class OperationListCubit extends Cubit<OperationListState> {
|
||||
final OperationsRepository _repository = GetIt.I<OperationsRepository>();
|
||||
final SessionCubit _sessionCubit = GetIt.I<SessionCubit>();
|
||||
|
||||
OperationListCubit() : super(const OperationListState());
|
||||
|
||||
StreamSubscription<void>? _operationsSubscription;
|
||||
int _fetchId = 0;
|
||||
|
||||
OperationListCubit() : super(const OperationListState()) {
|
||||
_operationsSubscription = _repository.onOperationsChanged.listen((_) {
|
||||
refreshCurrentPage();
|
||||
});
|
||||
}
|
||||
|
||||
void refreshCurrentPage() {
|
||||
loadSpecificPageDesktop(state.currentPage);
|
||||
}
|
||||
|
||||
// 🥷 MOTORE 1: DESKTOP (Sostituisce la lista)
|
||||
Future<void> loadSpecificPageDesktop(int page) async {
|
||||
final currentFetchId = ++_fetchId;
|
||||
@@ -124,4 +135,10 @@ class OperationListCubit extends Cubit<OperationListState> {
|
||||
|
||||
loadSpecificPageDesktop(1);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() {
|
||||
_operationsSubscription?.cancel();
|
||||
return super.close();
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flux/core/enums_and_consts/consts.dart';
|
||||
import 'package:flux/features/master_data/store/models/store_model.dart';
|
||||
@@ -10,6 +11,17 @@ import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
class OperationsRepository {
|
||||
final SupabaseClient _supabase = sl<SupabaseClient>();
|
||||
|
||||
final StreamController<void> _operationsChangedController =
|
||||
StreamController<void>.broadcast();
|
||||
|
||||
Stream<void> get onOperationsChanged => _operationsChangedController.stream;
|
||||
|
||||
void notifyOperationsChanged() {
|
||||
if (!_operationsChangedController.isClosed) {
|
||||
_operationsChangedController.add(null);
|
||||
}
|
||||
}
|
||||
|
||||
Future<OperationModel?> getOperationById(String id) async {
|
||||
try {
|
||||
final response = await _supabase
|
||||
@@ -45,6 +57,7 @@ class OperationsRepository {
|
||||
int itemsPerPage = 20,
|
||||
String? searchTerm,
|
||||
DateTimeRange? dateRange,
|
||||
bool onlyShowInDashboard = false,
|
||||
}) async {
|
||||
try {
|
||||
// Calcoliamo il range di partenza e fine per Supabase
|
||||
@@ -69,6 +82,10 @@ class OperationsRepository {
|
||||
''')
|
||||
.eq('company_id', companyId);
|
||||
|
||||
if (onlyShowInDashboard) {
|
||||
query = query.eq('show_in_dashboard', true);
|
||||
}
|
||||
|
||||
// Filtro Range Date
|
||||
if (dateRange != null) {
|
||||
query = query
|
||||
@@ -161,6 +178,7 @@ class OperationsRepository {
|
||||
staffId: createdById,
|
||||
companyId: currentStore.companyId,
|
||||
status: OperationStatus.success,
|
||||
showInDashboard: false,
|
||||
)
|
||||
.toMap(),
|
||||
)
|
||||
@@ -169,6 +187,7 @@ class OperationsRepository {
|
||||
${Tables.staffMembers}(*)
|
||||
''')
|
||||
.single();
|
||||
notifyOperationsChanged();
|
||||
return OperationModel.fromMap(res);
|
||||
} on Exception catch (e) {
|
||||
debugPrint('ERRORE CREAZIONE OPERAZIONE: $e');
|
||||
@@ -179,6 +198,7 @@ class OperationsRepository {
|
||||
Future<void> deleteOperation(String id) async {
|
||||
try {
|
||||
await _supabase.from(Tables.operations).delete().eq('id', id);
|
||||
notifyOperationsChanged();
|
||||
} catch (e) {
|
||||
debugPrint('ERRORE CANCELLAZIONE OPERAZIONE: $e');
|
||||
throw Exception('Errore nella cancellazione dell\'operazione: $e');
|
||||
@@ -225,6 +245,7 @@ class OperationsRepository {
|
||||
.from(Tables.operationDetails)
|
||||
.upsert(detailToSave.toMap());
|
||||
}
|
||||
notifyOperationsChanged();
|
||||
} catch (e) {
|
||||
debugPrint('ERRORE SALVATAGGIO OPERAZIONE: $e');
|
||||
throw Exception('Errore nel salvataggio dell\'operazione: $e');
|
||||
|
||||
@@ -126,6 +126,7 @@ class OperationModel extends Equatable {
|
||||
return OperationModel(
|
||||
id: null,
|
||||
createdAt: null,
|
||||
showInDashboard: false,
|
||||
companyId: '',
|
||||
details: const [],
|
||||
);
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
name: flux
|
||||
description: "Gestione attività negozio di telefonia"
|
||||
publish_to: 'none'
|
||||
version: 1.3.4+50
|
||||
version: 1.3.5+51
|
||||
|
||||
environment:
|
||||
sdk: ^3.11.3
|
||||
|
||||
Reference in new issue
Block a user