// import 'dart:async'; 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/requestCategory.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 { // final MessageRepository messageRepository = new MessageRepository(); MessageBloc() : super(InitialMessageState()) { on(_mapRefreshCourseToState); on(_mapUploadImageToState); on(_mapUploadVideoToState); on(_mapSendMessageRestToState); on(_mapLoadHistoryMessageToState); on(_mapLoadTopicMessageToState); on(_mapLoadChannelMessageToState); on(_mapQueryAnswerToState); on(_mapQueryCategoryToState); on(_mapMessageAnswerToState); on(_mapRateAnswerToState); } // @override // void mapEventToState(MessageEvent event, Emitter 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 emit) async { try { emit(ReceiveMessageState(message: event.message)); } catch (error) { print(error); } } void _mapUploadImageToState(UploadImageEvent event, Emitter emit) async { emit(MessageUpLoading()); try { final UploadJsonResult uploadJsonResult = await messageRepository.uploadImage(event.filePath); emit(UploadImageSuccess(uploadJsonResult)); } catch (error) { print(error); emit(UpLoadImageError()); } } void _mapUploadVideoToState(UploadVideoEvent event, Emitter emit) async { emit(MessageUpLoading()); try { final UploadJsonResult uploadJsonResult = await messageRepository.uploadVideo(event.filePath); emit(UploadVideoSuccess(uploadJsonResult)); } catch (error) { print(error); emit(UpLoadImageError()); } } void _mapSendMessageRestToState(SendMessageRestEvent event, Emitter emit) async { emit(RestMessageSending()); try { final JsonResult jsonResult = await messageRepository.sendMessageRest(event.json); emit(SendMessageRestSuccess(jsonResult)); } catch (error) { print(error); emit(SendMessageRestError()); } } void _mapLoadHistoryMessageToState(LoadHistoryMessageEvent event, Emitter emit) async { emit(MessageLoading()); try { final List messageList = await messageRepository .loadHistoryMessages(event.uid, event.page, event.size); emit(LoadHistoryMessageSuccess(messageList: messageList)); } catch (error) { print(error); emit(LoadHistoryMessageError()); } } void _mapLoadTopicMessageToState(LoadTopicMessageEvent event, Emitter emit) async { emit(MessageLoading()); try { final List messageList = await messageRepository .loadTopicMessages(event.topic, event.page, event.size); emit(LoadTopicMessageSuccess(messageList: messageList)); } catch (error) { print(error); emit(LoadTopicMessageError()); } } void _mapLoadChannelMessageToState(LoadChannelMessageEvent event, Emitter emit) async { emit(MessageLoading()); try { final List messageList = await messageRepository .loadChannelMessages(event.cid, event.page, event.size); emit(LoadChannelMessageSuccess(messageList: messageList)); } catch (error) { print(error); emit(LoadChannelMessageError()); } } void _mapQueryAnswerToState(QueryAnswerEvent event, Emitter emit) async { emit(MessageLoading()); try { final RequestAnswerResult requestAnswerResult = await messageRepository.queryAnswer(event.tid, event.aid, event.mid); emit(QueryAnswerSuccess( query: requestAnswerResult.query, answer: requestAnswerResult.anwser)); } catch (error) { print(error); emit(UpLoadImageError()); } } void _mapQueryCategoryToState( QueryCategoryEvent event, Emitter 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) { print(error); emit(UpLoadImageError()); } } void _mapMessageAnswerToState(MessageAnswerEvent event, Emitter emit) async { emit(MessageLoading()); try { final RequestAnswerResult requestAnswerResult = await messageRepository .messageAnswer(event.wid, event.content); emit(MessageAnswerSuccess( query: requestAnswerResult.query, answer: requestAnswerResult.anwser)); } catch (error) { print(error); emit(UpLoadImageError()); } } void _mapRateAnswerToState(RateAnswerEvent event, Emitter emit) async { emit(MessageLoading()); try { final RequestAnswerResult requestAnswerResult = await messageRepository.rateAnswer(event.aid, event.mid, event.rate); emit(RateAnswerSuccess(result: requestAnswerResult.anwser)); } catch (error) { print(error); emit(UpLoadImageError()); } } }