Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/elements/selector_picker_view_controller.h" | 5 #import "ios/chrome/browser/ui/elements/selector_picker_view_controller.h" |
| 6 | 6 |
| 7 #import "ios/chrome/browser/ui/elements/selector_view_controller_delegate.h" | 7 #import "ios/chrome/browser/ui/elements/selector_view_controller_delegate.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #import "base/mac/objc_property_releaser.h" | 9 |
| 10 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 11 #error "This file requires ARC support." | |
| 12 #endif | |
| 10 | 13 |
| 11 namespace { | 14 namespace { |
| 12 // Font size of text in the picker view. | 15 // Font size of text in the picker view. |
| 13 CGFloat kUIPickerFontSize = 26; | 16 CGFloat kUIPickerFontSize = 26; |
| 14 } // namespace | 17 } // namespace |
| 15 | 18 |
| 16 @interface SelectorPickerViewController ()<UIPickerViewDelegate, | 19 @interface SelectorPickerViewController ()<UIPickerViewDelegate, |
| 17 UIPickerViewDataSource> { | 20 UIPickerViewDataSource> { |
| 18 base::mac::ObjCPropertyReleaser | |
| 19 _propertyReleaser_SelectorPickerViewController; | |
| 20 __unsafe_unretained id<SelectorViewControllerDelegate> _delegate; | 21 __unsafe_unretained id<SelectorViewControllerDelegate> _delegate; |
| 21 } | 22 } |
| 22 // Options to display. | 23 // Options to display. |
| 23 @property(nonatomic, copy) NSOrderedSet<NSString*>* options; | 24 @property(nonatomic, copy) NSOrderedSet<NSString*>* options; |
| 24 // The default option. | 25 // The default option. |
| 25 @property(nonatomic, copy) NSString* defaultOption; | 26 @property(nonatomic, copy) NSString* defaultOption; |
| 26 // The displayed UINavigationBar. Exposed for testing. | 27 // The displayed UINavigationBar. Exposed for testing. |
| 27 @property(nonatomic, retain) UINavigationBar* navigationBar; | 28 @property(nonatomic, strong) UINavigationBar* navigationBar; |
| 28 // The displayed UIPickerView. Exposed for testing. | 29 // The displayed UIPickerView. Exposed for testing. |
| 29 @property(nonatomic, retain) UIPickerView* pickerView; | 30 @property(nonatomic, strong) UIPickerView* pickerView; |
| 30 // Action for the "Done" button. | 31 // Action for the "Done" button. |
| 31 - (void)doneButtonPressed; | 32 - (void)doneButtonPressed; |
| 32 // Action for the "Cancel" button. | 33 // Action for the "Cancel" button. |
| 33 - (void)cancelButtonPressed; | 34 - (void)cancelButtonPressed; |
| 34 @end | 35 @end |
| 35 | 36 |
| 36 @implementation SelectorPickerViewController | 37 @implementation SelectorPickerViewController |
| 37 | 38 |
| 38 @synthesize pickerView = _pickerView; | 39 @synthesize pickerView = _pickerView; |
| 39 @synthesize navigationBar = _navigationBar; | 40 @synthesize navigationBar = _navigationBar; |
| 40 | 41 |
| 41 @synthesize options = _options; | 42 @synthesize options = _options; |
| 42 @synthesize defaultOption = _defaultOption; | 43 @synthesize defaultOption = _defaultOption; |
| 43 | 44 |
| 44 - (instancetype)initWithOptions:(NSOrderedSet<NSString*>*)options | 45 - (instancetype)initWithOptions:(NSOrderedSet<NSString*>*)options |
| 45 default:(NSString*)defaultOption { | 46 default:(NSString*)defaultOption { |
| 46 self = [super initWithNibName:nil bundle:nil]; | 47 self = [super initWithNibName:nil bundle:nil]; |
| 47 if (self) { | 48 if (self) { |
| 48 _propertyReleaser_SelectorPickerViewController.Init( | 49 _defaultOption = [defaultOption copy]; |
| 49 self, [SelectorPickerViewController class]); | |
| 50 _options = [options copy]; | 50 _options = [options copy]; |
| 51 _defaultOption = [defaultOption copy]; | |
| 52 } | 51 } |
| 53 return self; | 52 return self; |
| 54 } | 53 } |
| 55 | 54 |
| 56 - (instancetype)initWithNibName:(NSString*)nibName bundle:(NSBundle*)nibBundle { | 55 - (instancetype)initWithNibName:(NSString*)nibName bundle:(NSBundle*)nibBundle { |
| 57 NOTREACHED(); | 56 NOTREACHED(); |
| 58 return nil; | 57 return nil; |
| 59 } | 58 } |
| 60 | 59 |
| 61 - (instancetype)initWithCoder:(NSCoder*)aDecoder { | 60 - (instancetype)initWithCoder:(NSCoder*)aDecoder { |
| 62 NOTREACHED(); | 61 NOTREACHED(); |
| 63 return nil; | 62 return nil; |
| 64 } | 63 } |
| 65 | 64 |
| 66 - (void)loadView { | 65 - (void)loadView { |
| 67 self.pickerView = | 66 self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectZero]; |
| 68 [[[UIPickerView alloc] initWithFrame:CGRectZero] autorelease]; | 67 self.navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectZero]; |
| 69 self.navigationBar = | |
| 70 [[[UINavigationBar alloc] initWithFrame:CGRectZero] autorelease]; | |
| 71 self.pickerView.translatesAutoresizingMaskIntoConstraints = NO; | 68 self.pickerView.translatesAutoresizingMaskIntoConstraints = NO; |
| 72 self.navigationBar.translatesAutoresizingMaskIntoConstraints = NO; | 69 self.navigationBar.translatesAutoresizingMaskIntoConstraints = NO; |
| 73 UIStackView* stackView = [[[UIStackView alloc] | 70 UIStackView* stackView = [[UIStackView alloc] |
| 74 initWithArrangedSubviews:@[ self.navigationBar, self.pickerView ]] | 71 initWithArrangedSubviews:@[ self.navigationBar, self.pickerView ]]; |
| 75 autorelease]; | |
| 76 stackView.axis = UILayoutConstraintAxisVertical; | 72 stackView.axis = UILayoutConstraintAxisVertical; |
| 77 self.view = stackView; | 73 self.view = stackView; |
| 78 } | 74 } |
| 79 | 75 |
| 80 - (void)viewDidLoad { | 76 - (void)viewDidLoad { |
| 81 [super viewDidLoad]; | 77 [super viewDidLoad]; |
| 82 self.view.backgroundColor = [UIColor whiteColor]; | 78 self.view.backgroundColor = [UIColor whiteColor]; |
| 83 | 79 |
| 84 self.pickerView.showsSelectionIndicator = YES; | 80 self.pickerView.showsSelectionIndicator = YES; |
| 85 self.pickerView.backgroundColor = [UIColor whiteColor]; | 81 self.pickerView.backgroundColor = [UIColor whiteColor]; |
| 86 self.pickerView.delegate = self; | 82 self.pickerView.delegate = self; |
| 87 self.pickerView.dataSource = self; | 83 self.pickerView.dataSource = self; |
| 88 NSInteger initialIndex = [self.options indexOfObject:self.defaultOption]; | 84 NSInteger initialIndex = [self.options indexOfObject:self.defaultOption]; |
| 89 if (initialIndex != NSNotFound) { | 85 if (initialIndex != NSNotFound) { |
| 90 [self.pickerView selectRow:initialIndex inComponent:0 animated:NO]; | 86 [self.pickerView selectRow:initialIndex inComponent:0 animated:NO]; |
| 91 } | 87 } |
| 92 | 88 |
| 93 UINavigationItem* navigationItem = | 89 UINavigationItem* navigationItem = |
| 94 [[[UINavigationItem alloc] initWithTitle:@""] autorelease]; | 90 [[UINavigationItem alloc] initWithTitle:@""]; |
| 95 UIBarButtonItem* doneButton = [[[UIBarButtonItem alloc] | 91 UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] |
| 96 initWithBarButtonSystemItem:UIBarButtonSystemItemDone | 92 initWithBarButtonSystemItem:UIBarButtonSystemItemDone |
| 97 target:self | 93 target:self |
| 98 action:@selector(doneButtonPressed)] autorelease]; | 94 action:@selector(doneButtonPressed)]; |
| 99 UIBarButtonItem* cancelButton = [[[UIBarButtonItem alloc] | 95 UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc] |
| 100 initWithBarButtonSystemItem:UIBarButtonSystemItemCancel | 96 initWithBarButtonSystemItem:UIBarButtonSystemItemCancel |
| 101 target:self | 97 target:self |
| 102 action:@selector(cancelButtonPressed)] autorelease]; | 98 action:@selector(cancelButtonPressed)]; |
| 103 [navigationItem setRightBarButtonItem:doneButton]; | 99 [navigationItem setRightBarButtonItem:doneButton]; |
| 104 [navigationItem setLeftBarButtonItem:cancelButton]; | 100 [navigationItem setLeftBarButtonItem:cancelButton]; |
| 105 [navigationItem setHidesBackButton:YES]; | 101 [navigationItem setHidesBackButton:YES]; |
| 106 [self.navigationBar pushNavigationItem:navigationItem animated:NO]; | 102 [self.navigationBar pushNavigationItem:navigationItem animated:NO]; |
| 107 } | 103 } |
| 108 | 104 |
| 109 - (id<SelectorViewControllerDelegate>)delegate { | 105 - (id<SelectorViewControllerDelegate>)delegate { |
| 110 return _delegate; | 106 return _delegate; |
| 111 } | 107 } |
| 112 | 108 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 126 } | 122 } |
| 127 | 123 |
| 128 #pragma mark UIPickerViewDelegate | 124 #pragma mark UIPickerViewDelegate |
| 129 | 125 |
| 130 - (UIView*)pickerView:(UIPickerView*)pickerView | 126 - (UIView*)pickerView:(UIPickerView*)pickerView |
| 131 viewForRow:(NSInteger)row | 127 viewForRow:(NSInteger)row |
| 132 forComponent:(NSInteger)component | 128 forComponent:(NSInteger)component |
| 133 reusingView:(UIView*)view { | 129 reusingView:(UIView*)view { |
| 134 DCHECK_EQ(0, component); | 130 DCHECK_EQ(0, component); |
| 135 UILabel* label = [view isKindOfClass:[UILabel class]] | 131 UILabel* label = [view isKindOfClass:[UILabel class]] |
| 136 ? (UILabel*)view | 132 ? (UILabel*)view |
|
sdefresne
2016/12/12 19:22:34
Please change this to ObjCCastStrict<UILabel>(view
stkhapugin
2016/12/13 13:08:30
Done.
| |
| 137 : [[[UILabel alloc] init] autorelease]; | 133 : [[UILabel alloc] init]; |
| 138 NSString* text = self.options[row]; | 134 NSString* text = self.options[row]; |
| 139 label.text = text; | 135 label.text = text; |
| 140 label.textAlignment = NSTextAlignmentCenter; | 136 label.textAlignment = NSTextAlignmentCenter; |
| 141 label.font = [text isEqualToString:self.defaultOption] | 137 label.font = [text isEqualToString:self.defaultOption] |
| 142 ? [UIFont boldSystemFontOfSize:kUIPickerFontSize] | 138 ? [UIFont boldSystemFontOfSize:kUIPickerFontSize] |
| 143 : [UIFont systemFontOfSize:kUIPickerFontSize]; | 139 : [UIFont systemFontOfSize:kUIPickerFontSize]; |
| 144 return label; | 140 return label; |
| 145 } | 141 } |
| 146 | 142 |
| 147 #pragma mark Private methods | 143 #pragma mark Private methods |
| 148 | 144 |
| 149 - (void)doneButtonPressed { | 145 - (void)doneButtonPressed { |
| 150 NSInteger selectedIndex = [self.pickerView selectedRowInComponent:0]; | 146 NSInteger selectedIndex = [self.pickerView selectedRowInComponent:0]; |
| 151 [_delegate selectorViewController:self | 147 [_delegate selectorViewController:self |
| 152 didSelectOption:self.options[selectedIndex]]; | 148 didSelectOption:self.options[selectedIndex]]; |
| 153 } | 149 } |
| 154 | 150 |
| 155 - (void)cancelButtonPressed { | 151 - (void)cancelButtonPressed { |
| 156 [_delegate selectorViewController:self didSelectOption:self.defaultOption]; | 152 [_delegate selectorViewController:self didSelectOption:self.defaultOption]; |
| 157 } | 153 } |
| 158 | 154 |
| 159 @end | 155 @end |
| OLD | NEW |