Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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/ui/settings/compose_email_handler_collection_view_co ntroller.h" | |
| 6 | |
| 7 #include "base/mac/foundation_util.h" | |
| 8 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h " | |
| 9 #import "ios/chrome/browser/web/mailto_handler.h" | |
| 10 #import "ios/chrome/browser/web/mailto_url_rewriter.h" | |
| 11 #include "ios/chrome/grit/ios_strings.h" | |
| 12 #include "ui/base/l10n/l10n_util.h" | |
| 13 #include "ui/base/l10n/l10n_util_mac.h" | |
| 14 | |
| 15 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 16 #error "This file requires ARC support." | |
| 17 #endif | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 typedef NS_ENUM(NSInteger, SectionIdentifier) { | |
| 22 SectionIdentifierMailtoHandlers = kSectionIdentifierEnumZero, | |
| 23 }; | |
| 24 | |
| 25 typedef NS_ENUM(NSInteger, ItemType) { | |
| 26 ItemTypeMailtoHandlers = kItemTypeEnumZero, | |
| 27 }; | |
| 28 | |
| 29 } // namespace | |
| 30 | |
| 31 @interface ComposeEmailHandlerCollectionViewController () { | |
| 32 MailtoURLRewriter* _rewriter; | |
| 33 } | |
| 34 @end | |
| 35 | |
| 36 @implementation ComposeEmailHandlerCollectionViewController | |
| 37 | |
| 38 - (instancetype)initWithRewriter:(MailtoURLRewriter*)rewriter { | |
| 39 self = [super initWithStyle:CollectionViewControllerStyleAppBar]; | |
| 40 if (self) { | |
| 41 self.title = l10n_util::GetNSString(IDS_IOS_COMPOSE_EMAIL_SETTING); | |
| 42 self.collectionViewAccessibilityIdentifier = | |
| 43 @"compose_email_handler_view_controller"; | |
| 44 _rewriter = rewriter; | |
| 45 [self loadModel]; | |
| 46 } | |
| 47 return self; | |
| 48 } | |
| 49 | |
| 50 - (instancetype)initWithStyle:(CollectionViewControllerStyle)style { | |
|
rohitrao (ping after 24h)
2017/05/12 01:57:57
This implementation should not be necessary if you
pkl (ping after 24h if needed)
2017/05/12 18:01:19
Done.
| |
| 51 NOTREACHED(); | |
| 52 return nil; | |
| 53 } | |
| 54 | |
| 55 - (void)loadModel { | |
| 56 [super loadModel]; | |
| 57 CollectionViewModel* model = self.collectionViewModel; | |
| 58 [model addSectionWithIdentifier:SectionIdentifierMailtoHandlers]; | |
| 59 | |
| 60 // Since this is a one-of-several selection UI, there must be more than one | |
| 61 // choice available to the user. If this UI is being presented when there is | |
| 62 // only one choice, it is considered a software error. | |
|
rohitrao (ping after 24h)
2017/05/12 01:57:57
Why not always allow this UI to be shown, even if
pkl (ping after 24h if needed)
2017/05/12 18:01:19
I've started a comment thread on the design doc. I
| |
| 63 NSArray<MailtoHandler*>* handlers = [_rewriter defaultHandlers]; | |
| 64 DCHECK([handlers count] > 1); | |
| 65 NSString* currentHandlerID = [_rewriter defaultHandlerID]; | |
| 66 for (MailtoHandler* handler in handlers) { | |
| 67 CollectionViewTextItem* item = | |
| 68 [[CollectionViewTextItem alloc] initWithType:ItemTypeMailtoHandlers]; | |
| 69 [item setText:[handler appName]]; | |
| 70 [item setAccessibilityTraits:UIAccessibilityTraitButton]; | |
| 71 if ([currentHandlerID isEqualToString:[handler appStoreID]]) | |
| 72 [item setAccessoryType:MDCCollectionViewCellAccessoryCheckmark]; | |
| 73 [model addItem:item | |
| 74 toSectionWithIdentifier:SectionIdentifierMailtoHandlers]; | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 #pragma mark UICollectionViewDelegate | |
| 79 | |
| 80 - (void)collectionView:(UICollectionView*)collectionView | |
| 81 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { | |
| 82 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; | |
| 83 CollectionViewModel* model = self.collectionViewModel; | |
| 84 | |
| 85 // Only handle taps on Mailto Handler items. | |
| 86 CollectionViewTextItem* selectedItem = | |
| 87 base::mac::ObjCCastStrict<CollectionViewTextItem>( | |
|
rohitrao (ping after 24h)
2017/05/12 01:57:57
ObjCCastStrict will DCHECK if the item is of a dif
pkl (ping after 24h if needed)
2017/05/12 18:01:20
Using "strict" is right.
| |
| 88 [model itemAtIndexPath:indexPath]); | |
| 89 if (selectedItem.type != ItemTypeMailtoHandlers) | |
|
rohitrao (ping after 24h)
2017/05/12 01:57:57
Do we display any rows that are not this type? Co
pkl (ping after 24h if needed)
2017/05/12 18:01:19
Since the items are created in loadModel, the chec
| |
| 90 return; | |
| 91 | |
| 92 // Do nothing if the tapped row was already chosen as the default. | |
| 93 if (selectedItem.accessoryType == MDCCollectionViewCellAccessoryCheckmark) | |
| 94 return; | |
| 95 | |
| 96 // Iterate through the rows and remove the checkmark from any that has it. | |
| 97 NSMutableArray* modifiedItems = [NSMutableArray array]; | |
| 98 for (CollectionViewTextItem* item in | |
|
rohitrao (ping after 24h)
2017/05/12 01:57:57
Are we somehow silently casting to CollectionViewT
pkl (ping after 24h if needed)
2017/05/12 18:01:19
Switched to use ObjCCastStrict<>.
| |
| 99 [model itemsInSectionWithIdentifier:SectionIdentifierMailtoHandlers]) { | |
| 100 if (item.type != ItemTypeMailtoHandlers) | |
|
rohitrao (ping after 24h)
2017/05/12 01:57:57
Same question -- could this be a DCHECK, or do we
pkl (ping after 24h if needed)
2017/05/12 18:01:19
...and made this a DCHECK_EQ
| |
| 101 continue; | |
| 102 if (item == selectedItem) { | |
| 103 // Shows the checkmark on the new default mailto: URL handler. | |
| 104 item.accessoryType = MDCCollectionViewCellAccessoryCheckmark; | |
| 105 [modifiedItems addObject:item]; | |
| 106 } else if (item.accessoryType == MDCCollectionViewCellAccessoryCheckmark) { | |
| 107 // Unchecks any currently checked selection. | |
| 108 item.accessoryType = MDCCollectionViewCellAccessoryNone; | |
| 109 [modifiedItems addObject:item]; | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 NSUInteger handlerIndex = [model indexInItemTypeForIndexPath:indexPath]; | |
| 114 MailtoHandler* handler = | |
| 115 [[_rewriter defaultHandlers] objectAtIndex:handlerIndex]; | |
| 116 [_rewriter setDefaultHandlerID:[handler appStoreID]]; | |
| 117 | |
| 118 [self reconfigureCellsForItems:modifiedItems]; | |
| 119 } | |
| 120 | |
| 121 @end | |
| OLD | NEW |