mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
Created EnergyConsumptionPerDeviceBloc
.
This commit is contained in:
@ -0,0 +1,42 @@
|
|||||||
|
import 'package:bloc/bloc.dart';
|
||||||
|
import 'package:equatable/equatable.dart';
|
||||||
|
import 'package:syncrow_web/pages/analytics/models/device_energy_data_model.dart';
|
||||||
|
import 'package:syncrow_web/pages/analytics/params/get_energy_consumption_per_device_param.dart';
|
||||||
|
import 'package:syncrow_web/pages/analytics/services/energy_consumption_per_device/energy_consumption_per_device_service.dart';
|
||||||
|
|
||||||
|
part 'energy_consumption_per_device_event.dart';
|
||||||
|
part 'energy_consumption_per_device_state.dart';
|
||||||
|
|
||||||
|
class EnergyConsumptionPerDeviceBloc
|
||||||
|
extends Bloc<EnergyConsumptionPerDeviceEvent, EnergyConsumptionPerDeviceState> {
|
||||||
|
EnergyConsumptionPerDeviceBloc(
|
||||||
|
this._energyConsumptionPerDeviceService,
|
||||||
|
) : super(const EnergyConsumptionPerDeviceState()) {
|
||||||
|
on<LoadEnergyConsumptionPerDeviceEvent>(_onLoadEnergyConsumptionPerDeviceEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
final EnergyConsumptionPerDeviceService _energyConsumptionPerDeviceService;
|
||||||
|
|
||||||
|
Future<void> _onLoadEnergyConsumptionPerDeviceEvent(
|
||||||
|
LoadEnergyConsumptionPerDeviceEvent event,
|
||||||
|
Emitter<EnergyConsumptionPerDeviceState> emit,
|
||||||
|
) async {
|
||||||
|
emit(state.copyWith(status: EnergyConsumptionPerDeviceStatus.loading));
|
||||||
|
try {
|
||||||
|
final chartData = await _energyConsumptionPerDeviceService.load(event.param);
|
||||||
|
emit(
|
||||||
|
state.copyWith(
|
||||||
|
status: EnergyConsumptionPerDeviceStatus.loaded,
|
||||||
|
chartData: chartData,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} on Exception catch (e) {
|
||||||
|
emit(
|
||||||
|
state.copyWith(
|
||||||
|
status: EnergyConsumptionPerDeviceStatus.failure,
|
||||||
|
errorMessage: e.toString(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
part of 'energy_consumption_per_device_bloc.dart';
|
||||||
|
|
||||||
|
sealed class EnergyConsumptionPerDeviceEvent extends Equatable {
|
||||||
|
const EnergyConsumptionPerDeviceEvent();
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object> get props => [];
|
||||||
|
}
|
||||||
|
|
||||||
|
final class LoadEnergyConsumptionPerDeviceEvent
|
||||||
|
extends EnergyConsumptionPerDeviceEvent {
|
||||||
|
const LoadEnergyConsumptionPerDeviceEvent(this.param);
|
||||||
|
|
||||||
|
final GetEnergyConsumptionPerDeviceParam param;
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object> get props => [param];
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
part of 'energy_consumption_per_device_bloc.dart';
|
||||||
|
|
||||||
|
enum EnergyConsumptionPerDeviceStatus { initial, loading, loaded, failure }
|
||||||
|
|
||||||
|
final class EnergyConsumptionPerDeviceState extends Equatable {
|
||||||
|
const EnergyConsumptionPerDeviceState({
|
||||||
|
this.status = EnergyConsumptionPerDeviceStatus.initial,
|
||||||
|
this.chartData = const <DeviceEnergyDataModel>[],
|
||||||
|
this.errorMessage,
|
||||||
|
});
|
||||||
|
|
||||||
|
final List<DeviceEnergyDataModel> chartData;
|
||||||
|
final EnergyConsumptionPerDeviceStatus status;
|
||||||
|
final String? errorMessage;
|
||||||
|
|
||||||
|
EnergyConsumptionPerDeviceState copyWith({
|
||||||
|
List<DeviceEnergyDataModel>? chartData,
|
||||||
|
EnergyConsumptionPerDeviceStatus? status,
|
||||||
|
String? errorMessage,
|
||||||
|
}) {
|
||||||
|
return EnergyConsumptionPerDeviceState(
|
||||||
|
chartData: chartData ?? this.chartData,
|
||||||
|
status: status ?? this.status,
|
||||||
|
errorMessage: errorMessage ?? this.errorMessage,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [chartData, status, errorMessage];
|
||||||
|
}
|
Reference in New Issue
Block a user