114 lines
3.4 KiB
Dart
114 lines
3.4 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
import 'dart:math';
|
|
|
|
import 'package:aitrainer_app/model/cache.dart';
|
|
import 'package:aitrainer_app/model/product.dart';
|
|
import 'package:aitrainer_app/model/product_test.dart';
|
|
import 'package:aitrainer_app/service/logging.dart';
|
|
import 'package:aitrainer_app/service/product_test_service.dart';
|
|
import 'package:aitrainer_app/util/platform_purchase.dart';
|
|
import 'package:bloc/bloc.dart';
|
|
import 'package:equatable/equatable.dart';
|
|
import 'package:flurry/flurry.dart';
|
|
|
|
part 'sales_event.dart';
|
|
part 'sales_state.dart';
|
|
|
|
class SalesBloc extends Bloc<SalesEvent, SalesState> with Logging {
|
|
List<ProductTest> tests = List();
|
|
List<Product> product2Display = List();
|
|
int productSet = -1;
|
|
SalesBloc() : super(SalesInitial());
|
|
|
|
@override
|
|
Stream<SalesState> mapEventToState(
|
|
SalesEvent event,
|
|
) async* {
|
|
try {
|
|
if (event is SalesLoad) {
|
|
yield SalesLoading();
|
|
//Flurry.logEvent("SalesPageOpen");
|
|
// await PlatformPurchaseApi().initPurchasePlatformState();
|
|
// this.getProductSet();
|
|
yield SalesReady();
|
|
} else if (event is SalesPurchase) {
|
|
final int productId = event.productId;
|
|
trace("Requesting purchase for" + productId.toString());
|
|
//Flurry.logEvent("PurchaseRequest");
|
|
// PlatformPurchaseApi().purchase(getSelectedProduct(productId));
|
|
}
|
|
} on Exception catch (ex) {
|
|
yield SalesError(message: ex.toString());
|
|
}
|
|
}
|
|
|
|
Product getSelectedProduct(int productId) {
|
|
Product prod;
|
|
for (var product in this.product2Display) {
|
|
if (product.productId == productId) {
|
|
prod = product;
|
|
}
|
|
}
|
|
return prod;
|
|
}
|
|
|
|
/* String getLocalizedPrice(String productId) {
|
|
String price = "";
|
|
for (var product in PlatformPurchaseApi().getIAPItems()) {
|
|
if (Platform.isAndroid) {
|
|
print("PlatformProduct " + product.toString());
|
|
if (productId == product.productId) {
|
|
price = product.localizedPrice;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return price;
|
|
}
|
|
|
|
void getProductSet() {
|
|
int productId = 0;
|
|
this.tests = Cache().productTests;
|
|
|
|
if (tests.isEmpty) {
|
|
var rand = Random.secure();
|
|
productSet = rand.nextInt(5) + 1;
|
|
} else {
|
|
trace("Previous ProductTest: " + tests[0].toJson().toString());
|
|
productId = tests[0].productId;
|
|
for (var elem in Cache().products) {
|
|
final Product product = elem as Product;
|
|
if (product.productId == productId) {
|
|
productSet = product.productSet;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
ProductTest productTest = ProductTest();
|
|
trace("ProductSet: " + productSet.toString());
|
|
for (var elem in Cache().products) {
|
|
Product product = elem as Product;
|
|
|
|
if (product.productSet == productSet) {
|
|
productId = product.productId;
|
|
final String platformProductId = Platform.isAndroid ? product.productIdAndroid : product.productIdIos;
|
|
product.localizedPrice = getLocalizedPrice(platformProductId);
|
|
product2Display.add(product);
|
|
}
|
|
}
|
|
|
|
product2Display.sort((a, b) {
|
|
return a.sort < b.sort ? a.sort : b.sort;
|
|
});
|
|
|
|
productTest.productId = productId;
|
|
productTest.customerId = Cache().userLoggedIn.customerId;
|
|
productTest.dateView = DateTime.now();
|
|
//ProductTestApi().saveProductTest(productTest);
|
|
//Cache().productTests.add(productTest);
|
|
} */
|
|
}
|