You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.5 KiB

3 years ago
import 'dart:async';
import 'package:bytedesk_kefu/blocs/leavemsg_bloc/bloc.dart';
import 'package:bloc/bloc.dart';
import 'package:bytedesk_kefu/repositories/leavemsg_repository.dart';
class LeaveMsgBloc extends Bloc<LeaveMsgEvent, LeaveMsgState> {
//
final LeaveMsgRepository leaveMsgRepository = new LeaveMsgRepository();
3 years ago
LeaveMsgBloc() : super(new UnLeaveMsgState()) {
on<SubmitLeaveMsgEvent>(_mapSubmitLeaveMsgToState);
on<UploadImageEvent>(_mapUploadImageToState);
3 years ago
}
3 years ago
// @override
// Stream<LeaveMsgState> mapEventToState(
// LeaveMsgEvent event,
// ) async* {
// if (event is SubmitLeaveMsgEvent) {
// yield* _mapSubmitLeaveMsgToState(event);
// } else if (event is UploadImageEvent) {
// yield* _mapUploadImageToState(event);
3 years ago
// }
// }
3 years ago
void _mapSubmitLeaveMsgToState(
SubmitLeaveMsgEvent event, Emitter<LeaveMsgState> emit) async {
emit(LeaveMsgSubmiting());
3 years ago
try {
// final JsonResult jsonResult =
await leaveMsgRepository.submitLeaveMsg(event.content, event.imageUrls);
3 years ago
emit(LeaveMsgSubmitSuccessState());
3 years ago
} catch (error) {
print(error);
3 years ago
emit(LeaveMsgSubmitError());
3 years ago
}
}
3 years ago
void _mapUploadImageToState(UploadImageEvent event, Emitter<LeaveMsgState> emit) async {
emit(ImageUploading());
3 years ago
try {
final String url = await leaveMsgRepository.upload(event.filePath);
3 years ago
emit(UploadImageSuccess(url));
3 years ago
} catch (error) {
print(error);
3 years ago
emit(UpLoadImageError());
3 years ago
}
}
}