initial commit (migrated)
This commit is contained in:
commit
b594facb51
143 changed files with 11057 additions and 0 deletions
|
|
@ -0,0 +1,59 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:in_app_purchase/in_app_purchase.dart';
|
||||
import 'package:kuwot/core/domain/no_params.dart';
|
||||
import 'package:kuwot/core/presentation/bloc/error_state.dart';
|
||||
import 'package:kuwot/features/in_app_purchase/domain/use_case/get_consumable_products.dart';
|
||||
import 'package:kuwot/features/in_app_purchase/domain/use_case/purchase_consumable_product.dart';
|
||||
|
||||
part 'in_app_purchase_events.dart';
|
||||
part 'in_app_purchase_states.dart';
|
||||
|
||||
class InAppPurchaseBloc extends Bloc<InAppPurchaseEvent, InAppPurchaseState> {
|
||||
InAppPurchaseBloc({
|
||||
required this.getConsumableProducts,
|
||||
required this.purchaseConsumableProduct,
|
||||
}) : super(const InAppPurchaseInitialState()) {
|
||||
on<GetConsumableProductsEvent>(_getConsumableProducts);
|
||||
on<PurchaseConsumableProductEvent>(_purchaseConsumableProduct);
|
||||
}
|
||||
|
||||
final GetConsumableProducts getConsumableProducts;
|
||||
final PurchaseConsumableProduct purchaseConsumableProduct;
|
||||
|
||||
Future<void> _getConsumableProducts(
|
||||
GetConsumableProductsEvent event,
|
||||
Emitter<InAppPurchaseState> emit,
|
||||
) async {
|
||||
emit(const GettingConsumableProductsState());
|
||||
final result = await getConsumableProducts(const NoParams());
|
||||
result.fold(
|
||||
(failure) {
|
||||
emit(PurchaseErrorState(message: failure.message));
|
||||
},
|
||||
(products) {
|
||||
emit(ConsumableProductsLoadedState(products));
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _purchaseConsumableProduct(
|
||||
PurchaseConsumableProductEvent event,
|
||||
Emitter<InAppPurchaseState> emit,
|
||||
) async {
|
||||
emit(PurchasingConsumableProductState(event.productDetails));
|
||||
final result = await purchaseConsumableProduct(
|
||||
PurchaseConsumableProductParams(productDetails: event.productDetails),
|
||||
);
|
||||
result.fold(
|
||||
(failure) {
|
||||
emit(PurchaseErrorState(message: failure.message));
|
||||
},
|
||||
(success) {
|
||||
emit(ConsumableProductPurchasedState(result: success));
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
part of 'in_app_purchase_bloc.dart';
|
||||
|
||||
abstract class InAppPurchaseEvent extends Equatable {
|
||||
const InAppPurchaseEvent();
|
||||
}
|
||||
|
||||
class GetConsumableProductsEvent extends InAppPurchaseEvent {
|
||||
const GetConsumableProductsEvent();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class PurchaseConsumableProductEvent extends InAppPurchaseEvent {
|
||||
final ProductDetails productDetails;
|
||||
|
||||
const PurchaseConsumableProductEvent(this.productDetails);
|
||||
|
||||
@override
|
||||
List<Object> get props => [productDetails];
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
part of 'in_app_purchase_bloc.dart';
|
||||
|
||||
abstract class InAppPurchaseState extends Equatable {
|
||||
const InAppPurchaseState();
|
||||
}
|
||||
|
||||
class InAppPurchaseInitialState extends InAppPurchaseState {
|
||||
const InAppPurchaseInitialState();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class GettingConsumableProductsState extends InAppPurchaseState {
|
||||
const GettingConsumableProductsState();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class ConsumableProductsLoadedState extends InAppPurchaseState {
|
||||
final List<ProductDetails> products;
|
||||
|
||||
const ConsumableProductsLoadedState(this.products);
|
||||
|
||||
@override
|
||||
List<Object> get props => [products];
|
||||
}
|
||||
|
||||
class PurchasingConsumableProductState extends InAppPurchaseState {
|
||||
final ProductDetails productDetails;
|
||||
|
||||
const PurchasingConsumableProductState(this.productDetails);
|
||||
|
||||
@override
|
||||
List<Object> get props => [productDetails];
|
||||
}
|
||||
|
||||
class ConsumableProductPurchasedState extends InAppPurchaseState {
|
||||
const ConsumableProductPurchasedState({required this.result});
|
||||
|
||||
final bool result;
|
||||
|
||||
@override
|
||||
List<Object> get props => [result];
|
||||
}
|
||||
|
||||
class PurchaseErrorState extends InAppPurchaseState implements ErrorState {
|
||||
@override
|
||||
final String message;
|
||||
|
||||
@override
|
||||
final Exception? cause;
|
||||
|
||||
const PurchaseErrorState({required this.message, this.cause});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [message, cause];
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:in_app_purchase/in_app_purchase.dart';
|
||||
import 'package:kuwot/features/in_app_purchase/domain/repositories/in_app_purchase_repository.dart';
|
||||
|
||||
/// Cubit that listens to purchase details stream from [InAppPurchaseRepository]
|
||||
class PurchaseDetailsCubit extends Cubit<List<PurchaseDetails>> {
|
||||
/// Repository to get purchase details stream
|
||||
final InAppPurchaseRepository repository;
|
||||
|
||||
/// Subscription to purchase details stream
|
||||
StreamSubscription<List<PurchaseDetails>>? _purchaseDetailsSubscription;
|
||||
|
||||
PurchaseDetailsCubit(this.repository) : super([]) {
|
||||
// listen to purchase details stream and emit the event to the UI
|
||||
_purchaseDetailsSubscription = repository.purchaseStream.listen((event) {
|
||||
emit(event);
|
||||
|
||||
// complete the purchases
|
||||
event
|
||||
.where((element) => element.status == PurchaseStatus.purchased)
|
||||
.forEach((element) {
|
||||
unawaited(repository.completePurchase(element));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() {
|
||||
_purchaseDetailsSubscription?.cancel();
|
||||
return super.close();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue