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.

243 lines
8.5 KiB

3 years ago
// import 'dart:async';
3 years ago
import 'package:bytedesk_kefu/model/jsonResult.dart';
import 'package:bytedesk_kefu/model/message.dart';
import 'package:bytedesk_kefu/model/requestAnswer.dart';
3 years ago
import 'package:bytedesk_kefu/model/requestCategory.dart';
3 years ago
import 'package:bytedesk_kefu/model/uploadJsonResult.dart';
import 'package:bytedesk_kefu/repositories/message_repository.dart';
3 years ago
import 'package:bytedesk_kefu/util/bytedesk_utils.dart';
3 years ago
import 'package:bloc/bloc.dart';
import './bloc.dart';
class MessageBloc extends Bloc<MessageEvent, MessageState> {
//
final MessageRepository messageRepository = new MessageRepository();
3 years ago
MessageBloc() : super(InitialMessageState()) {
on<ReceiveMessageEvent>(_mapRefreshCourseToState);
on<UploadImageEvent>(_mapUploadImageToState);
on<UploadVideoEvent>(_mapUploadVideoToState);
on<SendMessageRestEvent>(_mapSendMessageRestToState);
on<LoadHistoryMessageEvent>(_mapLoadHistoryMessageToState);
3 years ago
on<LoadUnreadMessagesEvent>(_mapLoadUnreadMessageToState);
on<LoadUnreadVisitorMessagesEvent>(_mapLoadUnreadVisitorMessageToState);
on<LoadUnreadAgentMessagesEvent>(_mapLoadUnreadAgentMessageToState);
3 years ago
on<LoadTopicMessageEvent>(_mapLoadTopicMessageToState);
3 years ago
3 years ago
on<LoadChannelMessageEvent>(_mapLoadChannelMessageToState);
on<QueryAnswerEvent>(_mapQueryAnswerToState);
3 years ago
on<QueryCategoryEvent>(_mapQueryCategoryToState);
3 years ago
on<MessageAnswerEvent>(_mapMessageAnswerToState);
on<RateAnswerEvent>(_mapRateAnswerToState);
3 years ago
}
3 years ago
// @override
// void mapEventToState(MessageEvent event, Emitter<MessageState> emit) async {
// if (event is ReceiveMessageEvent) {
// yield* _mapRefreshCourseToState(event);
// } else if (event is UploadImageEvent) {
// yield* _mapUploadImageToState(event);
// } else if (event is UploadVideoEvent) {
// yield* _mapUploadVideoToState(event);
// } else if (event is QueryAnswerEvent) {
// yield* _mapQueryAnswerToState(event);
// } else if (event is MessageAnswerEvent) {
// yield* _mapMessageAnswerToState(event);
// } else if (event is RateAnswerEvent) {
// yield* _mapRateAnswerToState(event);
// } else if (event is LoadHistoryMessageEvent) {
// yield* _mapLoadHistoryMessageToState(event);
// } else if (event is LoadTopicMessageEvent) {
// yield* _mapLoadTopicMessageToState(event);
// } else if (event is LoadChannelMessageEvent) {
// yield* _mapLoadChannelMessageToState(event);
// } else if (event is SendMessageRestEvent) {
// yield* _mapSendMessageRestToState(event);
// }
// }
3 years ago
void _mapRefreshCourseToState(
ReceiveMessageEvent event, Emitter<MessageState> emit) async {
3 years ago
try {
3 years ago
emit(ReceiveMessageState(message: event.message));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
}
}
3 years ago
void _mapUploadImageToState(
UploadImageEvent event, Emitter<MessageState> emit) async {
3 years ago
emit(MessageUpLoading());
3 years ago
try {
final UploadJsonResult uploadJsonResult =
await messageRepository.uploadImage(event.filePath);
3 years ago
emit(UploadImageSuccess(uploadJsonResult));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(UpLoadImageError());
3 years ago
}
}
3 years ago
void _mapUploadVideoToState(
UploadVideoEvent event, Emitter<MessageState> emit) async {
3 years ago
emit(MessageUpLoading());
3 years ago
try {
final UploadJsonResult uploadJsonResult =
await messageRepository.uploadVideo(event.filePath);
3 years ago
emit(UploadVideoSuccess(uploadJsonResult));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(UpLoadImageError());
3 years ago
}
}
3 years ago
void _mapSendMessageRestToState(
SendMessageRestEvent event, Emitter<MessageState> emit) async {
3 years ago
emit(RestMessageSending());
3 years ago
try {
final JsonResult jsonResult =
await messageRepository.sendMessageRest(event.json);
3 years ago
emit(SendMessageRestSuccess(jsonResult));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
2 years ago
emit(SendMessageRestError(event.json!));
3 years ago
}
}
3 years ago
void _mapLoadHistoryMessageToState(
LoadHistoryMessageEvent event, Emitter<MessageState> emit) async {
3 years ago
emit(MessageLoading());
3 years ago
try {
final List<Message> messageList = await messageRepository
.loadHistoryMessages(event.uid, event.page, event.size);
3 years ago
emit(LoadHistoryMessageSuccess(messageList: messageList));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(LoadHistoryMessageError());
3 years ago
}
}
3 years ago
void _mapLoadTopicMessageToState(
LoadTopicMessageEvent event, Emitter<MessageState> emit) async {
3 years ago
emit(MessageLoading());
3 years ago
try {
final List<Message> messageList = await messageRepository
.loadTopicMessages(event.topic, event.page, event.size);
3 years ago
emit(LoadTopicMessageSuccess(messageList: messageList));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(LoadTopicMessageError());
3 years ago
}
}
3 years ago
void _mapLoadChannelMessageToState(
LoadChannelMessageEvent event, Emitter<MessageState> emit) async {
3 years ago
emit(MessageLoading());
3 years ago
try {
final List<Message> messageList = await messageRepository
.loadChannelMessages(event.cid, event.page, event.size);
3 years ago
emit(LoadChannelMessageSuccess(messageList: messageList));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(LoadChannelMessageError());
3 years ago
}
}
3 years ago
void _mapLoadUnreadMessageToState(
LoadUnreadMessagesEvent event, Emitter<MessageState> emit) async {
emit(MessageLoading());
try {
final List<Message> messageList = await messageRepository
.loadUnreadMessages(event.wid, event.page, event.size);
emit(LoadUnreadMessageSuccess(messageList: messageList));
} catch (error) {
BytedeskUtils.printLog(error);
emit(LoadUnreadMessageError());
}
}
void _mapLoadUnreadVisitorMessageToState(
LoadUnreadVisitorMessagesEvent event, Emitter<MessageState> emit) async {
emit(MessageLoading());
try {
final List<Message> messageList = await messageRepository
.loadUnreadVisitorMessages(event.page, event.size);
emit(LoadUnreadVisitorMessageSuccess(messageList: messageList));
} catch (error) {
BytedeskUtils.printLog(error);
emit(LoadUnreadMessageError());
}
}
void _mapLoadUnreadAgentMessageToState(
LoadUnreadAgentMessagesEvent event, Emitter<MessageState> emit) async {
emit(MessageLoading());
try {
final List<Message> messageList = await messageRepository
.loadUnreadAgentMessages(event.page, event.size);
emit(LoadUnreadAgentMessageSuccess(messageList: messageList));
} catch (error) {
BytedeskUtils.printLog(error);
emit(LoadUnreadAgentMessageError());
}
}
void _mapQueryAnswerToState(
QueryAnswerEvent event, Emitter<MessageState> emit) async {
3 years ago
emit(MessageLoading());
3 years ago
try {
final RequestAnswerResult requestAnswerResult =
3 years ago
await messageRepository.queryAnswer(event.tid, event.aid, event.mid);
3 years ago
emit(QueryAnswerSuccess(
3 years ago
query: requestAnswerResult.query,
answer: requestAnswerResult.anwser));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(UpLoadImageError());
3 years ago
}
}
3 years ago
void _mapQueryCategoryToState(
QueryCategoryEvent event, Emitter<MessageState> emit) async {
emit(MessageLoading());
try {
final RequestCategoryResult requestAnswerResult =
await messageRepository.queryCategory(event.tid, event.cid);
emit(QueryCategorySuccess(
query: requestAnswerResult.query,
answer: requestAnswerResult.anwser));
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(UpLoadImageError());
}
}
3 years ago
void _mapMessageAnswerToState(
MessageAnswerEvent event, Emitter<MessageState> emit) async {
3 years ago
emit(MessageLoading());
3 years ago
try {
3 years ago
final RequestAnswerResult requestAnswerResult =
await messageRepository.messageAnswer(event.wid, event.content);
3 years ago
emit(MessageAnswerSuccess(
3 years ago
query: requestAnswerResult.query,
answer: requestAnswerResult.anwser));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(UpLoadImageError());
3 years ago
}
}
3 years ago
void _mapRateAnswerToState(
RateAnswerEvent event, Emitter<MessageState> emit) async {
3 years ago
emit(MessageLoading());
3 years ago
try {
final RequestAnswerResult requestAnswerResult =
await messageRepository.rateAnswer(event.aid, event.mid, event.rate);
3 years ago
emit(RateAnswerSuccess(result: requestAnswerResult.anwser));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(UpLoadImageError());
3 years ago
}
}
}