indentation and formatting of WaterHeaterBloc.

This commit is contained in:
Faris Armoush
2025-06-02 10:18:20 +03:00
parent b60c674496
commit 19548e99ab
2 changed files with 111 additions and 88 deletions

View File

@ -1,5 +1,3 @@
// water_heater_bloc.dart
import 'dart:async'; import 'dart:async';
import 'package:bloc/bloc.dart'; import 'package:bloc/bloc.dart';
@ -49,7 +47,7 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
on<StatusUpdated>(_onStatusUpdated); on<StatusUpdated>(_onStatusUpdated);
} }
FutureOr<void> _initializeAddSchedule( void _initializeAddSchedule(
InitializeAddScheduleEvent event, InitializeAddScheduleEvent event,
Emitter<WaterHeaterState> emit, Emitter<WaterHeaterState> emit,
) { ) {
@ -71,7 +69,7 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
} }
} }
FutureOr<void> _updateSelectedTime( void _updateSelectedTime(
UpdateSelectedTimeEvent event, UpdateSelectedTimeEvent event,
Emitter<WaterHeaterState> emit, Emitter<WaterHeaterState> emit,
) { ) {
@ -80,7 +78,7 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
emit(currentState.copyWith(selectedTime: event.selectedTime)); emit(currentState.copyWith(selectedTime: event.selectedTime));
} }
FutureOr<void> _updateSelectedDay( void _updateSelectedDay(
UpdateSelectedDayEvent event, UpdateSelectedDayEvent event,
Emitter<WaterHeaterState> emit, Emitter<WaterHeaterState> emit,
) { ) {
@ -91,7 +89,7 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
selectedDays: updatedDays, selectedTime: currentState.selectedTime)); selectedDays: updatedDays, selectedTime: currentState.selectedTime));
} }
FutureOr<void> _updateFunctionOn( void _updateFunctionOn(
UpdateFunctionOnEvent event, UpdateFunctionOnEvent event,
Emitter<WaterHeaterState> emit, Emitter<WaterHeaterState> emit,
) { ) {
@ -100,16 +98,18 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
functionOn: event.isOn, selectedTime: currentState.selectedTime)); functionOn: event.isOn, selectedTime: currentState.selectedTime));
} }
FutureOr<void> _updateScheduleEvent( Future<void> _updateScheduleEvent(
UpdateScheduleEvent event, UpdateScheduleEvent event,
Emitter<WaterHeaterState> emit, Emitter<WaterHeaterState> emit,
) async { ) async {
final currentState = state; final currentState = state;
if (currentState is WaterHeaterDeviceStatusLoaded) { if (currentState is WaterHeaterDeviceStatusLoaded) {
if (event.scheduleMode == ScheduleModes.schedule) { if (event.scheduleMode == ScheduleModes.schedule) {
emit(currentState.copyWith( emit(
scheduleMode: ScheduleModes.schedule, currentState.copyWith(
)); scheduleMode: ScheduleModes.schedule,
),
);
} }
if (event.scheduleMode == ScheduleModes.countdown) { if (event.scheduleMode == ScheduleModes.countdown) {
final countdownRemaining = final countdownRemaining =
@ -129,17 +129,19 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
} else if (event.scheduleMode == ScheduleModes.inching) { } else if (event.scheduleMode == ScheduleModes.inching) {
final inchingDuration = Duration(hours: event.hours, minutes: event.minutes); final inchingDuration = Duration(hours: event.hours, minutes: event.minutes);
emit(currentState.copyWith( emit(
scheduleMode: ScheduleModes.inching, currentState.copyWith(
inchingHours: inchingDuration.inHours, scheduleMode: ScheduleModes.inching,
inchingMinutes: inchingDuration.inMinutes % 60, inchingHours: inchingDuration.inHours,
isInchingActive: currentState.isInchingActive, inchingMinutes: inchingDuration.inMinutes % 60,
)); isInchingActive: currentState.isInchingActive,
),
);
} }
} }
} }
FutureOr<void> _controlWaterHeater( Future<void> _controlWaterHeater(
ToggleWaterHeaterEvent event, ToggleWaterHeaterEvent event,
Emitter<WaterHeaterState> emit, Emitter<WaterHeaterState> emit,
) async { ) async {
@ -148,9 +150,11 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
_updateLocalValue(event.code, event.value); _updateLocalValue(event.code, event.value);
emit(currentState.copyWith( emit(
status: deviceStatus, currentState.copyWith(
)); status: deviceStatus,
),
);
final success = await controlDeviceService.controlDevice( final success = await controlDeviceService.controlDevice(
deviceUuid: event.deviceId, deviceUuid: event.deviceId,
@ -164,37 +168,43 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
if (event.code == "countdown_1") { if (event.code == "countdown_1") {
final countdownDuration = Duration(seconds: event.value); final countdownDuration = Duration(seconds: event.value);
emit(currentState.copyWith( emit(
countdownHours: countdownDuration.inHours, currentState.copyWith(
countdownMinutes: countdownDuration.inMinutes % 60, countdownHours: countdownDuration.inHours,
countdownRemaining: countdownDuration, countdownMinutes: countdownDuration.inMinutes % 60,
isCountdownActive: true, countdownRemaining: countdownDuration,
)); isCountdownActive: true,
),
);
if (countdownDuration.inSeconds > 0) { if (countdownDuration.inSeconds > 0) {
_startCountdownTimer(emit, countdownDuration); _startCountdownTimer(emit, countdownDuration);
} else { } else {
_countdownTimer?.cancel(); _countdownTimer?.cancel();
emit(currentState.copyWith( emit(
countdownHours: 0, currentState.copyWith(
countdownMinutes: 0, countdownHours: 0,
countdownRemaining: Duration.zero, countdownMinutes: 0,
isCountdownActive: false, countdownRemaining: Duration.zero,
)); isCountdownActive: false,
),
);
} }
} else if (event.code == "switch_inching") { } else if (event.code == "switch_inching") {
final inchingDuration = Duration(seconds: event.value); final inchingDuration = Duration(seconds: event.value);
emit(currentState.copyWith( emit(
inchingHours: inchingDuration.inHours, currentState.copyWith(
inchingMinutes: inchingDuration.inMinutes % 60, inchingHours: inchingDuration.inHours,
isInchingActive: true, inchingMinutes: inchingDuration.inMinutes % 60,
)); isInchingActive: true,
),
);
} }
} }
} }
} }
FutureOr<void> _stopScheduleEvent( Future<void> _stopScheduleEvent(
StopScheduleEvent event, StopScheduleEvent event,
Emitter<WaterHeaterState> emit, Emitter<WaterHeaterState> emit,
) async { ) async {
@ -205,18 +215,22 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
_countdownTimer?.cancel(); _countdownTimer?.cancel();
if (isCountDown) { if (isCountDown) {
emit(currentState.copyWith( emit(
countdownHours: 0, currentState.copyWith(
countdownMinutes: 0, countdownHours: 0,
countdownRemaining: Duration.zero, countdownMinutes: 0,
isCountdownActive: false, countdownRemaining: Duration.zero,
)); isCountdownActive: false,
),
);
} else if (currentState.scheduleMode == ScheduleModes.inching) { } else if (currentState.scheduleMode == ScheduleModes.inching) {
emit(currentState.copyWith( emit(
inchingHours: 0, currentState.copyWith(
inchingMinutes: 0, inchingHours: 0,
isInchingActive: false, inchingMinutes: 0,
)); isInchingActive: false,
),
);
} }
try { try {
@ -233,7 +247,7 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
} }
} }
FutureOr<void> _fetchWaterHeaterStatus( Future<void> _fetchWaterHeaterStatus(
WaterHeaterFetchStatusEvent event, WaterHeaterFetchStatusEvent event,
Emitter<WaterHeaterState> emit, Emitter<WaterHeaterState> emit,
) async { ) async {
@ -334,7 +348,10 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
} catch (_) {} } catch (_) {}
} }
void _onStatusUpdated(StatusUpdated event, Emitter<WaterHeaterState> emit) { void _onStatusUpdated(
StatusUpdated event,
Emitter<WaterHeaterState> emit,
) {
deviceStatus = event.deviceStatus; deviceStatus = event.deviceStatus;
emit(WaterHeaterDeviceStatusLoaded(deviceStatus)); emit(WaterHeaterDeviceStatusLoaded(deviceStatus));
} }
@ -345,12 +362,13 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
) { ) {
_countdownTimer?.cancel(); _countdownTimer?.cancel();
_countdownTimer = Timer.periodic(const Duration(minutes: 1), (timer) { _countdownTimer = Timer.periodic(
add(DecrementCountdownEvent()); const Duration(minutes: 1),
}); (timer) => add(DecrementCountdownEvent()),
);
} }
_onDecrementCountdown( void _onDecrementCountdown(
DecrementCountdownEvent event, DecrementCountdownEvent event,
Emitter<WaterHeaterState> emit, Emitter<WaterHeaterState> emit,
) { ) {
@ -364,25 +382,28 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
if (newRemaining <= Duration.zero) { if (newRemaining <= Duration.zero) {
_countdownTimer?.cancel(); _countdownTimer?.cancel();
emit(currentState.copyWith( emit(
countdownHours: 0, currentState.copyWith(
countdownMinutes: 0, countdownHours: 0,
isCountdownActive: false, countdownMinutes: 0,
countdownRemaining: Duration.zero, isCountdownActive: false,
)); countdownRemaining: Duration.zero,
),
);
return; return;
} }
int totalSeconds = newRemaining.inSeconds; final totalSeconds = newRemaining.inSeconds;
final newHours = totalSeconds ~/ 3600;
final newMinutes = (totalSeconds % 3600) ~/ 60;
int newHours = totalSeconds ~/ 3600; emit(
int newMinutes = (totalSeconds % 3600) ~/ 60; currentState.copyWith(
countdownHours: newHours,
emit(currentState.copyWith( countdownMinutes: newMinutes,
countdownHours: newHours, countdownRemaining: newRemaining,
countdownMinutes: newMinutes, ),
countdownRemaining: newRemaining, );
));
} }
} }
} }
@ -422,13 +443,17 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
return super.close(); return super.close();
} }
FutureOr<void> _getSchedule( Future<void> _getSchedule(
GetSchedulesEvent event, Emitter<WaterHeaterState> emit) async { GetSchedulesEvent event,
Emitter<WaterHeaterState> emit,
) async {
emit(ScheduleLoadingState()); emit(ScheduleLoadingState());
try { try {
List<ScheduleModel> schedules = await DevicesManagementApi() final schedules = await DevicesManagementApi().getDeviceSchedules(
.getDeviceSchedules(deviceStatus.uuid, event.category); deviceStatus.uuid,
event.category,
);
emit(WaterHeaterDeviceStatusLoaded( emit(WaterHeaterDeviceStatusLoaded(
deviceStatus, deviceStatus,
@ -436,7 +461,6 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
scheduleMode: ScheduleModes.schedule, scheduleMode: ScheduleModes.schedule,
)); ));
} catch (e) { } catch (e) {
//(const WaterHeaterFailedState(error: 'Failed to fetch schedules.'));
emit(WaterHeaterDeviceStatusLoaded( emit(WaterHeaterDeviceStatusLoaded(
deviceStatus, deviceStatus,
schedules: const [], schedules: const [],
@ -444,7 +468,7 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
} }
} }
FutureOr<void> _onAddSchedule( Future<void> _onAddSchedule(
AddScheduleEvent event, AddScheduleEvent event,
Emitter<WaterHeaterState> emit, Emitter<WaterHeaterState> emit,
) async { ) async {
@ -458,8 +482,6 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
days: ScheduleModel.convertSelectedDaysToStrings(event.selectedDays), days: ScheduleModel.convertSelectedDaysToStrings(event.selectedDays),
); );
// emit(ScheduleLoadingState());
bool success = await DevicesManagementApi() bool success = await DevicesManagementApi()
.addScheduleRecord(newSchedule, currentState.status.uuid); .addScheduleRecord(newSchedule, currentState.status.uuid);
@ -467,12 +489,11 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
add(GetSchedulesEvent(category: 'switch_1', uuid: deviceStatus.uuid)); add(GetSchedulesEvent(category: 'switch_1', uuid: deviceStatus.uuid));
} else { } else {
emit(currentState); emit(currentState);
//emit(const WaterHeaterFailedState(error: 'Failed to add schedule.'));
} }
} }
} }
FutureOr<void> _onEditSchedule( Future<void> _onEditSchedule(
EditWaterHeaterScheduleEvent event, EditWaterHeaterScheduleEvent event,
Emitter<WaterHeaterState> emit, Emitter<WaterHeaterState> emit,
) async { ) async {
@ -500,7 +521,7 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
} }
} }
FutureOr<void> _onUpdateSchedule( Future<void> _onUpdateSchedule(
UpdateScheduleEntryEvent event, UpdateScheduleEntryEvent event,
Emitter<WaterHeaterState> emit, Emitter<WaterHeaterState> emit,
) async { ) async {
@ -531,7 +552,7 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
} }
} }
FutureOr<void> _onDeleteSchedule( Future<void> _onDeleteSchedule(
DeleteScheduleEvent event, DeleteScheduleEvent event,
Emitter<WaterHeaterState> emit, Emitter<WaterHeaterState> emit,
) async { ) async {
@ -553,12 +574,16 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
} }
} }
FutureOr<void> _batchFetchWaterHeater( Future<void> _batchFetchWaterHeater(
FetchWaterHeaterBatchStatusEvent event, Emitter<WaterHeaterState> emit) async { FetchWaterHeaterBatchStatusEvent event,
Emitter<WaterHeaterState> emit,
) async {
emit(WaterHeaterLoadingState()); emit(WaterHeaterLoadingState());
try { try {
final status = await DevicesManagementApi().getBatchStatus(event.devicesUuid); final status = await DevicesManagementApi().getBatchStatus(
event.devicesUuid,
);
deviceStatus = deviceStatus =
WaterHeaterStatusModel.fromJson(event.devicesUuid.first, status.status); WaterHeaterStatusModel.fromJson(event.devicesUuid.first, status.status);
@ -568,7 +593,7 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
} }
} }
FutureOr<void> _batchControlWaterHeater( Future<void> _batchControlWaterHeater(
ControlWaterHeaterBatchEvent event, Emitter<WaterHeaterState> emit) async { ControlWaterHeaterBatchEvent event, Emitter<WaterHeaterState> emit) async {
if (state is WaterHeaterDeviceStatusLoaded) { if (state is WaterHeaterDeviceStatusLoaded) {
final currentState = state as WaterHeaterDeviceStatusLoaded; final currentState = state as WaterHeaterDeviceStatusLoaded;

View File

@ -1,5 +1,3 @@
// water_heater_state.dart
part of 'water_heater_bloc.dart'; part of 'water_heater_bloc.dart';
sealed class WaterHeaterState extends Equatable { sealed class WaterHeaterState extends Equatable {