1// Copyright 2018 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/autofill/manual_fill/manual_fill_action_cell.h"
6
7#include "base/feature_list.h"
8#import "ios/chrome/browser/ui/autofill/manual_fill/manual_fill_cell_button.h"
9#import "ios/chrome/browser/ui/autofill/manual_fill/manual_fill_cell_utils.h"
10#import "ios/chrome/browser/ui/list_model/list_model.h"
11#include "ios/chrome/browser/ui/ui_feature_flags.h"
12#import "ios/chrome/common/ui/colors/semantic_color_names.h"
13#import "ios/chrome/common/ui/util/constraints_ui_util.h"
14#import "ios/chrome/common/ui/util/pointer_interaction_util.h"
15
16#if !defined(__has_feature) || !__has_feature(objc_arc)
17#error "This file requires ARC support."
18#endif
19
20@interface ManualFillActionItem ()
21
22// The action block to be called when the user taps the title.
23@property(nonatomic, copy, readonly) void (^action)(void);
24
25// The title for the action.
26@property(nonatomic, copy, readonly) NSString* title;
27
28@end
29
30@implementation ManualFillActionItem
31
32- (instancetype)initWithTitle:(NSString*)title action:(void (^)(void))action {
33  self = [super initWithType:kItemTypeEnumZero];
34  if (self) {
35    _title = [title copy];
36    _action = [action copy];
37    self.cellClass = [ManualFillActionCell class];
38  }
39  return self;
40}
41
42- (void)configureCell:(ManualFillActionCell*)cell
43           withStyler:(ChromeTableViewStyler*)styler {
44  [super configureCell:cell withStyler:styler];
45  cell.accessibilityIdentifier = nil;
46  [cell setUpWithTitle:self.title
47       accessibilityID:self.accessibilityIdentifier
48                action:self.action];
49}
50
51@end
52
53@interface ManualFillActionCell ()
54
55// The action block to be called when the user taps the title button.
56@property(nonatomic, copy) void (^action)(void);
57
58// The title button of this cell.
59@property(nonatomic, strong) UIButton* titleButton;
60
61@end
62
63@implementation ManualFillActionCell
64
65#pragma mark - Public
66
67- (void)prepareForReuse {
68  [super prepareForReuse];
69
70  self.action = nil;
71  [self.titleButton setTitle:nil forState:UIControlStateNormal];
72  self.titleButton.accessibilityIdentifier = nil;
73}
74
75- (void)setUpWithTitle:(NSString*)title
76       accessibilityID:(NSString*)accessibilityID
77                action:(void (^)(void))action {
78  if (self.contentView.subviews.count == 0) {
79    [self createView];
80  }
81
82  [self.titleButton setTitle:title forState:UIControlStateNormal];
83  self.titleButton.accessibilityIdentifier = accessibilityID;
84  self.action = action;
85}
86
87#pragma mark - Private
88
89- (void)createView {
90  self.selectionStyle = UITableViewCellSelectionStyleNone;
91
92  self.titleButton = [ManualFillCellButton buttonWithType:UIButtonTypeCustom];
93  [self.titleButton addTarget:self
94                       action:@selector(userDidTapTitleButton:)
95             forControlEvents:UIControlEventTouchUpInside];
96
97  [self.contentView addSubview:self.titleButton];
98
99  NSMutableArray<NSLayoutConstraint*>* constraints =
100      [[NSMutableArray alloc] initWithArray:@[
101        [self.contentView.topAnchor
102            constraintEqualToAnchor:self.titleButton.topAnchor],
103        [self.contentView.bottomAnchor
104            constraintEqualToAnchor:self.titleButton.bottomAnchor],
105      ]];
106  AppendHorizontalConstraintsForViews(constraints, @[ self.titleButton ],
107                                      self.contentView);
108  [NSLayoutConstraint activateConstraints:constraints];
109#if defined(__IPHONE_13_4)
110  if (@available(iOS 13.4, *)) {
111    if (base::FeatureList::IsEnabled(kPointerSupport)) {
112      [self addInteraction:[[ViewPointerInteraction alloc] init]];
113    }
114  }
115#endif  // defined(__IPHONE_13_4)
116}
117
118- (void)userDidTapTitleButton:(UIButton*)sender {
119  if (self.action) {
120    self.action();
121  }
122}
123
124@end
125