Chromium Code Reviews| Index: ios/chrome/browser/ui/settings/compose_email_handler_collection_view_controller.mm |
| diff --git a/ios/chrome/browser/ui/settings/compose_email_handler_collection_view_controller.mm b/ios/chrome/browser/ui/settings/compose_email_handler_collection_view_controller.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3fc3b1ea0e6211055058c2cfe8219c91d417cecb |
| --- /dev/null |
| +++ b/ios/chrome/browser/ui/settings/compose_email_handler_collection_view_controller.mm |
| @@ -0,0 +1,126 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import "ios/chrome/browser/ui/settings/compose_email_handler_collection_view_controller.h" |
| + |
| +#include "base/mac/foundation_util.h" |
| +#import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h" |
| +#import "ios/chrome/browser/web/mailto_handler.h" |
| +#import "ios/chrome/browser/web/mailto_url_rewriter.h" |
| +#include "ios/chrome/grit/ios_strings.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "ui/base/l10n/l10n_util_mac.h" |
| + |
| +#if !defined(__has_feature) || !__has_feature(objc_arc) |
| +#error "This file requires ARC support." |
| +#endif |
| + |
| +namespace { |
| + |
| +typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| + SectionIdentifierMailtoHandlers = kSectionIdentifierEnumZero, |
| +}; |
| + |
| +typedef NS_ENUM(NSInteger, ItemType) { |
| + ItemTypeMailtoHandlers = kItemTypeEnumZero, |
| +}; |
| + |
| +} // namespace |
| + |
| +@interface ComposeEmailHandlerCollectionViewController () { |
| + MailtoURLRewriter* _rewriter; |
| +} |
| +@end |
| + |
| +@implementation ComposeEmailHandlerCollectionViewController |
| + |
| +- (instancetype)initWithRewriter:(MailtoURLRewriter*)rewriter { |
| + self = [super initWithStyle:CollectionViewControllerStyleAppBar]; |
| + if (self) { |
| + self.title = l10n_util::GetNSString(IDS_IOS_COMPOSE_EMAIL_SETTING); |
| + self.collectionViewAccessibilityIdentifier = |
| + @"compose_email_handler_view_controller"; |
| + _rewriter = rewriter; |
| + [self loadModel]; |
| + } |
| + return self; |
| +} |
| + |
| +- (instancetype)initWithStyle:(CollectionViewControllerStyle)style { |
| + NOTREACHED(); |
| + return nil; |
| +} |
| + |
| +- (void)loadModel { |
| + [super loadModel]; |
| + CollectionViewModel* model = self.collectionViewModel; |
| + [model addSectionWithIdentifier:SectionIdentifierMailtoHandlers]; |
| + |
| + // Since this is a one-of-several selection UI, there must be more than one |
| + // choice available to the user. If this UI is being presented when there is |
| + // only one choice, it is considered a software error. |
| + NSArray<MailtoHandler*>* handlers = [_rewriter defaultHandlers]; |
| + DCHECK([handlers count] > 1); |
| + NSString* currentHandlerID = [_rewriter defaultHandlerID]; |
| + for (MailtoHandler* handler in handlers) { |
| + CollectionViewTextItem* item = |
| + [[CollectionViewTextItem alloc] initWithType:ItemTypeMailtoHandlers]; |
| + [item setText:[handler appName]]; |
| + [item setAccessibilityTraits:UIAccessibilityTraitButton]; |
| + if ([currentHandlerID isEqualToString:[handler appStoreID]]) |
| + [item setAccessoryType:MDCCollectionViewCellAccessoryCheckmark]; |
| + [model addItem:item |
| + toSectionWithIdentifier:SectionIdentifierMailtoHandlers]; |
| + } |
| +} |
| + |
| +#pragma mark UICollectionViewDelegate |
| + |
| +- (void)collectionView:(UICollectionView*)collectionView |
| + didSelectItemAtIndexPath:(NSIndexPath*)indexPath { |
| + [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; |
| + CollectionViewModel* model = self.collectionViewModel; |
| + |
| + // Only handle taps on Mailto Handler items. |
| + CollectionViewTextItem* selectedItem = |
| + base::mac::ObjCCastStrict<CollectionViewTextItem>( |
| + [model itemAtIndexPath:indexPath]); |
| + if (selectedItem.type != ItemTypeMailtoHandlers) { |
| + return; |
| + } |
| + |
| + // Do nothing if the tapped row was already chosen as the default. |
| + if (selectedItem.accessoryType == MDCCollectionViewCellAccessoryCheckmark) { |
| + return; |
| + } |
| + |
| + // Iterate through the rows and remove the checkmark from any that has it. |
| + NSMutableArray* modifiedItems = [NSMutableArray array]; |
| + for (CollectionViewTextItem* item in |
| + [model itemsInSectionWithIdentifier:SectionIdentifierMailtoHandlers]) { |
| + if (item.type != ItemTypeMailtoHandlers) { |
| + continue; |
| + } |
| + if (item.accessoryType == MDCCollectionViewCellAccessoryCheckmark) { |
| + item.accessoryType = MDCCollectionViewCellAccessoryNone; |
| + [modifiedItems addObject:item]; |
| + } |
| + } |
| + |
| + // Show the checkmark on the new default engine. |
| + 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
|
| + base::mac::ObjCCastStrict<CollectionViewTextItem>( |
| + [model itemAtIndexPath:indexPath]); |
| + newHandler.accessoryType = MDCCollectionViewCellAccessoryCheckmark; |
| + [modifiedItems addObject:newHandler]; |
| + |
| + NSUInteger handlerIndex = [model indexInItemTypeForIndexPath:indexPath]; |
| + MailtoHandler* handler = |
| + [[_rewriter defaultHandlers] objectAtIndex:handlerIndex]; |
| + [_rewriter setDefaultHandlerID:[handler appStoreID]]; |
| + |
| + [self reconfigureCellsForItems:modifiedItems]; |
| +} |
| + |
| +@end |