forked from BalestraPatrick/OpenIt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionDetailViewController.m
More file actions
280 lines (236 loc) · 11.7 KB
/
Copy pathActionDetailViewController.m
File metadata and controls
280 lines (236 loc) · 11.7 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//
// ActionDetailViewController.m
// OpenIt
//
// Created by Patrick Balestra on 24/07/14.
// Copyright (c) 2014 Patrick Balestra. All rights reserved.
//
#import "ActionDetailViewController.h"
#import "ChooseActionViewController.h"
@interface ActionDetailViewController ()
@end
@implementation ActionDetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = self.shortcutArray[0][@"Title"];
if (![self.shortcutArray[0][@"Title"] isEqualToString:@""]) {
self.title = self.shortcutArray[0][@"Title"];
} else {
self.title = self.shortcutArray[1][@"Type"];
}
if (!self.shortcutArray) {
self.shortcutArray = [NSMutableArray array];
}
self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
}
- (IBAction)save:(id)sender {
[self.view endEditing:YES];
if (self.shortcutArray.count > 0) {
if (![self.shortcutArray[0][@"Title"] isEqualToString:@""]) {
if (self.newAction) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"EditAction" object:self.shortcutArray];
} else {
[[NSNotificationCenter defaultCenter] postNotificationName:@"AddNewAction" object:self.shortcutArray];
}
if (!self.newAction) {
// Remove Choose Action View Controller from the hierarchy to pop directly to Your Actions View Controller
NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
[allViewControllers removeObjectAtIndex:1];
self.navigationController.viewControllers = allViewControllers;
}
[self.navigationController popViewControllerAnimated:YES];
} else {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Please Add a Title" message:nil preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[alert dismissViewControllerAnimated:YES completion:nil];
}]];
[self presentViewController:alert animated:YES completion:nil];
}
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (self.shortcutArray.count > 0) {
if (self.shortcutArray.count > 3) {
return 3;
} else {
return 2;
}
}
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return 1;
} else if (section == 1) {
if (self.shortcutArray.count == 0) {
return 1;
} else {
if (self.shortcutArray.count > 3) {
return self.shortcutArray.count - 2;
} else {
return self.shortcutArray.count - 1;
}
}
} else if (section == 2) {
return ((NSMutableArray *)self.shortcutArray[3]).count;
}
return 0;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
if (self.shortcutArray.count == 0) {
return @"Shortcut";
} else {
return @"Title";
}
} else if (section == 1) {
return @"Shortcut";
} else if (section == 2) {
return @"Parameters";
}
return @"";
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 45;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (indexPath.section == 0) {
if (indexPath.row == 0) {
if (self.shortcutArray.count == 0) {
cell.textLabel.text = @"+";
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:30];
cell.textLabel.textColor = [UIColor darkGrayColor];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.tag = -10;
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
} else {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 5, cell.frame.size.width-40, 35)];
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
textField.placeholder = @"Your Title";
textField.delegate = self;
textField.tag = -1;
[cell.contentView addSubview:textField];
cell.textLabel.text = @"";
if (self.shortcutArray.count != 0) {
textField.text = self.shortcutArray[0][@"Title"];
}
}
}
} else if (indexPath.section == 1) {
if (indexPath.row == 0) {
cell.textLabel.text = /*[[self.shortcutArray[1] allKeys] firstObject]*/@"App";
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:17];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.textAlignment = NSTextAlignmentLeft;
UIImageView *imageIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.shortcutArray[1][@"Type"]]];
imageIcon.frame = CGRectMake(0, 0, 35, 35);
cell.accessoryView = imageIcon;
if ([self.shortcutArray[1][@"Type"] isEqualToString:@"Custom"]) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width-190, 40)];
label.text = @"Custom";
label.textAlignment = NSTextAlignmentRight;
cell.accessoryView = label;
}
} else if (indexPath.row == 1) {
cell.textLabel.text = [[self.shortcutArray[2] allKeys] firstObject];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 150, 35)];
label.text = [self.shortcutArray[2] valueForKey:cell.textLabel.text];
label.textAlignment = NSTextAlignmentRight;
label.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:18];
cell.accessoryView = label;
if ([self.shortcutArray[1][@"Type"] isEqualToString:@"Custom"]) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width-190, 40)];
textField.placeholder = @"Custom URL";
textField.tag = -2;
textField.delegate = self;
textField.textAlignment = NSTextAlignmentRight;
textField.text = self.shortcutArray[2][@"URL"];
cell.accessoryView = textField;
}
}
} else if (indexPath.section == 2) {
NSArray *parameters = self.shortcutArray[3];
if (parameters.count == 1 && [parameters[0][@"type"] isEqualToString:@"Multiple"] && ![parameters[0][@"Value"] isEqualToString:@""]) {
// Set right cell title
cell.textLabel.text = parameters[0][@"placeholder"];
// Add Button to accessory view to then pop up Action Sheet
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 150, 35)];
button.layer.borderColor = [UIColor colorWithRed:0.8745 green:0.0784 blue:0.0745 alpha:1.0000].CGColor;
button.layer.borderWidth = 1.0;
button.layer.cornerRadius = 7.0;
[button setTitleColor:[UIColor colorWithRed:0.8745 green:0.0784 blue:0.0745 alpha:1.0000] forState:UIControlStateNormal];
[button setTitle:parameters[0][@"placeholder"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(presentActionSheet:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = button;
return cell;
}
[parameters enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
obj = parameters[indexPath.row];
cell.textLabel.text = obj[@"placeholder"];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 5, cell.frame.size.width-190, 35)];
textField.textAlignment = NSTextAlignmentRight;
textField.tag = idx;
textField.delegate = self;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.placeholder = obj[@"placeholder"];
textField.text = obj[@"value"];
if ([obj[@"type"] isEqualToString:@"Number"]) {
[textField setKeyboardType:UIKeyboardTypeNumberPad];
} else if ([obj[@"type"] isEqualToString:@"String"]) {
} else {
textField.keyboardType = UIKeyboardTypeDefault;
}
cell.accessoryView = textField;
}];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0 && self.shortcutArray.count == 0) {
[self presentViewController:[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"NewAction"] animated:YES completion:nil];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)presentActionSheet:(UIButton *)button {
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
// TODO: load dynamically items
[actionSheet addAction:[UIAlertAction actionWithTitle:@"App" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[actionSheet dismissViewControllerAnimated:YES completion:nil];
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[actionSheet dismissViewControllerAnimated:YES completion:nil];
}]];
UIPopoverPresentationController *popOver = [actionSheet popoverPresentationController];
popOver.sourceView = button;
popOver.sourceRect = button.bounds;
[self presentViewController:actionSheet animated:YES completion:nil];
}
# pragma UITextField
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
if (textField.tag == -1) {
// Save title at first index
NSMutableDictionary *mutableDictionary = [self.shortcutArray[0] mutableCopy];
[mutableDictionary setObject:textField.text forKey:@"Title"];
self.shortcutArray[0] = [mutableDictionary mutableCopy];
} else if (textField.tag == -2) {
// Custom action
NSMutableDictionary *mutableDictionary = [self.shortcutArray[2] mutableCopy];
[mutableDictionary setObject:textField.text forKey:@"URL"];
self.shortcutArray[2] = [mutableDictionary mutableCopy];
} else {
// Save parameters
NSMutableArray *arrayOfParameters = [self.shortcutArray[3] mutableCopy];
NSMutableDictionary *dictionaryOfParameter = [arrayOfParameters[textField.tag] mutableCopy];
NSUInteger index = [arrayOfParameters indexOfObject:dictionaryOfParameter];
[dictionaryOfParameter setValue:textField.text forKey:@"value"];
[arrayOfParameters setObject:dictionaryOfParameter atIndexedSubscript:index];
self.shortcutArray[3] = [arrayOfParameters mutableCopy];
}
}
@end