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.

205 lines
6.5 KiB

3 years ago
// import 'dart:async';
3 years ago
import 'package:bytedesk_kefu/model/jsonResult.dart';
import 'package:bytedesk_kefu/model/user.dart';
import 'package:bytedesk_kefu/repositories/user_repository.dart';
import 'package:bloc/bloc.dart';
3 years ago
import 'package:bytedesk_kefu/util/bytedesk_utils.dart';
3 years ago
import './bloc.dart';
class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
//
final UserRepository userRepository = new UserRepository();
3 years ago
ProfileBloc() : super(InitialProfileState()) {
on<GetProfileEvent>(_mapProfileState);
on<UploadImageEvent>(_mapUploadImageToState);
on<UpdateAvatarEvent>(_mapUpdateAvatarToState);
on<UpdateNicknameEvent>(_mapUpdateNicknameToState);
on<UpdateDescriptionEvent>(_mapUpdateDescriptionToState);
on<UpdateMobileEvent>(_mapUpdateMobileToState);
on<UpdateSexEvent>(_mapUpdateSexToState);
on<UpdateLocationEvent>(_mapUpdateLocationToState);
on<UpdateBirthdayEvent>(_mapUpdateBirthdayToState);
on<QueryFollowEvent>(_mapQueryFollowToState);
on<UserFollowEvent>(_mapUserFollowToState);
on<UserUnfollowEvent>(_mapUserUnfollowToState);
3 years ago
}
3 years ago
// @override
// void mapEventToState(ProfileEvent event, Emitter<ProfileState> emit) async {
// //
// if (event is GetProfileEvent) {
// yield* _mapProfileState();
// } else if (event is UploadImageEvent) {
// yield* _mapUploadImageToState(event);
// } else if (event is UpdateAvatarEvent) {
// yield* _mapUpdateAvatarToState(event);
// } else if (event is UpdateNicknameEvent) {
// yield* _mapUpdateNicknameToState(event);
// } else if (event is UpdateDescriptionEvent) {
// yield* _mapUpdateDescriptionToState(event);
// } else if (event is UpdateMobileEvent) {
// yield* _mapUpdateMobileToState(event);
// } else if (event is UpdateSexEvent) {
// yield* _mapUpdateSexToState(event);
// } else if (event is UpdateLocationEvent) {
// yield* _mapUpdateLocationToState(event);
// } else if (event is UpdateBirthdayEvent) {
// yield* _mapUpdateBirthdayToState(event);
// } else if (event is QueryFollowEvent) {
// yield* _mapQueryFollowToState(event);
// } else if (event is UserFollowEvent) {
// yield* _mapUserFollowToState(event);
// } else if (event is UserUnfollowEvent) {
// yield* _mapUserUnfollowToState(event);
// }
// }
3 years ago
void _mapProfileState(
GetProfileEvent event, Emitter<ProfileState> emit) async {
3 years ago
emit(ProfileInProgress());
3 years ago
try {
User user = await userRepository.getProfile();
3 years ago
emit(ProfileSuccess(user: user));
3 years ago
} catch (error) {
// 网络或其他错误
3 years ago
emit(ProfileFailure(error: error.toString()));
3 years ago
}
}
3 years ago
void _mapUploadImageToState(
UploadImageEvent event, Emitter<ProfileState> emit) async {
3 years ago
emit(ProfileInProgress());
3 years ago
try {
final String url = await userRepository.upload(event.filePath);
3 years ago
emit(UploadImageSuccess(url));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(UpLoadImageError());
3 years ago
}
}
3 years ago
void _mapUpdateAvatarToState(
UpdateAvatarEvent event, Emitter<ProfileState> emit) async {
3 years ago
emit(ProfileInProgress());
3 years ago
try {
final User user = await userRepository.updateAvatar(event.avatar);
3 years ago
emit(UpdateAvatarSuccess(user));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(UpLoadImageError());
3 years ago
}
}
3 years ago
void _mapUpdateNicknameToState(
UpdateNicknameEvent event, Emitter<ProfileState> emit) async {
3 years ago
emit(ProfileInProgress());
3 years ago
try {
final User user = await userRepository.updateNickname(event.nickname);
3 years ago
emit(UpdateNicknameSuccess(user));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(UpLoadImageError());
3 years ago
}
}
3 years ago
void _mapUpdateDescriptionToState(
UpdateDescriptionEvent event, Emitter<ProfileState> emit) async {
3 years ago
emit(ProfileInProgress());
3 years ago
try {
final User user =
await userRepository.updateDescription(event.description);
3 years ago
emit(UpdateDescriptionSuccess(user));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(UpLoadImageError());
3 years ago
}
}
3 years ago
void _mapUpdateMobileToState(
UpdateMobileEvent event, Emitter<ProfileState> emit) async {
3 years ago
emit(ProfileInProgress());
3 years ago
try {
final User user = await userRepository.updateMobile(event.mobile);
3 years ago
emit(UpdateMobileSuccess(user));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(UpLoadImageError());
3 years ago
}
}
3 years ago
void _mapUpdateSexToState(
UpdateSexEvent event, Emitter<ProfileState> emit) async {
3 years ago
emit(ProfileInProgress());
3 years ago
try {
final User user = await userRepository.updateSex(event.sex);
3 years ago
emit(UpdateSexSuccess(user));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(UpLoadImageError());
3 years ago
}
}
3 years ago
void _mapUpdateLocationToState(
UpdateLocationEvent event, Emitter<ProfileState> emit) async {
3 years ago
emit(ProfileInProgress());
3 years ago
try {
final User user = await userRepository.updateLocation(event.location);
3 years ago
emit(UpdateLocationSuccess(user));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(UpLoadImageError());
3 years ago
}
}
3 years ago
void _mapUpdateBirthdayToState(
UpdateBirthdayEvent event, Emitter<ProfileState> emit) async {
3 years ago
emit(ProfileInProgress());
3 years ago
try {
final User user = await userRepository.updateBirthday(event.birthday);
3 years ago
emit(UpdateBirthdaySuccess(user));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(UpLoadImageError());
3 years ago
}
}
3 years ago
void _mapQueryFollowToState(
QueryFollowEvent event, Emitter<ProfileState> emit) async {
3 years ago
emit(QueryFollowing());
3 years ago
try {
final bool isFollowed = await userRepository.isFollowed(event.uid);
3 years ago
emit(QueryFollowSuccess(isFollowed));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(QueryFollowError());
3 years ago
}
}
3 years ago
void _mapUserFollowToState(
UserFollowEvent event, Emitter<ProfileState> emit) async {
3 years ago
emit(Following());
3 years ago
try {
final JsonResult jsonResult = await userRepository.follow(event.uid);
3 years ago
emit(FollowResultSuccess(jsonResult));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(FollowError());
3 years ago
}
}
3 years ago
void _mapUserUnfollowToState(
UserUnfollowEvent event, Emitter<ProfileState> emit) async {
3 years ago
emit(Following());
3 years ago
try {
final JsonResult jsonResult = await userRepository.unfollow(event.uid);
3 years ago
emit(UnfollowResultSuccess(jsonResult));
3 years ago
} catch (error) {
3 years ago
BytedeskUtils.printLog(error);
3 years ago
emit(UnFollowError());
3 years ago
}
}
}