Skip to content

Commit 55cca10

Browse files
committed
Implement Settings VC for basic settings
Summary: This commit adds the basic Settings UI for all of the basic settings (e.g. everything besides account settings). It uses a UITableViewController with customized headers and cells in lieu of a collection view, for now. It also uses preformatted dummy data which can be factored out later. Test Plan: 1. Open the app 2. Tap the side menu 3. Tap "Settings" 4. Observe a Settings menu appears that has the same basic settings as the demo web application. 5. Observe that there is not yet an account cell within settings. Reviewers: junius, #material_components_ios_owners Reviewed By: junius, #material_components_ios_owners Projects: #material_components_ios Differential Revision: http://codereview.cc/D95
1 parent a1a9748 commit 55cca10

File tree

1 file changed

+155
-41
lines changed

1 file changed

+155
-41
lines changed

demos/Pesto/Pesto/PestoSettingsViewController.m

Lines changed: 155 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,45 @@
33
#import "MaterialSwitch.h"
44
#import "MaterialTypography.h"
55

6-
static CGFloat kPestoSettingsViewControllerCellViewInset = 20.f;
7-
static CGFloat kPestoSettingsViewControllerCellViewHeight = 48.f;
6+
static CGFloat kPestoSettingsTableViewOffsetTop = 10.f;
87

9-
@interface PestoSettingsViewControllerCellView : UIView
8+
static NSString *const kPestoSettingsTableViewCellReuseIdentifier = @"PestoSettingsTableViewCell";
9+
static NSString *const kPestoSettingsTableViewHeaderViewReuseIdentifier =
10+
@"PestoSettingsTableViewHeaderView";
11+
12+
static CGFloat kPestoSettingsTableViewHeaderSeparatorWidth = 1.f;
13+
14+
@interface PestoSettingsTableViewCell : UITableViewCell
1015

1116
@property(nonatomic) NSString *labelText;
1217
@property(nonatomic) BOOL on;
1318

1419
@end
1520

16-
@interface PestoSettingsViewControllerCellView ()
21+
@interface PestoSettingsTableViewCell ()
1722

18-
@property(nonatomic) UILabel *labelView;
1923
@property(nonatomic) MDCSwitch *switchView;
2024

2125
@end
2226

23-
@implementation PestoSettingsViewControllerCellView
27+
@implementation PestoSettingsTableViewCell
2428

25-
- (id)initWithCoder:(NSCoder *)aDecoder {
26-
self = [super initWithCoder:aDecoder];
27-
return self;
28-
}
29-
30-
- (id)initWithFrame:(CGRect)frame {
31-
self = [super initWithFrame:frame];
29+
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
30+
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
3231
if (self) {
33-
CGRect descRect = CGRectMake(0, 0, self.bounds.size.width * 0.75f, self.bounds.size.height);
34-
_labelView = [[UILabel alloc] initWithFrame:descRect];
35-
_labelView.font = [MDCTypography body1Font];
36-
_labelView.alpha = [MDCTypography body1FontOpacity];
37-
_labelView.autoresizingMask = UIViewAutoresizingFlexibleWidth |
38-
UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin;
32+
self.textLabel.font = [MDCTypography body1Font];
33+
self.textLabel.alpha = [MDCTypography body1FontOpacity];
3934

4035
_switchView = [[MDCSwitch alloc] initWithFrame:CGRectZero];
41-
CGFloat switchX = self.bounds.size.width - _switchView.frame.size.width;
42-
CGFloat switchY = (self.bounds.size.height - _switchView.frame.size.height) / 2.f;
43-
_switchView.frame = CGRectOffset(_switchView.frame, switchX, switchY);
4436
_switchView.onTintColor = [UIColor colorWithRed:0.09f green:0.54f blue:0.44f alpha:1.f];
45-
_switchView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
46-
UIViewAutoresizingFlexibleTopMargin |
47-
UIViewAutoresizingFlexibleBottomMargin;
48-
49-
[self addSubview:_labelView];
50-
[self addSubview:_switchView];
37+
self.accessoryView = _switchView;
5138
}
5239
return self;
5340
}
5441

5542
- (void)setLabelText:(NSString *)labelText {
5643
_labelText = labelText;
57-
_labelView.text = _labelText;
58-
[self setNeedsLayout];
44+
self.textLabel.text = _labelText;
5945
}
6046

