Files
be_happy_public/lib/presentation/viewmodel/top_up_bloc.dart
Polyanka 182be1049b some fix
# Conflicts:
#	lib/presentation/navigation/app_router.dart
#	pubspec.yaml
2026-08-11 00:23:16 +03:00

101 lines
3.4 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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('Ошибка при оплате'));
}
}
}
});
}
}