| 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 // 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 |
| 99 [model itemsInSectionWithIdentifier:SectionIdentifierMailtoHandlers]) { |
| 100 if (item.type != ItemTypeMailtoHandlers) |
| 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 |