From 7b20747157827d5e7aba14426c6843d1d4b50bbe Mon Sep 17 00:00:00 2001 From: Semyon Babushkin Date: Wed, 11 Oct 2017 18:32:59 +0300 Subject: [PATCH] purchase screen coupons list final --- android/app/build.gradle | 4 +- lib/network.dart | 11 +++ lib/screens/purchase.dart | 86 +++++++++++-------- lib/screens/purchase_success.dart | 136 +++++++++++++----------------- 4 files changed, 123 insertions(+), 114 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index 1239273..fcb7b7d 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -91,8 +91,8 @@ android { buildConfigField "String", "supportUrl", "\"http://yandex.ru/\"" buildConfigField "String", "endpoint", "\"https://pos-api-int.dinect.com/20130701/\"" buildConfigField "String", "appToken", "\"9fec83cdca38c357e6b65dbb17514cdd36bf2a08\"" - buildConfigField "String", "appTitle", "\"Autobonus\"" - buildConfigField "boolean", "showBonus", "false" + buildConfigField "String", "appTitle", "\"Autobonus (develop)\"" + buildConfigField "boolean", "showBonus", "true" } } diff --git a/lib/network.dart b/lib/network.dart index 9e3a238..7c26858 100644 --- a/lib/network.dart +++ b/lib/network.dart @@ -58,6 +58,17 @@ getPurchaseRequest(String endpoint, Map body, String token) async { 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 { return await platform.invokeMethod('getEndpoint'); } diff --git a/lib/screens/purchase.dart b/lib/screens/purchase.dart index 35f7c47..e01c4ea 100644 --- a/lib/screens/purchase.dart +++ b/lib/screens/purchase.dart @@ -219,44 +219,60 @@ class PurchaseScreenState extends BaseState { purchaseInProgress = true; String token = await helper.getToken(); - helper.getMerchantID().then((result) { - helper.getCurrency().then((currency) { - var body = { - 'doc_id': result, - 'curr_iso_code': currency.toString(), - 'commit': 'true', - 'sum_total': sumTotal - }; + var result = await helper.getMerchantID(); + var currency = await helper.getCurrency(); - getPurchaseRequest(user['purchases_url'], body, token) - .then((response) { - print(response.body); - Map parsedMap = JSON.decode(response.body); + var body = { + 'doc_id': result, + 'curr_iso_code': currency.toString(), + 'commit': 'true', + 'sum_total': sumTotal + }; - Navigator.of(context).pop(); + var purchaseResponse; - if (parsedMap.containsKey('errors')) { - List errors = parsedMap['errors']; - Scaffold - .of(context) - .showSnackBar(new SnackBar(content: new Text(errors[0]))); - } else { - pushRouteReplacement( - context, - new PurchaseSuccessScreen( - sumTotal, - user['first_name'] == null ? '' : user['first_name'], - helper, - app, - parsedMap - )); - } - }).catchError((error) { - purchaseInProgress = false; - print(error.toString()); - }); - }); - }); + try { + purchaseResponse = await getPurchaseRequest(user['purchases_url'], body, token); + print(purchaseResponse.body); + } catch(error) { + purchaseInProgress = false; + print(error.toString()); + } + + Map purchase = JSON.decode(purchaseResponse.body); + + Navigator.of(context).pop(); + + if (purchase.containsKey('errors')) { + List errors = purchase['errors']; + Scaffold + .of(context) + .showSnackBar(new SnackBar(content: new Text(errors[0]))); + } else { + 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'] + ) + ); + } } } } diff --git a/lib/screens/purchase_success.dart b/lib/screens/purchase_success.dart index fb95a44..10c30db 100644 --- a/lib/screens/purchase_success.dart +++ b/lib/screens/purchase_success.dart @@ -8,46 +8,40 @@ import 'package:flutter/material.dart'; /// Экран проведения покупки. 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 name; final String app; final SqliteHelper helper; final Map details; + final List coupons; @override State createState() => - new PurchaseSuccessScreenState(val, name, helper, app, details); + new PurchaseSuccessScreenState(val, name, helper, app, details, coupons); } class PurchaseSuccessScreenState extends BaseState { PurchaseSuccessScreenState( String sum, String username, SqliteHelper helper, - String app, Map details + String app, Map details, List coupons ){ this.sum = sum; this.username = username; this.helper = helper; this.app = app; this.details = details; - - 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)); - } - + this.coupons = coupons; } String sum, username; Map details; + List coupons; int bonusPlus = 0; int bonusMinus = 0; int currency; + bool showBonus; @override String getTitle() { return StringsLocalization.carryingPurchase(); @@ -65,31 +59,62 @@ class PurchaseSuccessScreenState extends BaseState { }); }); } + 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(); } @override Widget getScreenContent() { - - List widgetList = []; widgetList.add(getValueWithDescription(StringsLocalization.buyer(), username)); widgetList.add(getSuccessMessage()); - if (bonusPlus > 0) { - widgetList.add(getValueWithDescription(StringsLocalization.bonusPlus(), bonusPlus.toString())); - } - if (bonusMinus > 0) { - widgetList.add(getValueWithDescription(StringsLocalization.bonusMinus(), bonusMinus.toString())); + if (showBonus == true) { + if (bonusPlus > 0) { + widgetList.add(getValueWithDescription(StringsLocalization.bonusPlus(), bonusPlus.toString())); + } + if (bonusMinus > 0) { + widgetList.add(getValueWithDescription(StringsLocalization.bonusMinus(), bonusMinus.toString())); + } } + widgetList.add(getValueWithDescription(StringsLocalization.discountRate(), '${details['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())); - return new Column( - children: widgetList + return new ListView.builder( + itemBuilder: (BuildContext context, int index) => widgetList[index], + itemCount: widgetList.length ); + } getScreenMargins(double bottom) { @@ -109,66 +134,23 @@ class PurchaseSuccessScreenState extends BaseState { }; } - - 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 detailsRows = []; - -// this.details.forEach((key, value) => detailsRows.add( -// new Text(key) -// new Row( -// crossAxisAlignment: CrossAxisAlignment.start, -// children: [ -// 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: [ -// 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); - + getCoupons(couponItem) { return new Container( - margin: margin, + margin: new EdgeInsets.only(left: 5.0, right: 5.0, top: 5.0), child: new Card( child: new ExpansionTile( - //key: new PageStorageKey(root), title: new Text( - 'Coupons', - style: titleStyle + couponItem['offer_name'], + style: Theme.of(context).textTheme.button.copyWith( + fontWeight: FontWeight.bold, + color: faqTitlesColor + ) ), children: [ new Container( - margin: margin, - padding: new EdgeInsets.only(top: 12.0, bottom: 20.0), - child: new Text('Coupons'), - decoration: new BoxDecoration( - border: new Border( - top: new BorderSide( - color: greyTextColor, - width: 0.5 - ) - ) - ) + margin: new EdgeInsets.only(left: 20.0, right: 20.0), + padding: new EdgeInsets.only(top: 0.0, bottom: 10.0), + child: new Text(couponItem['coupon_condition']), ) ] )