OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "ios/chrome/browser/ui/settings/compose_email_handler_collection_view_co
ntroller.h" | 5 #import "ios/chrome/browser/ui/settings/compose_email_handler_collection_view_co
ntroller.h" |
6 | 6 |
7 #include "base/mac/foundation_util.h" | 7 #include "base/mac/foundation_util.h" |
8 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h
" | 8 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h
" |
9 #import "ios/chrome/browser/web/mailto_handler.h" | 9 #import "ios/chrome/browser/web/mailto_handler.h" |
10 #import "ios/chrome/browser/web/mailto_url_rewriter.h" | 10 #import "ios/chrome/browser/web/mailto_url_rewriter.h" |
11 #include "ios/chrome/grit/ios_strings.h" | 11 #include "ios/chrome/grit/ios_strings.h" |
| 12 #import "ios/third_party/material_components_ios/src/components/Palettes/src/MDC
Palettes.h" |
12 #include "ui/base/l10n/l10n_util.h" | 13 #include "ui/base/l10n/l10n_util.h" |
13 #include "ui/base/l10n/l10n_util_mac.h" | 14 #include "ui/base/l10n/l10n_util_mac.h" |
14 | 15 |
15 #if !defined(__has_feature) || !__has_feature(objc_arc) | 16 #if !defined(__has_feature) || !__has_feature(objc_arc) |
16 #error "This file requires ARC support." | 17 #error "This file requires ARC support." |
17 #endif | 18 #endif |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 typedef NS_ENUM(NSInteger, SectionIdentifier) { | 22 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
22 SectionIdentifierMailtoHandlers = kSectionIdentifierEnumZero, | 23 SectionIdentifierMailtoHandlers = kSectionIdentifierEnumZero, |
23 }; | 24 }; |
24 | 25 |
25 typedef NS_ENUM(NSInteger, ItemType) { | 26 typedef NS_ENUM(NSInteger, ItemType) { |
26 ItemTypeMailtoHandlers = kItemTypeEnumZero, | 27 ItemTypeMailtoHandlers = kItemTypeEnumZero, |
27 }; | 28 }; |
28 | 29 |
29 } // namespace | 30 } // namespace |
30 | 31 |
31 @interface ComposeEmailHandlerCollectionViewController () { | 32 @interface ComposeEmailHandlerCollectionViewController () { |
32 MailtoURLRewriter* _rewriter; | 33 MailtoURLRewriter* _rewriter; |
33 } | 34 } |
| 35 |
| 36 // Returns the MailtoHandler at |indexPath|. |
| 37 - (MailtoHandler*)handlerAtIndexPath:(NSIndexPath*)indexPath; |
34 @end | 38 @end |
35 | 39 |
36 @implementation ComposeEmailHandlerCollectionViewController | 40 @implementation ComposeEmailHandlerCollectionViewController |
37 | 41 |
38 - (instancetype)initWithRewriter:(MailtoURLRewriter*)rewriter { | 42 - (instancetype)initWithRewriter:(MailtoURLRewriter*)rewriter { |
39 self = [super initWithStyle:CollectionViewControllerStyleAppBar]; | 43 self = [super initWithStyle:CollectionViewControllerStyleAppBar]; |
40 if (self) { | 44 if (self) { |
41 self.title = l10n_util::GetNSString(IDS_IOS_COMPOSE_EMAIL_SETTING); | 45 self.title = l10n_util::GetNSString(IDS_IOS_COMPOSE_EMAIL_SETTING); |
42 self.collectionViewAccessibilityIdentifier = | 46 self.collectionViewAccessibilityIdentifier = |
43 @"compose_email_handler_view_controller"; | 47 @"compose_email_handler_view_controller"; |
44 _rewriter = rewriter; | 48 _rewriter = rewriter; |
45 [self loadModel]; | 49 [self loadModel]; |
46 } | 50 } |
47 return self; | 51 return self; |
48 } | 52 } |
49 | 53 |
50 - (void)loadModel { | 54 - (void)loadModel { |
51 [super loadModel]; | 55 [super loadModel]; |
52 CollectionViewModel* model = self.collectionViewModel; | 56 CollectionViewModel* model = self.collectionViewModel; |
53 [model addSectionWithIdentifier:SectionIdentifierMailtoHandlers]; | 57 [model addSectionWithIdentifier:SectionIdentifierMailtoHandlers]; |
54 | 58 |
55 // Since this is a one-of-several selection UI, there must be more than one | 59 // 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 | 60 // choice available to the user. If this UI is being presented when there is |
57 // only one choice, it is considered a software error. | 61 // only one choice, it is considered a software error. |
58 NSArray<MailtoHandler*>* handlers = [_rewriter defaultHandlers]; | 62 NSArray<MailtoHandler*>* handlers = [_rewriter defaultHandlers]; |
59 DCHECK([handlers count] > 1); | |
60 NSString* currentHandlerID = [_rewriter defaultHandlerID]; | 63 NSString* currentHandlerID = [_rewriter defaultHandlerID]; |
61 for (MailtoHandler* handler in handlers) { | 64 for (MailtoHandler* handler in handlers) { |
62 CollectionViewTextItem* item = | 65 CollectionViewTextItem* item = |
63 [[CollectionViewTextItem alloc] initWithType:ItemTypeMailtoHandlers]; | 66 [[CollectionViewTextItem alloc] initWithType:ItemTypeMailtoHandlers]; |
64 [item setText:[handler appName]]; | 67 [item setText:[handler appName]]; |
65 [item setAccessibilityTraits:UIAccessibilityTraitButton]; | 68 if ([handler isAvailable]) { |
| 69 [item setTextColor:[[MDCPalette greyPalette] tint900]]; |
| 70 [item setAccessibilityTraits:UIAccessibilityTraitButton]; |
| 71 } else { |
| 72 [item setTextColor:[[MDCPalette greyPalette] tint500]]; |
| 73 [item setAccessibilityTraits:UIAccessibilityTraitNotEnabled]; |
| 74 } |
66 if ([currentHandlerID isEqualToString:[handler appStoreID]]) | 75 if ([currentHandlerID isEqualToString:[handler appStoreID]]) |
67 [item setAccessoryType:MDCCollectionViewCellAccessoryCheckmark]; | 76 [item setAccessoryType:MDCCollectionViewCellAccessoryCheckmark]; |
68 [model addItem:item | 77 [model addItem:item |
69 toSectionWithIdentifier:SectionIdentifierMailtoHandlers]; | 78 toSectionWithIdentifier:SectionIdentifierMailtoHandlers]; |
70 } | 79 } |
71 } | 80 } |
72 | 81 |
73 #pragma mark UICollectionViewDelegate | 82 #pragma mark - UICollectionViewDelegate |
| 83 |
| 84 - (BOOL)collectionView:(UICollectionView*)collectionView |
| 85 shouldSelectItemAtIndexPath:(NSIndexPath*)indexPath { |
| 86 // Disallow selection if the handler for the tapped row is not available. |
| 87 return [[self handlerAtIndexPath:indexPath] isAvailable] && |
| 88 [super collectionView:collectionView |
| 89 shouldSelectItemAtIndexPath:indexPath]; |
| 90 } |
| 91 |
| 92 - (BOOL)collectionView:(UICollectionView*)collectionView |
| 93 shouldHighlightItemAtIndexPath:(NSIndexPath*)indexPath { |
| 94 // Disallow highlight (ripple effect) if the handler for the tapped row is not |
| 95 // available. |
| 96 return [[self handlerAtIndexPath:indexPath] isAvailable] && |
| 97 [super collectionView:collectionView |
| 98 shouldHighlightItemAtIndexPath:indexPath]; |
| 99 } |
74 | 100 |
75 - (void)collectionView:(UICollectionView*)collectionView | 101 - (void)collectionView:(UICollectionView*)collectionView |
76 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { | 102 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { |
77 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; | 103 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; |
78 CollectionViewModel* model = self.collectionViewModel; | 104 CollectionViewModel* model = self.collectionViewModel; |
79 | 105 |
80 // The items created in -loadModel are all MailtoHandlers type. | 106 // The items created in -loadModel are all MailtoHandlers type. |
81 CollectionViewTextItem* selectedItem = | 107 CollectionViewTextItem* selectedItem = |
82 base::mac::ObjCCastStrict<CollectionViewTextItem>( | 108 base::mac::ObjCCastStrict<CollectionViewTextItem>( |
83 [model itemAtIndexPath:indexPath]); | 109 [model itemAtIndexPath:indexPath]); |
(...skipping 15 matching lines...) Expand all Loading... |
99 textItem.accessoryType = MDCCollectionViewCellAccessoryCheckmark; | 125 textItem.accessoryType = MDCCollectionViewCellAccessoryCheckmark; |
100 [modifiedItems addObject:textItem]; | 126 [modifiedItems addObject:textItem]; |
101 } else if (textItem.accessoryType == | 127 } else if (textItem.accessoryType == |
102 MDCCollectionViewCellAccessoryCheckmark) { | 128 MDCCollectionViewCellAccessoryCheckmark) { |
103 // Unchecks any currently checked selection. | 129 // Unchecks any currently checked selection. |
104 textItem.accessoryType = MDCCollectionViewCellAccessoryNone; | 130 textItem.accessoryType = MDCCollectionViewCellAccessoryNone; |
105 [modifiedItems addObject:textItem]; | 131 [modifiedItems addObject:textItem]; |
106 } | 132 } |
107 } | 133 } |
108 | 134 |
109 NSUInteger handlerIndex = [model indexInItemTypeForIndexPath:indexPath]; | 135 // Sets the Mail client app that will handle mailto:// URLs. |
110 MailtoHandler* handler = | 136 MailtoHandler* handler = [self handlerAtIndexPath:indexPath]; |
111 [[_rewriter defaultHandlers] objectAtIndex:handlerIndex]; | 137 DCHECK([handler isAvailable]); |
112 [_rewriter setDefaultHandlerID:[handler appStoreID]]; | 138 [_rewriter setDefaultHandlerID:[handler appStoreID]]; |
113 | 139 |
114 [self reconfigureCellsForItems:modifiedItems]; | 140 [self reconfigureCellsForItems:modifiedItems]; |
115 } | 141 } |
116 | 142 |
| 143 #pragma mark - Private |
| 144 |
| 145 - (MailtoHandler*)handlerAtIndexPath:(NSIndexPath*)indexPath { |
| 146 CollectionViewModel* model = self.collectionViewModel; |
| 147 NSUInteger handlerIndex = [model indexInItemTypeForIndexPath:indexPath]; |
| 148 return [[_rewriter defaultHandlers] objectAtIndex:handlerIndex]; |
| 149 } |
| 150 |
117 @end | 151 @end |
OLD | NEW |