| 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/shipping_address_selection_view_controller.
h" | 5 #import "ios/chrome/browser/payments/shipping_address_selection_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/strings/sys_string_conversions.h" | 8 #include "base/strings/sys_string_conversions.h" |
| 10 #include "components/autofill/core/browser/autofill_profile.h" | 9 #include "components/autofill/core/browser/autofill_profile.h" |
| 11 #include "components/strings/grit/components_strings.h" | 10 #include "components/strings/grit/components_strings.h" |
| 12 #include "ios/chrome/browser/application_context.h" | 11 #include "ios/chrome/browser/application_context.h" |
| 13 #import "ios/chrome/browser/payments/cells/payments_text_item.h" | 12 #import "ios/chrome/browser/payments/cells/payments_text_item.h" |
| 14 #import "ios/chrome/browser/payments/cells/shipping_address_item.h" | 13 #import "ios/chrome/browser/payments/cells/shipping_address_item.h" |
| 15 #import "ios/chrome/browser/payments/payment_request_util.h" | 14 #import "ios/chrome/browser/payments/payment_request_util.h" |
| 16 #import "ios/chrome/browser/ui/autofill/cells/status_item.h" | 15 #import "ios/chrome/browser/ui/autofill/cells/status_item.h" |
| 17 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom
e.h" | 16 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom
e.h" |
| 18 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" | 17 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" |
| 19 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h
" | 18 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h
" |
| 20 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" | 19 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 21 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" | 20 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" |
| 22 #import "ios/chrome/browser/ui/icons/chrome_icon.h" | 21 #import "ios/chrome/browser/ui/icons/chrome_icon.h" |
| 23 #include "ios/chrome/browser/ui/uikit_ui_util.h" | 22 #include "ios/chrome/browser/ui/uikit_ui_util.h" |
| 24 #include "ios/chrome/grit/ios_strings.h" | 23 #include "ios/chrome/grit/ios_strings.h" |
| 25 #include "ios/chrome/grit/ios_theme_resources.h" | 24 #include "ios/chrome/grit/ios_theme_resources.h" |
| 26 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
| 27 | 26 |
| 27 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 28 #error "This file requires ARC support." |
| 29 #endif |
| 30 |
| 28 using payment_request_util::NameLabelFromAutofillProfile; | 31 using payment_request_util::NameLabelFromAutofillProfile; |
| 29 using payment_request_util::AddressLabelFromAutofillProfile; | 32 using payment_request_util::AddressLabelFromAutofillProfile; |
| 30 using payment_request_util::PhoneNumberLabelFromAutofillProfile; | 33 using payment_request_util::PhoneNumberLabelFromAutofillProfile; |
| 31 | 34 |
| 32 NSString* const kShippingAddressSelectionCollectionViewId = | 35 NSString* const kShippingAddressSelectionCollectionViewId = |
| 33 @"kShippingAddressSelectionCollectionViewId"; | 36 @"kShippingAddressSelectionCollectionViewId"; |
| 34 | 37 |
| 35 namespace { | 38 namespace { |
| 36 | 39 |
| 37 const CGFloat kSeparatorEdgeInset = 14; | 40 const CGFloat kSeparatorEdgeInset = 14; |
| 38 | 41 |
| 39 typedef NS_ENUM(NSInteger, SectionIdentifier) { | 42 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| 40 SectionIdentifierShippingAddress = kSectionIdentifierEnumZero, | 43 SectionIdentifierShippingAddress = kSectionIdentifierEnumZero, |
| 41 }; | 44 }; |
| 42 | 45 |
| 43 typedef NS_ENUM(NSInteger, ItemType) { | 46 typedef NS_ENUM(NSInteger, ItemType) { |
| 44 ItemTypeSpinner = kItemTypeEnumZero, | 47 ItemTypeSpinner = kItemTypeEnumZero, |
| 45 ItemTypeMessage, | 48 ItemTypeMessage, |
| 46 ItemTypeShippingAddress, // This is a repeated item type. | 49 ItemTypeShippingAddress, // This is a repeated item type. |
| 47 ItemTypeAddShippingAddress, | 50 ItemTypeAddShippingAddress, |
| 48 }; | 51 }; |
| 49 | 52 |
| 50 } // namespace | 53 } // namespace |
| 51 | 54 |
| 52 @interface ShippingAddressSelectionViewController () { | 55 @interface ShippingAddressSelectionViewController () { |
| 53 base::WeakNSProtocol<id<ShippingAddressSelectionViewControllerDelegate>> | |
| 54 _delegate; | |
| 55 | |
| 56 // The PaymentRequest object owning an instance of web::PaymentRequest as | 56 // The PaymentRequest object owning an instance of web::PaymentRequest as |
| 57 // provided by the page invoking the Payment Request API. This is a weak | 57 // provided by the page invoking the Payment Request API. This is a weak |
| 58 // pointer and should outlive this class. | 58 // pointer and should outlive this class. |
| 59 PaymentRequest* _paymentRequest; | 59 PaymentRequest* _paymentRequest; |
| 60 | 60 |
| 61 // The currently selected item. May be nil. | 61 // The currently selected item. May be nil. |
| 62 ShippingAddressItem* _selectedItem; | 62 __weak ShippingAddressItem* _selectedItem; |
| 63 } | 63 } |
| 64 | 64 |
| 65 // Called when the user presses the return button. | 65 // Called when the user presses the return button. |
| 66 - (void)onReturn; | 66 - (void)onReturn; |
| 67 | 67 |
| 68 @end | 68 @end |
| 69 | 69 |
| 70 @implementation ShippingAddressSelectionViewController | 70 @implementation ShippingAddressSelectionViewController |
| 71 | 71 |
| 72 @synthesize isLoading = _isLoading; | 72 @synthesize isLoading = _isLoading; |
| 73 @synthesize errorMessage = _errorMessage; | 73 @synthesize errorMessage = _errorMessage; |
| 74 @synthesize delegate = _delegate; |
| 74 | 75 |
| 75 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest { | 76 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest { |
| 76 DCHECK(paymentRequest); | 77 DCHECK(paymentRequest); |
| 77 if ((self = [super initWithStyle:CollectionViewControllerStyleAppBar])) { | 78 if ((self = [super initWithStyle:CollectionViewControllerStyleAppBar])) { |
| 78 self.title = | 79 self.title = |
| 79 payment_request_util::GetShippingAddressSelectorTitle(paymentRequest); | 80 payment_request_util::GetShippingAddressSelectorTitle(paymentRequest); |
| 80 | 81 |
| 81 UIBarButtonItem* returnButton = | 82 UIBarButtonItem* returnButton = |
| 82 [ChromeIcon templateBarButtonItemWithImage:[ChromeIcon backIcon] | 83 [ChromeIcon templateBarButtonItemWithImage:[ChromeIcon backIcon] |
| 83 target:nil | 84 target:nil |
| 84 action:@selector(onReturn)]; | 85 action:@selector(onReturn)]; |
| 85 returnButton.accessibilityLabel = l10n_util::GetNSString(IDS_ACCNAME_BACK); | 86 returnButton.accessibilityLabel = l10n_util::GetNSString(IDS_ACCNAME_BACK); |
| 86 self.navigationItem.leftBarButtonItem = returnButton; | 87 self.navigationItem.leftBarButtonItem = returnButton; |
| 87 | 88 |
| 88 _paymentRequest = paymentRequest; | 89 _paymentRequest = paymentRequest; |
| 89 } | 90 } |
| 90 return self; | 91 return self; |
| 91 } | 92 } |
| 92 | 93 |
| 93 - (id<ShippingAddressSelectionViewControllerDelegate>)delegate { | |
| 94 return _delegate.get(); | |
| 95 } | |
| 96 | |
| 97 - (void)setDelegate: | |
| 98 (id<ShippingAddressSelectionViewControllerDelegate>)delegate { | |
| 99 _delegate.reset(delegate); | |
| 100 } | |
| 101 | |
| 102 - (void)onReturn { | 94 - (void)onReturn { |
| 103 [_delegate shippingAddressSelectionViewControllerDidReturn:self]; | 95 [_delegate shippingAddressSelectionViewControllerDidReturn:self]; |
| 104 } | 96 } |
| 105 | 97 |
| 106 #pragma mark - CollectionViewController methods | 98 #pragma mark - CollectionViewController methods |
| 107 | 99 |
| 108 - (void)loadModel { | 100 - (void)loadModel { |
| 109 [super loadModel]; | 101 [super loadModel]; |
| 110 CollectionViewModel* model = self.collectionViewModel; | 102 CollectionViewModel* model = self.collectionViewModel; |
| 111 _selectedItem = nil; | 103 _selectedItem = nil; |
| 112 | 104 |
| 113 [model addSectionWithIdentifier:SectionIdentifierShippingAddress]; | 105 [model addSectionWithIdentifier:SectionIdentifierShippingAddress]; |
| 114 | 106 |
| 115 if (_isLoading) { | 107 if (_isLoading) { |
| 116 StatusItem* statusItem = | 108 StatusItem* statusItem = [[StatusItem alloc] initWithType:ItemTypeSpinner]; |
| 117 [[[StatusItem alloc] initWithType:ItemTypeSpinner] autorelease]; | |
| 118 statusItem.text = | 109 statusItem.text = |
| 119 l10n_util::GetNSString(IDS_IOS_PAYMENT_REQUEST_CHECKING_LABEL); | 110 l10n_util::GetNSString(IDS_IOS_PAYMENT_REQUEST_CHECKING_LABEL); |
| 120 [model addItem:statusItem | 111 [model addItem:statusItem |
| 121 toSectionWithIdentifier:SectionIdentifierShippingAddress]; | 112 toSectionWithIdentifier:SectionIdentifierShippingAddress]; |
| 122 return; | 113 return; |
| 123 } | 114 } |
| 124 | 115 |
| 125 PaymentsTextItem* messageItem = | 116 PaymentsTextItem* messageItem = |
| 126 [[[PaymentsTextItem alloc] initWithType:ItemTypeMessage] autorelease]; | 117 [[PaymentsTextItem alloc] initWithType:ItemTypeMessage]; |
| 127 if (_errorMessage) { | 118 if (_errorMessage) { |
| 128 messageItem.text = _errorMessage; | 119 messageItem.text = _errorMessage; |
| 129 messageItem.image = NativeImage(IDR_IOS_PAYMENTS_WARNING); | 120 messageItem.image = NativeImage(IDR_IOS_PAYMENTS_WARNING); |
| 130 } else { | 121 } else { |
| 131 messageItem.text = | 122 messageItem.text = |
| 132 payment_request_util::GetShippingAddressSelectorInfoMessage( | 123 payment_request_util::GetShippingAddressSelectorInfoMessage( |
| 133 _paymentRequest); | 124 _paymentRequest); |
| 134 } | 125 } |
| 135 [model addItem:messageItem | 126 [model addItem:messageItem |
| 136 toSectionWithIdentifier:SectionIdentifierShippingAddress]; | 127 toSectionWithIdentifier:SectionIdentifierShippingAddress]; |
| 137 | 128 |
| 138 for (const auto& shippingAddress : _paymentRequest->shipping_profiles()) { | 129 for (const auto& shippingAddress : _paymentRequest->shipping_profiles()) { |
| 139 ShippingAddressItem* item = [[[ShippingAddressItem alloc] | 130 ShippingAddressItem* item = |
| 140 initWithType:ItemTypeShippingAddress] autorelease]; | 131 [[ShippingAddressItem alloc] initWithType:ItemTypeShippingAddress]; |
| 141 item.accessibilityTraits |= UIAccessibilityTraitButton; | 132 item.accessibilityTraits |= UIAccessibilityTraitButton; |
| 142 item.name = NameLabelFromAutofillProfile(shippingAddress); | 133 item.name = NameLabelFromAutofillProfile(shippingAddress); |
| 143 item.address = AddressLabelFromAutofillProfile(shippingAddress); | 134 item.address = AddressLabelFromAutofillProfile(shippingAddress); |
| 144 item.phoneNumber = PhoneNumberLabelFromAutofillProfile(shippingAddress); | 135 item.phoneNumber = PhoneNumberLabelFromAutofillProfile(shippingAddress); |
| 145 if (_paymentRequest->selected_shipping_profile() == shippingAddress) { | 136 if (_paymentRequest->selected_shipping_profile() == shippingAddress) { |
| 146 item.accessoryType = MDCCollectionViewCellAccessoryCheckmark; | 137 item.accessoryType = MDCCollectionViewCellAccessoryCheckmark; |
| 147 _selectedItem = item; | 138 _selectedItem = item; |
| 148 } | 139 } |
| 149 [model addItem:item | 140 [model addItem:item |
| 150 toSectionWithIdentifier:SectionIdentifierShippingAddress]; | 141 toSectionWithIdentifier:SectionIdentifierShippingAddress]; |
| 151 } | 142 } |
| 152 | 143 |
| 153 PaymentsTextItem* addShippingAddress = [[[PaymentsTextItem alloc] | 144 PaymentsTextItem* addShippingAddress = |
| 154 initWithType:ItemTypeAddShippingAddress] autorelease]; | 145 [[PaymentsTextItem alloc] initWithType:ItemTypeAddShippingAddress]; |
| 155 addShippingAddress.text = l10n_util::GetNSString( | 146 addShippingAddress.text = l10n_util::GetNSString( |
| 156 IDS_IOS_PAYMENT_REQUEST_SHIPPING_ADDRESS_SELECTION_ADD_BUTTON); | 147 IDS_IOS_PAYMENT_REQUEST_SHIPPING_ADDRESS_SELECTION_ADD_BUTTON); |
| 157 addShippingAddress.image = NativeImage(IDR_IOS_PAYMENTS_ADD); | 148 addShippingAddress.image = NativeImage(IDR_IOS_PAYMENTS_ADD); |
| 158 addShippingAddress.accessibilityTraits |= UIAccessibilityTraitButton; | 149 addShippingAddress.accessibilityTraits |= UIAccessibilityTraitButton; |
| 159 [model addItem:addShippingAddress | 150 [model addItem:addShippingAddress |
| 160 toSectionWithIdentifier:SectionIdentifierShippingAddress]; | 151 toSectionWithIdentifier:SectionIdentifierShippingAddress]; |
| 161 } | 152 } |
| 162 | 153 |
| 163 - (void)viewDidLoad { | 154 - (void)viewDidLoad { |
| 164 [super viewDidLoad]; | 155 [super viewDidLoad]; |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath { | 254 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath { |
| 264 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; | 255 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
| 265 if (type == ItemTypeMessage) { | 256 if (type == ItemTypeMessage) { |
| 266 return YES; | 257 return YES; |
| 267 } else { | 258 } else { |
| 268 return NO; | 259 return NO; |
| 269 } | 260 } |
| 270 } | 261 } |
| 271 | 262 |
| 272 @end | 263 @end |
| OLD | NEW |