Compare commits
11 Commits
change-ope
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| f8504d466a | |||
| 22bb86f052 | |||
| fc850795c9 | |||
| b60ce96dd7 | |||
| 6582da60d4 | |||
| d42cc5af1d | |||
| 7ea0e2ac10 | |||
| 5ce0110197 | |||
| 4efc3ce182 | |||
| 01515910b6 | |||
| f27ede7625 |
@@ -1,4 +1,5 @@
|
||||
class Tables {
|
||||
static const String appConfig = 'app_config';
|
||||
static const String attachments = 'attachments';
|
||||
static const String brands = 'brands';
|
||||
static const String campaigns = 'campaigns';
|
||||
@@ -18,6 +19,7 @@ class Tables {
|
||||
static const String stores = 'stores';
|
||||
static const String tasks = 'tasks';
|
||||
static const String taskAssignments = 'task_assignments';
|
||||
static const String taskReminders = 'task_reminders';
|
||||
static const String tickets = 'tickets';
|
||||
static const String trackings = 'trackings';
|
||||
}
|
||||
|
||||
@@ -1,67 +1,94 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'dart:io' show Platform;
|
||||
import 'package:package_info_plus/package_info_plus.dart';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
|
||||
class VersionCheckService {
|
||||
final _supabase = Supabase.instance.client;
|
||||
|
||||
/// Controlla se l'app corrente deve essere bloccata o aggiornata.
|
||||
/// Ritorna il link di download se l'aggiornamento è obbligatorio, altrimenti null.
|
||||
Future<String?> checkForceUpdate() async {
|
||||
try {
|
||||
// 1. Determiniamo la piattaforma corrente
|
||||
String platformKey = 'web';
|
||||
if (!kIsWeb) {
|
||||
if (Platform.isAndroid) platformKey = 'android';
|
||||
if (Platform.isWindows) platformKey = 'windows';
|
||||
// 1. Capiamo su che piattaforma sta girando l'app in questo istante
|
||||
String currentPlatform = _getCurrentPlatform();
|
||||
|
||||
// 2. Recuperiamo SOLO la riga corrispondente alla nostra piattaforma
|
||||
final dbResponse = await Supabase.instance.client
|
||||
.from('app_config')
|
||||
.select('min_version, download_url')
|
||||
.eq('platform', currentPlatform)
|
||||
.maybeSingle(); // Usiamo maybeSingle così se non c'è la riga non crasha
|
||||
|
||||
if (dbResponse == null) {
|
||||
return null; // Nessuna regola per questa piattaforma
|
||||
}
|
||||
|
||||
// 2. Recuperiamo la configurazione minima da Supabase
|
||||
final data = await _supabase
|
||||
.from('app_config')
|
||||
.select()
|
||||
.eq('platform', platformKey)
|
||||
.maybeSingle();
|
||||
String minVersionFromDb = dbResponse['min_version'] as String;
|
||||
String downloadUrl = dbResponse['download_url'] as String;
|
||||
|
||||
if (data == null) return null;
|
||||
// 3. Recuperiamo la versione locale di Flutter
|
||||
PackageInfo packageInfo = await PackageInfo.fromPlatform();
|
||||
String localVersionRaw = packageInfo.version;
|
||||
|
||||
final String minVersion = data['min_version'];
|
||||
final String downloadUrl = data['download_url'];
|
||||
// 🥷 TRUCCO 1: Pulizia totale dai build number (+37) o tag "v"
|
||||
String cleanLocal = localVersionRaw
|
||||
.split('+')
|
||||
.first
|
||||
.replaceAll('v', '')
|
||||
.trim();
|
||||
String cleanDb = minVersionFromDb
|
||||
.split('+')
|
||||
.first
|
||||
.replaceAll('v', '')
|
||||
.trim();
|
||||
|
||||
// 3. Recuperiamo la versione attuale dell'app dal pubspec.yaml
|
||||
final packageInfo = await PackageInfo.fromPlatform();
|
||||
final String currentVersion = packageInfo.version;
|
||||
|
||||
// 4. Confronto matematico semantico (es. 1.2.3 vs 1.1.9)
|
||||
if (_isVersionLower(currentVersion, minVersion)) {
|
||||
return downloadUrl; // Aggiornamento obbligatorio richiesto!
|
||||
// 🥷 TRUCCO 2: Confronto Semantico Reale
|
||||
if (_isVersionLower(current: cleanLocal, minimum: cleanDb)) {
|
||||
// Ritorna il link VERO per questa specifica piattaforma preso dal CSV!
|
||||
return downloadUrl;
|
||||
}
|
||||
|
||||
return null;
|
||||
} catch (e) {
|
||||
debugPrint('Errore controllo versione: $e');
|
||||
return null; // In caso di errore non blocchiamo l'utente
|
||||
debugPrint("Errore durante il check versione: $e");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
bool _isVersionLower(String current, String min) {
|
||||
// Helper ninja per mappare le piattaforme in base alle stringhe del tuo DB
|
||||
String _getCurrentPlatform() {
|
||||
if (kIsWeb) return 'web';
|
||||
if (Platform.isAndroid) return 'android';
|
||||
if (Platform.isIOS) return 'ios';
|
||||
if (Platform.isWindows) return 'windows';
|
||||
if (Platform.isMacOS) return 'macos';
|
||||
if (Platform.isLinux) return 'linux';
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
// Il motore matematico (resta invariato)
|
||||
bool _isVersionLower({required String current, required String minimum}) {
|
||||
if (current == minimum) return false;
|
||||
|
||||
List<int> currentParts = current
|
||||
.split('.')
|
||||
.map((e) => int.tryParse(e) ?? 0)
|
||||
.toList();
|
||||
List<int> minParts = min
|
||||
List<int> minParts = minimum
|
||||
.split('.')
|
||||
.map((e) => int.tryParse(e) ?? 0)
|
||||
.toList();
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
int currentPart = currentParts.length > i ? currentParts[i] : 0;
|
||||
int minPart = minParts.length > i ? minParts[i] : 0;
|
||||
while (currentParts.length < 3) {
|
||||
currentParts.add(0);
|
||||
}
|
||||
while (minParts.length < 3) {
|
||||
minParts.add(0);
|
||||
}
|
||||
|
||||
if (currentPart < minPart) return true;
|
||||
if (currentPart > minPart) return false;
|
||||
if (currentParts[0] != minParts[0]) {
|
||||
return currentParts[0] < minParts[0];
|
||||
}
|
||||
return false;
|
||||
if (currentParts[1] != minParts[1]) {
|
||||
return currentParts[1] < minParts[1];
|
||||
}
|
||||
return currentParts[2] < minParts[2];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,14 +34,14 @@ class SharedAttachmentsSection extends StatefulWidget {
|
||||
final String? parentId;
|
||||
final String titleForUpload;
|
||||
final AttachmentParentType parentType;
|
||||
final Future<String?> Function()? onGenerateIdForQr;
|
||||
final Future<String?> Function()? onEnsureEntitySaved;
|
||||
|
||||
const SharedAttachmentsSection({
|
||||
super.key,
|
||||
this.parentId,
|
||||
this.titleForUpload = 'Cliente_sconosciuto',
|
||||
required this.parentType,
|
||||
this.onGenerateIdForQr,
|
||||
this.onEnsureEntitySaved,
|
||||
});
|
||||
|
||||
@override
|
||||
@@ -90,6 +90,32 @@ class _SharedAttachmentsSectionState extends State<SharedAttachmentsSection> {
|
||||
|
||||
// --- SELEZIONE FILE DAL PC/TELEFONO ---
|
||||
Future<void> _pickFiles() async {
|
||||
final attachmentsBloc = context.read<AttachmentsBloc>();
|
||||
String? targetId = attachmentsBloc.state.parentId;
|
||||
|
||||
// 🥷 SE L'ID NON C'È (Nuova Operazione), FORZIAMO IL SALVATAGGIO PREVENTIVO!
|
||||
if (targetId == null || targetId.isEmpty) {
|
||||
if (widget.onEnsureEntitySaved != null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Salvataggio rapido scheda per allegati... ⏳'),
|
||||
duration: Duration(seconds: 1),
|
||||
),
|
||||
);
|
||||
|
||||
// Chiamiamo la funzione passata dal TicketForm/OperationForm
|
||||
targetId = await widget.onEnsureEntitySaved!();
|
||||
}
|
||||
|
||||
// Se il salvataggio fallisce (es. form non valido), ci fermiamo per evitare file orfani
|
||||
if (targetId == null || targetId.isEmpty) return;
|
||||
|
||||
// Comunichiamo immediatamente al BLoC che l'entità padre è stata salvata e ha un nuovo ID.
|
||||
// Questo eviterà che i file finiscano nei `localFiles` temporanei.
|
||||
attachmentsBloc.add(ParentEntitySavedEvent(targetId));
|
||||
}
|
||||
|
||||
// Ora che abbiamo la certezza matematica di avere un targetId, apriamo il picker
|
||||
final result = await FilePicker.pickFiles(
|
||||
allowMultiple: true,
|
||||
type: FileType.custom,
|
||||
@@ -98,8 +124,8 @@ class _SharedAttachmentsSectionState extends State<SharedAttachmentsSection> {
|
||||
);
|
||||
|
||||
if (result != null && mounted) {
|
||||
// MAGIA: Passiamo direttamente la lista di PlatformFile al tuo BLoC!
|
||||
context.read<AttachmentsBloc>().add(AddAttachmentsEvent(result.files));
|
||||
// Ora il BLoC eseguirà l'ambiente di "Upload immediato" (Bivio 2) perché ha l'ID aggiornato!
|
||||
attachmentsBloc.add(AddAttachmentsEvent(result.files));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -481,7 +507,7 @@ class _SharedAttachmentsSectionState extends State<SharedAttachmentsSection> {
|
||||
|
||||
// SE L'ID NON C'È, CHIAMIAMO IL SALVATAGGIO IN BACKGROUND!
|
||||
if (targetId == null) {
|
||||
if (widget.onGenerateIdForQr != null) {
|
||||
if (widget.onEnsureEntitySaved != null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text(
|
||||
@@ -492,7 +518,7 @@ class _SharedAttachmentsSectionState extends State<SharedAttachmentsSection> {
|
||||
);
|
||||
|
||||
// Aspettiamo che il TicketFormCubit faccia il suo lavoro
|
||||
targetId = await widget.onGenerateIdForQr!();
|
||||
targetId = await widget.onEnsureEntitySaved!();
|
||||
}
|
||||
|
||||
// Se fallisce (es. validazione form non passata), ci fermiamo
|
||||
|
||||
@@ -51,17 +51,17 @@ class DashboardStoreOperationListCubit
|
||||
|
||||
void _loadOperationsSilently() async {
|
||||
try {
|
||||
final operations = await _repository.fetchOperations(
|
||||
final paginatedData = await _repository.fetchPaginatedOperations(
|
||||
companyId: companyId!,
|
||||
storeId: storeId!,
|
||||
limit: 10,
|
||||
offset: 0,
|
||||
page: 1,
|
||||
itemsPerPage: 20,
|
||||
);
|
||||
if (isClosed) return;
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: DashboardStoreOperationListStatus.success,
|
||||
operations: operations,
|
||||
operations: paginatedData.operations,
|
||||
error: null,
|
||||
),
|
||||
);
|
||||
|
||||
@@ -217,8 +217,6 @@ class OperationFormCubit extends Cubit<OperationFormState> {
|
||||
String? reference,
|
||||
String? note,
|
||||
String? type,
|
||||
String? providerId,
|
||||
String? providerDisplayName,
|
||||
String? subType,
|
||||
String? description,
|
||||
DateTime? expirationDate,
|
||||
@@ -248,10 +246,6 @@ class OperationFormCubit extends Cubit<OperationFormState> {
|
||||
final updated = current.copyWith(
|
||||
reference: reference ?? current.reference,
|
||||
note: note ?? current.note,
|
||||
providerId: clearProvider ? null : (providerId ?? current.providerId),
|
||||
providerDisplayName: clearProvider
|
||||
? null
|
||||
: (providerDisplayName ?? current.providerDisplayName),
|
||||
quantity: newQuantity ?? current.quantity,
|
||||
type: clearType ? null : (type ?? current.type),
|
||||
description: clearDescription
|
||||
@@ -274,6 +268,17 @@ class OperationFormCubit extends Cubit<OperationFormState> {
|
||||
emit(state.copyWith(operation: updated));
|
||||
}
|
||||
|
||||
void updateProvider(ProviderModel? newProvider) {
|
||||
final current = state.operation;
|
||||
|
||||
final updatedOperation = current.copyWith(
|
||||
// Se newProvider è null, passiamo una funzione che ritorna null per sbiancare i campi!
|
||||
provider: () => newProvider,
|
||||
);
|
||||
|
||||
emit(state.copyWith(operation: updatedOperation));
|
||||
}
|
||||
|
||||
void updateCustomer(CustomerModel customer) {
|
||||
final bool isBusiness = customer.isBusiness;
|
||||
final updatedOperation = state.operation.copyWith(
|
||||
@@ -293,13 +298,8 @@ class OperationFormCubit extends Cubit<OperationFormState> {
|
||||
}) {
|
||||
// 1. Aggiorniamo il tipo nel modello in canna
|
||||
// (Presumo tu abbia un metodo copyWith o simile)
|
||||
final updatedOp = state.operation.copyWith(type: newType, subType: '');
|
||||
|
||||
// 2. Prepariamoci ad auto-selezionare il provider
|
||||
String? newProviderId = updatedOp.providerId;
|
||||
String? newProviderName = updatedOp.providerDisplayName;
|
||||
|
||||
// 3. LA LOGICA DI DEFAULT
|
||||
// 2. LA LOGICA DI DEFAULT
|
||||
if (defaultProviderId != null) {
|
||||
// Troviamo il provider di default nella lista
|
||||
final defaultProvider = allProviders
|
||||
@@ -309,25 +309,13 @@ class OperationFormCubit extends Cubit<OperationFormState> {
|
||||
if (defaultProvider != null) {
|
||||
// Usiamo l'extension appena creata!
|
||||
if (defaultProvider.supportsOperation(newType)) {
|
||||
newProviderId = defaultProvider.id;
|
||||
newProviderName = defaultProvider.name;
|
||||
updateProvider(defaultProvider);
|
||||
} else {
|
||||
// Se cambi tipo (es. da Mobile a Luce) e il default non lo supporta, sbianchiamo
|
||||
newProviderId = null;
|
||||
newProviderName = null;
|
||||
updateProvider(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Emettiamo il nuovo stato
|
||||
emit(
|
||||
state.copyWith(
|
||||
operation: updatedOp.copyWith(
|
||||
providerId: newProviderId,
|
||||
providerDisplayName: newProviderName,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void setTypeWithSmartDefaults({
|
||||
@@ -338,7 +326,7 @@ class OperationFormCubit extends Cubit<OperationFormState> {
|
||||
final currentOp = state.operation;
|
||||
|
||||
// -----------------------------------------
|
||||
// 1. SMART DATES: Calcolo Scadenze Default
|
||||
// 1. SMART DATES: Calcolo Scadenze Default (Invariato)
|
||||
// -----------------------------------------
|
||||
DateTime? defaultDate;
|
||||
final now = DateTime.now();
|
||||
@@ -354,28 +342,19 @@ class OperationFormCubit extends Cubit<OperationFormState> {
|
||||
}
|
||||
|
||||
// -----------------------------------------
|
||||
// 2. SMART PROVIDER: Filtro e Auto-Selezione
|
||||
// 2. SMART PROVIDER: Filtro e Auto-Selezione ad Oggetti
|
||||
// -----------------------------------------
|
||||
String? newProviderId = currentOp.providerId;
|
||||
String? newProviderName = currentOp.providerDisplayName;
|
||||
// Pescatore direttamente l'oggetto dal modello corrente
|
||||
ProviderModel? targetProvider = currentOp.provider;
|
||||
|
||||
// A) Il provider attuale è ancora compatibile col nuovo tipo scelto?
|
||||
if (newProviderId != null && newProviderId.isNotEmpty) {
|
||||
final currentProvider = allProviders
|
||||
.where((p) => p.id == newProviderId)
|
||||
.firstOrNull;
|
||||
|
||||
if (currentProvider == null ||
|
||||
!currentProvider.supportsOperation(newType)) {
|
||||
if (targetProvider != null && !targetProvider.supportsOperation(newType)) {
|
||||
// Non è più compatibile (es. da TIM fisso passo a Energy). Lo sbianchiamo!
|
||||
newProviderId = null;
|
||||
newProviderName = null;
|
||||
}
|
||||
targetProvider = null;
|
||||
}
|
||||
|
||||
// B) Se non c'è un provider selezionato, proviamo ad auto-inserire quello di default del negozio
|
||||
if ((newProviderId == null || newProviderId.isEmpty) &&
|
||||
defaultProviderId != null) {
|
||||
if (targetProvider == null && defaultProviderId != null) {
|
||||
final defaultProvider = allProviders
|
||||
.where((p) => p.id == defaultProviderId)
|
||||
.firstOrNull;
|
||||
@@ -383,8 +362,7 @@ class OperationFormCubit extends Cubit<OperationFormState> {
|
||||
// Controlliamo che il default del negozio supporti questa specifica operazione
|
||||
if (defaultProvider != null &&
|
||||
defaultProvider.supportsOperation(newType)) {
|
||||
newProviderId = defaultProvider.id;
|
||||
newProviderName = defaultProvider.name;
|
||||
targetProvider = defaultProvider;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -395,13 +373,15 @@ class OperationFormCubit extends Cubit<OperationFormState> {
|
||||
state.copyWith(
|
||||
operation: currentOp.copyWith(
|
||||
type: newType,
|
||||
subType:
|
||||
'', // Resettiamo il sottotipo per evitare incongruenze (es. passo da Luce a DAZN)
|
||||
subType: '', // Resettiamo il sottotipo per evitare incongruenze
|
||||
expirationDate:
|
||||
defaultDate, // Impostiamo la scadenza di default se calcolata
|
||||
providerId: newProviderId,
|
||||
providerDisplayName: newProviderName,
|
||||
// 🥷 APPLICHIAMO IL TRUCCO NINJA DELLE FUNZIONI
|
||||
// Se targetProvider è null, le funzioni ritorneranno null sbiancando il DB!
|
||||
provider: () => targetProvider,
|
||||
|
||||
// Nota: Per azzerare davvero questi due, ricordati in futuro di applicare
|
||||
// il trucco delle funzioni anche a modelId e modelDisplayName nel modello!
|
||||
modelId: null,
|
||||
modelDisplayName: null,
|
||||
),
|
||||
|
||||
@@ -12,72 +12,103 @@ class OperationListCubit extends Cubit<OperationListState> {
|
||||
final OperationsRepository _repository = GetIt.I<OperationsRepository>();
|
||||
final SessionCubit _sessionCubit = GetIt.I<SessionCubit>();
|
||||
|
||||
OperationListCubit() : super(const OperationListState()) {
|
||||
loadOperations(refresh: true);
|
||||
}
|
||||
OperationListCubit() : super(const OperationListState());
|
||||
|
||||
Future<void> loadOperations({bool refresh = false}) async {
|
||||
// 🥷 MOTORE 1: DESKTOP (Sostituisce la lista)
|
||||
Future<void> loadSpecificPageDesktop(int page) async {
|
||||
if (state.status == OperationListStatus.loading) return;
|
||||
if (!refresh && state.hasReachedMax) return;
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: OperationListStatus.loading,
|
||||
errorMessage: null,
|
||||
operations: refresh ? [] : state.operations,
|
||||
hasReachedMax: refresh ? false : state.hasReachedMax,
|
||||
),
|
||||
);
|
||||
emit(state.copyWith(status: OperationListStatus.loading));
|
||||
|
||||
try {
|
||||
final currentOffset = refresh ? 0 : state.operations.length;
|
||||
final companyId = _sessionCubit.state.company?.id;
|
||||
|
||||
if (companyId == null) {
|
||||
throw Exception("Company ID non trovato nella sessione");
|
||||
}
|
||||
|
||||
final newOperations = await _repository.fetchOperations(
|
||||
companyId: companyId,
|
||||
offset: currentOffset,
|
||||
limit: 50,
|
||||
searchTerm: state.query,
|
||||
dateRange: state.dateRange,
|
||||
final paginatedData = await _repository.fetchPaginatedOperations(
|
||||
companyId: companyId!,
|
||||
page: page,
|
||||
itemsPerPage: state.itemsPerPage,
|
||||
);
|
||||
|
||||
final bool reachedMax = newOperations.length < 50;
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: OperationListStatus.success,
|
||||
operations: refresh
|
||||
? newOperations
|
||||
: [...state.operations, ...newOperations],
|
||||
hasReachedMax: reachedMax,
|
||||
operations: paginatedData.operations, // 🎯 SOSTITUISCE I DATI
|
||||
totalItems: paginatedData.totalCount,
|
||||
currentPage: page,
|
||||
hasReachedMax: paginatedData.operations.length < state.itemsPerPage,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: OperationListStatus.failure,
|
||||
errorMessage: "Errore nel caricamento operazioni: $e",
|
||||
errorMessage: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void updateFilters({String? query, DateTimeRange? range}) {
|
||||
// 🥷 MOTORE 2: MOBILE (Accoda alla lista)
|
||||
Future<void> loadNextPageMobile({bool refresh = false}) async {
|
||||
if (state.status == OperationListStatus.loading) return;
|
||||
if (state.hasReachedMax && !refresh) return;
|
||||
|
||||
// Se stiamo pullando verso il basso (refresh), ripartiamo da pagina 1
|
||||
final targetPage = refresh ? 1 : state.currentPage + 1;
|
||||
|
||||
// Mostriamo il loading solo se è un refresh totale, altrimenti manteniamo lo stato success
|
||||
// per non far sparire la UI mentre carica in fondo
|
||||
if (refresh) emit(state.copyWith(status: OperationListStatus.loading));
|
||||
|
||||
try {
|
||||
final companyId = _sessionCubit.state.company?.id;
|
||||
final paginatedData = await _repository.fetchPaginatedOperations(
|
||||
companyId: companyId!,
|
||||
page: targetPage,
|
||||
itemsPerPage: state.itemsPerPage,
|
||||
);
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
query: query ?? state.query,
|
||||
dateRange: range ?? state.dateRange,
|
||||
status: OperationListStatus.success,
|
||||
// 🎯 ACCODA I DATI SE NON È REFRESH, ALTRIMENTI SOSTITUISCE
|
||||
operations:
|
||||
refresh ? paginatedData.operations : List.of(state.operations)
|
||||
..addAll(paginatedData.operations),
|
||||
totalItems: paginatedData.totalCount,
|
||||
currentPage: targetPage,
|
||||
hasReachedMax: paginatedData.operations.length < state.itemsPerPage,
|
||||
),
|
||||
);
|
||||
loadOperations(refresh: true);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: OperationListStatus.failure,
|
||||
errorMessage: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void updateFilters({String? text, DateTimeRange? range}) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
// 🥷 FORZIAMO IL TIPO: Diciamo a Dart che il risultato del ternario è proprio una funzione
|
||||
searchTerm: text != null ? () => text : null,
|
||||
dateRange: range != null ? () => range : null,
|
||||
|
||||
currentPage: 1, // Reset obbligatorio alla prima pagina
|
||||
hasReachedMax: false,
|
||||
),
|
||||
);
|
||||
|
||||
// Ricarichiamo la pagina 1 con i nuovi filtri applicati
|
||||
loadSpecificPageDesktop(1);
|
||||
}
|
||||
|
||||
void clearFilters() {
|
||||
emit(const OperationListState()); // Resetta tutto allo stato iniziale
|
||||
loadOperations(refresh: true);
|
||||
// Invece di un const vuoto che potrebbe bruciarti l'impostazione itemsPerPage,
|
||||
// creiamo uno stato pulito ma manteniamo la preferenza di paginazione.
|
||||
emit(OperationListState(itemsPerPage: state.itemsPerPage));
|
||||
|
||||
loadSpecificPageDesktop(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,35 +5,57 @@ enum OperationListStatus { initial, loading, success, failure }
|
||||
class OperationListState extends Equatable {
|
||||
final OperationListStatus status;
|
||||
final List<OperationModel> operations;
|
||||
final bool hasReachedMax;
|
||||
final String? errorMessage;
|
||||
final String query;
|
||||
|
||||
// Paginazione Ibrida
|
||||
final int currentPage;
|
||||
final int itemsPerPage;
|
||||
final int totalItems;
|
||||
final bool hasReachedMax;
|
||||
|
||||
// 🥷 I FILTRI MANCANTI (Riparati!)
|
||||
final String? searchTerm;
|
||||
final DateTimeRange? dateRange;
|
||||
|
||||
const OperationListState({
|
||||
this.status = OperationListStatus.initial,
|
||||
this.operations = const [],
|
||||
this.hasReachedMax = false,
|
||||
this.errorMessage,
|
||||
this.query = '',
|
||||
this.currentPage = 1,
|
||||
this.itemsPerPage = 25,
|
||||
this.totalItems = 0,
|
||||
this.hasReachedMax = false,
|
||||
this.searchTerm,
|
||||
this.dateRange,
|
||||
});
|
||||
|
||||
int get totalPages => (totalItems / itemsPerPage).ceil();
|
||||
|
||||
// 🥷 COPYWITH AVANZATO: Gestisce lo sbiancamento dei filtri alla perfezione
|
||||
OperationListState copyWith({
|
||||
OperationListStatus? status,
|
||||
List<OperationModel>? operations,
|
||||
bool? hasReachedMax,
|
||||
String? errorMessage,
|
||||
String? query,
|
||||
DateTimeRange? dateRange,
|
||||
int? currentPage,
|
||||
int? itemsPerPage,
|
||||
int? totalItems,
|
||||
bool? hasReachedMax,
|
||||
String? Function()? searchTerm, // Callback per gestire il null esplicito
|
||||
DateTimeRange? Function()?
|
||||
dateRange, // Callback per gestire il null esplicito
|
||||
}) {
|
||||
return OperationListState(
|
||||
status: status ?? this.status,
|
||||
operations: operations ?? this.operations,
|
||||
errorMessage: errorMessage ?? this.errorMessage,
|
||||
currentPage: currentPage ?? this.currentPage,
|
||||
itemsPerPage: itemsPerPage ?? this.itemsPerPage,
|
||||
totalItems: totalItems ?? this.totalItems,
|
||||
hasReachedMax: hasReachedMax ?? this.hasReachedMax,
|
||||
errorMessage: errorMessage,
|
||||
query: query ?? this.query,
|
||||
dateRange: dateRange ?? this.dateRange,
|
||||
|
||||
// Se passi la funzione la eseguiamo, altrimenti teniamo il valore corrente
|
||||
searchTerm: searchTerm != null ? searchTerm() : this.searchTerm,
|
||||
dateRange: dateRange != null ? dateRange() : this.dateRange,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -41,9 +63,12 @@ class OperationListState extends Equatable {
|
||||
List<Object?> get props => [
|
||||
status,
|
||||
operations,
|
||||
hasReachedMax,
|
||||
errorMessage,
|
||||
query,
|
||||
currentPage,
|
||||
itemsPerPage,
|
||||
totalItems,
|
||||
hasReachedMax,
|
||||
searchTerm,
|
||||
dateRange,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -35,25 +35,31 @@ class OperationsRepository {
|
||||
}
|
||||
}
|
||||
|
||||
// --- RECUPERO PAGINATO CON FILTRI E JOIN ---
|
||||
Future<List<OperationModel>> fetchOperations({
|
||||
// 🥷 2. RECUPERO PAGINATO ASSOLUTO CON CONTEGGIO TOTALI
|
||||
Future<PaginatedOperations> fetchPaginatedOperations({
|
||||
required String companyId,
|
||||
String? storeId,
|
||||
String? staffId,
|
||||
String? providerId,
|
||||
required int offset,
|
||||
int limit = 50,
|
||||
required int page, // Usiamo 'page' (1, 2, 3...) invece di 'offset'
|
||||
int itemsPerPage = 25, // Default a 25 elementi per pagina
|
||||
String? searchTerm,
|
||||
DateTimeRange? dateRange,
|
||||
}) async {
|
||||
try {
|
||||
// Calcoliamo il range di partenza e fine per Supabase
|
||||
// Es. Pagina 1, 25 items -> range(0, 24)
|
||||
// Es. Pagina 2, 25 items -> range(25, 49)
|
||||
final from = (page - 1) * itemsPerPage;
|
||||
final to = from + itemsPerPage - 1;
|
||||
|
||||
var query = _supabase
|
||||
.from(Tables.operations)
|
||||
.select('''
|
||||
*,
|
||||
${Tables.customers}(*),
|
||||
${Tables.stores}(name),
|
||||
${Tables.providers}(name),
|
||||
${Tables.providers}(*),
|
||||
${Tables.models}(name_with_brand),
|
||||
${Tables.staffMembers}(name),
|
||||
${Tables.attachments}(*)
|
||||
@@ -80,7 +86,6 @@ class OperationsRepository {
|
||||
}
|
||||
|
||||
if (searchTerm != null && searchTerm.isNotEmpty) {
|
||||
// Filtra sui campi della tabella principale O su quelli della tabella joinata
|
||||
query = query.or(
|
||||
'reference.ilike.%$searchTerm%,note.ilike.%$searchTerm%,customer.name.ilike.%$searchTerm%',
|
||||
);
|
||||
@@ -88,17 +93,25 @@ class OperationsRepository {
|
||||
|
||||
final response = await query
|
||||
.order('created_at', ascending: false)
|
||||
.range(offset, offset + limit - 1);
|
||||
|
||||
return (response as List)
|
||||
.range(from, to)
|
||||
.count(CountOption.exact);
|
||||
// 3. Estrazione dei dati
|
||||
final List<OperationModel> operations = (response.data as List)
|
||||
.map((map) => OperationModel.fromMap(map))
|
||||
.toList();
|
||||
|
||||
final int totalCount = response.count;
|
||||
|
||||
return PaginatedOperations(
|
||||
operations: operations,
|
||||
totalCount: totalCount,
|
||||
);
|
||||
} catch (e) {
|
||||
throw Exception('$e');
|
||||
throw Exception('Errore nel recupero della pagina $page: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Stream<List<OperationModel>> watchStoreOperations({
|
||||
Stream<List<Map<String, dynamic>>> watchStoreOperations({
|
||||
required String storeId,
|
||||
required int limit,
|
||||
}) {
|
||||
@@ -107,11 +120,7 @@ class OperationsRepository {
|
||||
.stream(primaryKey: ['id'])
|
||||
.eq('store_id', storeId)
|
||||
.order('created_at', ascending: false)
|
||||
.limit(limit)
|
||||
.map(
|
||||
(listOfMaps) =>
|
||||
listOfMaps.map((map) => OperationModel.fromMap(map)).toList(),
|
||||
);
|
||||
.limit(limit);
|
||||
}
|
||||
|
||||
// --- SALVATAGGIO COMPLETO (PRIMA PADRE, POI FIGLI) ---
|
||||
@@ -325,3 +334,10 @@ class OperationsRepository {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class PaginatedOperations {
|
||||
final List<OperationModel> operations;
|
||||
final int totalCount;
|
||||
|
||||
PaginatedOperations({required this.operations, required this.totalCount});
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import 'package:flux/core/enums_and_consts/consts.dart';
|
||||
import 'package:flux/core/utils/extensions.dart';
|
||||
import 'package:flux/features/attachments/models/attachment_model.dart';
|
||||
import 'package:flux/features/customers/models/customer_model.dart';
|
||||
import 'package:flux/features/master_data/providers/models/provider_model.dart';
|
||||
|
||||
enum OperationStatus {
|
||||
success('success', 'OK'),
|
||||
@@ -29,8 +30,6 @@ class OperationModel extends Equatable {
|
||||
final DateTime? createdAt;
|
||||
final String type;
|
||||
final String? subType;
|
||||
final String? providerId;
|
||||
final String? providerDisplayName;
|
||||
final String? modelId;
|
||||
final String? modelDisplayName;
|
||||
final String? description;
|
||||
@@ -50,6 +49,7 @@ class OperationModel extends Equatable {
|
||||
final CustomerModel? customer;
|
||||
final String reference;
|
||||
final bool isBusiness;
|
||||
final ProviderModel? provider;
|
||||
|
||||
// ALLEGATI (Aggiunto)
|
||||
final List<AttachmentModel> attachments;
|
||||
@@ -59,8 +59,6 @@ class OperationModel extends Equatable {
|
||||
this.createdAt,
|
||||
this.type = '',
|
||||
this.subType,
|
||||
this.providerId,
|
||||
this.providerDisplayName,
|
||||
this.modelId,
|
||||
this.modelDisplayName,
|
||||
this.description,
|
||||
@@ -81,6 +79,7 @@ class OperationModel extends Equatable {
|
||||
this.reference = '',
|
||||
this.attachments = const [],
|
||||
this.isBusiness = false,
|
||||
this.provider,
|
||||
});
|
||||
|
||||
OperationModel copyWith({
|
||||
@@ -88,8 +87,8 @@ class OperationModel extends Equatable {
|
||||
DateTime? createdAt,
|
||||
String? type,
|
||||
String? subType,
|
||||
String? providerId,
|
||||
String? providerDisplayName,
|
||||
// 🥷 TRUCCO APPLICATO ANCHE QUI:
|
||||
ProviderModel? Function()? provider,
|
||||
String? modelId,
|
||||
String? modelDisplayName,
|
||||
String? description,
|
||||
@@ -115,8 +114,9 @@ class OperationModel extends Equatable {
|
||||
createdAt: createdAt ?? this.createdAt,
|
||||
type: type ?? this.type,
|
||||
subType: subType ?? this.subType,
|
||||
providerId: providerId ?? this.providerId,
|
||||
providerDisplayName: providerDisplayName ?? this.providerDisplayName,
|
||||
// Se la funzione è passata, la eseguiamo (anche se ritorna null), altrimenti teniamo il vecchio
|
||||
provider: provider != null ? provider() : this.provider,
|
||||
|
||||
modelId: modelId ?? this.modelId,
|
||||
modelDisplayName: modelDisplayName ?? this.modelDisplayName,
|
||||
description: description ?? this.description,
|
||||
@@ -145,8 +145,7 @@ class OperationModel extends Equatable {
|
||||
createdAt,
|
||||
type,
|
||||
subType,
|
||||
providerId,
|
||||
providerDisplayName,
|
||||
provider,
|
||||
modelId,
|
||||
modelDisplayName,
|
||||
description,
|
||||
@@ -183,10 +182,9 @@ class OperationModel extends Equatable {
|
||||
subType: map['sub_type'] as String?,
|
||||
|
||||
// I campi relazionali nullabili restano rigorosamente null!
|
||||
providerId: map['provider_id'] as String?,
|
||||
// MAGIA ANTI-CRASH: Usiamo ?['chiave'] per non far esplodere i join vuoti
|
||||
providerDisplayName: (map[Tables.providers]?['name'] as String?)
|
||||
?.myFormat(),
|
||||
provider: (map[Tables.providers] != null)
|
||||
? ProviderModel.fromMap(map[Tables.providers] as Map<String, dynamic>)
|
||||
: null,
|
||||
|
||||
modelId: map['model_id'] as String?,
|
||||
modelDisplayName: (map[Tables.models]?['name_with_brand'] as String?)
|
||||
@@ -238,7 +236,7 @@ class OperationModel extends Equatable {
|
||||
if (id != null) 'id': id,
|
||||
'type': type,
|
||||
'sub_type': subType,
|
||||
'provider_id': providerId,
|
||||
'provider_id': provider?.id,
|
||||
'model_id': modelId,
|
||||
'description': description,
|
||||
if (expirationDate != null)
|
||||
|
||||
@@ -26,6 +26,9 @@ class OperationFormScreen extends StatefulWidget {
|
||||
class _OperationFormScreenState extends State<OperationFormScreen> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
// 🥷 IL NUOVO FLAG: Ci ricorderà se vogliamo davvero uscire!
|
||||
bool _isClosingIntent = false;
|
||||
|
||||
final _referenceController = TextEditingController();
|
||||
final _noteController = TextEditingController();
|
||||
final _freeTextSubtypeController = TextEditingController();
|
||||
@@ -101,10 +104,11 @@ class _OperationFormScreenState extends State<OperationFormScreen> {
|
||||
required bool keepAdding,
|
||||
}) {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
// Se non stiamo facendo "Salva e Aggiungi Altro", il nostro intento è chiudere
|
||||
_isClosingIntent = !keepAdding;
|
||||
|
||||
_flushControllersToCubit();
|
||||
// Aggiorniamo prima lo stato bersaglio nel cubit
|
||||
context.read<OperationFormCubit>().updateFields(status: targetStatus);
|
||||
// Poi chiamiamo il salvataggio
|
||||
context.read<OperationFormCubit>().saveOperation(
|
||||
targetStatus: targetStatus,
|
||||
keepAdding: keepAdding,
|
||||
@@ -112,11 +116,15 @@ class _OperationFormScreenState extends State<OperationFormScreen> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<String?> _generateIdForQr() async {
|
||||
// RINOMINATA E RESA SICURA
|
||||
Future<String?> _ensureEntitySaved() async {
|
||||
if (!_formKey.currentState!.validate()) return null;
|
||||
|
||||
// 🥷 Diciamo esplicitamente al sistema che NON vogliamo uscire dalla pagina
|
||||
_isClosingIntent = false;
|
||||
|
||||
_flushControllersToCubit();
|
||||
|
||||
// Lo leggiamo pulito pulito dal context, perché c'è!
|
||||
final attachmentsBloc = context.read<AttachmentsBloc>();
|
||||
|
||||
final newId = await context.read<OperationFormCubit>().saveOperationDraft();
|
||||
@@ -151,8 +159,15 @@ class _OperationFormScreenState extends State<OperationFormScreen> {
|
||||
if (state.status == OperationFormStatus.ready && !_isInitialized) {
|
||||
_syncTextControllers(state.operation);
|
||||
}
|
||||
|
||||
// 🥷 ORA POPPA SOLO SE L'INTENTO ERA QUELLO DI USCIRE!
|
||||
if (state.status == OperationFormStatus.success) {
|
||||
if (_isClosingIntent) {
|
||||
Navigator.of(context).pop();
|
||||
} else {
|
||||
// È stato un salvataggio background (Pick Files o QR).
|
||||
// Non facciamo nulla, l'utente resta sulla pagina e i file vengono caricati!
|
||||
}
|
||||
} else if (state.status == OperationFormStatus.successAndAddAnother) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
@@ -626,7 +641,7 @@ class _OperationFormScreenState extends State<OperationFormScreen> {
|
||||
parentType: AttachmentParentType.operation,
|
||||
parentId: state.operation.id,
|
||||
titleForUpload: state.operation.customer?.name ?? 'Nuova Pratica',
|
||||
onGenerateIdForQr: _generateIdForQr,
|
||||
onEnsureEntitySaved: _ensureEntitySaved, // 🥷 Il nuovo nome!
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flux/core/routes/routes.dart';
|
||||
import 'package:flux/core/widgets/staff_selector_modal.dart';
|
||||
import 'package:flux/features/master_data/staff/models/staff_member_model.dart';
|
||||
import 'package:flux/features/operations/blocs/operation_list_cubit.dart';
|
||||
import 'package:flux/features/operations/models/operation_model.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
@@ -14,20 +16,39 @@ class OperationListScreen extends StatefulWidget {
|
||||
|
||||
class _OperationListScreenState extends State<OperationListScreen> {
|
||||
final ScrollController _scrollController = ScrollController();
|
||||
final TextEditingController _searchController = TextEditingController();
|
||||
|
||||
// 🥷 1. LO STATO PER LE BULK ACTIONS
|
||||
// Set per gestire le Bulk Actions (Selezione multipla)
|
||||
final Set<String> _selectedOperationIds = {};
|
||||
bool get _isSelectionMode => _selectedOperationIds.isNotEmpty;
|
||||
|
||||
// Flag per mostrare/nascondere la barra di ricerca integrata nell'AppBar
|
||||
bool _showSearchBar = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_scrollController.addListener(_onScroll);
|
||||
|
||||
// Primo caricamento: partiamo da pagina 1
|
||||
// (Il Cubit deciderà se fare il boot iniziale o se c'era già roba in cache)
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
final isDesktop = MediaQuery.sizeOf(context).width >= 900;
|
||||
if (isDesktop) {
|
||||
context.read<OperationListCubit>().loadSpecificPageDesktop(1);
|
||||
} else {
|
||||
context.read<OperationListCubit>().loadNextPageMobile(refresh: true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _onScroll() {
|
||||
final isDesktop = MediaQuery.sizeOf(context).width >= 900;
|
||||
// 🥷 COMPORTAMENTO IBRIDO: Lo scroll infinito si attiva SOLO su mobile
|
||||
if (isDesktop) return;
|
||||
|
||||
if (_isBottom) {
|
||||
context.read<OperationListCubit>().loadOperations();
|
||||
context.read<OperationListCubit>().loadNextPageMobile();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +62,7 @@ class _OperationListScreenState extends State<OperationListScreen> {
|
||||
@override
|
||||
void dispose() {
|
||||
_scrollController.dispose();
|
||||
_searchController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -62,8 +84,10 @@ class _OperationListScreenState extends State<OperationListScreen> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isDesktop = MediaQuery.sizeOf(context).width >= 900;
|
||||
|
||||
return Scaffold(
|
||||
// 🥷 2. APPBAR DINAMICA (Standard o Modalità Selezione)
|
||||
// --- APP BAR DINAMICA E INTEGRATA ---
|
||||
appBar: _isSelectionMode
|
||||
? AppBar(
|
||||
backgroundColor: Theme.of(context).colorScheme.primaryContainer,
|
||||
@@ -77,24 +101,53 @@ class _OperationListScreenState extends State<OperationListScreen> {
|
||||
icon: const Icon(Icons.edit_note),
|
||||
tooltip: 'Cambia Stato Massivo',
|
||||
onPressed: () {
|
||||
// TODO: Apri BottomSheet per cambiare stato a tutte le selezionate
|
||||
// TODO: Integrare bottom sheet per azioni massive
|
||||
},
|
||||
),
|
||||
],
|
||||
)
|
||||
: AppBar(
|
||||
title: const Text("Gestione Servizi"),
|
||||
title: _showSearchBar
|
||||
? TextField(
|
||||
controller: _searchController,
|
||||
autofocus: true,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'Cerca per cliente, nota o riferimento...',
|
||||
border: InputBorder.none,
|
||||
),
|
||||
style: const TextStyle(fontSize: 16),
|
||||
onChanged: (text) {
|
||||
context.read<OperationListCubit>().updateFilters(
|
||||
text: text,
|
||||
);
|
||||
},
|
||||
)
|
||||
: const Text("Gestione Servizi"),
|
||||
elevation: 0,
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.filter_list),
|
||||
icon: Icon(_showSearchBar ? Icons.close : Icons.search),
|
||||
onPressed: () {
|
||||
// TODO: Apri drawer laterale o modal per i filtri avanzati
|
||||
setState(() {
|
||||
_showSearchBar = !_showSearchBar;
|
||||
if (!_showSearchBar) {
|
||||
_searchController.clear();
|
||||
context.read<OperationListCubit>().clearFilters();
|
||||
}
|
||||
});
|
||||
},
|
||||
),
|
||||
if (!isDesktop) // Il pull-to-refresh c'è già su mobile, su desktop mettiamo un tasto manuale
|
||||
IconButton(
|
||||
icon: const Icon(Icons.filter_list),
|
||||
onPressed: () {
|
||||
// TODO: Bottone Filtri Avanzati (es. DateRange Picker)
|
||||
},
|
||||
),
|
||||
IconButton(icon: const Icon(Icons.search), onPressed: () {}),
|
||||
],
|
||||
),
|
||||
|
||||
// --- CORPO RESPONSIVO ---
|
||||
body: BlocBuilder<OperationListCubit, OperationListState>(
|
||||
builder: (context, state) {
|
||||
if (state.status == OperationListStatus.loading &&
|
||||
@@ -103,30 +156,69 @@ class _OperationListScreenState extends State<OperationListScreen> {
|
||||
}
|
||||
|
||||
if (state.operations.isEmpty) {
|
||||
return const Center(child: Text("Nessuna pratica trovata."));
|
||||
}
|
||||
|
||||
// 🥷 3. IL MOTORE RESPONSIVO
|
||||
return RefreshIndicator(
|
||||
onRefresh: () => context.read<OperationListCubit>().loadOperations(
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Text("Nessuna pratica trovata."),
|
||||
const SizedBox(height: 12),
|
||||
ElevatedButton(
|
||||
onPressed: () => isDesktop
|
||||
? context
|
||||
.read<OperationListCubit>()
|
||||
.loadSpecificPageDesktop(1)
|
||||
: context.read<OperationListCubit>().loadNextPageMobile(
|
||||
refresh: true,
|
||||
),
|
||||
child: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
// Se lo schermo è largo (Desktop/Tablet), usiamo la griglia
|
||||
final isDesktop = constraints.maxWidth > 700;
|
||||
child: const Text("Ricarica"),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return GridView.builder(
|
||||
// 🥷 SCENARIO DESKTOP: Griglia + Barra di Paginazione Gmail-Style
|
||||
if (isDesktop) {
|
||||
return Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: GridView.builder(
|
||||
controller: _scrollController,
|
||||
padding: const EdgeInsets.all(12).copyWith(bottom: 80),
|
||||
// Magia della griglia: si adatta!
|
||||
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
|
||||
padding: const EdgeInsets.all(16),
|
||||
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
|
||||
maxCrossAxisExtent:
|
||||
450, // Larghezza massima della singola card
|
||||
420, // Larghezza bilanciata per le card su desktop
|
||||
mainAxisExtent:
|
||||
180, // Altezza fissa della card (da aggiustare in base ai tuoi font)
|
||||
crossAxisSpacing: 12,
|
||||
mainAxisSpacing: 12,
|
||||
175, // Altezza controllata per evitare buchi bianchi
|
||||
crossAxisSpacing: 16,
|
||||
mainAxisSpacing: 16,
|
||||
),
|
||||
itemCount: state.operations.length,
|
||||
itemBuilder: (context, index) {
|
||||
final operation = state.operations[index];
|
||||
return _buildResponsiveCard(operation);
|
||||
},
|
||||
),
|
||||
),
|
||||
_buildDesktopPaginationFooter(
|
||||
state,
|
||||
), // La barra in fondo stile Gmail
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
// 🥷 SCENARIO MOBILE: ListView con Infinite Scroll e Pull-to-Refresh
|
||||
return RefreshIndicator(
|
||||
onRefresh: () => context
|
||||
.read<OperationListCubit>()
|
||||
.loadNextPageMobile(refresh: true),
|
||||
child: ListView.builder(
|
||||
controller: _scrollController,
|
||||
padding: const EdgeInsets.only(
|
||||
bottom: 80,
|
||||
left: 8,
|
||||
right: 8,
|
||||
top: 8,
|
||||
),
|
||||
itemCount: state.hasReachedMax
|
||||
? state.operations.length
|
||||
@@ -134,15 +226,45 @@ class _OperationListScreenState extends State<OperationListScreen> {
|
||||
itemBuilder: (context, index) {
|
||||
if (index >= state.operations.length) {
|
||||
return const Center(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(16.0),
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final operation = state.operations[index];
|
||||
final isSelected = _selectedOperationIds.contains(
|
||||
operation.id,
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
child: _buildResponsiveCard(operation),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
|
||||
floatingActionButton: _isSelectionMode
|
||||
? null
|
||||
: FloatingActionButton(
|
||||
onPressed: () async {
|
||||
StaffMemberModel? createdBy = await getStaffMember(context);
|
||||
if (createdBy == null || !context.mounted) return;
|
||||
context.pushNamed(
|
||||
Routes.operationForm,
|
||||
pathParameters: {'id': 'new'},
|
||||
extra: (createdBy: createdBy, operation: null),
|
||||
);
|
||||
},
|
||||
child: const Icon(Icons.add),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// --- COSTRUZIONE DELLA COMPONENTISTICA DETTAGLIATA ---
|
||||
|
||||
Widget _buildResponsiveCard(OperationModel operation) {
|
||||
final isSelected = _selectedOperationIds.contains(operation.id);
|
||||
return _RichOperationCard(
|
||||
operation: operation,
|
||||
isSelected: isSelected,
|
||||
@@ -160,26 +282,90 @@ class _OperationListScreenState extends State<OperationListScreen> {
|
||||
},
|
||||
onLongPress: () => _toggleSelection(operation.id!),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
}
|
||||
|
||||
// 🥷 LA BARRA DI PAGINAZIONE DESKTOP (Stile Gmail / Typesense)
|
||||
Widget _buildDesktopPaginationFooter(OperationListState state) {
|
||||
final theme = Theme.of(context);
|
||||
final cubit = context.read<OperationListCubit>();
|
||||
|
||||
// Calcolo intervallo visualizzato (es. 1-25 di 140)
|
||||
final fromItem = ((state.currentPage - 1) * state.itemsPerPage) + 1;
|
||||
final toItem =
|
||||
DateUtils.isSameDay(DateTime.now(), DateTime.now()) // segnaposto logico
|
||||
? (fromItem + state.operations.length - 1)
|
||||
: fromItem;
|
||||
|
||||
return Container(
|
||||
height: 56,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 24),
|
||||
decoration: BoxDecoration(
|
||||
color: theme.colorScheme.surface,
|
||||
border: Border(top: BorderSide(color: theme.dividerColor, width: 0.5)),
|
||||
),
|
||||
);
|
||||
},
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
// Info totali a sinistra
|
||||
Text(
|
||||
"$fromItem-$toItem di ${state.totalItems} pratiche totali",
|
||||
style: theme.textTheme.bodyMedium?.copyWith(
|
||||
color: Colors.grey[700],
|
||||
),
|
||||
floatingActionButton: _isSelectionMode
|
||||
? null // Nascondi il FAB se stai selezionando
|
||||
: FloatingActionButton(
|
||||
onPressed: () {
|
||||
/* Tuo codice per nuova operazione */
|
||||
},
|
||||
child: const Icon(Icons.add),
|
||||
),
|
||||
|
||||
// Controlli di navigazione a destra
|
||||
Row(
|
||||
children: [
|
||||
// Prima Pagina
|
||||
IconButton(
|
||||
icon: const Icon(Icons.first_page),
|
||||
onPressed: state.currentPage > 1
|
||||
? () => cubit.loadSpecificPageDesktop(1)
|
||||
: null,
|
||||
),
|
||||
// Pagina Precedente
|
||||
IconButton(
|
||||
icon: const Icon(Icons.chevron_left),
|
||||
onPressed: state.currentPage > 1
|
||||
? () => cubit.loadSpecificPageDesktop(state.currentPage - 1)
|
||||
: null,
|
||||
),
|
||||
|
||||
// Indicatore numerico centrale impacchettato
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16.0),
|
||||
child: Text(
|
||||
"Pagina ${state.currentPage} di ${state.totalPages}",
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
|
||||
// Pagina Successiva
|
||||
IconButton(
|
||||
icon: const Icon(Icons.chevron_right),
|
||||
onPressed: state.currentPage < state.totalPages
|
||||
? () => cubit.loadSpecificPageDesktop(state.currentPage + 1)
|
||||
: null,
|
||||
),
|
||||
// Ultima Pagina
|
||||
IconButton(
|
||||
icon: const Icon(Icons.last_page),
|
||||
onPressed: state.currentPage < state.totalPages
|
||||
? () => cubit.loadSpecificPageDesktop(state.totalPages)
|
||||
: null,
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 🥷 4. LA SUPER CARD ESTRATTA
|
||||
// =========================================================================
|
||||
// 🥷 3. LA CARD RICCA, REATTIVA E DEFINITIVA (Quella revisionata insieme)
|
||||
// =========================================================================
|
||||
class _RichOperationCard extends StatelessWidget {
|
||||
final OperationModel operation;
|
||||
final bool isSelected;
|
||||
@@ -195,7 +381,6 @@ class _RichOperationCard extends StatelessWidget {
|
||||
required this.onLongPress,
|
||||
});
|
||||
|
||||
// 🥷 1. IL COLORE DELLO STATO: Centralizzato per usarlo ovunque
|
||||
Color _getStatusColor(OperationStatus status) {
|
||||
switch (status) {
|
||||
case OperationStatus.success:
|
||||
@@ -206,11 +391,10 @@ class _RichOperationCard extends StatelessWidget {
|
||||
case OperationStatus.waitingForSupport:
|
||||
return Colors.blue;
|
||||
case OperationStatus.failure:
|
||||
return Colors.grey.shade800; // O Colors.red se preferisci
|
||||
return Colors.grey.shade800;
|
||||
}
|
||||
}
|
||||
|
||||
// 🥷 2. IL COLORE DEL TIPO: Per farlo risaltare
|
||||
Color _getTypeColor(String type) {
|
||||
switch (type) {
|
||||
case 'FIN':
|
||||
@@ -239,6 +423,7 @@ class _RichOperationCard extends StatelessWidget {
|
||||
final typeColor = _getTypeColor(operation.type);
|
||||
|
||||
return Card(
|
||||
margin: EdgeInsets.zero, // Gestito dai margini dei padri (griglia/lista)
|
||||
elevation: isSelected ? 4 : 1,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
@@ -256,32 +441,35 @@ class _RichOperationCard extends StatelessWidget {
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
color: isSelected
|
||||
? theme.colorScheme.primaryContainer.withValues(alpha: 0.2)
|
||||
? theme.colorScheme.primaryContainer.withValues(alpha: 0.15)
|
||||
: null,
|
||||
// BANDA LATERALE LEGATA ALLO STATO (Stilosissima)
|
||||
// 🥷 COERENZA 100%: Banda laterale legata allo status per eliminare i malintesi cromatici
|
||||
border: Border(left: BorderSide(color: statusColor, width: 6)),
|
||||
),
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// --- HEADER ---
|
||||
// --- LINEA HEADER ---
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
if (isSelectionMode)
|
||||
SizedBox(
|
||||
height: 24,
|
||||
width: 24,
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(right: 8.0),
|
||||
child: SizedBox(
|
||||
height: 18,
|
||||
width: 18,
|
||||
child: Checkbox(
|
||||
value: isSelected,
|
||||
onChanged: (_) => onTap(),
|
||||
),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: Text(
|
||||
operation.reference.isEmpty
|
||||
? 'Nessuna Riferimento'
|
||||
(operation.reference.isEmpty)
|
||||
? 'Senza Riferimento'
|
||||
: operation.reference,
|
||||
style: theme.textTheme.labelSmall?.copyWith(
|
||||
color: Colors.grey[600],
|
||||
@@ -291,7 +479,9 @@ class _RichOperationCard extends StatelessWidget {
|
||||
),
|
||||
),
|
||||
Text(
|
||||
"${operation.createdAt?.day.toString().padLeft(2, '0')}/${operation.createdAt?.month.toString().padLeft(2, '0')}/${operation.createdAt?.year}",
|
||||
operation.createdAt != null
|
||||
? "${operation.createdAt!.day.toString().padLeft(2, '0')}/${operation.createdAt!.month.toString().padLeft(2, '0')}/${operation.createdAt!.year}"
|
||||
: '',
|
||||
style: theme.textTheme.labelSmall?.copyWith(
|
||||
color: Colors.grey[600],
|
||||
),
|
||||
@@ -300,7 +490,7 @@ class _RichOperationCard extends StatelessWidget {
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
|
||||
// --- CLIENTE E TIPO OPERAZIONE ---
|
||||
// --- LINEA CENTRALE: CLIENTE + INSERTO OPERATIVO ---
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
@@ -309,24 +499,25 @@ class _RichOperationCard extends StatelessWidget {
|
||||
operation.customer?.name ?? "Cliente sconosciuto",
|
||||
style: const TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 16,
|
||||
fontSize: 15,
|
||||
),
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
// IL TIPO DI OPERAZIONE CHE SPICCA
|
||||
|
||||
// 🥷 IL RE DEL SERVICE: Il tipo operazione svetta con box e contrasto ad hoc
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 10,
|
||||
vertical: 6,
|
||||
horizontal: 8,
|
||||
vertical: 4,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: typeColor.withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
color: typeColor.withValues(alpha: 0.12),
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
border: Border.all(
|
||||
color: typeColor.withValues(alpha: 0.3),
|
||||
color: typeColor.withValues(alpha: 0.25),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
@@ -342,19 +533,20 @@ class _RichOperationCard extends StatelessWidget {
|
||||
operation.type,
|
||||
operation.subType,
|
||||
),
|
||||
size: 14,
|
||||
size: 13,
|
||||
color: typeColor,
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
],
|
||||
Text(
|
||||
operation.subType?.isNotEmpty == true
|
||||
(operation.subType != null &&
|
||||
operation.subType!.isNotEmpty)
|
||||
? operation.subType!
|
||||
: operation.type,
|
||||
style: TextStyle(
|
||||
color: typeColor,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 12,
|
||||
fontSize: 11,
|
||||
),
|
||||
),
|
||||
],
|
||||
@@ -362,14 +554,14 @@ class _RichOperationCard extends StatelessWidget {
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
const SizedBox(height: 10),
|
||||
|
||||
// --- I TAG COMPATTI (Business/Privato, Provider, Device) ---
|
||||
// --- LINEA DEI TAG TECNICI ---
|
||||
Wrap(
|
||||
spacing: 6,
|
||||
runSpacing: 6,
|
||||
runSpacing: 4,
|
||||
children: [
|
||||
// Espanso in "Business" e "Privato"
|
||||
// Tag Target Espanso (Privato / Business)
|
||||
_MiniChip(
|
||||
label: operation.isBusiness ? 'Business' : 'Privato',
|
||||
icon: operation.isBusiness
|
||||
@@ -378,17 +570,18 @@ class _RichOperationCard extends StatelessWidget {
|
||||
color: operation.isBusiness ? Colors.indigo : Colors.teal,
|
||||
),
|
||||
|
||||
// Tag Provider con il suo colore personalizzato dal DB
|
||||
if (operation.providerId != null)
|
||||
// Tag Gestore (Agganciato dinamicamente al displayColor generato dall'esadecimale del DB!)
|
||||
if (operation.provider != null)
|
||||
_MiniChip(
|
||||
label: operation.providerDisplayName ?? 'Gestore',
|
||||
// Se hai popolato il campo colorHex, qui puoi usare: operation.provider?.displayColor ?? Colors.grey
|
||||
color: Colors.redAccent,
|
||||
label: operation.provider?.name ?? 'Gestore',
|
||||
color:
|
||||
operation.provider?.displayColor ?? Colors.blueGrey,
|
||||
),
|
||||
|
||||
// Specifiche addizionali del Finanziamento
|
||||
if (operation.type == 'Fin' && operation.modelId != null)
|
||||
_MiniChip(
|
||||
label: operation.modelDisplayName ?? 'Modello',
|
||||
label: operation.modelDisplayName ?? 'Prodotto',
|
||||
icon: Icons.devices,
|
||||
color: Colors.deepPurple,
|
||||
),
|
||||
@@ -397,7 +590,7 @@ class _RichOperationCard extends StatelessWidget {
|
||||
|
||||
const Spacer(),
|
||||
|
||||
// --- FOOTER: Staff e Stato ---
|
||||
// --- FOOTER CARD: AGENTE + CHIP STATO ---
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
@@ -410,14 +603,31 @@ class _RichOperationCard extends StatelessWidget {
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
operation.staffDisplayName ?? 'Staff',
|
||||
operation.staffDisplayName ?? 'Assegnato',
|
||||
style: theme.textTheme.labelSmall?.copyWith(
|
||||
color: Colors.grey[700],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
_buildOperationStatus(operation.status, statusColor),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 8,
|
||||
vertical: 4,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: statusColor,
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
),
|
||||
child: Text(
|
||||
operation.status.displayName,
|
||||
style: const TextStyle(
|
||||
fontSize: 10,
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
@@ -435,26 +645,9 @@ class _RichOperationCard extends StatelessWidget {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Widget _buildOperationStatus(OperationStatus status, Color statusColor) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: statusColor,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Text(
|
||||
status.displayName,
|
||||
style: const TextStyle(
|
||||
fontSize: 10,
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Micro Widget di supporto per i tag interni
|
||||
class _MiniChip extends StatelessWidget {
|
||||
final String label;
|
||||
final IconData? icon;
|
||||
@@ -465,23 +658,23 @@ class _MiniChip extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 4),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 3),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withValues(alpha: 0.1),
|
||||
border: Border.all(color: color.withValues(alpha: 0.3)),
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
color: color.withValues(alpha: 0.08),
|
||||
border: Border.all(color: color.withValues(alpha: 0.25)),
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (icon != null) ...[
|
||||
Icon(icon, size: 12, color: color),
|
||||
Icon(icon, size: 11, color: color),
|
||||
const SizedBox(width: 4),
|
||||
],
|
||||
Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
fontSize: 10,
|
||||
color: color,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
|
||||
@@ -102,10 +102,9 @@ class OperationDetailsSection extends StatelessWidget {
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
onTap: () {
|
||||
context.read<OperationFormCubit>().updateFields(
|
||||
providerId: provider.id,
|
||||
providerDisplayName: provider.name,
|
||||
);
|
||||
context
|
||||
.read<OperationFormCubit>()
|
||||
.updateProvider(provider);
|
||||
Navigator.pop(modalContext);
|
||||
},
|
||||
);
|
||||
@@ -134,19 +133,18 @@ class OperationDetailsSection extends StatelessWidget {
|
||||
ListTile(
|
||||
title: const Text('Seleziona Gestore'),
|
||||
subtitle: Text(
|
||||
(currentOp?.providerDisplayName != null &&
|
||||
currentOp!.providerDisplayName!.isNotEmpty)
|
||||
? currentOp!.providerDisplayName!
|
||||
(currentOp?.provider != null)
|
||||
? currentOp!.provider!.name
|
||||
: 'Nessun gestore selezionato',
|
||||
style: TextStyle(
|
||||
color:
|
||||
(currentOp?.providerId == null ||
|
||||
currentOp!.providerId!.isEmpty)
|
||||
(currentOp?.provider == null ||
|
||||
currentOp!.provider!.name.isEmpty)
|
||||
? Colors.grey
|
||||
: null,
|
||||
fontWeight:
|
||||
(currentOp?.providerId == null ||
|
||||
currentOp!.providerId!.isEmpty)
|
||||
(currentOp?.provider == null ||
|
||||
currentOp!.provider!.name.isEmpty)
|
||||
? FontWeight.normal
|
||||
: FontWeight.bold,
|
||||
),
|
||||
|
||||
@@ -8,6 +8,7 @@ import 'package:flux/features/settings/data/settings_repository.dart';
|
||||
import 'package:flux/features/tasks/data/task_repository.dart';
|
||||
import 'package:flux/features/tasks/models/task_model.dart';
|
||||
import 'package:flux/features/tasks/models/task_reminder_config.dart';
|
||||
import 'package:flux/features/tasks/models/task_status.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
part 'task_form_state.dart';
|
||||
|
||||
@@ -30,7 +31,7 @@ class TaskFormCubit extends Cubit<TaskFormState> {
|
||||
}
|
||||
|
||||
String get _companyId => _sessionCubit.state.company!.id!;
|
||||
String get _currentUserId => _sessionCubit.state.currentStaffMember!.id!;
|
||||
StaffMemberModel get _currentUser => _sessionCubit.state.currentStaffMember!;
|
||||
String? get _currentStoreId => _sessionCubit.state.currentStore?.id;
|
||||
|
||||
// --- ARMED INITIALIZATION (Nuovo, Esistente o Deep Link) ---
|
||||
@@ -57,6 +58,7 @@ class TaskFormCubit extends Cubit<TaskFormState> {
|
||||
dueDate: task.dueDate,
|
||||
isGlobal: task.isGlobal, // Sfrutta il tuo getter storeId == null
|
||||
selectedStaffIds: task.assignedToIds,
|
||||
taskStatus: task.status,
|
||||
),
|
||||
);
|
||||
await _loadExistingTaskReminders(task.id!);
|
||||
@@ -129,7 +131,7 @@ class TaskFormCubit extends Cubit<TaskFormState> {
|
||||
try {
|
||||
final defaults = await _settingsRepository.getMyReminderDefaults(
|
||||
companyId: _companyId,
|
||||
staffId: _currentUserId,
|
||||
staffId: _currentUser.id!,
|
||||
);
|
||||
final initialReminders = defaults
|
||||
.map(
|
||||
@@ -155,7 +157,7 @@ class TaskFormCubit extends Cubit<TaskFormState> {
|
||||
try {
|
||||
final existingConfigs = await _repository.fetchPersonalReminders(
|
||||
taskId: taskId,
|
||||
staffId: _currentUserId,
|
||||
staffId: _currentUser.id!,
|
||||
);
|
||||
emit(state.copyWith(reminders: existingConfigs));
|
||||
} catch (e) {
|
||||
@@ -218,7 +220,7 @@ class TaskFormCubit extends Cubit<TaskFormState> {
|
||||
final taskToSave = TaskModel(
|
||||
id: state.id,
|
||||
companyId: _companyId,
|
||||
createdById: _currentUserId,
|
||||
createdBy: _currentUser,
|
||||
title: state.title.trim(),
|
||||
description: state.description.trim(),
|
||||
dueDate: state.dueDate,
|
||||
@@ -233,14 +235,14 @@ class TaskFormCubit extends Cubit<TaskFormState> {
|
||||
await _repository.createTask(
|
||||
task: taskToSave,
|
||||
assignedStaffIds: state.selectedStaffIds,
|
||||
currentUserId: _currentUserId,
|
||||
currentUserId: _currentUser.id!,
|
||||
currentUserCustomReminders: state.reminders,
|
||||
);
|
||||
} else {
|
||||
await _repository.updateTask(
|
||||
task: taskToSave,
|
||||
assignedStaffIds: state.selectedStaffIds,
|
||||
currentUserId: _currentUserId,
|
||||
currentUserId: _currentUser.id!,
|
||||
currentUserCustomReminders: state.reminders,
|
||||
);
|
||||
}
|
||||
@@ -254,4 +256,51 @@ class TaskFormCubit extends Cubit<TaskFormState> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> deleteTask() async {
|
||||
if (state.id == null) return;
|
||||
emit(state.copyWith(status: TaskFormStatus.submitting));
|
||||
try {
|
||||
await _repository.deleteTask(state.id!);
|
||||
emit(state.copyWith(status: TaskFormStatus.success));
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: TaskFormStatus.failure,
|
||||
errorMessage: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> updateTaskLocalStatus(TaskStatus newStatus) async {
|
||||
emit(state.copyWith(taskStatus: newStatus));
|
||||
}
|
||||
|
||||
Future<void> updateTaskStatus(TaskStatus newStatus) async {
|
||||
try {
|
||||
// Chiamiamo il repo passando il task aggiornato
|
||||
await _repository.updateTaskStatus(
|
||||
taskId: state.id!,
|
||||
newStatus: newStatus,
|
||||
updatedById: _currentUser.id!,
|
||||
);
|
||||
|
||||
if (!isClosed) {
|
||||
// Se l'update va a buon fine, aggiorniamo lo stato locale del cubit
|
||||
emit(
|
||||
state.copyWith(status: TaskFormStatus.success, taskStatus: newStatus),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (!isClosed) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: TaskFormStatus.failure,
|
||||
errorMessage: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,9 +11,9 @@ class TaskFormState extends Equatable {
|
||||
final bool isGlobal;
|
||||
final List<String> selectedStaffIds;
|
||||
final List<TaskReminderConfig> reminders;
|
||||
final Map<String, List<StaffMemberModel>>
|
||||
groupedAvailableStaff; // <-- RIPRISTINATO
|
||||
final Map<String, List<StaffMemberModel>> groupedAvailableStaff;
|
||||
final String? errorMessage;
|
||||
final TaskStatus taskStatus;
|
||||
|
||||
const TaskFormState({
|
||||
this.id,
|
||||
@@ -26,6 +26,7 @@ class TaskFormState extends Equatable {
|
||||
this.reminders = const [],
|
||||
this.groupedAvailableStaff = const {},
|
||||
this.errorMessage,
|
||||
this.taskStatus = TaskStatus.open,
|
||||
});
|
||||
|
||||
bool get isFormValid => title.trim().isNotEmpty;
|
||||
@@ -41,6 +42,7 @@ class TaskFormState extends Equatable {
|
||||
List<TaskReminderConfig>? reminders,
|
||||
Map<String, List<StaffMemberModel>>? groupedAvailableStaff,
|
||||
String? errorMessage,
|
||||
TaskStatus? taskStatus,
|
||||
}) {
|
||||
return TaskFormState(
|
||||
id: id ?? this.id,
|
||||
@@ -54,6 +56,7 @@ class TaskFormState extends Equatable {
|
||||
groupedAvailableStaff:
|
||||
groupedAvailableStaff ?? this.groupedAvailableStaff,
|
||||
errorMessage: errorMessage,
|
||||
taskStatus: taskStatus ?? this.taskStatus,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -69,5 +72,6 @@ class TaskFormState extends Equatable {
|
||||
reminders,
|
||||
groupedAvailableStaff,
|
||||
errorMessage,
|
||||
taskStatus,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ class TasksRepository {
|
||||
}) async {
|
||||
try {
|
||||
final response = await _supabase
|
||||
.from('task_reminders')
|
||||
.from(Tables.taskReminders)
|
||||
.select()
|
||||
.eq('task_id', taskId)
|
||||
.eq('staff_id', staffId)
|
||||
@@ -53,13 +53,17 @@ class TasksRepository {
|
||||
int? limit,
|
||||
}) async {
|
||||
try {
|
||||
// 1. FASE FILTRI: Usa il join esplicito stile "Notes"
|
||||
// 1. FASE FILTRI: Disambiguazione completa su Tasks e Assignments
|
||||
var filterBuilder = _supabase
|
||||
.from(Tables.tasks)
|
||||
.select('''
|
||||
*,
|
||||
creator:${Tables.staffMembers}!created_by_id(*),
|
||||
updater:${Tables.staffMembers}!updated_by_id(*),
|
||||
task_assignments:${Tables.taskAssignments} (
|
||||
${Tables.staffMembers} (*)
|
||||
*,
|
||||
assignee:${Tables.staffMembers}!staff_id(*),
|
||||
assigner:${Tables.staffMembers}!assigned_by_id(*)
|
||||
)
|
||||
''')
|
||||
.eq('company_id', companyId);
|
||||
@@ -71,7 +75,6 @@ class TasksRepository {
|
||||
}
|
||||
|
||||
if (staffId != null) {
|
||||
// Grazie al trigger, hai l'array pronto per il filtro senza impazzire!
|
||||
filterBuilder = filterBuilder.contains('assigned_to_ids', [staffId]);
|
||||
}
|
||||
|
||||
@@ -105,8 +108,12 @@ class TasksRepository {
|
||||
.from(Tables.tasks)
|
||||
.select('''
|
||||
*,
|
||||
creator:${Tables.staffMembers}!created_by_id(*),
|
||||
updater:${Tables.staffMembers}!updated_by_id(*),
|
||||
task_assignments:${Tables.taskAssignments} (
|
||||
${Tables.staffMembers} (*)
|
||||
*,
|
||||
assignee:${Tables.staffMembers}!staff_id(*),
|
||||
assigner:${Tables.staffMembers}!assigned_by_id(*)
|
||||
)
|
||||
''')
|
||||
.eq('id', taskId)
|
||||
@@ -122,14 +129,11 @@ class TasksRepository {
|
||||
// =========================================================================
|
||||
// REALTIME STREAM (La sentinella per la bacheca)
|
||||
// =========================================================================
|
||||
Stream<List<TaskModel>> watchCompanyTasks(String companyId) {
|
||||
Stream<List<Map<String, dynamic>>> watchCompanyTasks(String companyId) {
|
||||
return _supabase
|
||||
.from('tasks')
|
||||
.stream(primaryKey: ['id'])
|
||||
.eq('company_id', companyId)
|
||||
.map((listOfMaps) {
|
||||
return listOfMaps.map((map) => TaskModel.fromMap(map)).toList();
|
||||
});
|
||||
.eq('company_id', companyId);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
@@ -160,6 +164,7 @@ class TasksRepository {
|
||||
'task_id': taskId,
|
||||
'staff_id': staffId,
|
||||
'company_id': task.companyId,
|
||||
'assigned_by_id': currentUserId,
|
||||
},
|
||||
)
|
||||
.toList();
|
||||
@@ -276,7 +281,7 @@ class TasksRepository {
|
||||
|
||||
// 1. Aggiornamento dati Task Base
|
||||
await _supabase
|
||||
.from('tasks')
|
||||
.from(Tables.tasks)
|
||||
.update({
|
||||
'title': task.title,
|
||||
'description': task.description,
|
||||
@@ -286,15 +291,51 @@ class TasksRepository {
|
||||
})
|
||||
.eq('id', taskId);
|
||||
|
||||
// 2. Aggiornamento Assegnazioni: eliminiamo le vecchie, inseriamo le nuove
|
||||
await _supabase.from('task_assignments').delete().eq('task_id', taskId);
|
||||
if (assignedStaffIds.isNotEmpty) {
|
||||
final assignmentsToInsert = assignedStaffIds
|
||||
// 🥷 2. GESTIONE CHIRURGICA DELLE ASSEGNAZIONI (Addio spam!)
|
||||
|
||||
// A) Recuperiamo chi è GIÀ assegnato a questo task
|
||||
final existingAssignmentsResponse = await _supabase
|
||||
.from('task_assignments')
|
||||
.select('staff_id')
|
||||
.eq('task_id', taskId);
|
||||
|
||||
final List<String> existingStaffIds =
|
||||
(existingAssignmentsResponse as List)
|
||||
.map((row) => row['staff_id'] as String)
|
||||
.toList();
|
||||
|
||||
// B) Calcoliamo i Delta con i Set di Dart (Pura magia matematica)
|
||||
final newStaffIdsSet = assignedStaffIds.toSet();
|
||||
final existingStaffIdsSet = existingStaffIds.toSet();
|
||||
|
||||
// Quelli da inserire (presenti nei nuovi, ma non nei vecchi)
|
||||
final toInsertIds = newStaffIdsSet
|
||||
.difference(existingStaffIdsSet)
|
||||
.toList();
|
||||
|
||||
// Quelli da eliminare (presenti nei vecchi, ma non nei nuovi)
|
||||
final toDeleteIds = existingStaffIdsSet
|
||||
.difference(newStaffIdsSet)
|
||||
.toList();
|
||||
|
||||
// C) Eseguiamo solo lo stretto necessario
|
||||
if (toDeleteIds.isNotEmpty) {
|
||||
await _supabase
|
||||
.from('task_assignments')
|
||||
.delete()
|
||||
.eq('task_id', taskId)
|
||||
.inFilter('staff_id', toDeleteIds);
|
||||
}
|
||||
|
||||
if (toInsertIds.isNotEmpty) {
|
||||
final assignmentsToInsert = toInsertIds
|
||||
.map(
|
||||
(staffId) => {
|
||||
'task_id': taskId,
|
||||
'staff_id': staffId,
|
||||
'company_id': task.companyId,
|
||||
'assigned_by_id':
|
||||
currentUserId, // Il nostro salvavita anti-fantasma
|
||||
},
|
||||
)
|
||||
.toList();
|
||||
@@ -352,6 +393,35 @@ class TasksRepository {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> updateTaskStatus({
|
||||
required String taskId,
|
||||
required TaskStatus newStatus,
|
||||
required String? updatedById,
|
||||
}) async {
|
||||
try {
|
||||
await _supabase
|
||||
.from(Tables.tasks)
|
||||
.update({
|
||||
'status': newStatus.toValue,
|
||||
'updated_by_id': updatedById,
|
||||
'updated_at': DateTime.now().toIso8601String(),
|
||||
})
|
||||
.eq('id', taskId);
|
||||
} catch (e) {
|
||||
throw Exception(
|
||||
'Errore durante l\'aggiornamento dello stato del task: $e',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> deleteTask(String taskId) async {
|
||||
try {
|
||||
await _supabase.from(Tables.tasks).delete().eq('id', taskId);
|
||||
} catch (e) {
|
||||
throw Exception('Errore durante la cancellazione del task: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// --- HELPER PRIVATO PER LA MAPPA DEL REMINDER ---
|
||||
Map<String, dynamic> _buildReminderRow(
|
||||
TaskModel task,
|
||||
|
||||
@@ -9,11 +9,12 @@ class TaskModel extends Equatable {
|
||||
final String? description;
|
||||
final List<String> assignedToIds;
|
||||
final List<StaffMemberModel> assignedToStaff; // I dati completi dal JOIN
|
||||
final String? createdById;
|
||||
final StaffMemberModel? createdBy;
|
||||
final DateTime? dueDate;
|
||||
final TaskStatus status;
|
||||
final DateTime? createdAt;
|
||||
final String? storeId;
|
||||
final StaffMemberModel? updatedBy;
|
||||
|
||||
const TaskModel({
|
||||
this.id,
|
||||
@@ -22,24 +23,25 @@ class TaskModel extends Equatable {
|
||||
this.description,
|
||||
this.assignedToIds = const [],
|
||||
this.assignedToStaff = const [],
|
||||
this.createdById,
|
||||
this.createdBy,
|
||||
this.dueDate,
|
||||
this.status = TaskStatus.open,
|
||||
this.createdAt,
|
||||
this.storeId,
|
||||
this.updatedBy,
|
||||
});
|
||||
|
||||
bool get isGlobal => storeId == null;
|
||||
|
||||
// --- FACTORY: MODELLO VUOTO (Per le creazioni) ---
|
||||
factory TaskModel.empty({String? companyId, String? createdById}) {
|
||||
factory TaskModel.empty({String? companyId, StaffMemberModel? createdBy}) {
|
||||
return TaskModel(
|
||||
companyId: companyId,
|
||||
title: '',
|
||||
description: '',
|
||||
assignedToIds: const [],
|
||||
assignedToStaff: const [],
|
||||
createdById: createdById,
|
||||
createdBy: createdBy,
|
||||
status: TaskStatus.open,
|
||||
createdAt: DateTime.now(),
|
||||
);
|
||||
@@ -54,11 +56,12 @@ class TaskModel extends Equatable {
|
||||
description,
|
||||
assignedToIds,
|
||||
assignedToStaff,
|
||||
createdById,
|
||||
createdBy,
|
||||
dueDate,
|
||||
status,
|
||||
createdAt,
|
||||
storeId,
|
||||
updatedBy,
|
||||
];
|
||||
|
||||
// --- COPY WITH ---
|
||||
@@ -69,13 +72,15 @@ class TaskModel extends Equatable {
|
||||
String? description,
|
||||
List<String>? assignedToIds,
|
||||
List<StaffMemberModel>? assignedToStaff,
|
||||
String? createdById,
|
||||
StaffMemberModel? createdBy,
|
||||
DateTime? dueDate,
|
||||
bool clearDueDate = false, // Flag ninja per resettare la scadenza
|
||||
TaskStatus? status,
|
||||
DateTime? createdAt,
|
||||
String? storeId,
|
||||
bool clearStoreId = false,
|
||||
StaffMemberModel? updatedBy,
|
||||
String? updatedByDisplayName,
|
||||
}) {
|
||||
return TaskModel(
|
||||
id: id ?? this.id,
|
||||
@@ -84,11 +89,12 @@ class TaskModel extends Equatable {
|
||||
description: description ?? this.description,
|
||||
assignedToIds: assignedToIds ?? this.assignedToIds,
|
||||
assignedToStaff: assignedToStaff ?? this.assignedToStaff,
|
||||
createdById: createdById ?? this.createdById,
|
||||
createdBy: createdBy ?? this.createdBy,
|
||||
dueDate: clearDueDate ? null : (dueDate ?? this.dueDate),
|
||||
status: status ?? this.status,
|
||||
createdAt: createdAt ?? this.createdAt,
|
||||
storeId: clearStoreId ? null : (storeId ?? this.storeId),
|
||||
updatedBy: updatedBy ?? this.updatedBy,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -124,7 +130,9 @@ class TaskModel extends Equatable {
|
||||
description: map['description'] as String?,
|
||||
assignedToIds: parsedAssignedToIds,
|
||||
assignedToStaff: staffList,
|
||||
createdById: map['created_by_id'] as String?,
|
||||
createdBy: map['created_by_id'] != null
|
||||
? StaffMemberModel.fromMap(map['creator'])
|
||||
: null,
|
||||
dueDate: map['due_date'] != null
|
||||
? DateTime.parse(map['due_date'] as String).toLocal()
|
||||
: null,
|
||||
@@ -133,6 +141,9 @@ class TaskModel extends Equatable {
|
||||
? DateTime.parse(map['created_at'] as String).toLocal()
|
||||
: null,
|
||||
storeId: map['store_id'] as String?,
|
||||
updatedBy: map['updated_by_id'] != null
|
||||
? StaffMemberModel.fromMap(map['updater'])
|
||||
: null,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -145,10 +156,11 @@ class TaskModel extends Equatable {
|
||||
if (description != null) 'description': description,
|
||||
// Passiamo l'array vuoto se non ci sono assegnazioni
|
||||
'assigned_to_ids': assignedToIds.isEmpty ? null : assignedToIds,
|
||||
if (createdById != null) 'created_by_id': createdById,
|
||||
if (createdBy != null) 'created_by_id': createdBy!.id,
|
||||
'due_date': dueDate?.toUtc().toIso8601String(),
|
||||
'status': status.toValue,
|
||||
'store_id': storeId,
|
||||
if (updatedBy != null) 'updated_by_id': updatedBy!.id,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
enum TaskStatus { open, inProgress, completed }
|
||||
|
||||
extension TaskStatusExtension on TaskStatus {
|
||||
String get name {
|
||||
String get displayName {
|
||||
switch (this) {
|
||||
case TaskStatus.open:
|
||||
return 'Da Iniziare';
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flux/features/tasks/blocs/task_form_cubit.dart';
|
||||
import 'package:flux/features/tasks/models/task_status.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
class TaskFormScreen extends StatefulWidget {
|
||||
@@ -123,6 +124,30 @@ class _TaskFormScreenState extends State<TaskFormScreen> {
|
||||
appBar: AppBar(
|
||||
title: Text(isEditing ? 'Modifica Task' : 'Nuovo Task'),
|
||||
actions: [
|
||||
// 🥷 1. BOTTONE COMPLETAMENTO RAPIDO (Solo se in edit e non già completato)
|
||||
if (isEditing && state.taskStatus != TaskStatus.completed)
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 8.0,
|
||||
vertical: 8.0,
|
||||
),
|
||||
child: ElevatedButton.icon(
|
||||
onPressed: () {
|
||||
context.read<TaskFormCubit>().updateTaskStatus(
|
||||
TaskStatus.completed,
|
||||
);
|
||||
},
|
||||
icon: const Icon(Icons.check_circle_outline),
|
||||
label: const Text('Completa'),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.green.shade600,
|
||||
foregroundColor: Colors.white,
|
||||
elevation: 0,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// 🥷 2. LOADER O BOTTONE SALVA
|
||||
if (state.status == TaskFormStatus.submitting)
|
||||
const Padding(
|
||||
padding: EdgeInsets.all(16.0),
|
||||
@@ -133,8 +158,12 @@ class _TaskFormScreenState extends State<TaskFormScreen> {
|
||||
),
|
||||
)
|
||||
else
|
||||
TextButton.icon(
|
||||
onPressed: state.isFormValid ? () => cubit.saveTask() : null,
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(right: 8.0),
|
||||
child: TextButton.icon(
|
||||
onPressed: state.isFormValid
|
||||
? () => cubit.saveTask()
|
||||
: null,
|
||||
icon: const Icon(Icons.save),
|
||||
label: const Text('Salva'),
|
||||
style: TextButton.styleFrom(
|
||||
@@ -142,6 +171,7 @@ class _TaskFormScreenState extends State<TaskFormScreen> {
|
||||
disabledForegroundColor: Colors.grey,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: state.status == TaskFormStatus.loading
|
||||
@@ -264,6 +294,45 @@ class _TaskFormScreenState extends State<TaskFormScreen> {
|
||||
),
|
||||
onChanged: cubit.updateDescription,
|
||||
),
|
||||
if (state.id != null) ...[
|
||||
const SizedBox(height: 16),
|
||||
DropdownButtonFormField<TaskStatus>(
|
||||
// Leggiamo lo stato attuale dal Cubit (o usiamo un default per i nuovi task)
|
||||
initialValue: state.taskStatus,
|
||||
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Stato Attuale',
|
||||
prefixIcon: const Icon(Icons.flag_outlined),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
filled: true,
|
||||
fillColor: Theme.of(context).colorScheme.surface,
|
||||
),
|
||||
|
||||
// Mappiamo tutti i valori dell'enum in elementi della tendina
|
||||
items: TaskStatus.values.map((TaskStatus status) {
|
||||
return DropdownMenuItem<TaskStatus>(
|
||||
value: status,
|
||||
child: Text(
|
||||
status
|
||||
.displayName, // Usa la property displayName del tuo enum!
|
||||
style: TextStyle(
|
||||
fontWeight: status == state.taskStatus
|
||||
? FontWeight.bold
|
||||
: FontWeight.normal,
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
|
||||
onChanged: (TaskStatus? newStatus) {
|
||||
if (newStatus != null && newStatus != state.taskStatus) {
|
||||
context.read<TaskFormCubit>().updateTaskStatus(newStatus);
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// --- SCADENZA ---
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
@@ -47,6 +48,7 @@ import 'package:flux/features/master_data/store/data/store_repository.dart';
|
||||
import 'package:flux/features/settings/ui/settings.dart';
|
||||
import 'package:flutter_web_plugins/url_strategy.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
import 'package:universal_html/html.dart' as html;
|
||||
|
||||
String? initialRecoveryFragment;
|
||||
void main() async {
|
||||
@@ -304,21 +306,69 @@ class GlobalUpdateChecker extends StatefulWidget {
|
||||
State<GlobalUpdateChecker> createState() => _GlobalUpdateCheckerState();
|
||||
}
|
||||
|
||||
class _GlobalUpdateCheckerState extends State<GlobalUpdateChecker> {
|
||||
class _GlobalUpdateCheckerState extends State<GlobalUpdateChecker>
|
||||
with WidgetsBindingObserver {
|
||||
bool _mustUpdate = false;
|
||||
String? _updateUrl;
|
||||
StreamSubscription? _versionSubscription;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_checkVersionAndBlock();
|
||||
// 🥷 1. Registriamo questo widget per ascoltare i cicli vitali dell'app
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
_startRealtimeVersionCheck();
|
||||
}
|
||||
|
||||
Future<void> _checkVersionAndBlock() async {
|
||||
@override
|
||||
void dispose() {
|
||||
// 🥷 2. Rimuoviamo l'observer quando il widget muore (mai, in questo caso, ma è buona norma)
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
_stopRealtimeVersionCheck();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
// 🥷 3. IL VERO GESTORE DEL CICLO VITALE
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
super.didChangeAppLifecycleState(state);
|
||||
|
||||
if (state == AppLifecycleState.resumed) {
|
||||
// L'app è tornata attiva: riaccendiamo il radar
|
||||
_startRealtimeVersionCheck();
|
||||
} else if (state == AppLifecycleState.paused ||
|
||||
state == AppLifecycleState.hidden) {
|
||||
// L'app è andata in background: spegniamo il socket per evitare crash
|
||||
_stopRealtimeVersionCheck();
|
||||
}
|
||||
}
|
||||
|
||||
void _startRealtimeVersionCheck() {
|
||||
// Sicurezza: cancelliamo eventuali vecchi abbonamenti rimasti appesi
|
||||
_stopRealtimeVersionCheck();
|
||||
|
||||
// Facciamo un check immediato non appena rientriamo in app
|
||||
_checkVersion();
|
||||
|
||||
// Riapriamo il rubinetto di Supabase
|
||||
_versionSubscription = GetIt.I<SupabaseClient>()
|
||||
.from('app_config') // <-- Sostituisci col nome reale della tua tabella
|
||||
.stream(primaryKey: ['id'])
|
||||
.listen((_) {
|
||||
_checkVersion();
|
||||
});
|
||||
}
|
||||
|
||||
void _stopRealtimeVersionCheck() {
|
||||
// Chiudiamo gentilmente il socket
|
||||
_versionSubscription?.cancel();
|
||||
_versionSubscription = null;
|
||||
}
|
||||
|
||||
Future<void> _checkVersion() async {
|
||||
final updateUrl = await VersionCheckService().checkForceUpdate();
|
||||
|
||||
if (updateUrl != null && mounted) {
|
||||
// Invece di aprire un dialog, cambiamo lo stato e attiviamo lo "Scudo"
|
||||
setState(() {
|
||||
_mustUpdate = true;
|
||||
_updateUrl = updateUrl;
|
||||
@@ -328,21 +378,15 @@ class _GlobalUpdateCheckerState extends State<GlobalUpdateChecker> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// 1. Se l'app è aggiornata, mostriamo solo l'app normale
|
||||
if (!_mustUpdate) return widget.child;
|
||||
|
||||
// 2. Se l'app è vecchia, sovrapponiamo il blocco con uno Stack
|
||||
return Stack(
|
||||
children: [
|
||||
// L'app sotto continua ad esistere, ma è inaccessibile
|
||||
widget.child,
|
||||
|
||||
// IL BLOCCO INVALICABILE SOPRA A TUTTO
|
||||
Positioned.fill(
|
||||
child: Container(
|
||||
color: Colors.black.withValues(alpha: 0.85), // Sfondo oscurante
|
||||
color: Colors.black.withValues(alpha: 0.85),
|
||||
child: Center(
|
||||
// Usiamo Material per ereditare correttamente temi, font e colori
|
||||
child: Material(
|
||||
color: Colors.transparent,
|
||||
child: Container(
|
||||
@@ -386,7 +430,7 @@ class _GlobalUpdateCheckerState extends State<GlobalUpdateChecker> {
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
kIsWeb
|
||||
? "È stata rilasciata una nuova versione dell'applicazione. Ricarica la pagina per continuare."
|
||||
? "È stata rilasciata una nuova versione dell'applicazione. Ricarica la pagina per scaricare il nuovo codice."
|
||||
: "Per continuare ad utilizzare l'applicazione è necessario scaricare e installare l'ultimo aggiornamento.",
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(fontSize: 16),
|
||||
@@ -399,18 +443,14 @@ class _GlobalUpdateCheckerState extends State<GlobalUpdateChecker> {
|
||||
style: FilledButton.styleFrom(
|
||||
minimumSize: const Size(double.infinity, 50),
|
||||
),
|
||||
onPressed: () async {
|
||||
// Trick cross-platform per fare il reload
|
||||
await launchUrl(
|
||||
Uri.parse(Uri.base.toString()),
|
||||
webOnlyWindowName: '_self',
|
||||
);
|
||||
onPressed: () {
|
||||
// Hard reload aggirando la cache!
|
||||
html.window.location.reload();
|
||||
},
|
||||
)
|
||||
else
|
||||
FilledButton.icon(
|
||||
icon: const Icon(Icons.download),
|
||||
|
||||
label: const Text("SCARICA AGGIORNAMENTO"),
|
||||
style: FilledButton.styleFrom(
|
||||
minimumSize: const Size(double.infinity, 50),
|
||||
|
||||
@@ -1,27 +1,159 @@
|
||||
PODS:
|
||||
- app_links (6.4.1):
|
||||
- FlutterMacOS
|
||||
- file_picker (0.0.1):
|
||||
- FlutterMacOS
|
||||
- file_selector_macos (0.0.1):
|
||||
- FlutterMacOS
|
||||
- Firebase/CoreOnly (12.13.0):
|
||||
- FirebaseCore (~> 12.13.0)
|
||||
- Firebase/Messaging (12.13.0):
|
||||
- Firebase/CoreOnly
|
||||
- FirebaseMessaging (~> 12.13.0)
|
||||
- firebase_core (4.9.0):
|
||||
- Firebase/CoreOnly (~> 12.13.0)
|
||||
- FlutterMacOS
|
||||
- firebase_messaging (16.2.2):
|
||||
- Firebase/CoreOnly (~> 12.13.0)
|
||||
- Firebase/Messaging (~> 12.13.0)
|
||||
- firebase_core
|
||||
- FlutterMacOS
|
||||
- FirebaseCore (12.13.0):
|
||||
- FirebaseCoreInternal (~> 12.13.0)
|
||||
- GoogleUtilities/Environment (~> 8.1)
|
||||
- GoogleUtilities/Logger (~> 8.1)
|
||||
- FirebaseCoreInternal (12.13.0):
|
||||
- "GoogleUtilities/NSData+zlib (~> 8.1)"
|
||||
- FirebaseInstallations (12.13.0):
|
||||
- FirebaseCore (~> 12.13.0)
|
||||
- GoogleUtilities/Environment (~> 8.1)
|
||||
- GoogleUtilities/UserDefaults (~> 8.1)
|
||||
- PromisesObjC (~> 2.4)
|
||||
- FirebaseMessaging (12.13.0):
|
||||
- FirebaseCore (~> 12.13.0)
|
||||
- FirebaseInstallations (~> 12.13.0)
|
||||
- GoogleDataTransport (~> 10.1)
|
||||
- GoogleUtilities/AppDelegateSwizzler (~> 8.1)
|
||||
- GoogleUtilities/Environment (~> 8.1)
|
||||
- GoogleUtilities/Reachability (~> 8.1)
|
||||
- GoogleUtilities/UserDefaults (~> 8.1)
|
||||
- nanopb (~> 3.30910.0)
|
||||
- FlutterMacOS (1.0.0)
|
||||
- GoogleDataTransport (10.1.0):
|
||||
- nanopb (~> 3.30910.0)
|
||||
- PromisesObjC (~> 2.4)
|
||||
- GoogleUtilities/AppDelegateSwizzler (8.1.0):
|
||||
- GoogleUtilities/Environment
|
||||
- GoogleUtilities/Logger
|
||||
- GoogleUtilities/Network
|
||||
- GoogleUtilities/Privacy
|
||||
- GoogleUtilities/Environment (8.1.0):
|
||||
- GoogleUtilities/Privacy
|
||||
- GoogleUtilities/Logger (8.1.0):
|
||||
- GoogleUtilities/Environment
|
||||
- GoogleUtilities/Privacy
|
||||
- GoogleUtilities/Network (8.1.0):
|
||||
- GoogleUtilities/Logger
|
||||
- "GoogleUtilities/NSData+zlib"
|
||||
- GoogleUtilities/Privacy
|
||||
- GoogleUtilities/Reachability
|
||||
- "GoogleUtilities/NSData+zlib (8.1.0)":
|
||||
- GoogleUtilities/Privacy
|
||||
- GoogleUtilities/Privacy (8.1.0)
|
||||
- GoogleUtilities/Reachability (8.1.0):
|
||||
- GoogleUtilities/Logger
|
||||
- GoogleUtilities/Privacy
|
||||
- GoogleUtilities/UserDefaults (8.1.0):
|
||||
- GoogleUtilities/Logger
|
||||
- GoogleUtilities/Privacy
|
||||
- nanopb (3.30910.0):
|
||||
- nanopb/decode (= 3.30910.0)
|
||||
- nanopb/encode (= 3.30910.0)
|
||||
- nanopb/decode (3.30910.0)
|
||||
- nanopb/encode (3.30910.0)
|
||||
- package_info_plus (0.0.1):
|
||||
- FlutterMacOS
|
||||
- pdfx (1.0.0):
|
||||
- FlutterMacOS
|
||||
- printing (1.0.0):
|
||||
- FlutterMacOS
|
||||
- PromisesObjC (2.4.0)
|
||||
- shared_preferences_foundation (0.0.1):
|
||||
- Flutter
|
||||
- FlutterMacOS
|
||||
- url_launcher_macos (0.0.1):
|
||||
- FlutterMacOS
|
||||
|
||||
DEPENDENCIES:
|
||||
- app_links (from `Flutter/ephemeral/.symlinks/plugins/app_links/macos`)
|
||||
- file_picker (from `Flutter/ephemeral/.symlinks/plugins/file_picker/macos`)
|
||||
- file_selector_macos (from `Flutter/ephemeral/.symlinks/plugins/file_selector_macos/macos`)
|
||||
- firebase_core (from `Flutter/ephemeral/.symlinks/plugins/firebase_core/macos`)
|
||||
- firebase_messaging (from `Flutter/ephemeral/.symlinks/plugins/firebase_messaging/macos`)
|
||||
- FlutterMacOS (from `Flutter/ephemeral`)
|
||||
- package_info_plus (from `Flutter/ephemeral/.symlinks/plugins/package_info_plus/macos`)
|
||||
- pdfx (from `Flutter/ephemeral/.symlinks/plugins/pdfx/macos`)
|
||||
- printing (from `Flutter/ephemeral/.symlinks/plugins/printing/macos`)
|
||||
- shared_preferences_foundation (from `Flutter/ephemeral/.symlinks/plugins/shared_preferences_foundation/darwin`)
|
||||
- url_launcher_macos (from `Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos`)
|
||||
|
||||
SPEC REPOS:
|
||||
trunk:
|
||||
- Firebase
|
||||
- FirebaseCore
|
||||
- FirebaseCoreInternal
|
||||
- FirebaseInstallations
|
||||
- FirebaseMessaging
|
||||
- GoogleDataTransport
|
||||
- GoogleUtilities
|
||||
- nanopb
|
||||
- PromisesObjC
|
||||
|
||||
EXTERNAL SOURCES:
|
||||
app_links:
|
||||
:path: Flutter/ephemeral/.symlinks/plugins/app_links/macos
|
||||
file_picker:
|
||||
:path: Flutter/ephemeral/.symlinks/plugins/file_picker/macos
|
||||
file_selector_macos:
|
||||
:path: Flutter/ephemeral/.symlinks/plugins/file_selector_macos/macos
|
||||
firebase_core:
|
||||
:path: Flutter/ephemeral/.symlinks/plugins/firebase_core/macos
|
||||
firebase_messaging:
|
||||
:path: Flutter/ephemeral/.symlinks/plugins/firebase_messaging/macos
|
||||
FlutterMacOS:
|
||||
:path: Flutter/ephemeral
|
||||
package_info_plus:
|
||||
:path: Flutter/ephemeral/.symlinks/plugins/package_info_plus/macos
|
||||
pdfx:
|
||||
:path: Flutter/ephemeral/.symlinks/plugins/pdfx/macos
|
||||
printing:
|
||||
:path: Flutter/ephemeral/.symlinks/plugins/printing/macos
|
||||
shared_preferences_foundation:
|
||||
:path: Flutter/ephemeral/.symlinks/plugins/shared_preferences_foundation/darwin
|
||||
url_launcher_macos:
|
||||
:path: Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos
|
||||
|
||||
SPEC CHECKSUMS:
|
||||
app_links: 05a6ec2341985eb05e9f97dc63f5837c39895c3f
|
||||
file_picker: 7584aae6fa07a041af2b36a2655122d42f578c1a
|
||||
file_selector_macos: 9e9e068e90ebee155097d00e89ae91edb2374db7
|
||||
Firebase: 7d62445aeabdaea36f7d372f33052fed9a72514f
|
||||
firebase_core: 8a780c7f989df0ca42dcd55332fc1b203f11f848
|
||||
firebase_messaging: 3cb926039fe036f6b92834334d6e23ab33007f1f
|
||||
FirebaseCore: 58905958aa00a061397a0fd759ae4b55bddb3576
|
||||
FirebaseCoreInternal: 37bee58388fc6d183f0ab1b32d69ae44f2cf8aad
|
||||
FirebaseInstallations: 134bde50e477628ded76070efdb12d515d53f948
|
||||
FirebaseMessaging: 30564b85d2f81a96f9d312bd23acf8186ff092ae
|
||||
FlutterMacOS: d0db08ddef1a9af05a5ec4b724367152bb0500b1
|
||||
GoogleDataTransport: aae35b7ea0c09004c3797d53c8c41f66f219d6a7
|
||||
GoogleUtilities: 00c88b9a86066ef77f0da2fab05f65d7768ed8e1
|
||||
nanopb: fad817b59e0457d11a5dfbde799381cd727c1275
|
||||
package_info_plus: f0052d280d17aa382b932f399edf32507174e870
|
||||
pdfx: 1e79f57f7a6ce2f4a4c30f21fa54d3dc82441b51
|
||||
printing: c4cf83c78fd684f9bc318e6aadc18972aa48f617
|
||||
PromisesObjC: f5707f49cb48b9636751c5b2e7d227e43fba9f47
|
||||
shared_preferences_foundation: 7036424c3d8ec98dfe75ff1667cb0cd531ec82bb
|
||||
url_launcher_macos: f87a979182d112f911de6820aefddaf56ee9fbfd
|
||||
|
||||
PODFILE CHECKSUM: 54d867c82ac51cbd61b565781b9fada492027009
|
||||
|
||||
|
||||
@@ -248,6 +248,7 @@
|
||||
33CC110E2044A8840003C045 /* Bundle Framework */,
|
||||
3399D490228B24CF009A79C7 /* ShellScript */,
|
||||
E13C7961A1538309550A7824 /* [CP] Embed Pods Frameworks */,
|
||||
AC0584CA1EFD6A4D37AEE7BD /* [CP] Copy Pods Resources */,
|
||||
);
|
||||
buildRules = (
|
||||
);
|
||||
@@ -397,6 +398,23 @@
|
||||
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
|
||||
showEnvVarsInLog = 0;
|
||||
};
|
||||
AC0584CA1EFD6A4D37AEE7BD /* [CP] Copy Pods Resources */ = {
|
||||
isa = PBXShellScriptBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
);
|
||||
inputFileListPaths = (
|
||||
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-resources-${CONFIGURATION}-input-files.xcfilelist",
|
||||
);
|
||||
name = "[CP] Copy Pods Resources";
|
||||
outputFileListPaths = (
|
||||
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-resources-${CONFIGURATION}-output-files.xcfilelist",
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
shellPath = /bin/sh;
|
||||
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-resources.sh\"\n";
|
||||
showEnvVarsInLog = 0;
|
||||
};
|
||||
E13C7961A1538309550A7824 /* [CP] Embed Pods Frameworks */ = {
|
||||
isa = PBXShellScriptBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
|
||||
@@ -1,122 +0,0 @@
|
||||
{
|
||||
"pins" : [
|
||||
{
|
||||
"identity" : "abseil-cpp-binary",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/abseil-cpp-binary.git",
|
||||
"state" : {
|
||||
"revision" : "bbe8b69694d7873315fd3a4ad41efe043e1c07c5",
|
||||
"version" : "1.2024072200.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "app-check",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/app-check.git",
|
||||
"state" : {
|
||||
"revision" : "61b85103a1aeed8218f17c794687781505fbbef5",
|
||||
"version" : "11.2.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "firebase-ios-sdk",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/firebase/firebase-ios-sdk",
|
||||
"state" : {
|
||||
"revision" : "8d5b4189f1f482df8d5c58c9985ea70491ef5382",
|
||||
"version" : "12.14.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "google-ads-on-device-conversion-ios-sdk",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/googleads/google-ads-on-device-conversion-ios-sdk",
|
||||
"state" : {
|
||||
"revision" : "9bfcc6cf435b2e7c5562c1900b8680c594fa9a64",
|
||||
"version" : "3.6.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "googleappmeasurement",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/GoogleAppMeasurement.git",
|
||||
"state" : {
|
||||
"revision" : "219e564a8510e983e675c94f77f7f7c50049f22d",
|
||||
"version" : "12.14.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "googledatatransport",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/GoogleDataTransport.git",
|
||||
"state" : {
|
||||
"revision" : "617af071af9aa1d6a091d59a202910ac482128f9",
|
||||
"version" : "10.1.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "googleutilities",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/GoogleUtilities.git",
|
||||
"state" : {
|
||||
"revision" : "60da361632d0de02786f709bdc0c4df340f7613e",
|
||||
"version" : "8.1.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "grpc-binary",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/grpc-binary.git",
|
||||
"state" : {
|
||||
"revision" : "75b31c842f664a0f46a2e590a570e370249fd8f6",
|
||||
"version" : "1.69.1"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "gtm-session-fetcher",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/gtm-session-fetcher.git",
|
||||
"state" : {
|
||||
"revision" : "c0ac7575d70050c2973ba2318bd5af47f8e8153a",
|
||||
"version" : "5.3.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "interop-ios-for-google-sdks",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/interop-ios-for-google-sdks.git",
|
||||
"state" : {
|
||||
"revision" : "040d087ac2267d2ddd4cca36c757d1c6a05fdbfe",
|
||||
"version" : "101.0.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "leveldb",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/firebase/leveldb.git",
|
||||
"state" : {
|
||||
"revision" : "a0bc79961d7be727d258d33d5a6b2f1023270ba1",
|
||||
"version" : "1.22.5"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "nanopb",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/firebase/nanopb.git",
|
||||
"state" : {
|
||||
"revision" : "b7e1104502eca3a213b46303391ca4d3bc8ddec1",
|
||||
"version" : "2.30910.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "promises",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/promises.git",
|
||||
"state" : {
|
||||
"revision" : "540318ecedd63d883069ae7f1ed811a2df00b6ac",
|
||||
"version" : "2.4.0"
|
||||
}
|
||||
}
|
||||
],
|
||||
"version" : 2
|
||||
}
|
||||
@@ -1,122 +0,0 @@
|
||||
{
|
||||
"pins" : [
|
||||
{
|
||||
"identity" : "abseil-cpp-binary",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/abseil-cpp-binary.git",
|
||||
"state" : {
|
||||
"revision" : "bbe8b69694d7873315fd3a4ad41efe043e1c07c5",
|
||||
"version" : "1.2024072200.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "app-check",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/app-check.git",
|
||||
"state" : {
|
||||
"revision" : "61b85103a1aeed8218f17c794687781505fbbef5",
|
||||
"version" : "11.2.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "firebase-ios-sdk",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/firebase/firebase-ios-sdk",
|
||||
"state" : {
|
||||
"revision" : "8d5b4189f1f482df8d5c58c9985ea70491ef5382",
|
||||
"version" : "12.14.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "google-ads-on-device-conversion-ios-sdk",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/googleads/google-ads-on-device-conversion-ios-sdk",
|
||||
"state" : {
|
||||
"revision" : "9bfcc6cf435b2e7c5562c1900b8680c594fa9a64",
|
||||
"version" : "3.6.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "googleappmeasurement",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/GoogleAppMeasurement.git",
|
||||
"state" : {
|
||||
"revision" : "219e564a8510e983e675c94f77f7f7c50049f22d",
|
||||
"version" : "12.14.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "googledatatransport",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/GoogleDataTransport.git",
|
||||
"state" : {
|
||||
"revision" : "617af071af9aa1d6a091d59a202910ac482128f9",
|
||||
"version" : "10.1.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "googleutilities",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/GoogleUtilities.git",
|
||||
"state" : {
|
||||
"revision" : "60da361632d0de02786f709bdc0c4df340f7613e",
|
||||
"version" : "8.1.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "grpc-binary",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/grpc-binary.git",
|
||||
"state" : {
|
||||
"revision" : "75b31c842f664a0f46a2e590a570e370249fd8f6",
|
||||
"version" : "1.69.1"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "gtm-session-fetcher",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/gtm-session-fetcher.git",
|
||||
"state" : {
|
||||
"revision" : "c0ac7575d70050c2973ba2318bd5af47f8e8153a",
|
||||
"version" : "5.3.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "interop-ios-for-google-sdks",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/interop-ios-for-google-sdks.git",
|
||||
"state" : {
|
||||
"revision" : "040d087ac2267d2ddd4cca36c757d1c6a05fdbfe",
|
||||
"version" : "101.0.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "leveldb",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/firebase/leveldb.git",
|
||||
"state" : {
|
||||
"revision" : "a0bc79961d7be727d258d33d5a6b2f1023270ba1",
|
||||
"version" : "1.22.5"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "nanopb",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/firebase/nanopb.git",
|
||||
"state" : {
|
||||
"revision" : "b7e1104502eca3a213b46303391ca4d3bc8ddec1",
|
||||
"version" : "2.30910.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "promises",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/google/promises.git",
|
||||
"state" : {
|
||||
"revision" : "540318ecedd63d883069ae7f1ed811a2df00b6ac",
|
||||
"version" : "2.4.0"
|
||||
}
|
||||
}
|
||||
],
|
||||
"version" : 2
|
||||
}
|
||||
112
pubspec.lock
112
pubspec.lock
@@ -5,18 +5,18 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: _flutterfire_internals
|
||||
sha256: "8f89e371e2883de35cdc78f648e725fa4da5f3b6c927269f00fa68f1ea92b598"
|
||||
sha256: "0d1f0adfabbab9f46a1a80ce84a4d8b852b6e4dbf53ce413b30e0cf7d3631b71"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.3.71"
|
||||
version: "1.3.72"
|
||||
app_links:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: app_links
|
||||
sha256: "3462d9defc61565fde4944858b59bec5be2b9d5b05f20aed190adb3ad08a7abc"
|
||||
sha256: a350a5b37579b7227aaf9a59c07114617cd4283852e193f743b2b3d2d7483c18
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "7.0.0"
|
||||
version: "7.1.1"
|
||||
app_links_linux:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -105,6 +105,14 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.4.1"
|
||||
charcode:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: charcode
|
||||
sha256: fb0f1107cac15a5ea6ef0a6ef71a807b9e4267c713bb93e00e92d737cc8dbd8a
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.4.0"
|
||||
checked_yaml:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -133,10 +141,10 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: code_assets
|
||||
sha256: dad6bf6b9f4f378b0a69edbf42584d336efd1a9ce15deb1ba591cbb1b5ff440f
|
||||
sha256: bf394f466ba9205f1812a0433b392d6af280f155f56651eda7c18cc32ed493b8
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.1.0"
|
||||
version: "1.2.1"
|
||||
collection:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
@@ -169,6 +177,14 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "3.0.7"
|
||||
csslib:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: csslib
|
||||
sha256: "09bad715f418841f976c77db72d5398dc1253c21fb9c0c7f0b0b985860b2d58e"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.0.2"
|
||||
dart_jsonwebtoken:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -181,10 +197,10 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: dbus
|
||||
sha256: d0c98dcd4f5169878b6cf8f6e0a52403a9dff371a3e2f019697accbf6f44a270
|
||||
sha256: "0ce9b0a839e6dee59a37a623d2fc26a35bbbe6404213e419b0d6411023d62645"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.7.12"
|
||||
version: "0.7.14"
|
||||
equatable:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
@@ -269,10 +285,10 @@ packages:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: firebase_core
|
||||
sha256: "93a5bde9775fd5adcc937f39dfa04ae0bc89c4d79bea6abc49de3f7b049d9ff6"
|
||||
sha256: ec46a100a560d3bd5f97f2d89ba7492cb09b6dd0a4a28753d1258f360d6bd9f9
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "4.9.0"
|
||||
version: "4.10.0"
|
||||
firebase_core_platform_interface:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -285,34 +301,34 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: firebase_core_web
|
||||
sha256: "7c98f10b8c8e5adedc0b810b66a877120696675e2c22d9ca9caca092da0d9e57"
|
||||
sha256: "5ad1be848692ec148f2d6a8ad2a3838cb852ea5f3c9e6479a7afce479e1854f8"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "3.7.0"
|
||||
version: "3.8.0"
|
||||
firebase_messaging:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: firebase_messaging
|
||||
sha256: "8d0dc81a31cd030170508dc3e89bfd14355b20a1b991340af5f018e37daab5d7"
|
||||
sha256: "9651e833454156b9f0927eaedccffba0f7f6a6e20ceddef82517211e7017e9c4"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "16.2.2"
|
||||
version: "16.3.0"
|
||||
firebase_messaging_platform_interface:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: firebase_messaging_platform_interface
|
||||
sha256: "37abb0b0535c5497605ee94c12470e1ebbbe47e71a22d0c20bffcc912311f8cb"
|
||||
sha256: "292bb5dc9c4a429078895406c347d7c7690deb858c1adeed8f4b4346f769dfa3"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "4.7.11"
|
||||
version: "4.8.0"
|
||||
firebase_messaging_web:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: firebase_messaging_web
|
||||
sha256: "54e22b43e2c26a2728a3f68c188de0f9011993ae19ae959a06d476dad935c776"
|
||||
sha256: "08c565bc83729439f5de2dfea77b4832002b705eb2f840366cefa80e4e5c3c66"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "4.1.7"
|
||||
version: "4.2.0"
|
||||
fixnum:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -367,10 +383,10 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: flutter_plugin_android_lifecycle
|
||||
sha256: "38d1c268de9097ff59cf0e844ac38759fc78f76836d37edad06fa21e182055a0"
|
||||
sha256: "3854fe5e3bff0b113c658f260b90c95dea17c92db0f2addeac2e343dd9969785"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.0.34"
|
||||
version: "2.0.35"
|
||||
flutter_staggered_grid_view:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
@@ -425,10 +441,10 @@ packages:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: go_router
|
||||
sha256: "92d8cee7c57dff0a6c409c05597b460002434eccf7424a712283225b3962d03f"
|
||||
sha256: "5922b2861e2235a3504896f0d6fa07d84141b480cf52eecd2f42cd25585a9e8a"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "17.2.3"
|
||||
version: "17.3.0"
|
||||
google_fonts:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
@@ -457,10 +473,18 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: hooks
|
||||
sha256: a41af4e8fc687cd6d33de9751eb936c8c0204ebe2bcb6c15ecf707504bf47f31
|
||||
sha256: "9a62a50b50b769a737bc0a8ff381f333529df3ab746b2f6b02e83760231455ba"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.0.0"
|
||||
version: "2.0.2"
|
||||
html:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: html
|
||||
sha256: "6d1264f2dffa1b1101c25a91dff0dc2daee4c18e87cd8538729773c073dbf602"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.15.6"
|
||||
http:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -497,10 +521,10 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: image_picker_android
|
||||
sha256: d5b3e1774af29c9ab00103afb0d4614070f924d2e0057ac867ec98800114793f
|
||||
sha256: "6f3a1995eafb000333174fae92202622033b0ee7fd917a6cd3730295264df84a"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.8.13+17"
|
||||
version: "0.8.13+19"
|
||||
image_picker_for_web:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -802,10 +826,10 @@ packages:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: permission_handler
|
||||
sha256: bc917da36261b00137bbc8896bf1482169cd76f866282368948f032c8c1caae1
|
||||
sha256: fe54465bcc62a4564c6e4db337bbaded6c0c0fa6e10487414436d163114784f6
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "12.0.1"
|
||||
version: "12.0.3"
|
||||
permission_handler_android:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -818,10 +842,10 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: permission_handler_apple
|
||||
sha256: f000131e755c54cf4d84a5d8bd6e4149e262cc31c5a8b1d698de1ac85fa41023
|
||||
sha256: e20daf680eef1ca62ffe8c8c526b778cc386d50137c77ac71c8ec9c88c13fb9d
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "9.4.7"
|
||||
version: "9.4.9"
|
||||
permission_handler_html:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -986,10 +1010,10 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: shared_preferences_android
|
||||
sha256: e8d4762b1e2e8578fc4d0fd548cebf24afd24f49719c08974df92834565e2c53
|
||||
sha256: a2c49fc1fed7140cadd892d765bd47edbe4ac0b9c7e7e3c493dcb58126f99cf0
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.4.23"
|
||||
version: "2.4.25"
|
||||
shared_preferences_foundation:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -1095,10 +1119,10 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: synchronized
|
||||
sha256: "63896c27e81b28f8cb4e69ead0d3e8f03f1d1e5fc531a3e579cabed6a2c7c9e5"
|
||||
sha256: "93b153dcb6a26dcddee6ca087dd634b53e38c10b5aa163e8e49501a776456153"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "3.4.0+1"
|
||||
version: "3.4.1"
|
||||
term_glyph:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -1131,6 +1155,14 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.0.0"
|
||||
universal_html:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: universal_html
|
||||
sha256: c0bcae5c733c60f26c7dfc88b10b0fd27cbcc45cb7492311cdaa6067e21c9cd4
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.3.0"
|
||||
universal_io:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
@@ -1159,10 +1191,10 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: url_launcher_android
|
||||
sha256: "17bc677f0b301615530dd1d67e0a9828cafa2d0b6b6eae4cd3679b7eac4a273c"
|
||||
sha256: b413d49b73867ac08dd2f9890efd3cc11f2a0e577618d50843440a1fb3776c32
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "6.3.30"
|
||||
version: "6.3.32"
|
||||
url_launcher_ios:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -1239,10 +1271,10 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: vector_graphics_compiler
|
||||
sha256: b9b3f391857781aa96acacef96066f2f49b4cd03cf9fce3ca4d8da2ef5ea129e
|
||||
sha256: "7ee12e6dffe0fc8e755179d6d91b3b34f5924223fc104d85572ef9180d73d172"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.2.3"
|
||||
version: "1.2.5"
|
||||
vector_math:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -1324,5 +1356,5 @@ packages:
|
||||
source: hosted
|
||||
version: "2.1.0"
|
||||
sdks:
|
||||
dart: ">=3.11.3 <4.0.0"
|
||||
flutter: ">=3.38.4"
|
||||
dart: ">=3.12.0 <4.0.0"
|
||||
flutter: ">=3.44.0"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
name: flux
|
||||
description: "Gestione attività negozio di telefonia"
|
||||
publish_to: 'none'
|
||||
version: 1.1.14+32
|
||||
version: 1.1.22+40
|
||||
|
||||
environment:
|
||||
sdk: ^3.11.3
|
||||
@@ -41,6 +41,7 @@ dependencies:
|
||||
flutter_staggered_grid_view: ^0.7.0
|
||||
firebase_core: ^4.9.0
|
||||
firebase_messaging: ^16.2.2
|
||||
universal_html: ^2.3.0
|
||||
|
||||
dependency_overrides:
|
||||
pdfx:
|
||||
@@ -55,6 +56,8 @@ dev_dependencies:
|
||||
flutter_lints: ^6.0.0
|
||||
|
||||
flutter:
|
||||
config:
|
||||
enable-swift-package-manager: false
|
||||
uses-material-design: true
|
||||
generate: true
|
||||
|
||||
|
||||
136
supabase/functions/id_doc_manager/index.ts
Normal file
136
supabase/functions/id_doc_manager/index.ts
Normal file
@@ -0,0 +1,136 @@
|
||||
import { serve } from "https://deno.land/std@0.168.0/http/server.ts"
|
||||
import { createClient } from "https://esm.sh/@supabase/supabase-js@2"
|
||||
|
||||
const corsHeaders = {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Headers': 'authorization, x-client-info, apiKey, content-type',
|
||||
}
|
||||
|
||||
serve(async (req) => {
|
||||
if (req.method === 'OPTIONS') return new Response('ok', { headers: corsHeaders })
|
||||
|
||||
try {
|
||||
const payload = await req.json()
|
||||
const { table, type, record } = payload
|
||||
|
||||
// 🥷 1. FILTRO EVENTI: Agiamo solo sui nuovi allegati
|
||||
if (table !== 'attachments' || type !== 'INSERT') {
|
||||
return new Response(JSON.stringify({ message: "Ignorato: non è un INSERT su attachments." }), { status: 200, headers: corsHeaders })
|
||||
}
|
||||
|
||||
// Se l'allegato ha GIÀ un customer_id, l'operatore l'ha caricato direttamente
|
||||
// dalla scheda cliente. Non serve sprecare soldi con l'AI.
|
||||
if (record.customer_id) {
|
||||
return new Response(JSON.stringify({ message: "Customer già presente, analisi saltata." }), { status: 200, headers: corsHeaders })
|
||||
}
|
||||
|
||||
// Se non è collegato a un ticket, a un'operazione o a una nota, non abbiamo
|
||||
// modo di risalire al proprietario, quindi fermiamo tutto.
|
||||
if (!record.operation_id && !record.ticket_id && !record.note_id) {
|
||||
return new Response(JSON.stringify({ message: "Nessuna entità collegata da cui estrarre il customer." }), { status: 200, headers: corsHeaders })
|
||||
}
|
||||
|
||||
// 🥷 2. FILTRO FORMATI: Passiamo a OpenAI solo immagini vere (niente zip o pdf per ora)
|
||||
// Nota: OpenAI supporta anche i PDF in vision, ma le immagini sono più sicure/economiche per i documenti.
|
||||
const validExtensions = ['jpg', 'jpeg', 'png', 'webp']
|
||||
const ext = record.extension?.replace('.', '').toLowerCase()
|
||||
|
||||
if (!validExtensions.includes(ext)) {
|
||||
return new Response(JSON.stringify({ message: "Formato non supportato da Vision." }), { status: 200, headers: corsHeaders })
|
||||
}
|
||||
|
||||
const supabase = createClient(
|
||||
Deno.env.get('SUPABASE_URL') ?? '',
|
||||
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') ?? ''
|
||||
)
|
||||
|
||||
// 🥷 3. GENERAZIONE URL SICURO
|
||||
// Dal momento che il tuo storage bucket è (spero) privato, dobbiamo creare un URL
|
||||
// temporaneo firmato da passare a OpenAI, valido solo per i prossimi 60 secondi.
|
||||
const { data: urlData, error: urlError } = await supabase.storage
|
||||
.from('documents')
|
||||
.createSignedUrl(record.storage_path, 60)
|
||||
|
||||
if (urlError || !urlData) throw new Error(`Errore generazione URL: ${urlError?.message}`)
|
||||
|
||||
const imageUrl = urlData.signedUrl
|
||||
|
||||
// 🥷 4. L'INTERROGAZIONE ALL'AI (Il cuore del sistema)
|
||||
console.log(`Analizzo immagine ${record.id} con OpenAI...`)
|
||||
|
||||
const openAiResponse = await fetch('https://api.openai.com/v1/chat/completions', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${Deno.env.get('OPENAI_API_KEY')}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: "gpt-4o-mini", // Il modello super veloce ed economico
|
||||
response_format: { type: "json_object" }, // Vogliamo solo codice, niente chiacchiere
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: "Questa immagine è una foto o una scansione di un documento d'identità personale valido (es. carta d'identità, patente di guida, passaporto, tessera sanitaria, codice fiscale)? Rispondi ESCLUSIVAMENTE con un JSON in questo formato: { \"is_id\": true } oppure { \"is_id\": false }"
|
||||
},
|
||||
{
|
||||
type: "image_url",
|
||||
image_url: { "url": imageUrl }
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
})
|
||||
})
|
||||
|
||||
const aiData = await openAiResponse.json()
|
||||
const resultText = aiData.choices[0].message.content
|
||||
const result = JSON.parse(resultText)
|
||||
|
||||
// 🥷 5. LA LOGICA DI SMISTAMENTO
|
||||
if (result.is_id === true) {
|
||||
console.log(`🎯 Documento rilevato! Cerco il padrone...`)
|
||||
|
||||
let targetCustomerId = null
|
||||
|
||||
// Andiamo a pescare il customer_id esplorando l'albero delle relazioni
|
||||
if (record.operation_id) {
|
||||
const { data } = await supabase.from('operations').select('customer_id').eq('id', record.operation_id).single()
|
||||
targetCustomerId = data?.customer_id
|
||||
} else if (record.ticket_id) {
|
||||
const { data } = await supabase.from('tickets').select('customer_id').eq('id', record.ticket_id).single()
|
||||
targetCustomerId = data?.customer_id
|
||||
} else if (record.note_id) {
|
||||
// Se le note hanno una FK verso i clienti:
|
||||
const { data } = await supabase.from('notes').select('customer_id').eq('id', record.note_id).single()
|
||||
targetCustomerId = data?.customer_id
|
||||
}
|
||||
|
||||
// Se abbiamo trovato il proprietario, facciamo l'update dell'allegato!
|
||||
if (targetCustomerId) {
|
||||
await supabase
|
||||
.from('attachments')
|
||||
.update({ customer_id: targetCustomerId })
|
||||
.eq('id', record.id)
|
||||
|
||||
console.log(`✅ Allegato aggiornato. Legato al cliente: ${targetCustomerId}`)
|
||||
} else {
|
||||
console.log(`⚠️ Documento rilevato, ma non c'è nessun cliente legato all'entità genitore.`)
|
||||
}
|
||||
} else {
|
||||
console.log(`L'immagine non è un documento (è uno scontrino, una foto di un router, ecc.).`)
|
||||
}
|
||||
|
||||
return new Response(JSON.stringify({ success: true, is_id: result.is_id }), {
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' }, status: 200
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
console.error("ERRORE FATALE NELLA FUNZIONE:", error)
|
||||
return new Response(JSON.stringify({ error: error.toString() }), {
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' }, status: 500
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -11,173 +11,186 @@ serve(async (req) => {
|
||||
if (req.method === 'OPTIONS') return new Response('ok', { headers: corsHeaders })
|
||||
|
||||
try {
|
||||
const bodyText = await req.text();
|
||||
const payload = JSON.parse(bodyText);
|
||||
const bodyText = await req.text()
|
||||
const payload = JSON.parse(bodyText)
|
||||
|
||||
// Estraggo i dati dal payload standard di Supabase
|
||||
const tableName = payload.table;
|
||||
const record = payload.record;
|
||||
const tableName = payload.table
|
||||
const eventType = payload.type
|
||||
const record = payload.record
|
||||
const old_record = payload.old_record
|
||||
|
||||
if (!tableName || !record) {
|
||||
throw new Error("Payload non valido, manca table o record.");
|
||||
throw new Error("Payload non valido, manca table o record.")
|
||||
}
|
||||
|
||||
let event_type = '';
|
||||
let target_staff_id = '';
|
||||
let title = '';
|
||||
let description = '';
|
||||
let reference_id = '';
|
||||
|
||||
// Inizializziamo il client Supabase subito, ci serve per le query
|
||||
const supabaseClient = createClient(
|
||||
Deno.env.get('SUPABASE_URL') ?? '',
|
||||
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') ?? ''
|
||||
)
|
||||
|
||||
// SMISTAMENTO IN BASE ALLA TABELLA
|
||||
if (tableName === 'task_assignments') {
|
||||
event_type = 'task_assigned';
|
||||
target_staff_id = record.staff_id;
|
||||
reference_id = record.task_id;
|
||||
title = 'Nuovo Task Assegnato';
|
||||
// =========================================================================
|
||||
// 🥷 1. IDENTIFICARE ESTRARRE I BERSAGLI (CHI DEVO NOTIFICARE?)
|
||||
// =========================================================================
|
||||
let usersToNotify: string[] = []
|
||||
let notificationTitle = ''
|
||||
let notificationBody = ''
|
||||
let referenceId = ''
|
||||
let fluxEventType = '' // 'task_assigned', 'task_completed', etc.
|
||||
|
||||
// 1. Peschiamo i dettagli completi del task
|
||||
const { data: taskData } = await supabaseClient
|
||||
.from('tasks')
|
||||
.select('*')
|
||||
.eq('id', reference_id)
|
||||
.single();
|
||||
// SCENARIO A: ASSEGNAZIONE TASK
|
||||
if (tableName === 'task_assignments' && eventType === 'INSERT') {
|
||||
const assigneeId = record.staff_id
|
||||
const assignerId = record.assigned_by_id
|
||||
referenceId = record.task_id
|
||||
fluxEventType = 'task_assigned'
|
||||
|
||||
// 2. Peschiamo il nome del creatore
|
||||
let creatorName = "Admin";
|
||||
if (taskData?.created_by_id) {
|
||||
const { data: creatorData } = await supabaseClient
|
||||
.from('staff_members')
|
||||
.select('first_name, last_name')
|
||||
.eq('id', taskData.created_by_id)
|
||||
.single();
|
||||
|
||||
if (creatorData) {
|
||||
creatorName = `${creatorData.first_name} ${creatorData.last_name}`.trim();
|
||||
}
|
||||
if (assigneeId === assignerId) {
|
||||
return new Response(JSON.stringify({ message: "Auto-assegnazione ignorata." }), { status: 200, headers: corsHeaders })
|
||||
}
|
||||
|
||||
// 3. Formattiamo la data (se esiste)
|
||||
let dueDateStr = 'Nessuna scadenza';
|
||||
usersToNotify.push(assigneeId)
|
||||
|
||||
// Costruiamo i testi
|
||||
const { data: taskData } = await supabaseClient.from('tasks').select('title, description, due_date').eq('id', referenceId).single()
|
||||
const { data: assignerData } = await supabaseClient.from('staff_members').select('first_name, last_name').eq('id', assignerId).single()
|
||||
|
||||
const taskTitle = taskData?.title || 'Senza titolo'
|
||||
const taskDesc = taskData?.description || 'Nessuna descrizione'
|
||||
const assignerName = assignerData ? `${assignerData.first_name} ${assignerData.last_name}`.trim() : 'Un collega'
|
||||
|
||||
let dueDateStr = 'Nessuna scadenza'
|
||||
if (taskData?.due_date) {
|
||||
const d = new Date(taskData.due_date);
|
||||
dueDateStr = d.toLocaleDateString('it-IT');
|
||||
dueDateStr = new Date(taskData.due_date).toLocaleDateString('it-IT')
|
||||
}
|
||||
|
||||
// 4. Costruiamo il Body multilinea per Android
|
||||
const taskTitle = taskData?.title || 'Senza titolo';
|
||||
const taskDesc = taskData?.description || 'Nessuna descrizione fornita.';
|
||||
|
||||
description = `${taskTitle}\n\nCreato da: ${creatorName}\nScadenza: ${dueDateStr}\nDettagli: ${taskDesc}`;
|
||||
notificationTitle = 'Nuovo Task Assegnato'
|
||||
notificationBody = `${taskTitle}\n\nCreato da: ${assignerName}\nScadenza: ${dueDateStr}\nDettagli: ${taskDesc}`
|
||||
}
|
||||
|
||||
// 1. Leggiamo le preferenze specifiche di questo dipendente
|
||||
const { data: settings, error: settingsError } = await supabaseClient
|
||||
.from('staff_notification_settings')
|
||||
.select('*')
|
||||
.eq('staff_id', target_staff_id)
|
||||
.single()
|
||||
// SCENARIO B: COMPLETAMENTO TASK
|
||||
else if (tableName === 'tasks' && eventType === 'UPDATE') {
|
||||
const justCompleted = record.status === 'completed' && old_record.status !== 'completed';
|
||||
|
||||
if (settingsError || !settings) throw new Error('Preferenze utente non trovate')
|
||||
|
||||
// 2. Determiniamo QUALI canali usare in base all'evento e agli switch dell'utente
|
||||
let sendPush = false
|
||||
let sendEmail = false
|
||||
|
||||
switch (event_type) {
|
||||
case 'task_assigned':
|
||||
sendPush = settings.task_assigned_push
|
||||
sendEmail = settings.task_assigned_email
|
||||
break
|
||||
case 'note_invited':
|
||||
sendPush = settings.note_invited_push
|
||||
sendEmail = settings.note_invited_email
|
||||
break
|
||||
case 'new_operation':
|
||||
sendPush = settings.new_operation_push
|
||||
sendEmail = settings.new_operation_email
|
||||
break
|
||||
case 'new_ticket':
|
||||
sendPush = settings.new_ticket_push
|
||||
sendEmail = settings.new_ticket_email
|
||||
break
|
||||
default:
|
||||
throw new Error('Tipo evento non riconosciuto')
|
||||
if (!justCompleted) {
|
||||
return new Response(JSON.stringify({ message: "Update ignorato (non è un completamento)." }), { status: 200, headers: corsHeaders })
|
||||
}
|
||||
|
||||
// Se l'utente ha spento tutto, interrompiamo subito risparmiando risorse
|
||||
if (!sendPush && !sendEmail) {
|
||||
return new Response(JSON.stringify({ message: 'L\'utente ha disattivato le notifiche per questo evento.' }), {
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' }, status: 200
|
||||
})
|
||||
const completerId = record.updated_by_id
|
||||
referenceId = record.id
|
||||
fluxEventType = 'task_completed' // Nota: assicurati di avere questa colonna o un fallback nelle preferenze
|
||||
|
||||
const { data: assignments } = await supabaseClient.from('task_assignments').select('staff_id').eq('task_id', referenceId)
|
||||
|
||||
if (assignments && assignments.length > 0) {
|
||||
usersToNotify = assignments.map(a => a.staff_id).filter(id => id !== completerId)
|
||||
}
|
||||
|
||||
// Se arriviamo qui, dobbiamo inviare qualcosa. Prepariamo i dati dell'utente.
|
||||
const { data: staffMember } = await supabaseClient
|
||||
.from('staff_members')
|
||||
.select('email, first_name')
|
||||
.eq('id', target_staff_id)
|
||||
.single()
|
||||
if (usersToNotify.length === 0) {
|
||||
return new Response(JSON.stringify({ message: "Nessun altro assegnatario da notificare per la chiusura." }), { status: 200, headers: corsHeaders })
|
||||
}
|
||||
|
||||
// 3. LOGICA PUSH (FCM)
|
||||
if (sendPush) {
|
||||
const firebaseSecret = Deno.env.get('FIREBASE_SERVICE_ACCOUNT');
|
||||
const { data: completerData } = await supabaseClient.from('staff_members').select('first_name, last_name').eq('id', completerId).single()
|
||||
const completerName = completerData ? `${completerData.first_name} ${completerData.last_name}`.trim() : 'Un collega'
|
||||
const taskTitle = record.title || 'Senza titolo'
|
||||
|
||||
if (!firebaseSecret) {
|
||||
console.error("ERRORE: Secret FIREBASE_SERVICE_ACCOUNT mancante nel progetto!");
|
||||
} else {
|
||||
const credentials = JSON.parse(firebaseSecret);
|
||||
notificationTitle = 'Task Completato ✅'
|
||||
notificationBody = `${completerName} ha appena chiuso il task: ${taskTitle}`
|
||||
}
|
||||
|
||||
// SCENARIO C: ALTRI EVENTI (Es. note_invited, ecc. Mettili qui quando ti serviranno)
|
||||
else {
|
||||
return new Response(JSON.stringify({ message: "Tabella o evento non gestito." }), { status: 200, headers: corsHeaders })
|
||||
}
|
||||
|
||||
|
||||
// =========================================================================
|
||||
// 🥷 2. MOTORE DI INVIO MASSIVO PER I BERSAGLI IDENTIFICATI
|
||||
// =========================================================================
|
||||
|
||||
// Inizializziamo FCM una volta sola per risparmiare tempo se ci sono push da mandare
|
||||
const firebaseSecret = Deno.env.get('FIREBASE_SERVICE_ACCOUNT')
|
||||
let fcmAccessToken = ''
|
||||
let fcmProjectId = ''
|
||||
|
||||
if (firebaseSecret) {
|
||||
const credentials = JSON.parse(firebaseSecret)
|
||||
fcmProjectId = credentials.project_id
|
||||
const jwtClient = new JWT({
|
||||
email: credentials.client_email,
|
||||
key: credentials.private_key,
|
||||
scopes: ['https://www.googleapis.com/auth/firebase.messaging'],
|
||||
});
|
||||
const fcmAccessToken = (await jwtClient.getAccessToken()).token;
|
||||
})
|
||||
fcmAccessToken = (await jwtClient.getAccessToken()).token ?? ''
|
||||
}
|
||||
|
||||
const { data: devices } = await supabaseClient
|
||||
.from('staff_devices')
|
||||
.select('fcm_token')
|
||||
.eq('staff_id', target_staff_id);
|
||||
const resendApiKey = Deno.env.get('RESEND_API_KEY')
|
||||
|
||||
if (devices && devices.length > 0) {
|
||||
let pushSentCount = 0;
|
||||
let emailSentCount = 0;
|
||||
|
||||
// Cicliamo tutti gli utenti che meritano la notifica
|
||||
for (const targetStaffId of usersToNotify) {
|
||||
|
||||
// A) Leggiamo le preferenze dello specifico utente
|
||||
const { data: settings } = await supabaseClient
|
||||
.from('staff_notification_settings')
|
||||
.select('*')
|
||||
.eq('staff_id', targetStaffId)
|
||||
.single()
|
||||
|
||||
if (!settings) continue; // Salta se non ha le preferenze
|
||||
|
||||
let sendPush = false
|
||||
let sendEmail = false
|
||||
|
||||
// B) Traduciamo l'evento nelle sue preferenze
|
||||
// (Se aggiungi 'task_completed' al DB settings, mettilo qui. Per ora riuso le preesistenti se manca)
|
||||
switch (fluxEventType) {
|
||||
case 'task_assigned':
|
||||
sendPush = settings.task_assigned_push
|
||||
sendEmail = settings.task_assigned_email
|
||||
break
|
||||
case 'task_completed':
|
||||
// Se nel DB hai aggiunto task_completed_push usa quello.
|
||||
// Altrimenti puoi fare fallback su task_assigned_push per testare.
|
||||
sendPush = settings.task_assigned_push
|
||||
sendEmail = settings.task_assigned_email
|
||||
break
|
||||
// Aggiungi qui gli altri case (note_invited, new_operation)
|
||||
}
|
||||
|
||||
if (!sendPush && !sendEmail) continue; // Questo utente non vuole essere disturbato
|
||||
|
||||
// Recuperiamo nome ed email per questo specifico utente
|
||||
const { data: staffMember } = await supabaseClient.from('staff_members').select('email, first_name').eq('id', targetStaffId).single()
|
||||
|
||||
// C) INVIO PUSH (FCM)
|
||||
if (sendPush && fcmAccessToken) {
|
||||
const { data: devices } = await supabaseClient.from('staff_devices').select('fcm_token').eq('staff_id', targetStaffId)
|
||||
|
||||
if (devices) {
|
||||
for (const device of devices) {
|
||||
try {
|
||||
const res = await fetch(`https://fcm.googleapis.com/v1/projects/${credentials.project_id}/messages:send`, {
|
||||
const res = await fetch(`https://fcm.googleapis.com/v1/projects/${fcmProjectId}/messages:send`, {
|
||||
method: 'POST',
|
||||
headers: { 'Authorization': `Bearer ${fcmAccessToken}`, 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
message: {
|
||||
token: device.fcm_token,
|
||||
notification: { title, body: description },
|
||||
data: { click_action: 'FLUTTER_NOTIFICATION_CLICK', eventType: event_type, referenceId: reference_id },
|
||||
notification: { title: notificationTitle, body: notificationBody },
|
||||
data: { click_action: 'FLUTTER_NOTIFICATION_CLICK', eventType: fluxEventType, referenceId: referenceId },
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
// QUI È DOVE CATTURIAMO LA RISPOSTA DI GOOGLE
|
||||
const fcmResponseData = await res.json();
|
||||
|
||||
if (!res.ok) {
|
||||
console.error("FCM HA RIFIUTATO LA NOTIFICA:", fcmResponseData);
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
console.error('Errore di rete durante invio Push:', err);
|
||||
}
|
||||
}
|
||||
if (res.ok) pushSentCount++;
|
||||
else console.error("FCM HA RIFIUTATO LA NOTIFICA:", await res.json());
|
||||
} catch (err) { console.error('Errore rete FCM:', err) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. LOGICA EMAIL (Resend)
|
||||
if (sendEmail && staffMember?.email) {
|
||||
const resendApiKey = Deno.env.get('RESEND_API_KEY')
|
||||
if (resendApiKey) {
|
||||
// D) INVIO EMAIL (Resend)
|
||||
if (sendEmail && resendApiKey && staffMember?.email) {
|
||||
try {
|
||||
await fetch('https://api.resend.com/emails', {
|
||||
method: 'POST',
|
||||
@@ -185,20 +198,24 @@ serve(async (req) => {
|
||||
body: JSON.stringify({
|
||||
from: 'FLUX Notifiche <onboarding@resend.dev>',
|
||||
to: staffMember.email,
|
||||
subject: title,
|
||||
html: `<p>Ciao ${staffMember.first_name},</p><p>${description}</p><p><br>Il team FLUX</p>`,
|
||||
subject: notificationTitle,
|
||||
html: `<p>Ciao ${staffMember.first_name},</p><p>${notificationBody.replace(/\n/g, '<br>')}</p><p><br>Il team FLUX</p>`,
|
||||
}),
|
||||
})
|
||||
emailSentCount++;
|
||||
} catch (err) { console.error('Errore invio Email:', err) }
|
||||
}
|
||||
}
|
||||
|
||||
return new Response(JSON.stringify({ success: true, push_sent: sendPush, email_sent: sendEmail }), {
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' }, status: 200
|
||||
})
|
||||
return new Response(JSON.stringify({
|
||||
success: true,
|
||||
targets_found: usersToNotify.length,
|
||||
push_sent: pushSentCount,
|
||||
email_sent: emailSentCount
|
||||
}), { headers: { ...corsHeaders, 'Content-Type': 'application/json' }, status: 200 })
|
||||
|
||||
} catch (error) {
|
||||
console.error("ERRORE FATALE NELLA FUNZIONE:", error);
|
||||
console.error("ERRORE FATALE NELLA FUNZIONE:", error)
|
||||
return new Response(JSON.stringify({ error: error.message }), {
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' }, status: 500
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user