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/shipping_option_selection_view_controller.h " | |
6 | |
7 #import "base/ios/weak_nsobject.h" | |
8 #include "base/mac/foundation_util.h" | |
9 #include "base/strings/sys_string_conversions.h" | |
10 #include "components/strings/grit/components_strings.h" | |
11 #import "ios/chrome/browser/payments/payment_request_utils.h" | |
12 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" | |
13 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h " | |
14 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" | |
15 #import "ios/chrome/browser/ui/icons/chrome_icon.h" | |
16 #include "ios/chrome/grit/ios_strings.h" | |
17 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat erialPalettes.h" | |
18 #import "ios/third_party/material_components_ios/src/components/Typography/src/M aterialTypography.h" | |
19 #include "ui/base/l10n/l10n_util.h" | |
20 | |
21 NSString* const kShippingOptionSelectionCollectionViewId = | |
22 @"kShippingOptionSelectionCollectionViewId"; | |
23 | |
24 namespace { | |
25 | |
26 const CGFloat kSeparatorEdgeInset = 14; | |
27 | |
28 typedef NS_ENUM(NSInteger, SectionIdentifier) { | |
29 SectionIdentifierShippingOption = kSectionIdentifierEnumZero, | |
30 }; | |
31 | |
32 typedef NS_ENUM(NSInteger, ItemType) { | |
33 ItemTypeShippingOption = kItemTypeEnumZero, // This is a repeated item type. | |
34 }; | |
35 | |
36 } // namespace | |
37 | |
38 @interface ShippingOptionSelectionViewController () { | |
39 base::WeakNSProtocol<id<ShippingOptionSelectionViewControllerDelegate>> | |
40 _delegate; | |
41 | |
42 CollectionViewTextItem* _selectedItem; | |
43 } | |
44 | |
45 // Called when the user presses the return button. | |
46 - (void)onReturn; | |
47 | |
48 @end | |
49 | |
50 @implementation ShippingOptionSelectionViewController | |
51 | |
52 @synthesize shippingOptions = _shippingOptions; | |
53 @synthesize selectedShippingOption = _selectedShippingOption; | |
54 | |
55 - (instancetype)init { | |
56 if ((self = [super initWithStyle:CollectionViewControllerStyleAppBar])) { | |
57 self.title = l10n_util::GetNSString( | |
58 IDS_IOS_PAYMENT_REQUEST_SHIPPING_OPTION_SELECTION_TITLE); | |
59 | |
60 UIBarButtonItem* returnButton = | |
61 [ChromeIcon templateBarButtonItemWithImage:[ChromeIcon backIcon] | |
62 target:nil | |
63 action:@selector(onReturn)]; | |
64 returnButton.accessibilityLabel = l10n_util::GetNSString(IDS_ACCNAME_BACK); | |
65 self.navigationItem.leftBarButtonItem = returnButton; | |
66 } | |
67 return self; | |
68 } | |
69 | |
70 - (id<ShippingOptionSelectionViewControllerDelegate>)delegate { | |
71 return _delegate.get(); | |
72 } | |
73 | |
74 - (void)setDelegate: | |
75 (id<ShippingOptionSelectionViewControllerDelegate>)delegate { | |
76 _delegate.reset(delegate); | |
77 } | |
78 | |
79 - (void)onReturn { | |
80 [_delegate shippingOptionSelectionViewControllerDidReturn:self]; | |
81 } | |
82 | |
83 #pragma mark - CollectionViewController methods | |
84 | |
85 - (void)loadModel { | |
86 [super loadModel]; | |
87 CollectionViewModel* model = self.collectionViewModel; | |
88 | |
89 [model addSectionWithIdentifier:SectionIdentifierShippingOption]; | |
90 | |
91 for (size_t i = 0; i < _shippingOptions.size(); ++i) { | |
92 web::PaymentShippingOption* shippingOption = _shippingOptions[i]; | |
93 CollectionViewTextItem* item = [[[CollectionViewTextItem alloc] | |
94 initWithType:ItemTypeShippingOption] autorelease]; | |
95 item.text = base::SysUTF16ToNSString(shippingOption->label); | |
96 NSString* currencyCode = | |
97 base::SysUTF16ToNSString(shippingOption->amount.currency); | |
98 NSDecimalNumber* value = [NSDecimalNumber | |
99 decimalNumberWithString:SysUTF16ToNSString( | |
100 shippingOption->amount.value)]; | |
101 item.detailText = | |
102 payment_request_utils::FormattedCurrencyString(value, currencyCode); | |
103 | |
104 if (_selectedShippingOption == shippingOption) { | |
105 item.accessoryType = MDCCollectionViewCellAccessoryCheckmark; | |
106 _selectedItem = item; | |
107 } | |
108 | |
109 [model addItem:item | |
110 toSectionWithIdentifier:SectionIdentifierShippingOption]; | |
111 } | |
112 } | |
113 | |
114 - (void)viewDidLoad { | |
115 [super viewDidLoad]; | |
116 self.collectionView.accessibilityIdentifier = | |
117 kShippingOptionSelectionCollectionViewId; | |
118 | |
119 // Customize collection view settings. | |
120 self.styler.cellStyle = MDCCollectionViewCellStyleCard; | |
121 self.styler.separatorInset = | |
122 UIEdgeInsetsMake(0, kSeparatorEdgeInset, 0, kSeparatorEdgeInset); | |
123 } | |
124 | |
125 #pragma mark UICollectionViewDataSource | |
lpromero
2017/01/11 12:51:15
Add new line after.
Moe
2017/01/12 00:06:19
Done.
| |
126 - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView | |
127 cellForItemAtIndexPath:(nonnull NSIndexPath*)indexPath { | |
128 UICollectionViewCell* cell = | |
129 [super collectionView:collectionView cellForItemAtIndexPath:indexPath]; | |
130 | |
131 NSInteger itemType = | |
132 [self.collectionViewModel itemTypeForIndexPath:indexPath]; | |
133 DCHECK(ItemTypeShippingOption == itemType); | |
134 | |
135 MDCCollectionViewTextCell* textCell = | |
136 base::mac::ObjCCastStrict<MDCCollectionViewTextCell>(cell); | |
137 textCell.textLabel.font = [MDCTypography body2Font]; | |
138 textCell.textLabel.textColor = [[MDCPalette greyPalette] tint900]; | |
139 textCell.detailTextLabel.font = [MDCTypography body1Font]; | |
140 textCell.detailTextLabel.textColor = [[MDCPalette greyPalette] tint900]; | |
141 | |
142 return cell; | |
143 } | |
144 | |
145 #pragma mark UICollectionViewDelegate | |
146 | |
147 - (void)collectionView:(UICollectionView*)collectionView | |
148 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { | |
149 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; | |
150 | |
151 CollectionViewModel* model = self.collectionViewModel; | |
152 | |
153 NSInteger itemType = | |
154 [self.collectionViewModel itemTypeForIndexPath:indexPath]; | |
155 DCHECK(ItemTypeShippingOption == itemType); | |
156 | |
157 NSIndexPath* currentlySeletedIndexPath = [self.collectionViewModel | |
158 indexPathForItem:_selectedItem | |
159 inSectionWithIdentifier:SectionIdentifierShippingOption]; | |
160 if (currentlySeletedIndexPath != indexPath) { | |
161 // Update the cells. | |
162 CollectionViewItem* item = [model itemAtIndexPath:indexPath]; | |
163 CollectionViewTextItem* newlySelectedItem = | |
164 base::mac::ObjCCastStrict<CollectionViewTextItem>(item); | |
165 newlySelectedItem.accessoryType = MDCCollectionViewCellAccessoryCheckmark; | |
166 | |
167 _selectedItem.accessoryType = MDCCollectionViewCellAccessoryNone; | |
168 | |
169 [self reconfigureCellsForItems:@[ _selectedItem, newlySelectedItem ] | |
170 inSectionWithIdentifier:SectionIdentifierShippingOption]; | |
171 | |
172 // Update the selected shipping option and its respective item. | |
173 NSInteger index = [model indexInItemTypeForIndexPath:indexPath]; | |
174 DCHECK(index < (NSInteger)_shippingOptions.size()); | |
175 self.selectedShippingOption = _shippingOptions[index]; | |
176 _selectedItem = newlySelectedItem; | |
177 } | |
178 [_delegate shippingOptionSelectionViewController:self | |
179 selectedShippingOption:self.selectedShippingOption]; | |
180 } | |
181 | |
182 #pragma mark MDCCollectionViewStylingDelegate | |
183 | |
184 - (CGFloat)collectionView:(UICollectionView*)collectionView | |
185 cellHeightAtIndexPath:(NSIndexPath*)indexPath { | |
186 return MDCCellDefaultTwoLineHeight; | |
187 } | |
188 | |
189 @end | |
OLD | NEW |