-
-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathMFSVersionPickerViewController.m
More file actions
144 lines (121 loc) · 3.96 KB
/
Copy pathMFSVersionPickerViewController.m
File metadata and controls
144 lines (121 loc) · 3.96 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
#import "MFSVersionPickerViewController.h"
@interface MFSVersionPickerViewController () <UISearchResultsUpdating>
@property(nonatomic, strong) NSArray *versions;
@property(nonatomic, strong) NSArray *filteredVersions;
@property(nonatomic, strong) UISearchController *searchController;
@end
@implementation MFSVersionPickerViewController
- (instancetype)initWithVersions:(NSArray *)versions
completion:(MFSVersionPickerCompletion)completion
{
self = [super initWithStyle:UITableViewStyleInsetGrouped];
if (self)
{
_versions = versions ?: @[];
_filteredVersions = _versions;
_completionHandler = completion;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Select Version";
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:@"VersionCell"];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(cancelTapped)];
self.navigationItem.leftBarButtonItem = cancelButton;
self.searchController =
[[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.obscuresBackgroundDuringPresentation = NO;
self.searchController.searchBar.placeholder = @"Search versions";
self.navigationItem.searchController = self.searchController;
self.navigationItem.hidesSearchBarWhenScrolling = NO;
self.definesPresentationContext = YES;
}
- (void)cancelTapped
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)updateSearchResultsForSearchController:
(UISearchController *)searchController
{
NSString *query = searchController.searchBar.text;
if (query.length == 0)
{
self.filteredVersions = self.versions;
[self.tableView reloadData];
return;
}
NSString *needle = query.lowercaseString;
NSMutableArray *results = [NSMutableArray array];
for (NSDictionary *version in self.versions)
{
NSString *bundleVersion = [NSString
stringWithFormat:@"%@", version[@"bundle_version"] ?: @""];
NSString *externalIdentifier = [NSString
stringWithFormat:@"%@", version[@"external_identifier"] ?: @""];
NSString *haystack =
[[NSString stringWithFormat:@"%@ %@", bundleVersion,
externalIdentifier] lowercaseString];
if ([haystack containsString:needle])
{
[results addObject:version];
}
}
self.filteredVersions = results;
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return self.filteredVersions.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:@"VersionCell"
forIndexPath:indexPath];
NSDictionary *version = self.filteredVersions[indexPath.row];
cell.textLabel.text = version[@"bundle_version"];
cell.textLabel.font =
[UIFont monospacedDigitSystemFontOfSize:15 weight:UIFontWeightRegular];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSDictionary *selected = self.filteredVersions[indexPath.row];
[self dismissViewControllerAnimated:YES
completion:^{
if (self.completionHandler)
{
self.completionHandler(selected);
}
}];
}
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
{
if (self.searchController.searchBar.text.length > 0)
{
return [NSString
stringWithFormat:@"%lu of %lu versions",
(unsigned long)self.filteredVersions.count,
(unsigned long)self.versions.count];
}
return [NSString stringWithFormat:@"%lu versions available",
(unsigned long)self.versions.count];
}
@end