// import 'dart:async'; import 'package:bloc/bloc.dart'; import 'package:bytedesk_kefu/blocs/friend_bloc/bloc.dart'; import 'package:bytedesk_kefu/model/friend.dart'; import 'package:bytedesk_kefu/repositories/friend_repository.dart'; class FriendBloc extends Bloc { final FriendRepository friendRepository = new FriendRepository(); FriendBloc() : super(UnFriendState()) { on(_mapQueryFriendToState); on(_mapQueryFriendAddressToState); on(_mapUploadFriendAddressToState); on(_mapQueryFriendNearbyToState); on(_mapUpdateFriendNearbyToState); } // @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); // } // } void _mapQueryFriendToState(QueryFriendEvent event, Emitter emit) async { emit(FriendLoading()); try { final List friendList = await friendRepository.getFriends(event.page, event.size); emit(FriendLoadSuccess(friendList)); } catch (error) { print(error); emit(ErrorFriendState('friend error')); } } void _mapQueryFriendAddressToState( QueryFriendAddressEvent event, Emitter emit) async { emit(FriendLoading()); try { final List friendList = await friendRepository.getFriendsAddress(event.page, event.size); emit(FriendLoadSuccess(friendList)); } catch (error) { print(error); emit(ErrorFriendState('friend error')); } } void _mapUploadFriendAddressToState( UploadFriendAddressEvent event, Emitter emit) async { emit(FriendLoading()); try { final Friend friend = await friendRepository.uploadAddress(event.nickname, event.mobile); emit(FriendCreateSuccess(friend: friend)); } catch (error) { print(error); emit(ErrorFriendState('friend error')); } } void _mapQueryFriendNearbyToState( QueryFriendNearbyEvent event, Emitter emit) async { emit(FriendLoading()); try { final List friendList = await friendRepository.getFriendsNearby(event.page, event.size); emit(FriendLoadSuccess(friendList)); } catch (error) { print(error); emit(ErrorFriendState('friend error')); } } void _mapUpdateFriendNearbyToState( UpdateFriendNearbyEvent event, Emitter emit) async { emit(FriendLoading()); try { await friendRepository.updateLocation(event.latitude, event.longtitude); emit(FriendUpdateSuccess()); } catch (error) { print(error); emit(ErrorFriendState('friend error')); } } }