feat: refactor code
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import '../../../../core/resources/data_state.dart';
|
||||
import '../../../../core/usecase/use_case.dart';
|
||||
import '../../domain/use_cases/get_profile_usecase.dart';
|
||||
import '../../domain/use_cases/save_profile_usecase.dart';
|
||||
import 'profile_event.dart';
|
||||
import 'profile_state.dart';
|
||||
import 'profile_status.dart';
|
||||
|
||||
class ProfileBloc extends Bloc<ProfileEvent, ProfileBlocState> {
|
||||
final GetProfileUseCase getProfileUseCase;
|
||||
final SaveProfileUseCase saveProfileUseCase;
|
||||
|
||||
ProfileBloc(this.getProfileUseCase, this.saveProfileUseCase)
|
||||
: super(ProfileBlocState.initial()) {
|
||||
on<LoadProfileEvent>((event, emit) => _load(emit));
|
||||
|
||||
on<SaveProfileEvent>((event, emit) async {
|
||||
emit(state.copyWith(saveStatus: ProfileSaveLoading()));
|
||||
final res = await saveProfileUseCase(
|
||||
ProfileParams(event.firstName, event.avatar));
|
||||
if (res is DataSuccess) {
|
||||
emit(state.copyWith(saveStatus: ProfileSaveSuccess()));
|
||||
await _load(emit);
|
||||
} else {
|
||||
emit(state.copyWith(saveStatus: ProfileSaveError(res.error!)));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _load(Emitter<ProfileBlocState> emit) async {
|
||||
emit(state.copyWith(loadStatus: ProfileLoadLoading()));
|
||||
final res = await getProfileUseCase(const NoParams());
|
||||
if (res is DataSuccess) {
|
||||
emit(state.copyWith(loadStatus: ProfileLoadLoaded(res.data!)));
|
||||
} else {
|
||||
emit(state.copyWith(loadStatus: ProfileLoadError(res.error!)));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user