purchase screen coupons list final

This commit is contained in:
Semyon Babushkin
2017-10-11 18:32:59 +03:00
parent 3f9afd5569
commit 7b20747157
4 changed files with 123 additions and 114 deletions

View File

@@ -91,8 +91,8 @@ android {
buildConfigField "String", "supportUrl", "\"http://yandex.ru/\"" buildConfigField "String", "supportUrl", "\"http://yandex.ru/\""
buildConfigField "String", "endpoint", "\"https://pos-api-int.dinect.com/20130701/\"" buildConfigField "String", "endpoint", "\"https://pos-api-int.dinect.com/20130701/\""
buildConfigField "String", "appToken", "\"9fec83cdca38c357e6b65dbb17514cdd36bf2a08\"" buildConfigField "String", "appToken", "\"9fec83cdca38c357e6b65dbb17514cdd36bf2a08\""
buildConfigField "String", "appTitle", "\"Autobonus\"" buildConfigField "String", "appTitle", "\"Autobonus (develop)\""
buildConfigField "boolean", "showBonus", "false" buildConfigField "boolean", "showBonus", "true"
} }
} }

View File

@@ -58,6 +58,17 @@ getPurchaseRequest(String endpoint, Map body, String token) async {
return httpClient.post(endpoint, body: body, headers: headers); return httpClient.post(endpoint, body: body, headers: headers);
} }
getCouponsRequest(String endpoint, String token) async {
var headers = {
'DM-Authorization': 'dmapptoken ${await getToken()}',
'Authorization': 'dmtoken ${token}',
'Accept-Language': Intl.defaultLocale
};
return httpClient.get(endpoint, headers: headers);
}
getEndpoint() async { getEndpoint() async {
return await platform.invokeMethod('getEndpoint'); return await platform.invokeMethod('getEndpoint');
} }

View File

@@ -219,44 +219,60 @@ class PurchaseScreenState<T> extends BaseState<PurchaseScreen> {
purchaseInProgress = true; purchaseInProgress = true;
String token = await helper.getToken(); String token = await helper.getToken();
helper.getMerchantID().then((result) { var result = await helper.getMerchantID();
helper.getCurrency().then((currency) { var currency = await helper.getCurrency();
var body = {
'doc_id': result,
'curr_iso_code': currency.toString(),
'commit': 'true',
'sum_total': sumTotal
};
getPurchaseRequest(user['purchases_url'], body, token) var body = {
.then((response) { 'doc_id': result,
print(response.body); 'curr_iso_code': currency.toString(),
Map parsedMap = JSON.decode(response.body); 'commit': 'true',
'sum_total': sumTotal
};
Navigator.of(context).pop(); var purchaseResponse;
if (parsedMap.containsKey('errors')) { try {
List<String> errors = parsedMap['errors']; purchaseResponse = await getPurchaseRequest(user['purchases_url'], body, token);
Scaffold print(purchaseResponse.body);
.of(context) } catch(error) {
.showSnackBar(new SnackBar(content: new Text(errors[0]))); purchaseInProgress = false;
} else { print(error.toString());
pushRouteReplacement( }
context,
new PurchaseSuccessScreen( Map purchase = JSON.decode(purchaseResponse.body);
sumTotal,
user['first_name'] == null ? '' : user['first_name'], Navigator.of(context).pop();
helper,
app, if (purchase.containsKey('errors')) {
parsedMap List<String> errors = purchase['errors'];
)); Scaffold
} .of(context)
}).catchError((error) { .showSnackBar(new SnackBar(content: new Text(errors[0])));
purchaseInProgress = false; } else {
print(error.toString()); var couponsResponse;
});
}); try {
}); couponsResponse = await getCouponsRequest(purchase['coupons_url'], token);
print(couponsResponse.body);
} catch(error) {
purchaseInProgress = false;
print(error.toString());
}
Map coupons = JSON.decode(couponsResponse.body);
pushRouteReplacement(
context,
new PurchaseSuccessScreen(
sumTotal,
user['first_name'] == null ? '' : user['first_name'],
helper,
app,
purchase,
coupons['results']
)
);
}
} }
} }
} }

