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!)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
abstract class ProfileEvent {}
|
||||
|
||||
class LoadProfileEvent extends ProfileEvent {}
|
||||
|
||||
class SaveProfileEvent extends ProfileEvent {
|
||||
final String firstName;
|
||||
final String avatar;
|
||||
SaveProfileEvent(this.firstName, this.avatar);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import 'profile_status.dart';
|
||||
|
||||
class ProfileBlocState {
|
||||
final ProfileLoadStatus loadStatus;
|
||||
final ProfileSaveStatus saveStatus;
|
||||
|
||||
ProfileBlocState({required this.loadStatus, required this.saveStatus});
|
||||
|
||||
factory ProfileBlocState.initial() => ProfileBlocState(
|
||||
loadStatus: ProfileLoadInitial(),
|
||||
saveStatus: ProfileSaveIdle(),
|
||||
);
|
||||
|
||||
ProfileBlocState copyWith({
|
||||
ProfileLoadStatus? loadStatus,
|
||||
ProfileSaveStatus? saveStatus,
|
||||
}) =>
|
||||
ProfileBlocState(
|
||||
loadStatus: loadStatus ?? this.loadStatus,
|
||||
saveStatus: saveStatus ?? this.saveStatus,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import '../../domain/entities/profile_entity.dart';
|
||||
|
||||
abstract class ProfileLoadStatus {}
|
||||
|
||||
class ProfileLoadInitial extends ProfileLoadStatus {}
|
||||
|
||||
class ProfileLoadLoading extends ProfileLoadStatus {}
|
||||
|
||||
class ProfileLoadLoaded extends ProfileLoadStatus {
|
||||
final ProfileEntity profile;
|
||||
ProfileLoadLoaded(this.profile);
|
||||
}
|
||||
|
||||
class ProfileLoadError extends ProfileLoadStatus {
|
||||
final String message;
|
||||
ProfileLoadError(this.message);
|
||||
}
|
||||
|
||||
/// وضعیتِ ذخیرهی ویرایش پروفایل.
|
||||
abstract class ProfileSaveStatus {}
|
||||
|
||||
class ProfileSaveIdle extends ProfileSaveStatus {}
|
||||
|
||||
class ProfileSaveLoading extends ProfileSaveStatus {}
|
||||
|
||||
class ProfileSaveSuccess extends ProfileSaveStatus {}
|
||||
|
||||
class ProfileSaveError extends ProfileSaveStatus {
|
||||
final String message;
|
||||
ProfileSaveError(this.message);
|
||||
}
|
||||
Reference in New Issue
Block a user