Refactor ScheduleBloc and related components to use dynamic category … (#279)

<!--
  Thanks for contributing!

Provide a description of your changes below and a general summary in the
title

Please look at the following checklist to ensure that your PR can be
accepted quickly:
-->



## Description

<!--- Describe your changes in detail -->
Fix scheduling Bugs

## Type of Change

<!--- Put an `x` in all the boxes that apply: -->

- [ ]  New feature (non-breaking change which adds functionality)
- [x] 🛠️ Bug fix (non-breaking change which fixes an issue)
- [ ]  Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] 🧹 Code refactor
- [ ]  Build configuration change
- [ ] 📝 Documentation
- [ ] 🗑️ Chore
This commit is contained in:
mohammadnemer1
2025-06-22 14:44:18 +03:00
committed by GitHub
6 changed files with 20 additions and 47 deletions

View File

@ -257,11 +257,11 @@ class ScheduleBloc extends Bloc<ScheduleEvent, ScheduleState> {
category: event.category, category: event.category,
deviceId: deviceId, deviceId: deviceId,
time: getTimeStampWithoutSeconds(dateTime).toString(), time: getTimeStampWithoutSeconds(dateTime).toString(),
code: 'switch_1', code: event.category,
value: event.functionOn, value: event.functionOn,
days: event.selectedDays); days: event.selectedDays);
if (success) { if (success) {
add(const ScheduleGetEvent(category: 'switch_1')); add(ScheduleGetEvent(category: event.category));
} else { } else {
emit(const ScheduleError('Failed to add schedule')); emit(const ScheduleError('Failed to add schedule'));
} }
@ -282,7 +282,7 @@ class ScheduleBloc extends Bloc<ScheduleEvent, ScheduleState> {
scheduleId: event.scheduleId, scheduleId: event.scheduleId,
category: event.category, category: event.category,
time: getTimeStampWithoutSeconds(dateTime).toString(), time: getTimeStampWithoutSeconds(dateTime).toString(),
function: Status(code: 'switch_1', value: event.functionOn), function: Status(code: event.category, value: event.functionOn),
days: event.selectedDays, days: event.selectedDays,
); );
final success = await DevicesManagementApi().editScheduleRecord( final success = await DevicesManagementApi().editScheduleRecord(
@ -291,7 +291,9 @@ class ScheduleBloc extends Bloc<ScheduleEvent, ScheduleState> {
); );
if (success) { if (success) {
add(const ScheduleGetEvent(category: 'switch_1')); add(ScheduleGetEvent(
category: event.category,
));
} else { } else {
emit(const ScheduleError('Failed to update schedule')); emit(const ScheduleError('Failed to update schedule'));
} }
@ -312,7 +314,7 @@ class ScheduleBloc extends Bloc<ScheduleEvent, ScheduleState> {
final updatedSchedules = currentState.schedules.map((schedule) { final updatedSchedules = currentState.schedules.map((schedule) {
if (schedule.scheduleId == event.scheduleId) { if (schedule.scheduleId == event.scheduleId) {
return schedule.copyWith( return schedule.copyWith(
function: Status(code: 'switch_1', value: event.functionOn), function: Status(code: event.category, value: event.functionOn),
enable: event.enable, enable: event.enable,
); );
} }
@ -533,10 +535,6 @@ class ScheduleBloc extends Bloc<ScheduleEvent, ScheduleState> {
Duration.zero; Duration.zero;
} }
if (state is ScheduleLoaded) { if (state is ScheduleLoaded) {
print('Updating existing state with fetched status');
print('scheduleMode: $scheduleMode');
print('countdownRemaining: $countdownRemaining');
print('isCountdownActive: $isCountdownActive');
final currentState = state as ScheduleLoaded; final currentState = state as ScheduleLoaded;
emit(currentState.copyWith( emit(currentState.copyWith(
scheduleMode: scheduleMode, scheduleMode: scheduleMode,
@ -586,35 +584,4 @@ class ScheduleBloc extends Bloc<ScheduleEvent, ScheduleState> {
dateTime.day, dateTime.hour, dateTime.minute); dateTime.day, dateTime.hour, dateTime.minute);
return dateTimeWithoutSeconds.millisecondsSinceEpoch ~/ 1000; return dateTimeWithoutSeconds.millisecondsSinceEpoch ~/ 1000;
} }
// Future <void> _updateScheduleEvent(
// StatusUpdatedScheduleEvent event,
// Emitter<ScheduleState> emit,
// ) async {
// if (state is ScheduleLoaded) {
// final currentState = state as ScheduleLoaded;
// final updatedSchedules = currentState.schedules.map((schedule) {
// if (schedule.scheduleId == event.scheduleId) {
// return schedule.copyWith(
// function: Status(code: 'switch_1', value: event.functionOn),
// enable: event.enable,
// );
// }
// return schedule;
// }).toList();
// bool success = await DevicesManagementApi().updateScheduleRecord(
// enable: event.enable,
// uuid: currentState.status.uuid,
// scheduleId: event.scheduleId,
// );
// if (success) {
// emit(currentState.copyWith(schedules: updatedSchedules));
// } else {
// emit(currentState);
// }
// }
// }
} }

View File

@ -121,15 +121,17 @@ class ScheduleUpdateEntryEvent extends ScheduleEvent {
final String scheduleId; final String scheduleId;
final bool functionOn; final bool functionOn;
final bool enable; final bool enable;
final String category;
const ScheduleUpdateEntryEvent({ const ScheduleUpdateEntryEvent({
required this.scheduleId, required this.scheduleId,
required this.functionOn, required this.functionOn,
required this.enable, required this.enable,
required this.category,
}); });
@override @override
List<Object> get props => [scheduleId, functionOn, enable]; List<Object> get props => [scheduleId, functionOn, enable, category];
} }
class UpdateScheduleModeEvent extends ScheduleEvent { class UpdateScheduleModeEvent extends ScheduleEvent {

View File

@ -15,7 +15,7 @@ class BuildScheduleView extends StatelessWidget {
const BuildScheduleView( const BuildScheduleView(
{super.key, required this.deviceUuid, required this.category}); {super.key, required this.deviceUuid, required this.category});
final String deviceUuid; final String deviceUuid;
final String category; final String category;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -51,6 +51,7 @@ class BuildScheduleView extends StatelessWidget {
const SizedBox(height: 20), const SizedBox(height: 20),
if (state.scheduleMode == ScheduleModes.schedule) if (state.scheduleMode == ScheduleModes.schedule)
ScheduleManagementUI( ScheduleManagementUI(
category: category,
deviceUuid: deviceUuid, deviceUuid: deviceUuid,
onAddSchedule: () async { onAddSchedule: () async {
final entry = await ScheduleDialogHelper final entry = await ScheduleDialogHelper

View File

@ -7,11 +7,13 @@ import 'package:syncrow_web/utils/extension/build_context_x.dart';
class ScheduleManagementUI extends StatelessWidget { class ScheduleManagementUI extends StatelessWidget {
final String deviceUuid; final String deviceUuid;
final VoidCallback onAddSchedule; final VoidCallback onAddSchedule;
final String category;
const ScheduleManagementUI({ const ScheduleManagementUI({
super.key, super.key,
required this.deviceUuid, required this.deviceUuid,
required this.onAddSchedule, required this.onAddSchedule,
this.category = 'switch_1',
}); });
@override @override
@ -42,7 +44,7 @@ class ScheduleManagementUI extends StatelessWidget {
), ),
), ),
const SizedBox(height: 20), const SizedBox(height: 20),
ScheduleTableWidget(deviceUuid: deviceUuid), ScheduleTableWidget(deviceUuid: deviceUuid, category: category),
], ],
); );
} }

View File

@ -39,10 +39,10 @@ class ScheduleModeSelector extends StatelessWidget {
context, 'Countdown', ScheduleModes.countdown, currentMode), context, 'Countdown', ScheduleModes.countdown, currentMode),
_buildRadioTile( _buildRadioTile(
context, 'Schedule', ScheduleModes.schedule, currentMode), context, 'Schedule', ScheduleModes.schedule, currentMode),
_buildRadioTile( // _buildRadioTile(
context, 'Circulate', ScheduleModes.circulate, currentMode), // context, 'Circulate', ScheduleModes.circulate, currentMode),
_buildRadioTile( // _buildRadioTile(
context, 'Inching', ScheduleModes.inching, currentMode), // context, 'Inching', ScheduleModes.inching, currentMode),
], ],
), ),
], ],

View File

@ -164,6 +164,7 @@ class _ScheduleTableView extends StatelessWidget {
onTap: () { onTap: () {
context.read<ScheduleBloc>().add( context.read<ScheduleBloc>().add(
ScheduleUpdateEntryEvent( ScheduleUpdateEntryEvent(
category: schedule.category,
scheduleId: schedule.scheduleId, scheduleId: schedule.scheduleId,
functionOn: schedule.function.value, functionOn: schedule.function.value,
enable: !schedule.enable, enable: !schedule.enable,