View File

@@ -8,46 +8,40 @@ import 'package:flutter/material.dart';
/// Экран проведения покупки. /// Экран проведения покупки.
class PurchaseSuccessScreen extends StatefulWidget { class PurchaseSuccessScreen extends StatefulWidget {
PurchaseSuccessScreen(this.val, this.name, this.helper, this.app, this.details); PurchaseSuccessScreen(this.val, this.name, this.helper, this.app, this.details, this.coupons);
final String val; final String val;
final String name; final String name;
final String app; final String app;
final SqliteHelper helper; final SqliteHelper helper;
final Map details; final Map details;
final List<Map> coupons;
@override State createState() => @override State createState() =>
new PurchaseSuccessScreenState(val, name, helper, app, details); new PurchaseSuccessScreenState(val, name, helper, app, details, coupons);
} }
class PurchaseSuccessScreenState<T> extends BaseState<PurchaseSuccessScreen> { class PurchaseSuccessScreenState<T> extends BaseState<PurchaseSuccessScreen> {
PurchaseSuccessScreenState( PurchaseSuccessScreenState(
String sum, String username, SqliteHelper helper, String sum, String username, SqliteHelper helper,
String app, Map details String app, Map details, List<Map> coupons
){ ){
this.sum = sum; this.sum = sum;
this.username = username; this.username = username;
this.helper = helper; this.helper = helper;
this.app = app; this.app = app;
this.details = details; this.details = details;
this.coupons = coupons;
String regexString = r'(\d+) начислено, (\d+).*';
RegExp regExp = new RegExp(regexString);
var matches = regExp.allMatches(this.details['sum_bonus']);
if(matches.length != 0) {
var match = matches.elementAt(0); // => extract the first (and only) match
bonusPlus = int.parse(match.group(1));
bonusMinus = int.parse(match.group(2));
}
} }
String sum, username; String sum, username;
Map details; Map details;
List<Map> coupons;
int bonusPlus = 0; int bonusPlus = 0;
int bonusMinus = 0; int bonusMinus = 0;
int currency; int currency;
bool showBonus;
@override String getTitle() { @override String getTitle() {
return StringsLocalization.carryingPurchase(); return StringsLocalization.carryingPurchase();
@@ -65,31 +59,62 @@ class PurchaseSuccessScreenState<T> extends BaseState<PurchaseSuccessScreen> {
}); });
}); });
} }
if (showBonus == null) {
platform.invokeMethod('showBonus').then((showBonus) {
setState(() {
this.showBonus = showBonus;
if (showBonus == true) {
if (this.details['sum_bonus'] is String) {
String regexString = r'(\d+) начислено, (\d+).*';
RegExp regExp = new RegExp(regexString);
var matches = regExp.allMatches(this.details['sum_bonus']);
if(matches.length != 0) {
var match = matches.elementAt(0); // => extract the first (and only) match
this.bonusPlus = int.parse(match.group(1));
this.bonusMinus = int.parse(match.group(2));
}
}
if (this.details['sum_bonus'] is int) {
this.bonusPlus = this.details['sum_bonus'];
//bonusMinus = this.details['sum_bonus'];
}
}
});
});
}
return getMainWidget(); return getMainWidget();
} }
@override Widget getScreenContent() { @override Widget getScreenContent() {
List <Widget> widgetList = <Widget>[]; List <Widget> widgetList = <Widget>[];
widgetList.add(getValueWithDescription(StringsLocalization.buyer(), username)); widgetList.add(getValueWithDescription(StringsLocalization.buyer(), username));
widgetList.add(getSuccessMessage()); widgetList.add(getSuccessMessage());
if (bonusPlus > 0) { if (showBonus == true) {
widgetList.add(getValueWithDescription(StringsLocalization.bonusPlus(), bonusPlus.toString())); if (bonusPlus > 0) {
} widgetList.add(getValueWithDescription(StringsLocalization.bonusPlus(), bonusPlus.toString()));
if (bonusMinus > 0) { }
widgetList.add(getValueWithDescription(StringsLocalization.bonusMinus(), bonusMinus.toString())); if (bonusMinus > 0) {
widgetList.add(getValueWithDescription(StringsLocalization.bonusMinus(), bonusMinus.toString()));
}
} }
widgetList.add(getValueWithDescription(StringsLocalization.discountRate(), '${details['discount']}%')); widgetList.add(getValueWithDescription(StringsLocalization.discountRate(), '${details['discount']}%'));
widgetList.add(getValueWithDescription(StringsLocalization.discountSum(), '${details['sum_discount']}')); widgetList.add(getValueWithDescription(StringsLocalization.discountSum(), '${details['sum_discount']}'));
widgetList.add(getCoupons()); // widgetList.add( new Expanded(child: new Center()));
widgetList.add( new Expanded(child: new Center())); this.coupons.forEach((couponItem) {
widgetList.add(getCoupons(couponItem));
});
widgetList.add(wrapButton(getScreenMargins(74.0), getScanButton())); widgetList.add(wrapButton(getScreenMargins(74.0), getScanButton()));
return new Column( return new ListView.builder(
children: widgetList itemBuilder: (BuildContext context, int index) => widgetList[index],
itemCount: widgetList.length
); );
} }
getScreenMargins(double bottom) { getScreenMargins(double bottom) {
@@ -109,66 +134,23 @@ class PurchaseSuccessScreenState<T> extends BaseState<PurchaseSuccessScreen> {
}; };
} }
getCoupons(couponItem) {
getCoupons() {
EdgeInsets margin = new EdgeInsets.only(left: 20.0, right: 20.0);
TextStyle titleStyle = Theme.of(context).textTheme.button.copyWith(
fontWeight: FontWeight.bold,
color: faqTitlesColor
);
final List<Widget> detailsRows = <Widget>[];
// this.details.forEach((key, value) => detailsRows.add(
// new Text(key)
// new Row(
// crossAxisAlignment: CrossAxisAlignment.start,
// children: <Widget>[
// new Text('1'),
// new Text('2'),
//
// new Container(
// margin: const EdgeInsets.only(right: 16.0),
// child: new CircleAvatar(child: new Text('D')),
// ),
// new Column(
// crossAxisAlignment: CrossAxisAlignment.start,
// children: <Widget>[
// new Text(key, style: Theme.of(context).textTheme.subhead),
// new Container(
// margin: const EdgeInsets.only(top: 0.5),
// child: new Text(key)
// )
// ]
// )
// ],
// )
// ));
print(detailsRows.length);
return new Container( return new Container(
margin: margin, margin: new EdgeInsets.only(left: 5.0, right: 5.0, top: 5.0),
child: new Card( child: new Card(
child: new ExpansionTile( child: new ExpansionTile(
//key: new PageStorageKey<Entry>(root),
title: new Text( title: new Text(
'Coupons', couponItem['offer_name'],
style: titleStyle style: Theme.of(context).textTheme.button.copyWith(
fontWeight: FontWeight.bold,
color: faqTitlesColor
)
), ),
children: [ children: [
new Container( new Container(
margin: margin, margin: new EdgeInsets.only(left: 20.0, right: 20.0),
padding: new EdgeInsets.only(top: 12.0, bottom: 20.0), padding: new EdgeInsets.only(top: 0.0, bottom: 10.0),
child: new Text('Coupons'), child: new Text(couponItem['coupon_condition']),
decoration: new BoxDecoration(
border: new Border(
top: new BorderSide(
color: greyTextColor,
width: 0.5
)
)
)
) )
] ]
) )