| 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 - (void)loadModel { |
| 51 [super loadModel]; |
| 52 CollectionViewModel* model = self.collectionViewModel; |
| 53 [model addSectionWithIdentifier:SectionIdentifierMailtoHandlers]; |
| 54 |
| 55 // Since this is a one-of-several selection UI, there must be more than one |
| 56 // choice available to the user. If this UI is being presented when there is |
| 57 // only one choice, it is considered a software error. |
| 58 NSArray<MailtoHandler*>* handlers = [_rewriter defaultHandlers]; |
| 59 DCHECK([handlers count] > 1); |
| 60 NSString* currentHandlerID = [_rewriter defaultHandlerID]; |
| 61 for (MailtoHandler* handler in handlers) { |
| 62 CollectionViewTextItem* item = |
| 63 [[CollectionViewTextItem alloc] initWithType:ItemTypeMailtoHandlers]; |
| 64 [item setText:[handler appName]]; |
| 65 [item setAccessibilityTraits:UIAccessibilityTraitButton]; |
| 66 if ([currentHandlerID isEqualToString:[handler appStoreID]]) |
| 67 [item setAccessoryType:MDCCollectionViewCellAccessoryCheckmark]; |
| 68 [model addItem:item |
| 69 toSectionWithIdentifier:SectionIdentifierMailtoHandlers]; |
| 70 } |
| 71 } |
| 72 |
| 73 #pragma mark UICollectionViewDelegate |
| 74 |
| 75 - (void)collectionView:(UICollectionView*)collectionView |
| 76 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { |
| 77 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; |
| 78 CollectionViewModel* model = self.collectionViewModel; |
| 79 |
| 80 // The items created in -loadModel are all MailtoHandlers type. |
| 81 CollectionViewTextItem* selectedItem = |
| 82 base::mac::ObjCCastStrict<CollectionViewTextItem>( |
| 83 [model itemAtIndexPath:indexPath]); |
| 84 DCHECK_EQ(ItemTypeMailtoHandlers, selectedItem.type); |
| 85 |
| 86 // Do nothing if the tapped row is already chosen as the default. |
| 87 if (selectedItem.accessoryType == MDCCollectionViewCellAccessoryCheckmark) |
| 88 return; |
| 89 |
| 90 // Iterate through the rows and remove the checkmark from any that has it. |
| 91 NSMutableArray* modifiedItems = [NSMutableArray array]; |
| 92 for (id item in |
| 93 [model itemsInSectionWithIdentifier:SectionIdentifierMailtoHandlers]) { |
| 94 CollectionViewTextItem* textItem = |
| 95 base::mac::ObjCCastStrict<CollectionViewTextItem>(item); |
| 96 DCHECK_EQ(ItemTypeMailtoHandlers, textItem.type); |
| 97 if (textItem == selectedItem) { |
| 98 // Shows the checkmark on the new default mailto: URL handler. |
| 99 textItem.accessoryType = MDCCollectionViewCellAccessoryCheckmark; |
| 100 [modifiedItems addObject:textItem]; |
| 101 } else if (textItem.accessoryType == |
| 102 MDCCollectionViewCellAccessoryCheckmark) { |
| 103 // Unchecks any currently checked selection. |
| 104 textItem.accessoryType = MDCCollectionViewCellAccessoryNone; |
| 105 [modifiedItems addObject:textItem]; |
| 106 } |
| 107 } |
| 108 |
| 109 NSUInteger handlerIndex = [model indexInItemTypeForIndexPath:indexPath]; |
| 110 MailtoHandler* handler = |
| 111 [[_rewriter defaultHandlers] objectAtIndex:handlerIndex]; |
| 112 [_rewriter setDefaultHandlerID:[handler appStoreID]]; |
| 113 |
| 114 [self reconfigureCellsForItems:modifiedItems]; |
| 115 } |
| 116 |
| 117 @end |
| OLD | NEW |