101 lines
3.4 KiB
Dart
101 lines
3.4 KiB
Dart
import 'package:be_happy/domain/usecase/get_certificates_usecase.dart';
|
||
import 'package:be_happy/domain/usecase/get_payment_cards_usecase.dart';
|
||
import 'package:be_happy/domain/usecase/purchase_certificate_usecase.dart';
|
||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||
|
||
import '../../core/result.dart';
|
||
import '../../domain/entities/certificate.dart';
|
||
import '../../domain/entities/payment_card.dart';
|
||
import '../event/top_up_event.dart';
|
||
import '../state/top_up_state.dart';
|
||
|
||
class TopUpBloc extends Bloc<TopUpEvent, TopUpState> {
|
||
final GetCertificatesUsecase getCertificatesUsecase;
|
||
final PurchaseCertificateUsecase purchaseCertificateUsecase;
|
||
final GetPaymentCardsUsecase getUserCards;
|
||
|
||
TopUpBloc({
|
||
required this.getCertificatesUsecase,
|
||
required this.purchaseCertificateUsecase,
|
||
required this.getUserCards,
|
||
}) : super(const TopUpLoading()) {
|
||
|
||
on<LoadTopUpData>((event, emit) async {
|
||
emit(const TopUpLoading());
|
||
|
||
final results = await Future.wait([
|
||
getCertificatesUsecase(),
|
||
getUserCards(),
|
||
]);
|
||
|
||
final certResult = results[0] as Result<List<Certificate>>;
|
||
final cardResult = results[1] as Result<List<PaymentCard>>;
|
||
|
||
switch ((certResult, cardResult)) {
|
||
case (Success(data: final certs), Success(data: final cards)):
|
||
emit(
|
||
TopUpLoaded(
|
||
certificates: certs ?? [],
|
||
cards: cards ?? [],
|
||
selectedTariff: certs != null && certs.length > 1
|
||
? certs[1]
|
||
: (certs != null && certs.isNotEmpty ? certs.first : null),
|
||
selectedCard: cards == null || cards.isEmpty
|
||
? null
|
||
: cards.firstWhere(
|
||
(c) => c.isMain,
|
||
orElse: () => cards.first,
|
||
),
|
||
),
|
||
);
|
||
default:
|
||
emit(const TopUpPaymentFailure('Не удалось загрузить данные'));
|
||
}
|
||
});
|
||
|
||
on<SelectCertificate>((event, emit) {
|
||
if (state is TopUpLoaded) {
|
||
final currentState = state as TopUpLoaded;
|
||
emit(currentState.copyWith(selectedTariff: event.certificate));
|
||
}
|
||
});
|
||
|
||
on<SelectCard>((event, emit) {
|
||
if (state is TopUpLoaded) {
|
||
final currentState = state as TopUpLoaded;
|
||
emit(currentState.copyWith(selectedCard: event.card));
|
||
}
|
||
});
|
||
|
||
on<ToggleAgreement>((event, emit) {
|
||
if (state is TopUpLoaded) {
|
||
final currentState = state as TopUpLoaded;
|
||
emit(currentState.copyWith(isAgreed: event.value));
|
||
}
|
||
});
|
||
|
||
on<TopUpButtonPressed>((event, emit) async {
|
||
if (state is TopUpLoaded) {
|
||
final currentState = state as TopUpLoaded;
|
||
|
||
if (currentState.isAgreed &&
|
||
currentState.selectedTariff != null &&
|
||
currentState.selectedCard != null) {
|
||
|
||
emit(currentState.copyWith(isProcessingPayment: true));
|
||
|
||
final result = await purchaseCertificateUsecase(
|
||
certificateId: currentState.selectedTariff!.id,
|
||
cardId: currentState.selectedCard!.id,
|
||
);
|
||
|
||
if (result is Success) {
|
||
emit(const TopUpPaymentSuccess());
|
||
} else {
|
||
emit(const TopUpPaymentFailure('Ошибка при оплате'));
|
||
}
|
||
}
|
||
}
|
||
});
|
||
}
|
||
} |