6147
- (void)setOn:(BOOL)on {
@@ -65,24 +51,152 @@ - (void)setOn:(BOOL)on {
6551

6652
@end
6753

54+
@interface PestoSettingsTableViewHeaderView : UITableViewHeaderFooterView
55+
56+
@property(nonatomic) UIColor *separatorColor;
57+
58+
@end
59+
60+
@interface PestoSettingsTableViewHeaderView ()
61+
62+
@property(nonatomic) CALayer *separator;
63+
64+
@end
65+
66+
@implementation PestoSettingsTableViewHeaderView
67+
68+
- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier {
69+
self = [super initWithReuseIdentifier:reuseIdentifier];
70+
if (self) {
71+
self.textLabel.font = [MDCTypography headlineFont];
72+
self.textLabel.alpha = [MDCTypography headlineFontOpacity];
73+
self.textLabel.textColor = [UIColor colorWithRed:0.09f green:0.54f blue:0.44f alpha:1.f];
74+
75+
self.backgroundView = [[UIView alloc] initWithFrame:self.bounds];
76+
self.backgroundView.backgroundColor = [UIColor whiteColor];
77+
78+
_separator = [CALayer layer];
79+
[self.contentView.layer addSublayer:_separator];
80+
}
81+
return self;
82+
}
83+
84+
- (void)layoutSubviews {
85+
[super layoutSubviews];
86+
CGFloat borderBottomYPos = CGRectGetMaxY(self.contentView.bounds) -
87+
kPestoSettingsTableViewHeaderSeparatorWidth;
88+
_separator.frame = CGRectMake(0,
89+
borderBottomYPos,
90+
CGRectGetWidth(self.contentView.bounds),
91+
kPestoSettingsTableViewHeaderSeparatorWidth);
92+
self.backgroundView.frame = self.bounds;
93+
}
94+
95+
- (void)setSeparatorColor:(UIColor *)separatorColor {
96+
_separatorColor = separatorColor;
97+
_separator.backgroundColor = _separatorColor.CGColor;
98+
}
99+
100+
@end
101+
102+
@interface PestoSettingsViewController () <UITableViewDataSource, UITableViewDelegate>
103+
104+
@property(nonatomic) NSArray *dummySettingHeaders;
105+
@property(nonatomic) NSArray *dummySettingTitles;
106+
@property(nonatomic) NSArray *dummySettingVals;
107+
@property(nonatomic) UITableView *settingsTableView;
108+
109+
@end
110+
68111
@implementation PestoSettingsViewController
69112

70113
- (void)viewDidLoad {
71114
[super viewDidLoad];
72-
CGRect testViewFrame = CGRectMake(0,
73-
10.f,
74-
self.view.bounds.size.width,
75-
kPestoSettingsViewControllerCellViewHeight);
76-
CGRect insetFrame = CGRectInset(testViewFrame, kPestoSettingsViewControllerCellViewInset, 0);
77-
PestoSettingsViewControllerCellView *testCellView = [[PestoSettingsViewControllerCellView alloc]
78-
initWithFrame:insetFrame];
79-
testCellView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
80-
testCellView.labelText = @"Settings view needs more work?";
81-
testCellView.on = YES;
82115

83116
self.view.backgroundColor = [UIColor whiteColor];
84117

85-
[self.view addSubview:testCellView];
118+
_dummySettingHeaders = @[ @"Account", @"Notification" ];
119+
_dummySettingTitles = @[ @[ @"Public Profile", @"Subscribe to Daily Digest" ],
120+
@[ @"Get email notifications", @"Get text notifications" ] ];
121+
_dummySettingVals = @[ @[ @YES, @NO ], @[ @NO, @YES ] ];
122+
123+
CGRect settingsTableViewFrame =
124+
CGRectMake(0,
125+
kPestoSettingsTableViewOffsetTop,
126+
self.view.bounds.size.width,
127+
self.view.bounds.size.height - kPestoSettingsTableViewOffsetTop);
128+
_settingsTableView = [[UITableView alloc] initWithFrame:settingsTableViewFrame
129+
style:UITableViewStylePlain];
130+
_settingsTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
131+
_settingsTableView.allowsSelection = NO;
132+
_settingsTableView.backgroundColor = self.view.backgroundColor;
133+
_settingsTableView.dataSource = self;
134+
_settingsTableView.delegate = self;
135+
_settingsTableView.separatorColor = [[self class] tableViewSeparatorColor];
136+
// Ensure empty rows are not shown.
137+
_settingsTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
138+
139+
[_settingsTableView registerClass:[PestoSettingsTableViewCell class]
140+
forCellReuseIdentifier:kPestoSettingsTableViewCellReuseIdentifier];
141+
[_settingsTableView registerClass:[PestoSettingsTableViewHeaderView class]
142+
forHeaderFooterViewReuseIdentifier:kPestoSettingsTableViewHeaderViewReuseIdentifier];
143+
144+
[_settingsTableView reloadData];
145+
146+
[self.view addSubview:_settingsTableView];
147+
}
148+
149+
+ (UIColor *)tableViewSeparatorColor {
150+
return [UIColor colorWithWhite:0.f alpha:0.1f];
151+
}
152+
153+
#pragma mark - UITableViewDataSource
154+
155+
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
156+
return [_dummySettingHeaders count];
157+
}
158+
159+
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
160+
return [(NSArray *)_dummySettingTitles[section] count];
161+
}
162+
163+
- (UITableViewCell *)tableView:(UITableView *)tableView
164+
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
165+
NSString *settingLabel = _dummySettingTitles[indexPath.section][indexPath.row];
166+
PestoSettingsTableViewCell *cell =
167+
[tableView dequeueReusableCellWithIdentifier:kPestoSettingsTableViewCellReuseIdentifier
168+
forIndexPath:indexPath];
169+
cell.labelText = settingLabel;
170+
cell.on = [_dummySettingVals[indexPath.section][indexPath.row] boolValue];
171+
172+
return cell;
173+
}
174+
175+
#pragma mark - UITableViewDelegate
176+
177+
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
178+
NSString *reuseId = kPestoSettingsTableViewHeaderViewReuseIdentifier;
179+
PestoSettingsTableViewHeaderView *header =
180+
[tableView dequeueReusableHeaderFooterViewWithIdentifier:reuseId];
181+
header.textLabel.text = _dummySettingHeaders[section];
182+
header.separatorColor = [[self class] tableViewSeparatorColor];
183+
return header;
184+
}
185+
186+
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
187+
return 50.f;
188+
}
189+
190+
- (void)tableView:(UITableView *)tableView willDisplayCell:(nonnull UITableViewCell *)cell
191+
forRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
192+
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
193+
[cell setSeparatorInset:UIEdgeInsetsZero];
194+
}
195+
196+
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
197+
[cell setLayoutMargins:UIEdgeInsetsZero];
198+
cell.preservesSuperviewLayoutMargins = NO;
199+
}
86200
}
87201

88202
@end

0 commit comments

Comments
 (0)