Fix: Resolve critical linting issues and bugs (concurrency, syntax, dead code)
This commit is contained in:
@@ -2,10 +2,12 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../../domain/usecases/apply_quick_set_usecase.dart';
|
||||
import '../../domain/usecases/get_weekly_availability_usecase.dart';
|
||||
import '../../domain/usecases/update_day_availability_usecase.dart';
|
||||
import 'package:krow_core/core.dart';
|
||||
import 'availability_event.dart';
|
||||
import 'availability_state.dart';
|
||||
|
||||
class AvailabilityBloc extends Bloc<AvailabilityEvent, AvailabilityState> {
|
||||
class AvailabilityBloc extends Bloc<AvailabilityEvent, AvailabilityState>
|
||||
with BlocErrorHandler<AvailabilityState> {
|
||||
final GetWeeklyAvailabilityUseCase getWeeklyAvailability;
|
||||
final UpdateDayAvailabilityUseCase updateDayAvailability;
|
||||
final ApplyQuickSetUseCase applyQuickSet;
|
||||
@@ -28,27 +30,34 @@ class AvailabilityBloc extends Bloc<AvailabilityEvent, AvailabilityState> {
|
||||
Emitter<AvailabilityState> emit,
|
||||
) async {
|
||||
emit(AvailabilityLoading());
|
||||
try {
|
||||
final days = await getWeeklyAvailability(
|
||||
GetWeeklyAvailabilityParams(event.weekStart));
|
||||
emit(AvailabilityLoaded(
|
||||
days: days,
|
||||
currentWeekStart: event.weekStart,
|
||||
selectedDate: event.preselectedDate ??
|
||||
(days.isNotEmpty ? days.first.date : DateTime.now()),
|
||||
));
|
||||
} catch (e) {
|
||||
emit(AvailabilityError(e.toString()));
|
||||
}
|
||||
await handleError(
|
||||
emit: emit,
|
||||
action: () async {
|
||||
final days = await getWeeklyAvailability(
|
||||
GetWeeklyAvailabilityParams(event.weekStart),
|
||||
);
|
||||
emit(
|
||||
AvailabilityLoaded(
|
||||
days: days,
|
||||
currentWeekStart: event.weekStart,
|
||||
selectedDate: event.preselectedDate ??
|
||||
(days.isNotEmpty ? days.first.date : DateTime.now()),
|
||||
),
|
||||
);
|
||||
},
|
||||
onError: (String errorKey) => AvailabilityError(errorKey),
|
||||
);
|
||||
}
|
||||
|
||||
void _onSelectDate(SelectDate event, Emitter<AvailabilityState> emit) {
|
||||
if (state is AvailabilityLoaded) {
|
||||
// Clear success message on navigation
|
||||
emit((state as AvailabilityLoaded).copyWith(
|
||||
selectedDate: event.date,
|
||||
clearSuccessMessage: true,
|
||||
));
|
||||
emit(
|
||||
(state as AvailabilityLoaded).copyWith(
|
||||
selectedDate: event.date,
|
||||
clearSuccessMessage: true,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,14 +67,17 @@ class AvailabilityBloc extends Bloc<AvailabilityEvent, AvailabilityState> {
|
||||
) async {
|
||||
if (state is AvailabilityLoaded) {
|
||||
final currentState = state as AvailabilityLoaded;
|
||||
|
||||
|
||||
// Clear message
|
||||
emit(currentState.copyWith(clearSuccessMessage: true));
|
||||
|
||||
final newWeekStart = currentState.currentWeekStart
|
||||
.add(Duration(days: event.direction * 7));
|
||||
|
||||
final diff = currentState.selectedDate.difference(currentState.currentWeekStart).inDays;
|
||||
final newWeekStart = currentState.currentWeekStart.add(
|
||||
Duration(days: event.direction * 7),
|
||||
);
|
||||
|
||||
final diff = currentState.selectedDate
|
||||
.difference(currentState.currentWeekStart)
|
||||
.inDays;
|
||||
final newSelectedDate = newWeekStart.add(Duration(days: diff));
|
||||
|
||||
add(LoadAvailability(newWeekStart, preselectedDate: newSelectedDate));
|
||||
@@ -78,7 +90,7 @@ class AvailabilityBloc extends Bloc<AvailabilityEvent, AvailabilityState> {
|
||||
) async {
|
||||
if (state is AvailabilityLoaded) {
|
||||
final currentState = state as AvailabilityLoaded;
|
||||
|
||||
|
||||
final newDay = event.day.copyWith(isAvailable: !event.day.isAvailable);
|
||||
final updatedDays = currentState.days.map((d) {
|
||||
return d.date == event.day.date ? newDay : d;
|
||||
@@ -90,18 +102,29 @@ class AvailabilityBloc extends Bloc<AvailabilityEvent, AvailabilityState> {
|
||||
clearSuccessMessage: true,
|
||||
));
|
||||
|
||||
try {
|
||||
await updateDayAvailability(UpdateDayAvailabilityParams(newDay));
|
||||
// Success feedback
|
||||
if (state is AvailabilityLoaded) {
|
||||
emit((state as AvailabilityLoaded).copyWith(successMessage: 'Availability updated'));
|
||||
}
|
||||
} catch (e) {
|
||||
// Revert
|
||||
if (state is AvailabilityLoaded) {
|
||||
emit((state as AvailabilityLoaded).copyWith(days: currentState.days));
|
||||
}
|
||||
}
|
||||
await handleError(
|
||||
emit: emit,
|
||||
action: () async {
|
||||
await updateDayAvailability(UpdateDayAvailabilityParams(newDay));
|
||||
// Success feedback
|
||||
if (state is AvailabilityLoaded) {
|
||||
emit(
|
||||
(state as AvailabilityLoaded).copyWith(
|
||||
successMessage: 'Availability updated',
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
onError: (String errorKey) {
|
||||
// Revert
|
||||
if (state is AvailabilityLoaded) {
|
||||
return (state as AvailabilityLoaded).copyWith(
|
||||
days: currentState.days,
|
||||
);
|
||||
}
|
||||
return AvailabilityError(errorKey);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,7 +143,7 @@ class AvailabilityBloc extends Bloc<AvailabilityEvent, AvailabilityState> {
|
||||
}).toList();
|
||||
|
||||
final newDay = event.day.copyWith(slots: updatedSlots);
|
||||
|
||||
|
||||
final updatedDays = currentState.days.map((d) {
|
||||
return d.date == event.day.date ? newDay : d;
|
||||
}).toList();
|
||||
@@ -131,18 +154,29 @@ class AvailabilityBloc extends Bloc<AvailabilityEvent, AvailabilityState> {
|
||||
clearSuccessMessage: true,
|
||||
));
|
||||
|
||||
try {
|
||||
await updateDayAvailability(UpdateDayAvailabilityParams(newDay));
|
||||
// Success feedback
|
||||
if (state is AvailabilityLoaded) {
|
||||
emit((state as AvailabilityLoaded).copyWith(successMessage: 'Availability updated'));
|
||||
}
|
||||
} catch (e) {
|
||||
// Revert
|
||||
if (state is AvailabilityLoaded) {
|
||||
emit((state as AvailabilityLoaded).copyWith(days: currentState.days));
|
||||
}
|
||||
}
|
||||
await handleError(
|
||||
emit: emit,
|
||||
action: () async {
|
||||
await updateDayAvailability(UpdateDayAvailabilityParams(newDay));
|
||||
// Success feedback
|
||||
if (state is AvailabilityLoaded) {
|
||||
emit(
|
||||
(state as AvailabilityLoaded).copyWith(
|
||||
successMessage: 'Availability updated',
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
onError: (String errorKey) {
|
||||
// Revert
|
||||
if (state is AvailabilityLoaded) {
|
||||
return (state as AvailabilityLoaded).copyWith(
|
||||
days: currentState.days,
|
||||
);
|
||||
}
|
||||
return AvailabilityError(errorKey);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,28 +186,39 @@ class AvailabilityBloc extends Bloc<AvailabilityEvent, AvailabilityState> {
|
||||
) async {
|
||||
if (state is AvailabilityLoaded) {
|
||||
final currentState = state as AvailabilityLoaded;
|
||||
|
||||
emit(currentState.copyWith(
|
||||
isActionInProgress: true,
|
||||
clearSuccessMessage: true,
|
||||
));
|
||||
|
||||
try {
|
||||
final newDays = await applyQuickSet(
|
||||
ApplyQuickSetParams(currentState.currentWeekStart, event.type));
|
||||
|
||||
emit(currentState.copyWith(
|
||||
days: newDays,
|
||||
isActionInProgress: false,
|
||||
successMessage: 'Availability updated',
|
||||
));
|
||||
} catch (e) {
|
||||
emit(currentState.copyWith(
|
||||
isActionInProgress: false,
|
||||
// Could set error message here if we had a field for it, or emit AvailabilityError
|
||||
// But emitting AvailabilityError would replace the whole screen.
|
||||
));
|
||||
}
|
||||
|
||||
emit(
|
||||
currentState.copyWith(
|
||||
isActionInProgress: true,
|
||||
clearSuccessMessage: true,
|
||||
),
|
||||
);
|
||||
|
||||
await handleError(
|
||||
emit: emit,
|
||||
action: () async {
|
||||
final newDays = await applyQuickSet(
|
||||
ApplyQuickSetParams(currentState.currentWeekStart, event.type),
|
||||
);
|
||||
|
||||
emit(
|
||||
currentState.copyWith(
|
||||
days: newDays,
|
||||
isActionInProgress: false,
|
||||
successMessage: 'Availability updated',
|
||||
),
|
||||
);
|
||||
},
|
||||
onError: (String errorKey) {
|
||||
if (state is AvailabilityLoaded) {
|
||||
return (state as AvailabilityLoaded).copyWith(
|
||||
isActionInProgress: false,
|
||||
);
|
||||
}
|
||||
return AvailabilityError(errorKey);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user