Files
be_happy_public/lib/presentation/viewmodel/send_photo_bloc.dart
Polyanka 182be1049b some fix
# Conflicts:
#	lib/presentation/navigation/app_router.dart
#	pubspec.yaml
2026-08-11 00:23:16 +03:00

77 lines
2.6 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:io';
import 'package:be_happy/domain/entities/scooter_order.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../domain/usecase/finish_ride_usecase.dart';
import '../../domain/usecase/upload_scooter_photos_usecase.dart';
import '../event/send_photo_event.dart';
import '../state/send_photo_state.dart';
import '../../core/result.dart';
import '../../core/failures.dart';
class SendPhotoBloc extends Bloc<SendPhotoEvent, SendPhotoState> {
final UploadScooterPhotosUsecase uploadScooterPhotosUsecase;
final FinishRideUsecase _finishRideUsecase;
SendPhotoBloc(this.uploadScooterPhotosUsecase, this._finishRideUsecase) : super(const SendPhotoState()) {
on<PhotoSelected>(_onPhotoSelected);
on<PhotoUploadSubmitted>(_onPhotoUploadSubmitted);
}
void _onPhotoSelected(PhotoSelected event, Emitter<SendPhotoState> emit) {
emit(state.copyWith(selectedImages: event.imagePaths));
}
Future<void> _onPhotoUploadSubmitted(
PhotoUploadSubmitted event, Emitter<SendPhotoState> emit) async {
if (state.selectedImages.isEmpty) {
emit(state.copyWith(
status: SendPhotoStatus.failure,
errorMessage: 'Выберите хотя бы одно фото',
));
return;
}
emit(state.copyWith(status: SendPhotoStatus.loading));
final imageFiles = state.selectedImages.map((path) => File(path)).toList();
final result = await uploadScooterPhotosUsecase(imageFiles);
switch (result) {
case Success<List<int>>():
if (result.data == null || result.data!.isEmpty) {
emit(state.copyWith(
status: SendPhotoStatus.failure,
errorMessage: 'Не удалось получить ID загруженных фото',
));
return;
}
final finishResult = await _finishRideUsecase(
event.orderId,
result.data!
);
switch (finishResult) {
case Success():
emit(state.copyWith(
status: SendPhotoStatus.success,
recievedPhotoIds: result.data,
));
case Failure():
print("FINISH ERROR: ${finishResult.failure.message}");
emit(state.copyWith(
status: SendPhotoStatus.failure,
errorMessage: 'Ошибка завершения поездки',
));
}
case Failure():
emit(state.copyWith(
status: SendPhotoStatus.failure,
errorMessage: 'Ошибка загрузки фото: ${result.failure.message}',
));
}
}
}