// import 'dart:async'; import 'package:bytedesk_kefu/model/markThread.dart'; import 'package:bloc/bloc.dart'; import './bloc.dart'; import 'package:bytedesk_kefu/repositories/repositories.dart'; import 'package:bytedesk_kefu/model/model.dart'; class ThreadBloc extends Bloc { // final ThreadRepository threadRepository = new ThreadRepository(); // ThreadBloc() : super(ThreadEmpty()); ThreadBloc() : super(ThreadEmpty()) { // on(InitialThreadState); on(_mapRefreshThreadToState); on(_mapRefreshHistoryThreadToState); on(_mapRefreshVisitorThreadToState); on(_mapRefreshVisitorThreadAllToState); on(_mapRequestThreadToState); on(_mapRequestAgentToState); on(_mapRequestContactThreadToState); on(_mapRequestGroupThreadToState); on(_mapMarkTopThreadEventToState); on(_mapUnMarkTopThreadEventToState); on(_mapMarkNodisturbThreadEventToState); on(_mapUnMarkNodisturbThreadEventToState); on(_mapMarkUnreadThreadEventToState); on(_mapUnMarkUnreadThreadEventToState); on(_mapDeleteThreadEventToState); } void _mapRefreshThreadToState( RefreshThreadEvent event, Emitter emit) async { emit(ThreadLoading()); try { final List threadList = await threadRepository.getThreads(); emit(ThreadLoadSuccess(threadList)); } catch (error) { print(error); emit(ThreadLoadError()); } } void _mapRefreshHistoryThreadToState( RefreshHistoryThreadEvent event, Emitter emit) async { emit(ThreadHistoryLoading()); try { final List threadList = await threadRepository.getHistoryThreads(event.page, event.size); emit(ThreadLoadSuccess(threadList)); } catch (error) { print(error); emit(ThreadLoadError()); } } void _mapRefreshVisitorThreadToState( RefreshVisitorThreadEvent event, Emitter emit) async { emit(ThreadVisitorLoading()); try { final List threadList = await threadRepository.getVisitorThreads(event.page, event.size); emit(ThreadLoadSuccess(threadList)); } catch (error) { print(error); emit(ThreadLoadError()); } } void _mapRefreshVisitorThreadAllToState( RefreshVisitorThreadAllEvent event, Emitter emit) async { emit(ThreadLoading()); try { final List threadList = await threadRepository.getVisitorThreadsAll(); emit(ThreadLoadSuccess(threadList)); } catch (error) { print(error); emit(ThreadLoadError()); } } void _mapRequestThreadToState( RequestThreadEvent event, Emitter emit) async { print('RequestThreadEvent'); emit(RequestThreading()); try { final RequestThreadResult thread = await threadRepository.requestThread( event.wid, event.type, event.aid); emit(RequestThreadSuccess(thread)); } catch (error) { print(error); emit(RequestThreadError()); } } void _mapRequestAgentToState( RequestAgentEvent event, Emitter emit) async { emit(RequestAgentThreading()); try { final RequestThreadResult thread = await threadRepository.requestAgent(event.wid, event.type, event.aid); emit(RequestAgentSuccess(thread)); } catch (error) { print(error); emit(RequestAgentThreadError()); } } void _mapRequestContactThreadToState( RequestContactThreadEvent event, Emitter emit) async { emit(ThreadLoading()); try { final RequestThreadResult thread = await threadRepository.requestContactThread(event.cid); emit(RequestContactThreadSuccess(thread)); } catch (error) { print(error); emit(ThreadLoadError()); } } void _mapRequestGroupThreadToState( RequestGroupThreadEvent event, Emitter emit) async { emit(ThreadLoading()); try { final RequestThreadResult thread = await threadRepository.requestGroupThread(event.gid); emit(RequestGroupThreadSuccess(thread)); } catch (error) { print(error); emit(ThreadLoadError()); } } void _mapMarkTopThreadEventToState( MarkTopThreadEvent event, Emitter emit) async { emit(ThreadLoading()); try { final MarkThreadResult thread = await threadRepository.markTop(event.tid); emit(MarkTopThreadSuccess(thread)); } catch (error) { print(error); emit(ThreadLoadError()); } } void _mapUnMarkTopThreadEventToState( UnMarkTopThreadEvent event, Emitter emit) async { emit(ThreadLoading()); try { final MarkThreadResult thread = await threadRepository.unmarkTop(event.tid); emit(UnMarkTopThreadSuccess(thread)); } catch (error) { print(error); emit(ThreadLoadError()); } } void _mapMarkNodisturbThreadEventToState( MarkNodisturbThreadEvent event, Emitter emit) async { emit(ThreadLoading()); try { final MarkThreadResult thread = await threadRepository.markNodisturb(event.tid); emit(MarkNodisturbThreadSuccess(thread)); } catch (error) { print(error); emit(ThreadLoadError()); } } void _mapUnMarkNodisturbThreadEventToState( UnMarkNodisturbThreadEvent event, Emitter emit) async { emit(ThreadLoading()); try { final MarkThreadResult thread = await threadRepository.unmarkNodisturb(event.tid); emit(UnMarkNodisturbThreadSuccess(thread)); } catch (error) { print(error); emit(ThreadLoadError()); } } void _mapMarkUnreadThreadEventToState( MarkUnreadThreadEvent event, Emitter emit) async { emit(ThreadLoading()); try { final MarkThreadResult thread = await threadRepository.markUnread(event.tid); emit(MarkUnreadThreadSuccess(thread)); } catch (error) { print(error); emit(ThreadLoadError()); } } void _mapUnMarkUnreadThreadEventToState( UnMarkUnreadThreadEvent event, Emitter emit) async { emit(ThreadLoading()); try { final MarkThreadResult thread = await threadRepository.unmarkUnread(event.tid); emit(UnMarkUnreadThreadSuccess(thread)); } catch (error) { print(error); emit(ThreadLoadError()); } } void _mapDeleteThreadEventToState( DeleteThreadEvent event, Emitter emit) async { emit(ThreadLoading()); try { final MarkThreadResult thread = await threadRepository.delete(event.tid); emit(DeleteThreadSuccess(thread)); } catch (error) { print(error); emit(ThreadLoadError()); } } }