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.

171 lines
6.0 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';
import 'package:bytedesk_kefu/model/uploadJsonResult.dart';
import 'package:bytedesk_kefu/repositories/message_repository.dart';
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);
on<LoadTopicMessageEvent>(_mapLoadTopicMessageToState);
3 years ago
3 years ago
on<LoadChannelMessageEvent>(_mapLoadChannelMessageToState);
on<QueryAnswerEvent>(_mapQueryAnswerToState);
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);
// }
// }
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) {
print(error);
}
}
3 years ago
void _mapUploadImageToState(UploadImageEvent event, Emitter<MessageState> emit) async {
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) {
print(error);
3 years ago
emit(UpLoadImageError());
3 years ago
}
}
3 years ago
void _mapUploadVideoToState(UploadVideoEvent event, Emitter<MessageState> emit) async {
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) {
print(error);
3 years ago
emit(UpLoadImageError());
3 years ago
}
}
3 years ago
void _mapSendMessageRestToState(SendMessageRestEvent event, Emitter<MessageState> emit) async {
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) {
print(error);
3 years ago
emit(SendMessageRestError());
3 years ago
}
}
3 years ago
void _mapLoadHistoryMessageToState(LoadHistoryMessageEvent event, Emitter<MessageState> emit) async {
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) {
print(error);
3 years ago
emit(LoadHistoryMessageError());
3 years ago
}
}
3 years ago
void _mapLoadTopicMessageToState(LoadTopicMessageEvent event, Emitter<MessageState> emit) async {
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) {
print(error);
3 years ago
emit(LoadTopicMessageError());
3 years ago
}
}
3 years ago
void _mapLoadChannelMessageToState(LoadChannelMessageEvent event, Emitter<MessageState> emit) async {
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) {
print(error);
3 years ago
emit(LoadChannelMessageError());
3 years ago
}
}
3 years ago
void _mapQueryAnswerToState(QueryAnswerEvent event, Emitter<MessageState> emit) async {
emit(MessageLoading());
3 years ago
try {
final RequestAnswerResult requestAnswerResult =
await messageRepository.queryAnswer(event.tid, event.aid);
3 years ago
emit(QueryAnswerSuccess(
query: requestAnswerResult.query, answer: requestAnswerResult.anwser));
3 years ago
} catch (error) {
print(error);
3 years ago
emit(UpLoadImageError());
3 years ago
}
}
3 years ago
void _mapMessageAnswerToState(MessageAnswerEvent event, Emitter<MessageState> emit) async {
emit(MessageLoading());
3 years ago
try {
final RequestAnswerResult requestAnswerResult = await messageRepository
.messageAnswer(event.type, event.wid, event.aid, event.content);
3 years ago
emit(MessageAnswerSuccess(
query: requestAnswerResult.query, answer: requestAnswerResult.anwser));
3 years ago
} catch (error) {
print(error);
3 years ago
emit(UpLoadImageError());
3 years ago
}
}
3 years ago
void _mapRateAnswerToState(RateAnswerEvent event, Emitter<MessageState> emit) async {
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) {
print(error);
3 years ago
emit(UpLoadImageError());
3 years ago
}
}
}