aboutsummaryrefslogtreecommitdiffhomepage
path: root/AuthSamples/Sample/StaticContentTableViewManager.m
blob: 7ac7eb746602a3fb711888ef5d4ae4dd1d7de167 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * Copyright 2017 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import "StaticContentTableViewManager.h"

/** @var kCellReuseIdentitfier
    @brief The reuse identifier for default style table view cell.
 */
static NSString *const kCellReuseIdentitfier = @"reuseIdentifier";

/** @var kCellReuseIdentitfier
    @brief The reuse identifier for value style table view cell.
 */
static NSString *const kValueCellReuseIdentitfier = @"reuseValueIdentifier";

#pragma mark -

@implementation StaticContentTableViewManager

- (void)setContents:(StaticContentTableViewContent *)contents {
  _contents = contents;
  [self.tableView reloadData];
}

- (void)setTableView:(UITableView *)tableView {
  _tableView = tableView;
  [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kCellReuseIdentitfier];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return _contents.sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return _contents.sections[section].cells.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  return _contents.sections[section].title;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  StaticContentTableViewCell *cellData = _contents.sections[indexPath.section].cells[indexPath.row];
  if (cellData.customCell) {
    return cellData.customCell.frame.size.height;
  }
  return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  StaticContentTableViewCell *cellData = _contents.sections[indexPath.section].cells[indexPath.row];
  UITableViewCell *cell = cellData.customCell;
  if (cell) {
    return cell;
  }
  if (cellData.value.length) {
    cell = [tableView dequeueReusableCellWithIdentifier:kValueCellReuseIdentitfier];
    if (!cell) {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                    reuseIdentifier:kValueCellReuseIdentitfier];
      cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
      cell.detailTextLabel.minimumScaleFactor = 0.5;
    }
    cell.detailTextLabel.text = cellData.value;
  } else {
    // kCellReuseIdentitfier has already been registered.
    cell = [tableView dequeueReusableCellWithIdentifier:kCellReuseIdentitfier
                                           forIndexPath:indexPath];
  }
  cell.textLabel.text = cellData.title;
  cell.accessibilityIdentifier = cellData.accessibilityIdentifier;
  return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  StaticContentTableViewCell *cellData = _contents.sections[indexPath.section].cells[indexPath.row];
  BOOL hasAssociatedAction = cellData.action != nil;
  if (hasAssociatedAction) {
    cellData.action();
  }
  [tableView deselectRowAtIndexPath:indexPath animated:hasAssociatedAction];
}

@end

#pragma mark -

@implementation StaticContentTableViewContent

+ (nullable instancetype)contentWithSections:
    (nullable NSArray<StaticContentTableViewSection *> *)sections {
  return [[self alloc] initWithSections:sections];
}

- (nullable instancetype)initWithSections:
    (nullable NSArray<StaticContentTableViewSection *> *)sections {
  self = [super init];
  if (self) {
    _sections = [sections copy];
  }
  return self;
}

@end

#pragma mark -

@implementation StaticContentTableViewSection

+ (nullable instancetype)sectionWithTitle:(nullable NSString *)title
                                    cells:(nullable NSArray<StaticContentTableViewCell *> *)cells {
  return [[self alloc] initWithTitle:title cells:cells];
}

- (nullable instancetype)initWithTitle:(nullable NSString *)title
                                 cells:(nullable NSArray<StaticContentTableViewCell *> *)cells {
  self = [super init];
  if (self) {
    _title = [title copy];
    _cells = [cells copy];
  }
  return self;
}

@end

#pragma mark -

@implementation StaticContentTableViewCell

+ (nullable instancetype)cellWithTitle:(nullable NSString *)title {
  return [[self alloc] initWithCustomCell:nil
                                    title:title
                                    value:nil
                                   action:nil
                          accessibilityID:nil];
}

+ (nullable instancetype)cellWithTitle:(nullable NSString *)title
                                 value:(nullable NSString *)value {
  return [[self alloc] initWithCustomCell:nil
                                    title:title
                                    value:value
                                   action:nil
                          accessibilityID:nil];
}

+ (nullable instancetype)cellWithTitle:(nullable NSString *)title
                                action:(nullable StaticContentTableViewCellAction)action {
  return [[self alloc] initWithCustomCell:nil
                                    title:title
                                    value:nil
                                   action:action
                          accessibilityID:nil];
}

+ (nullable instancetype)cellWithTitle:(nullable NSString *)title
                                 value:(nullable NSString *)value
                                action:(nullable StaticContentTableViewCellAction)action {
  return [[self alloc] initWithCustomCell:nil
                                    title:title
                                    value:value
                                   action:action
                          accessibilityID:nil];
}

+ (nullable instancetype)cellWithTitle:(nullable NSString *)title
                                 value:(nullable NSString *)value
                                action:(nullable StaticContentTableViewCellAction)action
                       accessibilityID:(nullable NSString *)accessibilityID {
      return [[self alloc] initWithCustomCell:nil
                                        title:title
                                        value:value
                                       action:action
                              accessibilityID:accessibilityID];
}

+ (nullable instancetype)cellWithCustomCell:(nullable UITableViewCell *)customCell {
  return [[self alloc] initWithCustomCell:customCell
                                    title:nil
                                    value:nil
                                   action:nil
                          accessibilityID:nil];
}

+ (nullable instancetype)cellWithCustomCell:(nullable UITableViewCell *)customCell
                                     action:(nullable StaticContentTableViewCellAction)action {
  return [[self alloc] initWithCustomCell:customCell
                                    title:nil
                                    value:nil action:action
                          accessibilityID:nil];
}

- (nullable instancetype)initWithCustomCell:(nullable UITableViewCell *)customCell
                                      title:(nullable NSString *)title
                                      value:(nullable NSString *)value
                                     action:(nullable StaticContentTableViewCellAction)action
                            accessibilityID:(nullable NSString *)accessibilityID {
  self = [super init];
  if (self) {
    _customCell = customCell;
    _title = [title copy];
    _value = [value copy];
    _action = action;
    if (accessibilityID) {
      _accessibilityIdentifier = [accessibilityID copy];
      self.isAccessibilityElement = YES;
    }
  }
  return self;
}

@end