-
Notifications
You must be signed in to change notification settings - Fork 10
/
IBRootViewController.m
173 lines (137 loc) · 6.95 KB
/
IBRootViewController.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
#import "IBRootViewController.h"
#import "IBWiFiManager.h"
#import "MobileWiFi.h"
#import "IBWiFiNetwork.h"
#import "IBDetailViewController.h"
#import "IBTableViewCell.h"
#import <MessageUI/MessageUI.h>
@interface IBRootViewController () <UISearchResultsUpdating, MFMailComposeViewControllerDelegate, IBTableViewCellDelegate>
@property (nonatomic, strong) UISearchController *searchController;
@end
@implementation IBRootViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIMenuItem *copyMenuItem = [[UIMenuItem alloc] initWithTitle:@"Copy Password" action:@selector(copyPassword:)];
UIMenuItem *deleteMenuItem = [[UIMenuItem alloc] initWithTitle:@"Forget Network" action:@selector(deleteNetwork:)];
[[UIMenuController sharedMenuController] setMenuItems: @[copyMenuItem, deleteMenuItem]];
[[UIMenuController sharedMenuController] update];
}
- (void)loadView {
[super loadView];
self.title = @"WiFi List";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Sort"] style:UIBarButtonItemStylePlain target:self action:@selector(sortTapped:)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareTapped:)];
self.searchController = [[UISearchController alloc] init];
self.searchController.searchResultsUpdater = self;
self.searchController.obscuresBackgroundDuringPresentation = NO;
self.searchController.searchBar.placeholder = @"Search Networks";
self.navigationItem.searchController = self.searchController;
self.definesPresentationContext = YES;
}
- (void)shareTapped:(id)sender {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
mailVC.mailComposeDelegate = self;
[mailVC setSubject:[NSString stringWithFormat:@"WiFi List from %@", [[UIDevice currentDevice] name]]];
NSMutableString *mailContent = [NSMutableString string];
for(IBWiFiNetwork *network in [IBWiFiManager sharedManager].networks) {
[mailContent appendFormat:@"<b>%@</b><br />%@<br /><br />", network.name, network.password];
}
[mailVC setMessageBody:mailContent isHTML:YES];
[self presentViewController:mailVC animated:YES completion:nil];
}
}
- (void) sortTapped:(id)sender {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Sort Order" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *nameActionAsc = [UIAlertAction actionWithTitle:@"Name Ascending" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
[self setSortOrder:NAME_ASC];
}];
UIAlertAction *nameActionDesc = [UIAlertAction actionWithTitle:@"Name Descending" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
[self setSortOrder:NAME_DESC];
}];
UIAlertAction *addedActionAsc = [UIAlertAction actionWithTitle:@"Added Ascending" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
[self setSortOrder:ADDED_ASC];
}];
UIAlertAction *addedActionDesc = [UIAlertAction actionWithTitle:@"Added Descending" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
[self setSortOrder:ADDED_DESC];
}];
UIAlertAction *joinedActionAsc = [UIAlertAction actionWithTitle:@"Last Joined Ascending" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
[self setSortOrder:LAST_JOINED_ASC];
}];
UIAlertAction *joinedActionDesc = [UIAlertAction actionWithTitle:@"Last Joined Descending" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
[self setSortOrder:LAST_JOINED_DESC];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:nameActionAsc];
[alertController addAction:nameActionDesc];
[alertController addAction:addedActionAsc];
[alertController addAction:addedActionDesc];
[alertController addAction:joinedActionAsc];
[alertController addAction:joinedActionDesc];
[alertController addAction:cancelAction];
[[alertController popoverPresentationController] setSourceView:self.view];
[[alertController popoverPresentationController] setSourceRect:CGRectMake(0,0,1,1)];
[[alertController popoverPresentationController] setPermittedArrowDirections:UIPopoverArrowDirectionUp];
[self presentViewController:alertController animated:YES completion:nil];
}
- (void) setSortOrder:(SortCriteria) newCriteria {
if ([IBWiFiManager sharedManager].sortCriteria == newCriteria) {
return;
}
[IBWiFiManager sharedManager].sortCriteria = newCriteria;
[[IBWiFiManager sharedManager] refreshNetworks];
[self.tableView reloadData];
[[NSUserDefaults standardUserDefaults] setInteger:newCriteria forKey:@"sortOrder"];
}
#pragma mark - Table View Data Source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [IBWiFiManager sharedManager].networks.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
IBTableViewCell *cell = (IBTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[IBTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.delegate = self;
}
IBWiFiNetwork *network = [IBWiFiManager sharedManager].networks[indexPath.row];
[cell setUpForNetwork:network];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
return (action == @selector(copyPassword:) || action == @selector(deleteNetwork:));
}
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
}
#pragma mark - Table View Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
IBWiFiNetwork *network = [IBWiFiManager sharedManager].networks[indexPath.row];
IBDetailViewController *detail = [[IBDetailViewController alloc] initWithNetwork:network];
[self.navigationController pushViewController:detail animated:true];
}
#pragma mark - UISearch
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
UISearchBar *searchBar = searchController.searchBar;
NSString *text = searchBar.text;
[[IBWiFiManager sharedManager] setFilter:text];
[self.tableView reloadData];
}
#pragma mark - MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - IBTableViewCellDelegate
- (void) refreshTable {
[self.tableView reloadData];
}
@end