83 lines
2.4 KiB
Dart
83 lines
2.4 KiB
Dart
import 'dart:async';
|
|
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: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");
|
|
this.getProductSet();
|
|
yield SalesReady();
|
|
} else if (event is SalesPurchase) {
|
|
final int productId = event.productId;
|
|
trace("Requesting purchase for" + productId.toString());
|
|
Flurry.logEvent("PurchaseRequest");
|
|
//PlatformPurchaseApi().requestPurchase(null);
|
|
}
|
|
} on Exception catch (ex) {
|
|
yield SalesError(message: ex.toString());
|
|
}
|
|
}
|
|
|
|
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) {
|
|
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;
|
|
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);
|
|
}
|
|
}
|