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.

99 lines
3.3 KiB

3 years ago
// import 'dart:async';
3 years ago
import 'package:bloc/bloc.dart';
import 'package:bytedesk_kefu/blocs/friend_bloc/bloc.dart';
import 'package:bytedesk_kefu/model/friend.dart';
3 years ago
import 'package:bytedesk_kefu/util/bytedesk_utils.dart';
3 years ago
import 'package:bytedesk_kefu/repositories/friend_repository.dart';
class FriendBloc extends Bloc<FriendEvent, FriendState> {
final FriendRepository friendRepository = new FriendRepository();
3 years ago
FriendBloc() : super(UnFriendState()) {
on<QueryFriendEvent>(_mapQueryFriendToState);
on<QueryFriendAddressEvent>(_mapQueryFriendAddressToState);
on<UploadFriendAddressEvent>(_mapUploadFriendAddressToState);
on<QueryFriendNearbyEvent>(_mapQueryFriendNearbyToState);
on<UpdateFriendNearbyEvent>(_mapUpdateFriendNearbyToState);
3 years ago
}
3 years ago
// @override
// void mapEventToState(FriendEvent event) async {
// //
// if (event is QueryFriendEvent) {
// yield* _mapQueryFriendToState(event);
// } else if (event is UploadFriendAddressEvent) {
// yield* _mapUploadFriendAddressToState(event);
// } else if (event is QueryFriendAddressEvent) {
// yield* _mapQueryFriendAddressToState(event);
// } else if (event is QueryFriendNearbyEvent) {
// yield* _mapQueryFriendNearbyToState(event);
// } else if (event is UpdateFriendNearbyEvent) {
// yield* _mapUpdateFriendNearbyToState(event);
// }
// }
3 years ago
void _mapQueryFriendToState(
QueryFriendEvent event, Emitter<FriendState> emit) async {
3 years ago
emit(FriendLoading());
3 years ago
try {
final List<Friend> friendList =
await friendRepository.getFriends(event.page, event.size);
3 years ago
emit(FriendLoadSuccess(friendList));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(ErrorFriendState('friend error'));
3 years ago
}
}
3 years ago
void _mapQueryFriendAddressToState(
QueryFriendAddressEvent event, Emitter<FriendState> emit) async {
emit(FriendLoading());
3 years ago
try {
final List<Friend> friendList =
await friendRepository.getFriendsAddress(event.page, event.size);
3 years ago
emit(FriendLoadSuccess(friendList));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(ErrorFriendState('friend error'));
3 years ago
}
}
3 years ago
void _mapUploadFriendAddressToState(
UploadFriendAddressEvent event, Emitter<FriendState> emit) async {
emit(FriendLoading());
3 years ago
try {
final Friend friend =
await friendRepository.uploadAddress(event.nickname, event.mobile);
3 years ago
emit(FriendCreateSuccess(friend: friend));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(ErrorFriendState('friend error'));
3 years ago
}
}
3 years ago
void _mapQueryFriendNearbyToState(
QueryFriendNearbyEvent event, Emitter<FriendState> emit) async {
emit(FriendLoading());
3 years ago
try {
final List<Friend> friendList =
await friendRepository.getFriendsNearby(event.page, event.size);
3 years ago
emit(FriendLoadSuccess(friendList));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(ErrorFriendState('friend error'));
3 years ago
}
}
3 years ago
void _mapUpdateFriendNearbyToState(
UpdateFriendNearbyEvent event, Emitter<FriendState> emit) async {
emit(FriendLoading());
3 years ago
try {
await friendRepository.updateLocation(event.latitude, event.longtitude);
3 years ago
emit(FriendUpdateSuccess());
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(ErrorFriendState('friend error'));
3 years ago
}
}
}