feat: refactor code
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import '../../../../core/resources/data_state.dart';
|
||||
import '../../../../core/usecase/use_case.dart';
|
||||
import '../../domain/use_cases/ad_reward_usecase.dart';
|
||||
import '../../domain/use_cases/buy_card_usecase.dart';
|
||||
import '../../domain/use_cases/get_shop_usecase.dart';
|
||||
import '../../domain/use_cases/purchase_usecase.dart';
|
||||
import '../../domain/use_cases/select_card_usecase.dart';
|
||||
import 'shop_event.dart';
|
||||
import 'shop_state.dart';
|
||||
import 'shop_status.dart';
|
||||
|
||||
class ShopBloc extends Bloc<ShopEvent, ShopBlocState> {
|
||||
final GetShopUseCase getShopUseCase;
|
||||
final BuyCardUseCase buyCardUseCase;
|
||||
final SelectCardUseCase selectCardUseCase;
|
||||
final PurchaseUseCase purchaseUseCase;
|
||||
final AdRewardUseCase adRewardUseCase;
|
||||
|
||||
ShopBloc(
|
||||
this.getShopUseCase,
|
||||
this.buyCardUseCase,
|
||||
this.selectCardUseCase,
|
||||
this.purchaseUseCase,
|
||||
this.adRewardUseCase,
|
||||
) : super(ShopBlocState.initial()) {
|
||||
on<LoadShopEvent>((event, emit) => _load(emit));
|
||||
|
||||
on<BuyCardEvent>((event, emit) =>
|
||||
_action(emit, () => buyCardUseCase(event.cardId)));
|
||||
|
||||
on<SelectCardEvent>((event, emit) =>
|
||||
_action(emit, () => selectCardUseCase(event.cardId)));
|
||||
|
||||
on<PurchaseEvent>((event, emit) => _action(
|
||||
emit,
|
||||
() => purchaseUseCase(PurchaseParams(
|
||||
store: 'bazaar',
|
||||
kind: event.kind,
|
||||
productId: event.productId,
|
||||
token:
|
||||
'dev-${event.kind}-${event.productId}-${DateTime.now().millisecondsSinceEpoch}',
|
||||
))));
|
||||
|
||||
on<AdRewardEvent>((event, emit) => _action(
|
||||
emit,
|
||||
() async {
|
||||
final res = await adRewardUseCase(
|
||||
'dev-ad-${DateTime.now().millisecondsSinceEpoch}');
|
||||
if (res is DataSuccess) {
|
||||
return const DataSuccess('سکه رایگان دریافت شد');
|
||||
}
|
||||
return DataError<String>(res.error!);
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
Future<void> _load(Emitter<ShopBlocState> emit) async {
|
||||
emit(state.copyWith(loadStatus: ShopLoading()));
|
||||
final res = await getShopUseCase(const NoParams());
|
||||
if (res is DataSuccess) {
|
||||
emit(state.copyWith(loadStatus: ShopLoaded(res.data!)));
|
||||
} else {
|
||||
emit(state.copyWith(loadStatus: ShopLoadError(res.error!)));
|
||||
}
|
||||
}
|
||||
|
||||
/// اجرای یک عملیات، نمایش وضعیت و سپس بازخوانی کاتالوگ.
|
||||
Future<void> _action(
|
||||
Emitter<ShopBlocState> emit,
|
||||
Future<DataState<String>> Function() action,
|
||||
) async {
|
||||
if (state.busy) return;
|
||||
emit(state.copyWith(actionStatus: ActionLoading()));
|
||||
final res = await action();
|
||||
if (res is DataSuccess) {
|
||||
emit(state.copyWith(actionStatus: ActionSuccess(res.data!)));
|
||||
await _load(emit);
|
||||
} else {
|
||||
emit(state.copyWith(actionStatus: ActionError(res.error!)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
abstract class ShopEvent {}
|
||||
|
||||
class LoadShopEvent extends ShopEvent {}
|
||||
|
||||
class BuyCardEvent extends ShopEvent {
|
||||
final String cardId;
|
||||
BuyCardEvent(this.cardId);
|
||||
}
|
||||
|
||||
class SelectCardEvent extends ShopEvent {
|
||||
final String cardId;
|
||||
SelectCardEvent(this.cardId);
|
||||
}
|
||||
|
||||
class PurchaseEvent extends ShopEvent {
|
||||
final String kind;
|
||||
final String productId;
|
||||
PurchaseEvent(this.kind, this.productId);
|
||||
}
|
||||
|
||||
class AdRewardEvent extends ShopEvent {}
|
||||
@@ -0,0 +1,22 @@
|
||||
import 'shop_status.dart';
|
||||
|
||||
class ShopBlocState {
|
||||
final ShopLoadStatus loadStatus;
|
||||
final ShopActionStatus actionStatus;
|
||||
|
||||
ShopBlocState({required this.loadStatus, required this.actionStatus});
|
||||
|
||||
factory ShopBlocState.initial() =>
|
||||
ShopBlocState(loadStatus: ShopInitial(), actionStatus: ActionIdle());
|
||||
|
||||
bool get busy => actionStatus is ActionLoading;
|
||||
|
||||
ShopBlocState copyWith({
|
||||
ShopLoadStatus? loadStatus,
|
||||
ShopActionStatus? actionStatus,
|
||||
}) =>
|
||||
ShopBlocState(
|
||||
loadStatus: loadStatus ?? this.loadStatus,
|
||||
actionStatus: actionStatus ?? this.actionStatus,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import '../../domain/entities/shop_entities.dart';
|
||||
|
||||
abstract class ShopLoadStatus {}
|
||||
|
||||
class ShopInitial extends ShopLoadStatus {}
|
||||
|
||||
class ShopLoading extends ShopLoadStatus {}
|
||||
|
||||
class ShopLoaded extends ShopLoadStatus {
|
||||
final ShopData data;
|
||||
ShopLoaded(this.data);
|
||||
}
|
||||
|
||||
class ShopLoadError extends ShopLoadStatus {
|
||||
final String message;
|
||||
ShopLoadError(this.message);
|
||||
}
|
||||
|
||||
/// وضعیتِ یک عملیات (خرید/انتخاب/تبلیغ).
|
||||
abstract class ShopActionStatus {}
|
||||
|
||||
class ActionIdle extends ShopActionStatus {}
|
||||
|
||||
class ActionLoading extends ShopActionStatus {}
|
||||
|
||||
class ActionSuccess extends ShopActionStatus {
|
||||
final String message;
|
||||
ActionSuccess(this.message);
|
||||
}
|
||||
|
||||
class ActionError extends ShopActionStatus {
|
||||
final String message;
|
||||
ActionError(this.message);
|
||||
}
|
||||
Reference in New Issue
Block a user