| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/chrome/browser/payments/payment_items_display_view_controller.h" | 5 #import "ios/chrome/browser/payments/payment_items_display_view_controller.h" |
| 6 | 6 |
| 7 #import "base/ios/weak_nsobject.h" | |
| 8 #include "base/mac/foundation_util.h" | 7 #include "base/mac/foundation_util.h" |
| 9 #include "base/mac/scoped_nsobject.h" | |
| 10 #include "base/strings/sys_string_conversions.h" | 8 #include "base/strings/sys_string_conversions.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 12 #include "components/autofill/core/browser/credit_card.h" | 10 #include "components/autofill/core/browser/credit_card.h" |
| 13 #include "components/payments/currency_formatter.h" | 11 #include "components/payments/currency_formatter.h" |
| 14 #include "components/strings/grit/components_strings.h" | 12 #include "components/strings/grit/components_strings.h" |
| 15 #import "ios/chrome/browser/payments/cells/price_item.h" | 13 #import "ios/chrome/browser/payments/cells/price_item.h" |
| 16 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" | 14 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" |
| 17 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" | 15 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 18 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" | 16 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" |
| 19 #import "ios/chrome/browser/ui/icons/chrome_icon.h" | 17 #import "ios/chrome/browser/ui/icons/chrome_icon.h" |
| 20 #include "ios/chrome/browser/ui/rtl_geometry.h" | 18 #include "ios/chrome/browser/ui/rtl_geometry.h" |
| 21 #include "ios/chrome/grit/ios_strings.h" | 19 #include "ios/chrome/grit/ios_strings.h" |
| 22 #import "ios/third_party/material_components_ios/src/components/Buttons/src/Mate
rialButtons.h" | 20 #import "ios/third_party/material_components_ios/src/components/Buttons/src/Mate
rialButtons.h" |
| 23 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF
ontLoader.h" | 21 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF
ontLoader.h" |
| 24 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
| 25 | 23 |
| 24 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 25 #error "This file requires ARC support." |
| 26 #endif |
| 27 |
| 26 NSString* const kPaymentItemsDisplayCollectionViewId = | 28 NSString* const kPaymentItemsDisplayCollectionViewId = |
| 27 @"kPaymentItemsDisplayCollectionViewId"; | 29 @"kPaymentItemsDisplayCollectionViewId"; |
| 28 NSString* const kPaymentItemsDisplayItemId = @"kPaymentItemsDisplayItemId"; | 30 NSString* const kPaymentItemsDisplayItemId = @"kPaymentItemsDisplayItemId"; |
| 29 | 31 |
| 30 namespace { | 32 namespace { |
| 31 | 33 |
| 32 const CGFloat kButtonEdgeInset = 9; | 34 const CGFloat kButtonEdgeInset = 9; |
| 33 const CGFloat kSeparatorEdgeInset = 14; | 35 const CGFloat kSeparatorEdgeInset = 14; |
| 34 | 36 |
| 35 typedef NS_ENUM(NSInteger, SectionIdentifier) { | 37 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| 36 SectionIdentifierPayment = kSectionIdentifierEnumZero, | 38 SectionIdentifierPayment = kSectionIdentifierEnumZero, |
| 37 }; | 39 }; |
| 38 | 40 |
| 39 typedef NS_ENUM(NSInteger, ItemType) { | 41 typedef NS_ENUM(NSInteger, ItemType) { |
| 40 ItemTypePaymentItemTotal = kItemTypeEnumZero, | 42 ItemTypePaymentItemTotal = kItemTypeEnumZero, |
| 41 ItemTypePaymentItem, // This is a repeated item type. | 43 ItemTypePaymentItem, // This is a repeated item type. |
| 42 }; | 44 }; |
| 43 | 45 |
| 44 } // namespace | 46 } // namespace |
| 45 | 47 |
| 46 @interface PaymentItemsDisplayViewController () { | 48 @interface PaymentItemsDisplayViewController () { |
| 47 base::WeakNSProtocol<id<PaymentItemsDisplayViewControllerDelegate>> _delegate; | 49 MDCFlatButton* _payButton; |
| 48 base::scoped_nsobject<MDCFlatButton> _payButton; | |
| 49 | 50 |
| 50 // The PaymentRequest object owning an instance of web::PaymentRequest as | 51 // The PaymentRequest object owning an instance of web::PaymentRequest as |
| 51 // provided by the page invoking the Payment Request API. This is a weak | 52 // provided by the page invoking the Payment Request API. This is a weak |
| 52 // pointer and should outlive this class. | 53 // pointer and should outlive this class. |
| 53 PaymentRequest* _paymentRequest; | 54 PaymentRequest* _paymentRequest; |
| 54 } | 55 } |
| 55 | 56 |
| 56 // Called when the user presses the return button. | 57 // Called when the user presses the return button. |
| 57 - (void)onReturn; | 58 - (void)onReturn; |
| 58 | 59 |
| 59 // Called when the user presses the pay button. | 60 // Called when the user presses the pay button. |
| 60 - (void)onConfirm; | 61 - (void)onConfirm; |
| 61 | 62 |
| 62 @end | 63 @end |
| 63 | 64 |
| 64 @implementation PaymentItemsDisplayViewController | 65 @implementation PaymentItemsDisplayViewController |
| 66 @synthesize delegate = _delegate; |
| 65 | 67 |
| 66 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest | 68 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest |
| 67 payButtonEnabled:(BOOL)payButtonEnabled { | 69 payButtonEnabled:(BOOL)payButtonEnabled { |
| 68 DCHECK(paymentRequest); | 70 DCHECK(paymentRequest); |
| 69 if ((self = [super initWithStyle:CollectionViewControllerStyleAppBar])) { | 71 if ((self = [super initWithStyle:CollectionViewControllerStyleAppBar])) { |
| 70 [self setTitle:l10n_util::GetNSString( | 72 [self setTitle:l10n_util::GetNSString( |
| 71 IDS_IOS_PAYMENT_REQUEST_PAYMENT_ITEMS_TITLE)]; | 73 IDS_IOS_PAYMENT_REQUEST_PAYMENT_ITEMS_TITLE)]; |
| 72 | 74 |
| 73 // Set up left (return) button. | 75 // Set up left (return) button. |
| 74 UIBarButtonItem* returnButton = | 76 UIBarButtonItem* returnButton = |
| 75 [ChromeIcon templateBarButtonItemWithImage:[ChromeIcon backIcon] | 77 [ChromeIcon templateBarButtonItemWithImage:[ChromeIcon backIcon] |
| 76 target:nil | 78 target:nil |
| 77 action:@selector(onReturn)]; | 79 action:@selector(onReturn)]; |
| 78 [returnButton | 80 [returnButton |
| 79 setAccessibilityLabel:l10n_util::GetNSString(IDS_ACCNAME_BACK)]; | 81 setAccessibilityLabel:l10n_util::GetNSString(IDS_ACCNAME_BACK)]; |
| 80 [self navigationItem].leftBarButtonItem = returnButton; | 82 [self navigationItem].leftBarButtonItem = returnButton; |
| 81 | 83 |
| 82 // Set up right (pay) button. | 84 // Set up right (pay) button. |
| 83 _payButton.reset([[MDCFlatButton alloc] init]); | 85 _payButton = [[MDCFlatButton alloc] init]; |
| 84 [_payButton | 86 [_payButton |
| 85 setTitle:l10n_util::GetNSString(IDS_IOS_PAYMENT_REQUEST_PAY_BUTTON) | 87 setTitle:l10n_util::GetNSString(IDS_IOS_PAYMENT_REQUEST_PAY_BUTTON) |
| 86 forState:UIControlStateNormal]; | 88 forState:UIControlStateNormal]; |
| 87 [_payButton setBackgroundColor:[[MDCPalette cr_bluePalette] tint500] | 89 [_payButton setBackgroundColor:[[MDCPalette cr_bluePalette] tint500] |
| 88 forState:UIControlStateNormal]; | 90 forState:UIControlStateNormal]; |
| 89 [_payButton setInkColor:[UIColor colorWithWhite:1 alpha:0.2]]; | 91 [_payButton setInkColor:[UIColor colorWithWhite:1 alpha:0.2]]; |
| 90 [_payButton setBackgroundColor:[UIColor grayColor] | 92 [_payButton setBackgroundColor:[UIColor grayColor] |
| 91 forState:UIControlStateDisabled]; | 93 forState:UIControlStateDisabled]; |
| 92 [_payButton addTarget:nil | 94 [_payButton addTarget:nil |
| 93 action:@selector(onConfirm) | 95 action:@selector(onConfirm) |
| 94 forControlEvents:UIControlEventTouchUpInside]; | 96 forControlEvents:UIControlEventTouchUpInside]; |
| 95 [_payButton sizeToFit]; | 97 [_payButton sizeToFit]; |
| 96 [_payButton setEnabled:payButtonEnabled]; | 98 [_payButton setEnabled:payButtonEnabled]; |
| 97 [_payButton setAutoresizingMask:UIViewAutoresizingFlexibleTrailingMargin() | | 99 [_payButton setAutoresizingMask:UIViewAutoresizingFlexibleTrailingMargin() | |
| 98 UIViewAutoresizingFlexibleTopMargin | | 100 UIViewAutoresizingFlexibleTopMargin | |
| 99 UIViewAutoresizingFlexibleBottomMargin]; | 101 UIViewAutoresizingFlexibleBottomMargin]; |
| 100 | 102 |
| 101 // The navigation bar will set the rightBarButtonItem's height to the full | 103 // The navigation bar will set the rightBarButtonItem's height to the full |
| 102 // height of the bar. We don't want that for the button so we use a UIView | 104 // height of the bar. We don't want that for the button so we use a UIView |
| 103 // here to contain the button instead and the button is vertically centered | 105 // here to contain the button instead and the button is vertically centered |
| 104 // inside the full bar height. | 106 // inside the full bar height. |
| 105 UIView* buttonView = | 107 UIView* buttonView = [[UIView alloc] initWithFrame:CGRectZero]; |
| 106 [[[UIView alloc] initWithFrame:CGRectZero] autorelease]; | |
| 107 [buttonView addSubview:_payButton]; | 108 [buttonView addSubview:_payButton]; |
| 108 // Navigation bar button items are aligned with the trailing edge of the | 109 // Navigation bar button items are aligned with the trailing edge of the |
| 109 // screen. Make the enclosing view larger here. The pay button will be | 110 // screen. Make the enclosing view larger here. The pay button will be |
| 110 // aligned with the leading edge of the enclosing view leaving an inset on | 111 // aligned with the leading edge of the enclosing view leaving an inset on |
| 111 // the trailing edge. | 112 // the trailing edge. |
| 112 CGRect buttonViewBounds = buttonView.bounds; | 113 CGRect buttonViewBounds = buttonView.bounds; |
| 113 buttonViewBounds.size.width = | 114 buttonViewBounds.size.width = |
| 114 [_payButton frame].size.width + kButtonEdgeInset; | 115 [_payButton frame].size.width + kButtonEdgeInset; |
| 115 buttonView.bounds = buttonViewBounds; | 116 buttonView.bounds = buttonViewBounds; |
| 116 | 117 |
| 117 UIBarButtonItem* payButtonItem = | 118 UIBarButtonItem* payButtonItem = |
| 118 [[[UIBarButtonItem alloc] initWithCustomView:buttonView] autorelease]; | 119 [[UIBarButtonItem alloc] initWithCustomView:buttonView]; |
| 119 [self navigationItem].rightBarButtonItem = payButtonItem; | 120 [self navigationItem].rightBarButtonItem = payButtonItem; |
| 120 | 121 |
| 121 _paymentRequest = paymentRequest; | 122 _paymentRequest = paymentRequest; |
| 122 } | 123 } |
| 123 return self; | 124 return self; |
| 124 } | 125 } |
| 125 | 126 |
| 126 - (id<PaymentItemsDisplayViewControllerDelegate>)delegate { | |
| 127 return _delegate.get(); | |
| 128 } | |
| 129 | |
| 130 - (void)setDelegate:(id<PaymentItemsDisplayViewControllerDelegate>)delegate { | |
| 131 _delegate.reset(delegate); | |
| 132 } | |
| 133 | |
| 134 - (void)onReturn { | 127 - (void)onReturn { |
| 135 [_payButton setEnabled:NO]; | 128 [_payButton setEnabled:NO]; |
| 136 [_delegate paymentItemsDisplayViewControllerDidReturn:self]; | 129 [_delegate paymentItemsDisplayViewControllerDidReturn:self]; |
| 137 } | 130 } |
| 138 | 131 |
| 139 - (void)onConfirm { | 132 - (void)onConfirm { |
| 140 [_payButton setEnabled:NO]; | 133 [_payButton setEnabled:NO]; |
| 141 [_delegate paymentItemsDisplayViewControllerDidConfirm:self]; | 134 [_delegate paymentItemsDisplayViewControllerDidConfirm:self]; |
| 142 } | 135 } |
| 143 | 136 |
| 144 #pragma mark - CollectionViewController methods | 137 #pragma mark - CollectionViewController methods |
| 145 | 138 |
| 146 - (void)loadModel { | 139 - (void)loadModel { |
| 147 [super loadModel]; | 140 [super loadModel]; |
| 148 CollectionViewModel* model = self.collectionViewModel; | 141 CollectionViewModel* model = self.collectionViewModel; |
| 149 [model addSectionWithIdentifier:SectionIdentifierPayment]; | 142 [model addSectionWithIdentifier:SectionIdentifierPayment]; |
| 150 | 143 |
| 151 // Add the total entry. | 144 // Add the total entry. |
| 152 PriceItem* totalItem = | 145 PriceItem* totalItem = |
| 153 [[[PriceItem alloc] initWithType:ItemTypePaymentItemTotal] autorelease]; | 146 [[PriceItem alloc] initWithType:ItemTypePaymentItemTotal]; |
| 154 totalItem.accessibilityIdentifier = kPaymentItemsDisplayItemId; | 147 totalItem.accessibilityIdentifier = kPaymentItemsDisplayItemId; |
| 155 totalItem.item = | 148 totalItem.item = |
| 156 base::SysUTF16ToNSString(_paymentRequest->payment_details().total.label); | 149 base::SysUTF16ToNSString(_paymentRequest->payment_details().total.label); |
| 157 payments::CurrencyFormatter* currencyFormatter = | 150 payments::CurrencyFormatter* currencyFormatter = |
| 158 _paymentRequest->GetOrCreateCurrencyFormatter(); | 151 _paymentRequest->GetOrCreateCurrencyFormatter(); |
| 159 totalItem.price = SysUTF16ToNSString(l10n_util::GetStringFUTF16( | 152 totalItem.price = SysUTF16ToNSString(l10n_util::GetStringFUTF16( |
| 160 IDS_IOS_PAYMENT_REQUEST_PAYMENT_ITEMS_TOTAL_FORMAT, | 153 IDS_IOS_PAYMENT_REQUEST_PAYMENT_ITEMS_TOTAL_FORMAT, |
| 161 base::UTF8ToUTF16(currencyFormatter->formatted_currency_code()), | 154 base::UTF8ToUTF16(currencyFormatter->formatted_currency_code()), |
| 162 currencyFormatter->Format(base::UTF16ToASCII( | 155 currencyFormatter->Format(base::UTF16ToASCII( |
| 163 _paymentRequest->payment_details().total.amount.value)))); | 156 _paymentRequest->payment_details().total.amount.value)))); |
| 164 | 157 |
| 165 [model addItem:totalItem toSectionWithIdentifier:SectionIdentifierPayment]; | 158 [model addItem:totalItem toSectionWithIdentifier:SectionIdentifierPayment]; |
| 166 | 159 |
| 167 // Add the line item entries. | 160 // Add the line item entries. |
| 168 for (const auto& paymentItem : | 161 for (const auto& paymentItem : |
| 169 _paymentRequest->payment_details().display_items) { | 162 _paymentRequest->payment_details().display_items) { |
| 170 PriceItem* paymentItemItem = | 163 PriceItem* paymentItemItem = |
| 171 [[[PriceItem alloc] initWithType:ItemTypePaymentItem] autorelease]; | 164 [[PriceItem alloc] initWithType:ItemTypePaymentItem]; |
| 172 paymentItemItem.accessibilityIdentifier = kPaymentItemsDisplayItemId; | 165 paymentItemItem.accessibilityIdentifier = kPaymentItemsDisplayItemId; |
| 173 paymentItemItem.item = base::SysUTF16ToNSString(paymentItem.label); | 166 paymentItemItem.item = base::SysUTF16ToNSString(paymentItem.label); |
| 174 payments::CurrencyFormatter* currencyFormatter = | 167 payments::CurrencyFormatter* currencyFormatter = |
| 175 _paymentRequest->GetOrCreateCurrencyFormatter(); | 168 _paymentRequest->GetOrCreateCurrencyFormatter(); |
| 176 paymentItemItem.price = SysUTF16ToNSString(currencyFormatter->Format( | 169 paymentItemItem.price = SysUTF16ToNSString(currencyFormatter->Format( |
| 177 base::UTF16ToASCII(paymentItem.amount.value))); | 170 base::UTF16ToASCII(paymentItem.amount.value))); |
| 178 [model addItem:paymentItemItem | 171 [model addItem:paymentItemItem |
| 179 toSectionWithIdentifier:SectionIdentifierPayment]; | 172 toSectionWithIdentifier:SectionIdentifierPayment]; |
| 180 } | 173 } |
| 181 } | 174 } |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 #pragma mark MDCCollectionViewStylingDelegate | 223 #pragma mark MDCCollectionViewStylingDelegate |
| 231 | 224 |
| 232 // There are no effects from touching the payment items so there should not be | 225 // There are no effects from touching the payment items so there should not be |
| 233 // an ink ripple. | 226 // an ink ripple. |
| 234 - (BOOL)collectionView:(UICollectionView*)collectionView | 227 - (BOOL)collectionView:(UICollectionView*)collectionView |
| 235 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath { | 228 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath { |
| 236 return YES; | 229 return YES; |
| 237 } | 230 } |
| 238 | 231 |
| 239 @end | 232 @end |
| OLD | NEW |