mah....volare
This commit is contained in:
@@ -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(
|
||||
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,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: OperationListStatus.failure,
|
||||
errorMessage: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void updateFilters({String? text, DateTimeRange? range}) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
query: query ?? state.query,
|
||||
dateRange: range ?? state.dateRange,
|
||||
// 🥷 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,
|
||||
),
|
||||
);
|
||||
loadOperations(refresh: true);
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user