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 { | |
| 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. | |
| 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>( | |
| 88 [model itemAtIndexPath:indexPath]); | |
| 89 if (selectedItem.type != ItemTypeMailtoHandlers) { | |
| 90 return; | |
| 91 } | |
| 92 | |
| 93 // Do nothing if the tapped row was already chosen as the default. | |
| 94 if (selectedItem.accessoryType == MDCCollectionViewCellAccessoryCheckmark) { | |
| 95 return; | |
| 96 } | |
| 97 | |
| 98 // Iterate through the rows and remove the checkmark from any that has it. | |
| 99 NSMutableArray* modifiedItems = [NSMutableArray array]; | |
| 100 for (CollectionViewTextItem* item in | |
| 101 [model itemsInSectionWithIdentifier:SectionIdentifierMailtoHandlers]) { | |
| 102 if (item.type != ItemTypeMailtoHandlers) { | |
| 103 continue; | |
| 104 } | |
| 105 if (item.accessoryType == MDCCollectionViewCellAccessoryCheckmark) { | |
| 106 item.accessoryType = MDCCollectionViewCellAccessoryNone; | |
| 107 [modifiedItems addObject:item]; | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 // Show the checkmark on the new default engine. | |
| 112 CollectionViewTextItem* newHandler = | |
|
jif
2017/05/11 14:03:58
we already have the new default engine with |selec
pkl (ping after 24h if needed)
2017/05/11 17:23:16
Integrating into the for-loop on line 100 makes ev
| |
| 113 base::mac::ObjCCastStrict<CollectionViewTextItem>( | |
| 114 [model itemAtIndexPath:indexPath]); | |
| 115 newHandler.accessoryType = MDCCollectionViewCellAccessoryCheckmark; | |
| 116 [modifiedItems addObject:newHandler]; | |
| 117 | |
| 118 NSUInteger handlerIndex = [model indexInItemTypeForIndexPath:indexPath]; | |
| 119 MailtoHandler* handler = | |
| 120 [[_rewriter defaultHandlers] objectAtIndex:handlerIndex]; | |
| 121 [_rewriter setDefaultHandlerID:[handler appStoreID]]; | |
| 122 | |
| 123 [self reconfigureCellsForItems:modifiedItems]; | |
| 124 } | |
| 125 | |
| 126 @end | |
| OLD | NEW |