Fix: Resolve critical linting issues and bugs (concurrency, syntax, dead code)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:krow_core/core.dart';
|
||||
import '../../../domain/usecases/apply_for_shift_usecase.dart';
|
||||
import '../../../domain/usecases/decline_shift_usecase.dart';
|
||||
import '../../../domain/usecases/get_shift_details_usecase.dart';
|
||||
@@ -6,7 +7,8 @@ import '../../../domain/arguments/get_shift_details_arguments.dart';
|
||||
import 'shift_details_event.dart';
|
||||
import 'shift_details_state.dart';
|
||||
|
||||
class ShiftDetailsBloc extends Bloc<ShiftDetailsEvent, ShiftDetailsState> {
|
||||
class ShiftDetailsBloc extends Bloc<ShiftDetailsEvent, ShiftDetailsState>
|
||||
with BlocErrorHandler<ShiftDetailsState> {
|
||||
final GetShiftDetailsUseCase getShiftDetails;
|
||||
final ApplyForShiftUseCase applyForShift;
|
||||
final DeclineShiftUseCase declineShift;
|
||||
@@ -26,47 +28,54 @@ class ShiftDetailsBloc extends Bloc<ShiftDetailsEvent, ShiftDetailsState> {
|
||||
Emitter<ShiftDetailsState> emit,
|
||||
) async {
|
||||
emit(ShiftDetailsLoading());
|
||||
try {
|
||||
final shift = await getShiftDetails(
|
||||
GetShiftDetailsArguments(shiftId: event.shiftId, roleId: event.roleId),
|
||||
);
|
||||
if (shift != null) {
|
||||
emit(ShiftDetailsLoaded(shift));
|
||||
} else {
|
||||
emit(const ShiftDetailsError("Shift not found"));
|
||||
}
|
||||
} catch (e) {
|
||||
emit(ShiftDetailsError(e.toString()));
|
||||
}
|
||||
await handleError(
|
||||
emit: emit,
|
||||
action: () async {
|
||||
final shift = await getShiftDetails(
|
||||
GetShiftDetailsArguments(shiftId: event.shiftId, roleId: event.roleId),
|
||||
);
|
||||
if (shift != null) {
|
||||
emit(ShiftDetailsLoaded(shift));
|
||||
} else {
|
||||
emit(const ShiftDetailsError("Shift not found"));
|
||||
}
|
||||
},
|
||||
onError: (String errorKey) => ShiftDetailsError(errorKey),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _onBookShift(
|
||||
BookShiftDetailsEvent event,
|
||||
Emitter<ShiftDetailsState> emit,
|
||||
) async {
|
||||
try {
|
||||
await applyForShift(
|
||||
event.shiftId,
|
||||
isInstantBook: true,
|
||||
roleId: event.roleId,
|
||||
);
|
||||
emit(
|
||||
ShiftActionSuccess("Shift successfully booked!", shiftDate: event.date),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(ShiftDetailsError(e.toString()));
|
||||
}
|
||||
await handleError(
|
||||
emit: emit,
|
||||
action: () async {
|
||||
await applyForShift(
|
||||
event.shiftId,
|
||||
isInstantBook: true,
|
||||
roleId: event.roleId,
|
||||
);
|
||||
emit(
|
||||
ShiftActionSuccess("Shift successfully booked!", shiftDate: event.date),
|
||||
);
|
||||
},
|
||||
onError: (String errorKey) => ShiftDetailsError(errorKey),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _onDeclineShift(
|
||||
DeclineShiftDetailsEvent event,
|
||||
Emitter<ShiftDetailsState> emit,
|
||||
) async {
|
||||
try {
|
||||
await declineShift(event.shiftId);
|
||||
emit(const ShiftActionSuccess("Shift declined"));
|
||||
} catch (e) {
|
||||
emit(ShiftDetailsError(e.toString()));
|
||||
}
|
||||
await handleError(
|
||||
emit: emit,
|
||||
action: () async {
|
||||
await declineShift(event.shiftId);
|
||||
emit(const ShiftActionSuccess("Shift declined"));
|
||||
},
|
||||
onError: (String errorKey) => ShiftDetailsError(errorKey),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:krow_core/core.dart';
|
||||
import 'package:krow_domain/krow_domain.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
@@ -14,7 +15,8 @@ import '../../../domain/usecases/get_pending_assignments_usecase.dart';
|
||||
part 'shifts_event.dart';
|
||||
part 'shifts_state.dart';
|
||||
|
||||
class ShiftsBloc extends Bloc<ShiftsEvent, ShiftsState> {
|
||||
class ShiftsBloc extends Bloc<ShiftsEvent, ShiftsState>
|
||||
with BlocErrorHandler<ShiftsState> {
|
||||
final GetMyShiftsUseCase getMyShifts;
|
||||
final GetAvailableShiftsUseCase getAvailableShifts;
|
||||
final GetPendingAssignmentsUseCase getPendingAssignments;
|
||||
@@ -43,33 +45,32 @@ class ShiftsBloc extends Bloc<ShiftsEvent, ShiftsState> {
|
||||
if (state is! ShiftsLoaded) {
|
||||
emit(ShiftsLoading());
|
||||
}
|
||||
|
||||
// Determine what to load based on current tab?
|
||||
// Or load all for simplicity as per prototype logic which had them all in memory.
|
||||
|
||||
try {
|
||||
final List<DateTime> days = _getCalendarDaysForOffset(0);
|
||||
final myShiftsResult = await getMyShifts(
|
||||
GetMyShiftsArguments(start: days.first, end: days.last),
|
||||
);
|
||||
|
||||
emit(ShiftsLoaded(
|
||||
myShifts: myShiftsResult,
|
||||
pendingShifts: const [],
|
||||
cancelledShifts: const [],
|
||||
availableShifts: const [],
|
||||
historyShifts: const [],
|
||||
availableLoading: false,
|
||||
availableLoaded: false,
|
||||
historyLoading: false,
|
||||
historyLoaded: false,
|
||||
myShiftsLoaded: true,
|
||||
searchQuery: '',
|
||||
jobType: 'all',
|
||||
));
|
||||
} catch (_) {
|
||||
emit(const ShiftsError('Failed to load shifts'));
|
||||
}
|
||||
|
||||
await handleError(
|
||||
emit: emit,
|
||||
action: () async {
|
||||
final List<DateTime> days = _getCalendarDaysForOffset(0);
|
||||
final myShiftsResult = await getMyShifts(
|
||||
GetMyShiftsArguments(start: days.first, end: days.last),
|
||||
);
|
||||
|
||||
emit(ShiftsLoaded(
|
||||
myShifts: myShiftsResult,
|
||||
pendingShifts: const [],
|
||||
cancelledShifts: const [],
|
||||
availableShifts: const [],
|
||||
historyShifts: const [],
|
||||
availableLoading: false,
|
||||
availableLoaded: false,
|
||||
historyLoading: false,
|
||||
historyLoaded: false,
|
||||
myShiftsLoaded: true,
|
||||
searchQuery: '',
|
||||
jobType: 'all',
|
||||
));
|
||||
},
|
||||
onError: (String errorKey) => ShiftsError(errorKey),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _onLoadHistoryShifts(
|
||||
@@ -81,17 +82,24 @@ class ShiftsBloc extends Bloc<ShiftsEvent, ShiftsState> {
|
||||
if (currentState.historyLoading || currentState.historyLoaded) return;
|
||||
|
||||
emit(currentState.copyWith(historyLoading: true));
|
||||
try {
|
||||
final historyResult = await getHistoryShifts();
|
||||
emit(currentState.copyWith(
|
||||
myShiftsLoaded: true,
|
||||
historyShifts: historyResult,
|
||||
historyLoading: false,
|
||||
historyLoaded: true,
|
||||
));
|
||||
} catch (_) {
|
||||
emit(currentState.copyWith(historyLoading: false));
|
||||
}
|
||||
await handleError(
|
||||
emit: emit,
|
||||
action: () async {
|
||||
final historyResult = await getHistoryShifts();
|
||||
emit(currentState.copyWith(
|
||||
myShiftsLoaded: true,
|
||||
historyShifts: historyResult,
|
||||
historyLoading: false,
|
||||
historyLoaded: true,
|
||||
));
|
||||
},
|
||||
onError: (String errorKey) {
|
||||
if (state is ShiftsLoaded) {
|
||||
return (state as ShiftsLoaded).copyWith(historyLoading: false);
|
||||
}
|
||||
return ShiftsError(errorKey);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _onLoadAvailableShifts(
|
||||
@@ -103,17 +111,24 @@ class ShiftsBloc extends Bloc<ShiftsEvent, ShiftsState> {
|
||||
if (currentState.availableLoading || currentState.availableLoaded) return;
|
||||
|
||||
emit(currentState.copyWith(availableLoading: true));
|
||||
try {
|
||||
final availableResult =
|
||||
await getAvailableShifts(const GetAvailableShiftsArguments());
|
||||
emit(currentState.copyWith(
|
||||
availableShifts: _filterPastShifts(availableResult),
|
||||
availableLoading: false,
|
||||
availableLoaded: true,
|
||||
));
|
||||
} catch (_) {
|
||||
emit(currentState.copyWith(availableLoading: false));
|
||||
}
|
||||
await handleError(
|
||||
emit: emit,
|
||||
action: () async {
|
||||
final availableResult =
|
||||
await getAvailableShifts(const GetAvailableShiftsArguments());
|
||||
emit(currentState.copyWith(
|
||||
availableShifts: _filterPastShifts(availableResult),
|
||||
availableLoading: false,
|
||||
availableLoaded: true,
|
||||
));
|
||||
},
|
||||
onError: (String errorKey) {
|
||||
if (state is ShiftsLoaded) {
|
||||
return (state as ShiftsLoaded).copyWith(availableLoading: false);
|
||||
}
|
||||
return ShiftsError(errorKey);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _onLoadFindFirst(
|
||||
@@ -137,81 +152,86 @@ class ShiftsBloc extends Bloc<ShiftsEvent, ShiftsState> {
|
||||
));
|
||||
}
|
||||
|
||||
final currentState =
|
||||
state is ShiftsLoaded ? state as ShiftsLoaded : null;
|
||||
final currentState = state is ShiftsLoaded ? state as ShiftsLoaded : null;
|
||||
if (currentState != null && currentState.availableLoaded) return;
|
||||
|
||||
if (currentState != null) {
|
||||
emit(currentState.copyWith(availableLoading: true));
|
||||
}
|
||||
|
||||
try {
|
||||
final availableResult =
|
||||
await getAvailableShifts(const GetAvailableShiftsArguments());
|
||||
final loadedState = state is ShiftsLoaded
|
||||
? state as ShiftsLoaded
|
||||
: const ShiftsLoaded(
|
||||
myShifts: [],
|
||||
pendingShifts: [],
|
||||
cancelledShifts: [],
|
||||
availableShifts: [],
|
||||
historyShifts: [],
|
||||
availableLoading: true,
|
||||
availableLoaded: false,
|
||||
historyLoading: false,
|
||||
historyLoaded: false,
|
||||
myShiftsLoaded: false,
|
||||
searchQuery: '',
|
||||
jobType: 'all',
|
||||
);
|
||||
emit(loadedState.copyWith(
|
||||
availableShifts: _filterPastShifts(availableResult),
|
||||
availableLoading: false,
|
||||
availableLoaded: true,
|
||||
));
|
||||
} catch (_) {
|
||||
if (state is ShiftsLoaded) {
|
||||
final current = state as ShiftsLoaded;
|
||||
emit(current.copyWith(availableLoading: false));
|
||||
}
|
||||
}
|
||||
await handleError(
|
||||
emit: emit,
|
||||
action: () async {
|
||||
final availableResult =
|
||||
await getAvailableShifts(const GetAvailableShiftsArguments());
|
||||
final loadedState = state is ShiftsLoaded
|
||||
? state as ShiftsLoaded
|
||||
: const ShiftsLoaded(
|
||||
myShifts: [],
|
||||
pendingShifts: [],
|
||||
cancelledShifts: [],
|
||||
availableShifts: [],
|
||||
historyShifts: [],
|
||||
availableLoading: true,
|
||||
availableLoaded: false,
|
||||
historyLoading: false,
|
||||
historyLoaded: false,
|
||||
myShiftsLoaded: false,
|
||||
searchQuery: '',
|
||||
jobType: 'all',
|
||||
);
|
||||
emit(loadedState.copyWith(
|
||||
availableShifts: _filterPastShifts(availableResult),
|
||||
availableLoading: false,
|
||||
availableLoaded: true,
|
||||
));
|
||||
},
|
||||
onError: (String errorKey) {
|
||||
if (state is ShiftsLoaded) {
|
||||
return (state as ShiftsLoaded).copyWith(availableLoading: false);
|
||||
}
|
||||
return ShiftsError(errorKey);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _onLoadShiftsForRange(
|
||||
LoadShiftsForRangeEvent event,
|
||||
Emitter<ShiftsState> emit,
|
||||
) async {
|
||||
try {
|
||||
final myShiftsResult = await getMyShifts(
|
||||
GetMyShiftsArguments(start: event.start, end: event.end),
|
||||
);
|
||||
await handleError(
|
||||
emit: emit,
|
||||
action: () async {
|
||||
final myShiftsResult = await getMyShifts(
|
||||
GetMyShiftsArguments(start: event.start, end: event.end),
|
||||
);
|
||||
|
||||
if (state is ShiftsLoaded) {
|
||||
final currentState = state as ShiftsLoaded;
|
||||
emit(currentState.copyWith(
|
||||
if (state is ShiftsLoaded) {
|
||||
final currentState = state as ShiftsLoaded;
|
||||
emit(currentState.copyWith(
|
||||
myShifts: myShiftsResult,
|
||||
myShiftsLoaded: true,
|
||||
));
|
||||
return;
|
||||
}
|
||||
|
||||
emit(ShiftsLoaded(
|
||||
myShifts: myShiftsResult,
|
||||
pendingShifts: const [],
|
||||
cancelledShifts: const [],
|
||||
availableShifts: const [],
|
||||
historyShifts: const [],
|
||||
availableLoading: false,
|
||||
availableLoaded: false,
|
||||
historyLoading: false,
|
||||
historyLoaded: false,
|
||||
myShiftsLoaded: true,
|
||||
searchQuery: '',
|
||||
jobType: 'all',
|
||||
));
|
||||
return;
|
||||
}
|
||||
|
||||
emit(ShiftsLoaded(
|
||||
myShifts: myShiftsResult,
|
||||
pendingShifts: const [],
|
||||
cancelledShifts: const [],
|
||||
availableShifts: const [],
|
||||
historyShifts: const [],
|
||||
availableLoading: false,
|
||||
availableLoaded: false,
|
||||
historyLoading: false,
|
||||
historyLoaded: false,
|
||||
myShiftsLoaded: true,
|
||||
searchQuery: '',
|
||||
jobType: 'all',
|
||||
));
|
||||
} catch (_) {
|
||||
emit(const ShiftsError('Failed to load shifts'));
|
||||
}
|
||||
},
|
||||
onError: (String errorKey) => ShiftsError(errorKey),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _onFilterAvailableShifts(
|
||||
@@ -224,23 +244,27 @@ class ShiftsBloc extends Bloc<ShiftsEvent, ShiftsState> {
|
||||
add(LoadAvailableShiftsEvent());
|
||||
return;
|
||||
}
|
||||
// Optimistic update or loading indicator?
|
||||
// Since it's filtering, we can just reload available.
|
||||
|
||||
try {
|
||||
final result = await getAvailableShifts(GetAvailableShiftsArguments(
|
||||
query: event.query ?? currentState.searchQuery,
|
||||
type: event.jobType ?? currentState.jobType,
|
||||
));
|
||||
|
||||
emit(currentState.copyWith(
|
||||
availableShifts: _filterPastShifts(result),
|
||||
searchQuery: event.query ?? currentState.searchQuery,
|
||||
jobType: event.jobType ?? currentState.jobType,
|
||||
));
|
||||
} catch (_) {
|
||||
// Error handling if filter fails
|
||||
}
|
||||
await handleError(
|
||||
emit: emit,
|
||||
action: () async {
|
||||
final result = await getAvailableShifts(GetAvailableShiftsArguments(
|
||||
query: event.query ?? currentState.searchQuery,
|
||||
type: event.jobType ?? currentState.jobType,
|
||||
));
|
||||
|
||||
emit(currentState.copyWith(
|
||||
availableShifts: _filterPastShifts(result),
|
||||
searchQuery: event.query ?? currentState.searchQuery,
|
||||
jobType: event.jobType ?? currentState.jobType,
|
||||
));
|
||||
},
|
||||
onError: (String errorKey) {
|
||||
// Stay on current state for filtering errors, maybe show a snackbar?
|
||||
// For now just logging is enough via handleError mixin.
|
||||
return currentState;
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -268,3 +292,4 @@ class ShiftsBloc extends Bloc<ShiftsEvent, ShiftsState> {
|
||||
}).toList();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user