diff --git a/android/app/src/main/java/com/dinect/checker/AbstractScannerActivity.java b/android/app/src/main/java/com/dinect/checker/AbstractScannerActivity.java index 9eb0299..eb602a7 100644 --- a/android/app/src/main/java/com/dinect/checker/AbstractScannerActivity.java +++ b/android/app/src/main/java/com/dinect/checker/AbstractScannerActivity.java @@ -137,7 +137,7 @@ public abstract class AbstractScannerActivity extends AppCompatActivity { EditText manualInput = (EditText) findViewById(R.id.manual_input); // для удобства, чтоб не вводить постоянно руками при разработке - //manualInput.setText("4620011139016317023320337"); + manualInput.setText("4620011139016317023320337"); manualInput.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override diff --git a/lib/base/base_state.dart b/lib/base/base_state.dart index ab05916..28172ad 100644 --- a/lib/base/base_state.dart +++ b/lib/base/base_state.dart @@ -143,6 +143,70 @@ abstract class BaseState extends State { ); } + getItemToggle(String title, String description) { + if (title.length == 0) { + title = description.substring(0,30) + " ..."; + } + return new Container( + margin: new EdgeInsets.only(left: 5.0, right: 5.0, top: 5.0), + child: new Card( + child: new ExpansionTile( + title: new Text( + title, + style: Theme.of(context).textTheme.button.copyWith( + fontWeight: FontWeight.bold, + color: faqTitlesColor + ) + ), + children: [ + new Container( + margin: new EdgeInsets.only(left: 20.0, right: 20.0), + padding: new EdgeInsets.only(top: 0.0, bottom: 10.0), + child: new Text(description), + ) + ] + ) + ) + ); + } + + // Возвращает контейнер с раскрывающимся элементом и переключателем + // title - название, description - описание, isSet - состояние переключаиеля + // handler - обработчик события переключения + + getItemToggleSwitch(String title, String description, bool isSet, handler) { + + if (title.length == 0) { + title = description.substring(0,25) + " ..."; + } + + return new Container( + margin: new EdgeInsets.only(left: 5.0, right: 5.0, top: 5.0), + child: new Card( + child: new ExpansionTile( + title: new SwitchListTile( + title: new Text( + title, + style: Theme.of(context).textTheme.button.copyWith( + fontWeight: FontWeight.bold, + color: faqTitlesColor + ) + ), + value: isSet, + onChanged: handler + ), + children: [ + new Container( + margin: new EdgeInsets.only(left: 20.0, right: 20.0), + padding: new EdgeInsets.only(top: 0.0, bottom: 10.0), + child: new Text(description), + ), + ] + ) + ) + ); + } + /// Метод возвращает контейнер только с названием элемента Widget getItemTitle(String title) { return new Container( diff --git a/lib/i18n/messages_en.dart b/lib/i18n/messages_en.dart index c0f1cca..3633a50 100644 --- a/lib/i18n/messages_en.dart +++ b/lib/i18n/messages_en.dart @@ -42,6 +42,7 @@ class MessageLookup extends MessageLookupByLibrary { "bonus_minus" : MessageLookupByLibrary.simpleMessage("Bonus was charged"), "bonus_hint" : MessageLookupByLibrary.simpleMessage("Points to charge"), "coupons_used" : MessageLookupByLibrary.simpleMessage("Coupons were used"), + "select_coupons" : MessageLookupByLibrary.simpleMessage("Select coupons for using in purchase"), "bonus_explanation" : MessageLookupByLibrary.simpleMessage("If not set, points will be added"), "scan" : MessageLookupByLibrary.simpleMessage("Scan"), "sign_up" : MessageLookupByLibrary.simpleMessage("Sign Up"), diff --git a/lib/i18n/messages_ru.dart b/lib/i18n/messages_ru.dart index 0d7390d..7e9053b 100644 --- a/lib/i18n/messages_ru.dart +++ b/lib/i18n/messages_ru.dart @@ -42,6 +42,7 @@ class MessageLookup extends MessageLookupByLibrary { "bonus_minus" : MessageLookupByLibrary.simpleMessage("Бонусов списано"), "bonus_hint" : MessageLookupByLibrary.simpleMessage("Сколько баллов списать?"), "coupons_used" : MessageLookupByLibrary.simpleMessage("Были погашены купоны"), + "select_coupons" : MessageLookupByLibrary.simpleMessage("Выберите купоны для гашения"), "bonus_explanation" : MessageLookupByLibrary.simpleMessage("Если не указано сколько баллов списать, баллы будут начислены"), "scan" : MessageLookupByLibrary.simpleMessage("Сканировать"), "sign_up" : MessageLookupByLibrary.simpleMessage("Зарегистрироваться"), diff --git a/lib/network.dart b/lib/network.dart index d143592..f78bd3c 100644 --- a/lib/network.dart +++ b/lib/network.dart @@ -35,9 +35,7 @@ getDeleteTokenRequest(String token) async { await getToken(), headers: {'Accept-Language': Intl.defaultLocale}); } -getLoyaltyRequest(String endpoint, SqliteHelper helper) async { - String token = await helper.getToken(); - +getLoyaltyRequest(String endpoint, String token) async { var headers = { 'DM-Authorization': 'dmapptoken ${await getToken()}', 'Authorization': 'dmtoken ${token}', diff --git a/lib/screens/purchase.dart b/lib/screens/purchase.dart index 8678189..43343dd 100644 --- a/lib/screens/purchase.dart +++ b/lib/screens/purchase.dart @@ -43,7 +43,7 @@ class PurchaseScreenState extends BaseState { @override Widget build(BuildContext ctx) { setState(() { - requestLoyalty(user['loyalty_url']); + requestAsyncData(user); }); return getMainWidget(); } @@ -55,33 +55,51 @@ class PurchaseScreenState extends BaseState { String loyalityType = ''; String loyalty = ''; String bonus = ''; - bool dataLoaded = false; + List coupons = []; + bool dataRequested = false; @override Widget getScreenContent() { - List widgets = []; - widgets.add(getValueWithDescription( + List widgetList = []; + widgetList.add(getValueWithDescription( StringsLocalization.buyer(), user['first_name'] == null ? '' : user['first_name'] )); - widgets.add(getValueWithDescription(StringsLocalization.card(), card)); + widgetList.add(getValueWithDescription(StringsLocalization.card(), card)); - widgets.add(getValueWithDescription(StringsLocalization.reward(), loyalty)); + widgetList.add(getValueWithDescription(StringsLocalization.reward(), loyalty)); if (bonus.length > 0) { - widgets.add(getValueWithDescription(StringsLocalization.bonus(), bonus)); + widgetList.add(getValueWithDescription(StringsLocalization.bonus(), bonus)); } - widgets.add(getHintLabel()); + widgetList.add(getHintLabel()); - widgets.add(getInputField()); // Нельзя добавить еще одно поле таким же способом + widgetList.add(getInputField()); // Нельзя добавить еще одно поле таким же способом if (this.loyalityType == 'bonus') { - widgets.add(getBonusInputField()); + widgetList.add(getBonusInputField()); } - widgets.add(wrapButton(getScreenMargins(36.0), getCompleteButton())); - widgets.add(wrapButton( + if (this.coupons.length > 0) { + widgetList.add(getItemTitle(StringsLocalization.selectCoupons())); + this.coupons.forEach((couponItem) { + widgetList.add(getItemToggleSwitch( + couponItem['offer_name'], + couponItem['coupon_condition'], + couponItem['isSet'], + (val) { + setState(() { + couponItem['isSet'] = val; + }); + } + )); + }); + } + + widgetList.add(wrapButton(getScreenMargins(36.0), getCompleteButton())); + + widgetList.add(wrapButton( getScreenMargins(24.0), getScanButton( context, @@ -91,7 +109,7 @@ class PurchaseScreenState extends BaseState { )); return new Column(children: [ new Expanded( - child: new ListView(children: widgets)) + child: new ListView(children: widgetList)) ]); } @@ -189,22 +207,37 @@ class PurchaseScreenState extends BaseState { ); } - requestLoyalty(String url) async { + requestAsyncData(Map user) async { bool showBonus = await platform.invokeMethod('showBonus'); String flavor = await platform.invokeMethod('getFlavor'); - if (await platform.invokeMethod('isOnline') && !this.dataLoaded) { - getLoyaltyRequest(url, helper).then((response) { - print(response); - this.dataLoaded = true; - Map loyality = JSON.decode(response.body); - setState(() { - this.flavor = flavor; - this.loyalityType = loyality['type']; - setBonuses(loyality, showBonus); - }); - }).catchError((error) { + if (await platform.invokeMethod('isOnline') && !this.dataRequested) { + this.dataRequested = true; + var response, couponResponse; + print(user['coupons_url']); + try { + var token = await helper.getToken(); + response = await getLoyaltyRequest(user['loyalty_url'], token); + couponResponse = await getCouponsRequest( + user['coupons_url'] + '?status=active', + token + ); + } catch(error) { print(error.toString()); + } + Map loyality = JSON.decode(response.body); + Map coupons = JSON.decode(couponResponse.body); + + coupons['results'].forEach((couponItem) { + couponItem['isSet'] = false; }); + + setState(() { + this.flavor = flavor; + this.coupons = coupons['results']; + this.loyalityType = loyality['type']; + setBonuses(loyality, showBonus); + }); + } } @@ -302,18 +335,30 @@ class PurchaseScreenState extends BaseState { String token = await helper.getToken(); var result = await helper.getMerchantID(); var currency = await helper.getCurrency(); + var couponIds = []; + + this.coupons.forEach((item) { + if (item['isSet'] == true) couponIds.add(item['id']); + }); + + print(couponIds); var body = { 'doc_id': result, 'curr_iso_code': currency.toString(), 'commit': 'true', - 'sum_total': sumTotal + 'sum_total': sumTotal, +// 'coupons': [] }; if (bonusController.text.length > 0) { body['bonus_payment'] = bonusController.text; } + if (couponIds.length > 0) { + body['coupons'] = couponIds.join(","); + } + var purchaseResponse; Map purchase; diff --git a/lib/screens/purchase_success.dart b/lib/screens/purchase_success.dart index e40b006..0cc46a5 100644 --- a/lib/screens/purchase_success.dart +++ b/lib/screens/purchase_success.dart @@ -108,12 +108,13 @@ class PurchaseSuccessScreenState extends BaseState { if (this.coupons.length > 0) { widgetList.add(getItemTitle(StringsLocalization.couponsUsed())); this.coupons.forEach((couponItem) { - widgetList.add(getCoupons(couponItem)); + widgetList.add(getItemToggle( + couponItem['offer_name'], + couponItem['coupon_condition'] + )); }); } - - widgetList.add(wrapButton(getScreenMargins(174.0), getScanButton())); return new ListView.builder( @@ -140,33 +141,7 @@ class PurchaseSuccessScreenState extends BaseState { }; } - getCoupons(couponItem) { - String title = couponItem['offer_name']; - if (title.length == 0) { - title = couponItem['coupon_condition'].substring(0,30) + " ..."; - } - return new Container( - margin: new EdgeInsets.only(left: 5.0, right: 5.0, top: 5.0), - child: new Card( - child: new ExpansionTile( - title: new Text( - title, - style: Theme.of(context).textTheme.button.copyWith( - fontWeight: FontWeight.bold, - color: faqTitlesColor - ) - ), - children: [ - new Container( - 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']), - ) - ] - ) - ) - ); - } + getSuccessMessage() { return new Row( diff --git a/lib/strings.dart b/lib/strings.dart index c84cca6..4bb19c0 100644 --- a/lib/strings.dart +++ b/lib/strings.dart @@ -98,6 +98,7 @@ class StringsLocalization { static String sum() => Intl.message('sum', name: 'sum', locale: Intl.defaultLocale); static String bonus() => Intl.message('bonus', name: 'bonus', locale: Intl.defaultLocale); static String couponsUsed() => Intl.message('coupons_used', name: 'coupons_used', locale: Intl.defaultLocale); + static String selectCoupons() => Intl.message('select_coupons', name: 'select_coupons', locale: Intl.defaultLocale); static String carryingPurchase() => Intl.message('carry_purchase', name: 'carry_purchase', locale: Intl.defaultLocale); static String completePurchase() => Intl.message('complite_purchase', name: 'complite_purchase', locale: Intl.defaultLocale); static String scan() => Intl.message('scan', name: 'scan', locale: Intl.defaultLocale);