OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/reading_list/reading_list_toolbar.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #import "ios/chrome/browser/ui/alert_coordinator/action_sheet_coordinator.h" |
| 9 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" |
| 10 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 11 #include "ios/chrome/grit/ios_strings.h" |
| 12 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.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 // Shadow opacity. |
| 22 const CGFloat kShadowOpacity = 0.2f; |
| 23 // Horizontal margin for the content. |
| 24 const CGFloat kHorizontalMargin = 8.0f; |
| 25 |
| 26 } // namespace |
| 27 |
| 28 @interface ReadingListToolbar () |
| 29 |
| 30 // Container for the edit button, preventing it to have the same width as the |
| 31 // stack view. |
| 32 @property(nonatomic, strong) UIView* editButtonContainer; |
| 33 // Button that displays "Delete". |
| 34 @property(nonatomic, strong) UIButton* deleteButton; |
| 35 // Button that displays "Delete All Read". |
| 36 @property(nonatomic, strong) UIButton* deleteAllButton; |
| 37 // Button that displays "Cancel". |
| 38 @property(nonatomic, strong) UIButton* cancelButton; |
| 39 // Button that displays the mark options. |
| 40 @property(nonatomic, strong) UIButton* markButton; |
| 41 // Stack view for arranging the buttons. |
| 42 @property(nonatomic, strong) UIStackView* stackView; |
| 43 |
| 44 // Creates a button with a |title| and a style according to |destructive|. |
| 45 - (UIButton*)buttonWithText:(NSString*)title destructive:(BOOL)isDestructive; |
| 46 // Set the mark button label to |text|. |
| 47 - (void)setMarkButtonText:(NSString*)text; |
| 48 // Updates the button labels to match an empty selection. |
| 49 - (void)updateButtonsForEmptySelection; |
| 50 // Updates the button labels to match a selection containing only read items. |
| 51 - (void)updateButtonsForOnlyReadSelection; |
| 52 // Updates the button labels to match a selection containing only unread items. |
| 53 - (void)updateButtonsForOnlyUnreadSelection; |
| 54 // Updates the button labels to match a selection containing unread and read |
| 55 // items. |
| 56 - (void)updateButtonsForOnlyMixedSelection; |
| 57 // Action for the Edit button. |
| 58 - (void)enterEdit; |
| 59 // Action for the Cancel button. |
| 60 - (void)exitEdit; |
| 61 // Action for the Mark button. |
| 62 - (void)markAction; |
| 63 // Action for the Delete button. |
| 64 - (void)deleteAction; |
| 65 |
| 66 @end |
| 67 |
| 68 @implementation ReadingListToolbar |
| 69 |
| 70 @synthesize editButtonContainer = _editButtonContainer; |
| 71 @synthesize deleteButton = _deleteButton; |
| 72 @synthesize deleteAllButton = _deleteAllButton; |
| 73 @synthesize cancelButton = _cancelButton; |
| 74 @synthesize stackView = _stackView; |
| 75 @synthesize markButton = _markButton; |
| 76 @synthesize state = _state; |
| 77 @synthesize delegate = _delegate; |
| 78 |
| 79 - (instancetype)initWithFrame:(CGRect)frame { |
| 80 self = [super initWithFrame:frame]; |
| 81 if (self) { |
| 82 UIButton* editButton = [self |
| 83 buttonWithText:l10n_util::GetNSString(IDS_IOS_READING_LIST_EDIT_BUTTON) |
| 84 destructive:NO]; |
| 85 |
| 86 _deleteButton = [self buttonWithText:l10n_util::GetNSString( |
| 87 IDS_IOS_READING_LIST_DELETE_BUTTON) |
| 88 destructive:YES]; |
| 89 |
| 90 _deleteAllButton = |
| 91 [self buttonWithText:l10n_util::GetNSString( |
| 92 IDS_IOS_READING_LIST_DELETE_ALL_READ_BUTTON) |
| 93 destructive:YES]; |
| 94 |
| 95 _markButton = [self buttonWithText:l10n_util::GetNSString( |
| 96 IDS_IOS_READING_LIST_MARK_ALL_BUTTON) |
| 97 destructive:NO]; |
| 98 |
| 99 _cancelButton = [self buttonWithText:l10n_util::GetNSString( |
| 100 IDS_IOS_READING_LIST_CANCEL_BUTTON) |
| 101 destructive:NO]; |
| 102 |
| 103 [editButton addTarget:self |
| 104 action:@selector(enterEdit) |
| 105 forControlEvents:UIControlEventTouchUpInside]; |
| 106 |
| 107 [_deleteButton addTarget:self |
| 108 action:@selector(deleteAction) |
| 109 forControlEvents:UIControlEventTouchUpInside]; |
| 110 |
| 111 [_deleteAllButton addTarget:self |
| 112 action:@selector(deleteAction) |
| 113 forControlEvents:UIControlEventTouchUpInside]; |
| 114 |
| 115 [_markButton addTarget:self |
| 116 action:@selector(markAction) |
| 117 forControlEvents:UIControlEventTouchUpInside]; |
| 118 |
| 119 [_cancelButton addTarget:self |
| 120 action:@selector(exitEdit) |
| 121 forControlEvents:UIControlEventTouchUpInside]; |
| 122 |
| 123 _editButtonContainer = [[UIView alloc] initWithFrame:CGRectZero]; |
| 124 [_editButtonContainer addSubview:editButton]; |
| 125 editButton.translatesAutoresizingMaskIntoConstraints = NO; |
| 126 NSDictionary* views = @{ @"button" : editButton }; |
| 127 NSArray* constraints = @[ @"V:|[button]|", @"H:[button]|" ]; |
| 128 ApplyVisualConstraints(constraints, views); |
| 129 |
| 130 _stackView = [[UIStackView alloc] initWithArrangedSubviews:@[ |
| 131 _editButtonContainer, _deleteButton, _deleteAllButton, _markButton, |
| 132 _cancelButton |
| 133 ]]; |
| 134 _stackView.axis = UILayoutConstraintAxisHorizontal; |
| 135 _stackView.alignment = UIStackViewAlignmentFill; |
| 136 _stackView.distribution = UIStackViewDistributionEqualCentering; |
| 137 |
| 138 [self addSubview:_stackView]; |
| 139 _stackView.translatesAutoresizingMaskIntoConstraints = NO; |
| 140 AddSameSizeConstraint(_stackView, self); |
| 141 _stackView.layoutMargins = |
| 142 UIEdgeInsetsMake(0, kHorizontalMargin, 0, kHorizontalMargin); |
| 143 _stackView.layoutMarginsRelativeArrangement = YES; |
| 144 |
| 145 self.backgroundColor = [UIColor whiteColor]; |
| 146 [[self layer] setShadowOpacity:kShadowOpacity]; |
| 147 [self setEditing:NO]; |
| 148 } |
| 149 return self; |
| 150 } |
| 151 |
| 152 #pragma mark Public Methods |
| 153 |
| 154 - (void)setEditing:(BOOL)editing { |
| 155 self.editButtonContainer.hidden = editing; |
| 156 self.deleteButton.hidden = YES; |
| 157 self.deleteAllButton.hidden = !editing; |
| 158 self.cancelButton.hidden = !editing; |
| 159 self.markButton.hidden = !editing; |
| 160 } |
| 161 |
| 162 - (void)setState:(ReadingListToolbarState)state { |
| 163 switch (state) { |
| 164 case NoneSelected: |
| 165 [self updateButtonsForEmptySelection]; |
| 166 break; |
| 167 case OnlyReadSelected: |
| 168 [self updateButtonsForOnlyReadSelection]; |
| 169 break; |
| 170 case OnlyUnreadSelected: |
| 171 [self updateButtonsForOnlyUnreadSelection]; |
| 172 break; |
| 173 case MixedItemsSelected: |
| 174 [self updateButtonsForOnlyMixedSelection]; |
| 175 break; |
| 176 } |
| 177 _state = state; |
| 178 } |
| 179 |
| 180 - (void)setHasReadItem:(BOOL)hasRead { |
| 181 self.deleteAllButton.enabled = hasRead; |
| 182 } |
| 183 |
| 184 - (ActionSheetCoordinator*)actionSheetForMarkWithBaseViewController: |
| 185 (UIViewController*)viewController { |
| 186 return [[ActionSheetCoordinator alloc] |
| 187 initWithBaseViewController:viewController |
| 188 title:nil |
| 189 message:nil |
| 190 rect:self.markButton.bounds |
| 191 view:self.markButton]; |
| 192 } |
| 193 |
| 194 #pragma mark Private Methods |
| 195 |
| 196 - (void)enterEdit { |
| 197 [_delegate enterEditingModePressed]; |
| 198 } |
| 199 |
| 200 - (void)exitEdit { |
| 201 [_delegate exitEditingModePressed]; |
| 202 } |
| 203 |
| 204 - (void)markAction { |
| 205 [_delegate markPressed]; |
| 206 } |
| 207 |
| 208 - (void)deleteAction { |
| 209 [_delegate deletePressed]; |
| 210 } |
| 211 |
| 212 - (void)updateButtonsForEmptySelection { |
| 213 self.deleteAllButton.hidden = NO; |
| 214 self.deleteButton.hidden = YES; |
| 215 [self setMarkButtonText:l10n_util::GetNSStringWithFixup( |
| 216 IDS_IOS_READING_LIST_MARK_ALL_BUTTON)]; |
| 217 } |
| 218 |
| 219 - (void)updateButtonsForOnlyReadSelection { |
| 220 self.deleteAllButton.hidden = YES; |
| 221 self.deleteButton.hidden = NO; |
| 222 [self setMarkButtonText:l10n_util::GetNSStringWithFixup( |
| 223 IDS_IOS_READING_LIST_MARK_UNREAD_BUTTON)]; |
| 224 } |
| 225 |
| 226 - (void)updateButtonsForOnlyUnreadSelection { |
| 227 self.deleteAllButton.hidden = YES; |
| 228 self.deleteButton.hidden = NO; |
| 229 [self setMarkButtonText:l10n_util::GetNSStringWithFixup( |
| 230 IDS_IOS_READING_LIST_MARK_READ_BUTTON)]; |
| 231 } |
| 232 |
| 233 - (void)updateButtonsForOnlyMixedSelection { |
| 234 self.deleteAllButton.hidden = YES; |
| 235 self.deleteButton.hidden = NO; |
| 236 [self setMarkButtonText:l10n_util::GetNSStringWithFixup( |
| 237 IDS_IOS_READING_LIST_MARK_BUTTON)]; |
| 238 } |
| 239 |
| 240 - (UIButton*)buttonWithText:(NSString*)title destructive:(BOOL)isDestructive { |
| 241 UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 242 button.contentEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 8); |
| 243 [button setTitle:title forState:UIControlStateNormal]; |
| 244 |
| 245 button.backgroundColor = [UIColor whiteColor]; |
| 246 UIColor* textColor = isDestructive ? [[MDCPalette cr_redPalette] tint500] |
| 247 : [[MDCPalette cr_bluePalette] tint500]; |
| 248 [button setTitleColor:textColor forState:UIControlStateNormal]; |
| 249 [button setTitleColor:[UIColor lightGrayColor] |
| 250 forState:UIControlStateDisabled]; |
| 251 [[button titleLabel] |
| 252 setFont:[[MDCTypography fontLoader] regularFontOfSize:14]]; |
| 253 |
| 254 return button; |
| 255 } |
| 256 |
| 257 - (void)setMarkButtonText:(NSString*)text { |
| 258 [self.markButton setTitle:text forState:UIControlStateNormal]; |
| 259 } |
| 260 |
| 261 @end |
OLD | NEW |