feat: service set repos and cubit
This commit is contained in:
111
lib/features/services/blocs/services_cubit.dart
Normal file
111
lib/features/services/blocs/services_cubit.dart
Normal file
@@ -0,0 +1,111 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flux/features/services/data/services_repository.dart';
|
||||
import 'package:flux/features/services/models/service_model.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
|
||||
class ServicesState extends Equatable {
|
||||
final List<ServiceModel> allServices;
|
||||
final bool isLoading;
|
||||
final bool hasReachedMax; // Per lo scroll infinito
|
||||
final String? errorMessage;
|
||||
// Parametri di ricerca
|
||||
final String query;
|
||||
final DateTimeRange? dateRange;
|
||||
|
||||
const ServicesState({
|
||||
this.allServices = const [],
|
||||
this.isLoading = false,
|
||||
this.hasReachedMax = false,
|
||||
this.errorMessage,
|
||||
this.query = '',
|
||||
this.dateRange,
|
||||
});
|
||||
ServicesState copyWith({
|
||||
List<ServiceModel>? allServices,
|
||||
bool? isLoading,
|
||||
String? errorMessage,
|
||||
bool? hasReachedMax,
|
||||
String? query,
|
||||
DateTimeRange? dateRange,
|
||||
}) {
|
||||
return ServicesState(
|
||||
allServices: allServices ?? this.allServices,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
errorMessage:
|
||||
errorMessage, // Se non lo passiamo, torna null (pulisce l'errore)
|
||||
hasReachedMax: hasReachedMax ?? this.hasReachedMax,
|
||||
query: query ?? this.query,
|
||||
dateRange: dateRange ?? this.dateRange,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
allServices,
|
||||
isLoading,
|
||||
hasReachedMax,
|
||||
errorMessage,
|
||||
query,
|
||||
dateRange,
|
||||
];
|
||||
}
|
||||
|
||||
class ServicesCubit extends Cubit<ServicesState> {
|
||||
final ServicesRepository _repository = GetIt.I<ServicesRepository>();
|
||||
|
||||
ServicesCubit() : super(const ServicesState());
|
||||
|
||||
// Carica tutto il pacchetto
|
||||
Future<void> loadServices({bool refresh = false}) async {
|
||||
if (state.isLoading) return;
|
||||
|
||||
// Se facciamo refresh, resettiamo tutto
|
||||
final currentOffset = refresh ? 0 : state.allServices.length;
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
isLoading: true,
|
||||
allServices: refresh ? [] : state.allServices,
|
||||
hasReachedMax: refresh ? false : state.hasReachedMax,
|
||||
),
|
||||
);
|
||||
|
||||
try {
|
||||
final newServices = await _repository.fetchServices(
|
||||
offset: currentOffset,
|
||||
searchTerm: state.query,
|
||||
dateRange: state.dateRange,
|
||||
);
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
isLoading: false,
|
||||
allServices: List.from(state.allServices)..addAll(newServices),
|
||||
hasReachedMax:
|
||||
newServices.length <
|
||||
50, // Se ne arrivano meno di 50, siamo alla fine
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(state.copyWith(isLoading: false, errorMessage: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
void updateFilters({String? query, DateTimeRange? range}) {
|
||||
emit(state.copyWith(query: query, dateRange: range));
|
||||
loadServices(refresh: true); // Applica i filtri e riparte da zero
|
||||
}
|
||||
|
||||
// Salva e ricarica
|
||||
Future<void> addService(ServiceModel service) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
try {
|
||||
await _repository.saveFullService(service);
|
||||
await loadServices(); // Ricarichiamo la lista aggiornata
|
||||
} catch (e) {
|
||||
emit(state.copyWith(isLoading: false, errorMessage: e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user