Files
be_happy_public/lib/presentation/viewmodel/payment_method_sheet_bloc.dart
2026-07-08 13:09:20 +03:00

96 lines
3.3 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:flutter_bloc/flutter_bloc.dart';
import '../../core/result.dart';
import '../../domain/entities/payment_card.dart';
import '../../domain/usecase/get_payment_cards_usecase.dart';
import '../../domain/usecase/set_main_payment_card_usecase.dart';
import '../event/payment_method_sheet_event.dart';
import '../state/payment_method_sheet_state.dart';
class PaymentMethodSheetBloc extends Bloc<PaymentMethodSheetEvent, PaymentMethodSheetState> {
final GetPaymentCardsUsecase _getPaymentCardsUsecase;
final SetMainPaymentCardUsecase _setMainPaymentCardUsecase;
PaymentMethodSheetBloc(this._getPaymentCardsUsecase, this._setMainPaymentCardUsecase)
: super(PaymentMethodSheetState(status: PaymentMethodSheetStatus.initial)) {
on<PaymentMethodSheetStarted>(_onStarted);
}
Future<void> _onStarted(
PaymentMethodSheetStarted event,
Emitter<PaymentMethodSheetState> emit,
) async {
emit(state.copyWith(status: PaymentMethodSheetStatus.loading));
try {
final result = await _getPaymentCardsUsecase();
if (result is Success<List<PaymentCard>>) {
emit(state.copyWith(
status: PaymentMethodSheetStatus.success,
cards: result.data ?? [],
));
} else {
emit(state.copyWith(
status: PaymentMethodSheetStatus.failure,
errorMessage: 'Failed to load payment cards',
));
}
} catch (e) {
emit(state.copyWith(
status: PaymentMethodSheetStatus.failure,
errorMessage: e.toString(),
));
}
}
Future<void> _onSelectCard(
SelectPaymentCardRequested event,
Emitter<PaymentMethodSheetState> emit,
) async {
// Включаем локальный лоадер на шторке, либо просто ждем ответа
emit(state.copyWith(status: PaymentMethodSheetStatus.loading));
final result = await _setMainPaymentCardUsecase(event.card.id);
if (result is Success<void>) {
final cardsResult = await _getPaymentCardsUsecase();
if (cardsResult is Success<List<PaymentCard>>) {
emit(state.copyWith(
status: PaymentMethodSheetStatus.success,
cards: cardsResult.data ?? [],
));
} else {
emit(state.copyWith(status: PaymentMethodSheetStatus.failure, errorMessage: 'Ошибка обновления списка карт'));
}
} else {
emit(state.copyWith(
status: PaymentMethodSheetStatus.failure,
errorMessage: 'Не удалось установить карту основной',
));
}
}
// Выносим загрузку в отдельный метод для переиспользования
Future<void> _loadCards(Emitter<PaymentMethodSheetState> emit) async {
try {
final result = await _getPaymentCardsUsecase();
if (result is Success<List<PaymentCard>>) {
emit(state.copyWith(
status: PaymentMethodSheetStatus.success,
cards: result.data ?? [],
));
} else {
emit(state.copyWith(
status: PaymentMethodSheetStatus.failure,
errorMessage: 'Failed to load payment cards',
));
}
} catch (e) {
emit(state.copyWith(
status: PaymentMethodSheetStatus.failure,
errorMessage: e.toString(),
));
}
}
}