OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/payments/payment_items_display_view_controller.h" |
| 6 |
| 7 #import "base/ios/weak_nsobject.h" |
| 8 #include "base/mac/scoped_nsobject.h" |
| 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "components/autofill/core/browser/credit_card.h" |
| 11 #import "ios/chrome/browser/payments/cells/order_summary_line_item.h" |
| 12 #import "ios/chrome/browser/payments/cells/order_summary_total_item.h" |
| 13 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_detail_item
.h" |
| 14 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" |
| 15 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 16 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" |
| 17 #import "ios/chrome/browser/ui/icons/chrome_icon.h" |
| 18 #include "ios/chrome/browser/ui/rtl_geometry.h" |
| 19 #include "ios/chrome/grit/ios_strings.h" |
| 20 #import "ios/third_party/material_components_ios/src/components/Buttons/src/Mate
rialButtons.h" |
| 21 #include "ui/base/l10n/l10n_util.h" |
| 22 |
| 23 NSString* const kPaymentItemsDisplayCollectionViewId = |
| 24 @"kPaymentItemsDisplayCollectionViewId"; |
| 25 |
| 26 namespace { |
| 27 |
| 28 const CGFloat kButtonEdgeInset = 9; |
| 29 const CGFloat kSeparatorEdgeInset = 14; |
| 30 |
| 31 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| 32 SectionIdentifierPayment = kSectionIdentifierEnumZero, |
| 33 }; |
| 34 |
| 35 typedef NS_ENUM(NSInteger, ItemType) { |
| 36 ItemTypePaymentItem = kItemTypeEnumZero, // This is a repeated item type. |
| 37 }; |
| 38 |
| 39 } // namespace |
| 40 |
| 41 @interface PaymentItemsDisplayViewController () { |
| 42 base::WeakNSProtocol<id<PaymentItemsDisplayViewControllerDelegate>> _delegate; |
| 43 base::scoped_nsobject<MDCFlatButton> _payButton; |
| 44 } |
| 45 |
| 46 // Called when the user presses the return button. |
| 47 - (void)onReturn; |
| 48 |
| 49 // Called when the user presses the pay button. |
| 50 - (void)onConfirm; |
| 51 |
| 52 @end |
| 53 |
| 54 @implementation PaymentItemsDisplayViewController |
| 55 |
| 56 @synthesize total = _total; |
| 57 @synthesize paymentItems = _paymentItems; |
| 58 |
| 59 - (instancetype)initWithPayButtonEnabled:(BOOL)payButtonEnabled { |
| 60 if ((self = [super initWithStyle:CollectionViewControllerStyleAppBar])) { |
| 61 [self setTitle:l10n_util::GetNSString( |
| 62 IDS_IOS_PAYMENT_REQUEST_PAYMENT_ITEMS_TITLE)]; |
| 63 |
| 64 // Set up left (return) button. |
| 65 UIBarButtonItem* returnButton = |
| 66 [ChromeIcon templateBarButtonItemWithImage:[ChromeIcon backIcon] |
| 67 target:nil |
| 68 action:@selector(onReturn)]; |
| 69 [self navigationItem].leftBarButtonItem = returnButton; |
| 70 |
| 71 // Set up right (pay) button. |
| 72 _payButton.reset([[MDCFlatButton alloc] init]); |
| 73 [_payButton |
| 74 setTitle:l10n_util::GetNSString(IDS_IOS_PAYMENT_REQUEST_PAY_BUTTON) |
| 75 forState:UIControlStateNormal]; |
| 76 [_payButton setBackgroundColor:[[MDCPalette cr_bluePalette] tint500] |
| 77 forState:UIControlStateNormal]; |
| 78 [_payButton setInkColor:[UIColor colorWithWhite:1 alpha:0.2]]; |
| 79 [_payButton setBackgroundColor:[UIColor grayColor] |
| 80 forState:UIControlStateDisabled]; |
| 81 [_payButton addTarget:nil |
| 82 action:@selector(onConfirm) |
| 83 forControlEvents:UIControlEventTouchUpInside]; |
| 84 [_payButton sizeToFit]; |
| 85 [_payButton setEnabled:payButtonEnabled]; |
| 86 [_payButton setAutoresizingMask:UIViewAutoresizingFlexibleTrailingMargin() | |
| 87 UIViewAutoresizingFlexibleTopMargin | |
| 88 UIViewAutoresizingFlexibleBottomMargin]; |
| 89 |
| 90 // The navigation bar will set the rightBarButtonItem's height to the full |
| 91 // height of the bar. We don't want that for the button so we use a UIView |
| 92 // here to contain the button instead and the button is vertically centered |
| 93 // inside the full bar height. |
| 94 UIView* buttonView = |
| 95 [[[UIView alloc] initWithFrame:CGRectZero] autorelease]; |
| 96 [buttonView addSubview:_payButton]; |
| 97 // Navigation bar button items are aligned with the trailing edge of the |
| 98 // screen. Make the enclosing view larger here. The pay button will be |
| 99 // aligned with the leading edge of the enclosing view leaving an inset on |
| 100 // the trailing edge. |
| 101 CGRect buttonViewBounds = buttonView.bounds; |
| 102 buttonViewBounds.size.width = |
| 103 [_payButton frame].size.width + kButtonEdgeInset; |
| 104 buttonView.bounds = buttonViewBounds; |
| 105 |
| 106 UIBarButtonItem* payButtonItem = |
| 107 [[[UIBarButtonItem alloc] initWithCustomView:buttonView] autorelease]; |
| 108 [self navigationItem].rightBarButtonItem = payButtonItem; |
| 109 } |
| 110 return self; |
| 111 } |
| 112 |
| 113 - (id<PaymentItemsDisplayViewControllerDelegate>)delegate { |
| 114 return _delegate.get(); |
| 115 } |
| 116 |
| 117 - (void)setDelegate:(id<PaymentItemsDisplayViewControllerDelegate>)delegate { |
| 118 _delegate.reset(delegate); |
| 119 } |
| 120 |
| 121 - (void)onReturn { |
| 122 [_payButton setEnabled:NO]; |
| 123 [_delegate paymentItemsDisplayViewControllerDidReturn:self]; |
| 124 } |
| 125 |
| 126 - (void)onConfirm { |
| 127 [_payButton setEnabled:NO]; |
| 128 [_delegate paymentItemsDisplayViewControllerDidConfirm:self]; |
| 129 } |
| 130 |
| 131 #pragma mark - CollectionViewController methods |
| 132 |
| 133 - (void)loadModel { |
| 134 [super loadModel]; |
| 135 CollectionViewModel* model = self.collectionViewModel; |
| 136 [model addSectionWithIdentifier:SectionIdentifierPayment]; |
| 137 |
| 138 NSNumberFormatter* currencyFormatter = |
| 139 [[[NSNumberFormatter alloc] init] autorelease]; |
| 140 [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle]; |
| 141 |
| 142 // Add the total entry. |
| 143 OrderSummaryTotalItem* totalItem = [[[OrderSummaryTotalItem alloc] |
| 144 initWithType:ItemTypePaymentItem] autorelease]; |
| 145 totalItem.text = base::SysUTF16ToNSString(_total.label); |
| 146 |
| 147 NSString* currencyCode = base::SysUTF16ToNSString(_total.amount.currency); |
| 148 NSDecimalNumber* value = [NSDecimalNumber |
| 149 decimalNumberWithString:SysUTF16ToNSString(_total.amount.value)]; |
| 150 [currencyFormatter setCurrencyCode:currencyCode]; |
| 151 totalItem.detailText = [currencyFormatter stringFromNumber:value]; |
| 152 |
| 153 [model addItem:totalItem toSectionWithIdentifier:SectionIdentifierPayment]; |
| 154 |
| 155 // Add the line item entries. |
| 156 for (size_t i = 0; i < _paymentItems.size(); ++i) { |
| 157 web::PaymentItem paymentItem = _paymentItems[i]; |
| 158 OrderSummaryLineItem* paymentItemItem = [[[OrderSummaryLineItem alloc] |
| 159 initWithType:ItemTypePaymentItem] autorelease]; |
| 160 paymentItemItem.text = base::SysUTF16ToNSString(paymentItem.label); |
| 161 |
| 162 NSString* currencyCode = |
| 163 base::SysUTF16ToNSString(paymentItem.amount.currency); |
| 164 NSDecimalNumber* value = [NSDecimalNumber |
| 165 decimalNumberWithString:SysUTF16ToNSString(paymentItem.amount.value)]; |
| 166 [currencyFormatter setCurrencyCode:currencyCode]; |
| 167 paymentItemItem.detailText = [currencyFormatter stringFromNumber:value]; |
| 168 |
| 169 [model addItem:paymentItemItem |
| 170 toSectionWithIdentifier:SectionIdentifierPayment]; |
| 171 } |
| 172 } |
| 173 |
| 174 - (void)viewDidLoad { |
| 175 [super viewDidLoad]; |
| 176 self.collectionView.accessibilityIdentifier = |
| 177 kPaymentItemsDisplayCollectionViewId; |
| 178 |
| 179 // Customize collection view settings. |
| 180 self.styler.cellStyle = MDCCollectionViewCellStyleCard; |
| 181 self.styler.separatorInset = |
| 182 UIEdgeInsetsMake(0, kSeparatorEdgeInset, 0, kSeparatorEdgeInset); |
| 183 } |
| 184 |
| 185 #pragma mark MDCCollectionViewStylingDelegate |
| 186 |
| 187 // There are no effects from touching the payment items so there should not be |
| 188 // an ink ripple. |
| 189 - (BOOL)collectionView:(UICollectionView*)collectionView |
| 190 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath { |
| 191 return YES; |
| 192 } |
| 193 |
| 194 @end |
OLD | NEW |