-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTRRootViewController.m
221 lines (173 loc) · 7.72 KB
/
TRRootViewController.m
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
#import "TRRootViewController.h"
#import "TRRuntimeTool.h"
#import "TRUtils.h"
@interface TRRootViewController ()
@property(strong, nonatomic) NSArray *processes;
@property(strong, nonatomic) NSArray *filtered;
@property(strong, nonatomic) NSString *searchText;
@property(strong, nonatomic) UISearchController *searchController;
@end
@implementation TRRootViewController
- (void)loadView {
[super loadView];
_processes = [AppRuntimeTool listProcess];
_filtered = _processes;
self.title = @"TrollR2ool";
self.navigationController.navigationBar.prefersLargeTitles = YES;
[self setupTableFooterView];
// 刷新
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self
action:@selector(refreshProcesses:)
forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;
// 初始化搜索
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchController.searchResultsUpdater = self;
_searchController.searchBar.placeholder = @"Search...";
self.tableView.tableHeaderView = _searchController.searchBar;
}
- (void)setupTableFooterView {
UIView *footerView =
[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 35)];
UILabel *projectLabel =
[[UILabel alloc] initWithFrame:CGRectMake(10, 10, footerView.frame.size.width, 20)];
projectLabel.numberOfLines = 0;
projectLabel.font = [UIFont boldSystemFontOfSize:10];
projectLabel.textColor = [UIColor purpleColor];
projectLabel.text = @"TrollR2ool v1.0 Copyright © 2024\n"
@"巴斯.zznQ.";
[projectLabel sizeToFit];
[footerView addSubview:projectLabel];
self.tableView.tableFooterView = footerView;
}
- (void)refreshProcesses:(UIRefreshControl *)refreshControl {
_processes = [AppRuntimeTool listProcess];
if (_searchController.isActive) {
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@"bundleID CONTAINS[cd] %@", _searchText];
_filtered = [_processes filteredArrayUsingPredicate:predicate];
} else {
_filtered = _processes;
}
[self.tableView reloadData];
[refreshControl endRefreshing];
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString *searchText = searchController.searchBar.text;
if (searchText.length > 0) {
_searchText = searchText;
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@"bundleID CONTAINS[cd] %@", searchText];
_filtered = [_processes filteredArrayUsingPredicate:predicate];
} else {
_filtered = _processes;
}
[self.tableView reloadData];
}
#pragma mark - Table View Data Source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _searchController.isActive ? _filtered.count : _processes.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 55.0f;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"AppCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:cellIdentifier];
NSDictionary *app =
_searchController.isActive ? _filtered[indexPath.row] : _processes[indexPath.row];
cell.textLabel.text = app[@"name"];
cell.detailTextLabel.text =
[NSString stringWithFormat:@"pid:%@ • %@", app[@"pid"], app[@"bundleID"]];
cell.imageView.image = applicationIconImageForBundleIdentifier(app[@"bundleID"]);
return cell;
}
#pragma mark - Table View Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSDictionary *app =
_searchController.isActive ? _filtered[indexPath.row] : _processes[indexPath.row];
DetailViewController *detail = [[DetailViewController alloc] init];
detail.title = app[@"name"];
detail.pid = [app[@"pid"] intValue];
detail.navigationItem.prompt = [NSString stringWithFormat:@"pid: %d", detail.pid];
self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@"TrollR2ool"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
[self.navigationController pushViewController:detail animated:YES];
}
@end
@interface DetailViewController () <UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong) NSArray *sections;
@property(nonatomic, strong) NSDictionary *items;
@end
static NSString *const secUtilities = @"Utilities";
static NSString *const secCustomization = @"Customization";
static NSString *const itemLookupPtraceSvc = @"Lookup Ptrace svc";
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_sections = @[ secUtilities, secCustomization ];
_items = @{
secUtilities : @[ itemLookupPtraceSvc ],
secCustomization : @[],
};
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds
style:UITableViewStyleGrouped];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return _sections.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *items = _items[_sections[section]];
return items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"cell"];
}
NSArray *items = _items[_sections[indexPath.section]];
cell.textLabel.text = items[indexPath.row];
cell.textLabel.textColor = [UIColor systemBlueColor];
return cell;
}
#pragma mark - UITableViewDelegate
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return _sections[section];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *selected = _items[_sections[indexPath.section]][indexPath.row];
// hanlde item click
if ([selected isEqualToString:(itemLookupPtraceSvc)]) {
NSString *msg = [AppRuntimeTool lookupPtraceSvc:_pid];
UIAlertController *alert;
alert = [UIAlertController alertControllerWithTitle:itemLookupPtraceSvc
message:msg
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"ojbk👌"
style:UIAlertActionStyleCancel
handler:nil];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
}
}
@end