push schedule basic design and bloc manegment

This commit is contained in:
ashrafzarkanisala
2024-09-20 02:27:07 +03:00
parent 26816b99cd
commit 921ccf0132
4 changed files with 725 additions and 439 deletions

View File

@ -3,6 +3,7 @@
import 'dart:async'; import 'dart:async';
import 'package:bloc/bloc.dart'; import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart'; import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart'; import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
import 'package:syncrow_web/pages/device_managment/water_heater/models/water_heater_status_model.dart'; import 'package:syncrow_web/pages/device_managment/water_heater/models/water_heater_status_model.dart';
import 'package:syncrow_web/services/devices_mang_api.dart'; import 'package:syncrow_web/services/devices_mang_api.dart';
@ -17,6 +18,9 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
on<UpdateScheduleEvent>(_updateScheduleEvent); on<UpdateScheduleEvent>(_updateScheduleEvent);
on<StopScheduleEvent>(_stopScheduleEvent); on<StopScheduleEvent>(_stopScheduleEvent);
on<DecrementCountdownEvent>(_onDecrementCountdown); on<DecrementCountdownEvent>(_onDecrementCountdown);
on<AddScheduleEvent>(_onAddSchedule);
on<DeleteScheduleEvent>(_onDeleteSchedule);
on<UpdateScheduleEntryEvent>(_onUpdateSchedule);
} }
late WaterHeaterStatusModel deviceStatus; late WaterHeaterStatusModel deviceStatus;
@ -179,7 +183,7 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
} }
} }
_onDecrementCountdown( _onDecrementCountdown(
DecrementCountdownEvent event, DecrementCountdownEvent event,
Emitter<WaterHeaterState> emit, Emitter<WaterHeaterState> emit,
) { ) {
@ -294,6 +298,54 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
} }
} }
FutureOr<void> _onAddSchedule(
AddScheduleEvent event,
Emitter<WaterHeaterState> emit,
) {
if (state is WaterHeaterDeviceStatusLoaded) {
final currentState = state as WaterHeaterDeviceStatusLoaded;
final newSchedule = ScheduleEntry(
selectedDays: event.selectedDays,
time: event.time,
functionOn: event.functionOn,
);
final updatedSchedules = List<ScheduleEntry>.from(currentState.schedules)
..add(newSchedule);
emit(currentState.copyWith(schedules: updatedSchedules));
}
}
FutureOr<void> _onDeleteSchedule(
DeleteScheduleEvent event,
Emitter<WaterHeaterState> emit,
) {
if (state is WaterHeaterDeviceStatusLoaded) {
final currentState = state as WaterHeaterDeviceStatusLoaded;
final updatedSchedules = List<ScheduleEntry>.from(currentState.schedules)
..removeAt(event.index);
emit(currentState.copyWith(schedules: updatedSchedules));
}
}
FutureOr<void> _onUpdateSchedule(
UpdateScheduleEntryEvent event,
Emitter<WaterHeaterState> emit,
) {
if (state is WaterHeaterDeviceStatusLoaded) {
final currentState = state as WaterHeaterDeviceStatusLoaded;
final updatedSchedules = List<ScheduleEntry>.from(currentState.schedules);
updatedSchedules[event.index] = ScheduleEntry(
selectedDays: event.selectedDays,
time: event.time,
functionOn: event.functionOn,
);
emit(currentState.copyWith(schedules: updatedSchedules));
}
}
@override @override
Future<void> close() { Future<void> close() {
_countdownTimer?.cancel(); _countdownTimer?.cancel();

View File

@ -1,4 +1,3 @@
part of 'water_heater_bloc.dart'; part of 'water_heater_bloc.dart';
sealed class WaterHeaterEvent extends Equatable { sealed class WaterHeaterEvent extends Equatable {
@ -63,3 +62,44 @@ final class WaterHeaterFetchBatchStatusEvent extends WaterHeaterEvent {
} }
final class DecrementCountdownEvent extends WaterHeaterEvent {} final class DecrementCountdownEvent extends WaterHeaterEvent {}
final class AddScheduleEvent extends WaterHeaterEvent {
final List<bool> selectedDays;
final TimeOfDay time;
final bool functionOn;
const AddScheduleEvent({
required this.selectedDays,
required this.time,
required this.functionOn,
});
@override
List<Object?> get props => [selectedDays, time, functionOn];
}
final class DeleteScheduleEvent extends WaterHeaterEvent {
final int index;
const DeleteScheduleEvent(this.index);
@override
List<Object?> get props => [index];
}
final class UpdateScheduleEntryEvent extends WaterHeaterEvent {
final int index;
final List<bool> selectedDays;
final TimeOfDay time;
final bool functionOn;
const UpdateScheduleEntryEvent({
required this.index,
required this.selectedDays,
required this.time,
required this.functionOn,
});
@override
List<Object?> get props => [index, selectedDays, time, functionOn];
}

View File

@ -20,6 +20,7 @@ final class WaterHeaterDeviceStatusLoaded extends WaterHeaterState {
final int? minutes; final int? minutes;
final bool? isActive; final bool? isActive;
final Duration? countdownRemaining; final Duration? countdownRemaining;
final List<ScheduleEntry> schedules;
const WaterHeaterDeviceStatusLoaded( const WaterHeaterDeviceStatusLoaded(
this.status, { this.status, {
@ -28,13 +29,20 @@ final class WaterHeaterDeviceStatusLoaded extends WaterHeaterState {
this.minutes, this.minutes,
this.isActive, this.isActive,
this.countdownRemaining, this.countdownRemaining,
this.schedules = const [],
}); });
@override @override
List<Object?> get props => List<Object?> get props => [
[status, scheduleMode, hours, minutes, isActive, countdownRemaining]; status,
scheduleMode,
hours,
minutes,
isActive,
countdownRemaining,
schedules,
];
/// Creates a new instance with updated fields.
WaterHeaterDeviceStatusLoaded copyWith({ WaterHeaterDeviceStatusLoaded copyWith({
WaterHeaterStatusModel? status, WaterHeaterStatusModel? status,
ScheduleModes? scheduleMode, ScheduleModes? scheduleMode,
@ -42,6 +50,7 @@ final class WaterHeaterDeviceStatusLoaded extends WaterHeaterState {
int? minutes, int? minutes,
bool? isActive, bool? isActive,
Duration? countdownRemaining, Duration? countdownRemaining,
List<ScheduleEntry>? schedules,
}) { }) {
return WaterHeaterDeviceStatusLoaded( return WaterHeaterDeviceStatusLoaded(
status ?? this.status, status ?? this.status,
@ -50,10 +59,27 @@ final class WaterHeaterDeviceStatusLoaded extends WaterHeaterState {
minutes: minutes ?? this.minutes, minutes: minutes ?? this.minutes,
isActive: isActive ?? this.isActive, isActive: isActive ?? this.isActive,
countdownRemaining: countdownRemaining ?? this.countdownRemaining, countdownRemaining: countdownRemaining ?? this.countdownRemaining,
schedules: schedules ?? this.schedules,
); );
} }
} }
class ScheduleEntry {
final List<bool> selectedDays;
final TimeOfDay time;
final bool functionOn;
ScheduleEntry({
required this.selectedDays,
required this.time,
required this.functionOn,
});
@override
String toString() =>
'ScheduleEntry(selectedDays: $selectedDays, time: $time, functionOn: $functionOn)';
}
final class WaterHeaterFailedState extends WaterHeaterState { final class WaterHeaterFailedState extends WaterHeaterState {
final String error; final String error;