latest store services
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:equatable/equatable.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';
|
||||
|
||||
part 'latest_store_services_events.dart';
|
||||
part 'latest_store_services_state.dart';
|
||||
|
||||
class LatestStoreServicesBloc
|
||||
extends Bloc<LatestStoreServicesEvent, LatestStoreServicesState> {
|
||||
final _repository = GetIt.I.get<ServicesRepository>();
|
||||
|
||||
LatestStoreServicesBloc()
|
||||
: super(
|
||||
const LatestStoreServicesState(
|
||||
status: LatestStoreServicesStatus.initial,
|
||||
),
|
||||
) {
|
||||
on<InitLastServicesEvent>((event, emit) async {
|
||||
emit(state.copyWith(status: LatestStoreServicesStatus.loading));
|
||||
|
||||
try {
|
||||
await emit.forEach(
|
||||
_repository.getLastStoreServicesStream(
|
||||
storeId: event.storeId,
|
||||
limit: 5,
|
||||
),
|
||||
onData: (List<ServiceModel> data) => state.copyWith(
|
||||
status: LatestStoreServicesStatus.success,
|
||||
services: data,
|
||||
),
|
||||
onError: (error, stackTrace) => state.copyWith(
|
||||
status: LatestStoreServicesStatus.failure,
|
||||
error: error.toString(),
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: LatestStoreServicesStatus.failure,
|
||||
error: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
part of 'latest_store_services_bloc.dart';
|
||||
|
||||
sealed class LatestStoreServicesEvent extends Equatable {
|
||||
const LatestStoreServicesEvent();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class InitLastServicesEvent extends LatestStoreServicesEvent {
|
||||
final String storeId;
|
||||
|
||||
const InitLastServicesEvent(this.storeId);
|
||||
|
||||
@override
|
||||
List<Object> get props => [storeId];
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
part of 'latest_store_services_bloc.dart';
|
||||
|
||||
enum LatestStoreServicesStatus { initial, loading, success, failure }
|
||||
|
||||
class LatestStoreServicesState extends Equatable {
|
||||
final LatestStoreServicesStatus status;
|
||||
final String? error;
|
||||
final List<ServiceModel> services;
|
||||
|
||||
const LatestStoreServicesState({
|
||||
required this.status,
|
||||
this.error,
|
||||
this.services = const [],
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [status, error, services];
|
||||
|
||||
LatestStoreServicesState copyWith({
|
||||
LatestStoreServicesStatus? status,
|
||||
String? error,
|
||||
List<ServiceModel>? services,
|
||||
}) {
|
||||
return LatestStoreServicesState(
|
||||
status: status ?? this.status,
|
||||
error: error,
|
||||
services: services ?? this.services,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user