forked from MiniKeePass/MiniKeePass
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectGroupViewController.m
More file actions
167 lines (132 loc) · 6.48 KB
/
SelectGroupViewController.m
File metadata and controls
167 lines (132 loc) · 6.48 KB
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
/*
* Copyright 2011-2012 Jason Rush and John Flanagan. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#import "SelectGroupViewController.h"
#import "MiniKeePassAppDelegate.h"
#define DEFAULT_SPACER_WIDTH 10.0f
static NSString *const kKeySelectable = @"selectable";
static NSString *const kKeyGroup = @"group";
static NSString *const kKeyLevel = @"level";
static NSString *const kKeyName = @"name";
@interface IndentTableViewCell : UITableViewCell
@property (nonatomic, assign) NSUInteger level;
@property (nonatomic, assign) NSUInteger spacerWidth;
@end
@implementation IndentTableViewCell
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat imageWidth = self.imageView.bounds.size.width;
if ([self respondsToSelector:@selector(setSeparatorInset:)]) {
self.imageView.frame = CGRectMake(self.spacerWidth + (imageWidth + self.spacerWidth) * self.level,
self.imageView.frame.origin.y,
self.imageView.frame.size.width,
self.imageView.frame.size.height);
self.separatorInset = UIEdgeInsetsMake(0,
self.spacerWidth + (imageWidth + self.spacerWidth) * (self.level + 1),
0,
0);
} else {
CGFloat indentation = (imageWidth + self.spacerWidth) * self.level;
self.contentView.frame = CGRectMake(indentation,
self.contentView.frame.origin.y,
self.contentView.frame.size.width - indentation,
self.contentView.frame.size.height);
self.textLabel.frame = CGRectMake(self.textLabel.frame.origin.x,
self.textLabel.frame.origin.y,
self.textLabel.frame.size.width - indentation,
self.textLabel.frame.size.height);
}
}
@end
@interface SelectGroupViewController ()
@property (nonatomic, strong) NSMutableArray *allGroups;
@property (nonatomic, weak) MiniKeePassAppDelegate *appDelegate;
@end
@implementation SelectGroupViewController
- (id)initWithStyle:(UITableViewStyle)style {
self = [super initWithStyle:style];
if (self) {
self.title = NSLocalizedString(@"Choose Group", nil);
self.allGroups = [[NSMutableArray alloc] init];
self.appDelegate = [MiniKeePassAppDelegate appDelegate];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(dismissModalViewControllerAnimated:)];
self.navigationItem.leftBarButtonItem = cancelButton;
}
return self;
}
- (void)viewDidLoad {
// Get parameters for the root
KdbGroup *rootGroup = self.appDelegate.databaseDocument.kdbTree.root;
NSString *filename = [self.appDelegate.databaseDocument.filename lastPathComponent];
// Recursivly add subgroups
[self addGroup:rootGroup withName:filename atLevel:0];
}
- (void)addGroup:(KdbGroup *)group withName:(NSString *)name atLevel:(NSInteger)level {
BOOL selectable = [self.delegate selectGroupViewController:self canSelectGroup:group];
[self.allGroups addObject:@{kKeyGroup : group,
kKeyLevel : [NSNumber numberWithInteger:level],
kKeyName : name,
kKeySelectable : [NSNumber numberWithBool:selectable]}];
// Sort all the sub-groups
NSArray *sortedGroups = [group.groups sortedArrayUsingComparator:^NSComparisonResult(KdbGroup *group1, KdbGroup *group2) {
return [group1.name localizedCaseInsensitiveCompare:group2.name];
}];
// Add sub-groups
for (KdbGroup *group in sortedGroups) {
[self addGroup:group withName:group.name atLevel:level + 1];
}
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.allGroups count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
IndentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[IndentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *dict = [self.allGroups objectAtIndex:indexPath.row];
KdbGroup *group = [dict objectForKey:kKeyGroup];
cell.textLabel.text = [dict objectForKey:kKeyName];
cell.imageView.image = [self.appDelegate loadImage:group.image];
cell.level = [[dict objectForKey:kKeyLevel] integerValue];
if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
cell.spacerWidth = tableView.separatorInset.left;
} else {
cell.spacerWidth = DEFAULT_SPACER_WIDTH;
}
BOOL selectable = [[dict objectForKey:kKeySelectable] boolValue];
if (selectable) {
cell.textLabel.textColor = [UIColor blackColor];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
} else {
cell.textLabel.textColor = [UIColor lightGrayColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *groupDict = [self.allGroups objectAtIndex:indexPath.row];
BOOL selectable = [[groupDict objectForKey:kKeySelectable] boolValue];
if (selectable) {
KdbGroup *selectedGroup = [groupDict objectForKey:kKeyGroup];
[self.delegate selectGroupViewController:self selectedGroup:selectedGroup];
[self dismissModalViewControllerAnimated:YES];
}
}
@end