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.

228 lines
7.3 KiB

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