-
Notifications
You must be signed in to change notification settings - Fork 0
/
notificationsVC.m
463 lines (375 loc) · 17.1 KB
/
notificationsVC.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//
// notificationsVC.m
// FeedIoT
//
// Created by Robert on 4/1/16.
// Copyright © 2016 Colin Eberhardt. All rights reserved.
//
#import "notificationsVC.h"
#import "AppDelegate.h"
#import "FirstViewController.h"
#import "TextFieldValidator.h"
#import "httpVC.h"
#import "emailCellTableViewCell.h"
#import "btSimplePopUp.h"
@implementation notificationsVC
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
@synthesize mqtt,notis,IFTTT,domain,bffIcon,exception,emailTable,editab;
-(void)blurScreen
{
CGRect screenSize = [UIScreen mainScreen].bounds;
UIImage *screenShot = [self.view screenshot];
UIImage *blurImage = [screenShot blurredImageWithRadius:10.5 iterations:2 tintColor:nil];
backGroundBlurr = [[UIImageView alloc]initWithImage:blurImage];
backGroundBlurr.frame = CGRectMake(0, 0, screenSize.size.width, screenSize.size.height);
[self.view addSubview:backGroundBlurr];
}
-(void)showErrorMessage
{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"TimeOut"
message:@"Maybe out of range or off."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[backGroundBlurr removeFromSuperview];
// [self performSegueWithIdentifier:@"doneEditVC" sender:self];
}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
-(void)showOkMessage:(NSString *)title
{
[self blurScreen];
UIAlertController* alert = [UIAlertController alertControllerWithTitle:title
message:[NSString stringWithFormat:@"Confirmed by %@",[appDelegate.workingBFF valueForKey:@"bffName"]]
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
// [self performSegueWithIdentifier:@"doneEditVC" sender:self];
[backGroundBlurr removeFromSuperview];
}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
-(void)workingIcon
{
UIImage *licon;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *final=[NSString stringWithFormat:@"%@.png",[appDelegate.workingBFF valueForKey:@"bffName"]];
NSString *final=[NSString stringWithFormat:@"%@.txt",[appDelegate.workingBFF valueForKey:@"bffName"]];
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:final];
licon=[UIImage imageWithContentsOfFile:filePath];
if (licon==NULL)
licon = [UIImage imageNamed:@"camera"];//need a photo
bffIcon.image=licon;
}
- (IBAction)pickerAction:(UIDatePicker*)sender
{
changef=true;
}
-(IBAction)regresa:(id)sender
{
if (changef)
{
[appDelegate.workingBFF setValue:[domain.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] forKey:@"bffDomain"];
[appDelegate.workingBFF setValue:[mqtt.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] forKey:@"bffMQTT"];
// int excep=exception.isOn;
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSError *error;
if(![context save:&error])
{
NSLog(@"Save error Notifications %@",error);
return;//if we cant save it return and dont send anything toi the esp8266
}
NSString *stIF=[NSString stringWithFormat:@"%@",[IFTTT.text isEqualToString:@""]?@"%20":IFTTT.text];//update server
NSString *stMQ=[NSString stringWithFormat:@"%@",[mqtt.text isEqualToString:@""]?@"%20":mqtt.text];
mis=[NSString stringWithFormat:@"notifications?mqtt=%@&uid=%@&domain=%@",stMQ,[[NSUserDefaults standardUserDefaults] objectForKey:@"bffUID"],stIF];//multiple arguments
int reply=[comm lsender:mis andAnswer:NULL andTimeOut:[[[NSUserDefaults standardUserDefaults]objectForKey:@"txTimeOut"] intValue] vcController:self];
if (!reply)
[self showErrorMessage];
else
{
if(oldtrans==0 && (int)notis.selectedSegmentIndex==1)
[appDelegate startTelegramService:[appDelegate.workingBFF valueForKey:@"bffMQTT"] withPort:@"1883"]; //connect to MQTT server
[self performSegueWithIdentifier:@"notiRegresa" sender:self];
}
}
else
[self performSegueWithIdentifier:@"notiRegresa" sender:self];
}
-(IBAction)cancel:(id)sender{
// [appDelegate.emailsArray removeAllObjects];
// appDelegate.emailsArray =[copyArray mutableCopy];
[self performSegueWithIdentifier:@"notiRegresa" sender:self];
}
-(IBAction)editingBegin:(UITextField*)sender{
copyemail=[sender.text mutableCopy];
// NSLog(@"To edit address %@",copyemail);
}
-(IBAction)editingEnded:(UITextField*)sender{
[sender resignFirstResponder];
if(sender==mqtt || sender==domain || sender==IFTTT) return;
//Get the cell to access fields inside
NSIndexPath *tempIndexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
emailCellTableViewCell *tempCell = [emailTable cellForRowAtIndexPath:tempIndexPath];
int lexception=tempCell.rule.isOn;
NSString *trimmed=[tempCell.emailAddress.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *trim2=[tempCell.emailName.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSManagedObject *cual=[appDelegate.emailsArray objectAtIndex:sender.tag];
[cual setValue:trimmed forKey:@"address"];
[cual setValue:trim2 forKey:@"emailName"];
changef=YES;
mis=[NSString stringWithFormat:@"changeEmail?address=%@&pos=%d&ex=%d&name=%@",trimmed,(int)sender.tag,lexception,trim2];//multiple arguments
int reply=[comm lsender:mis andAnswer:NULL andTimeOut:[[[NSUserDefaults standardUserDefaults]objectForKey:@"txTimeOut"] intValue] vcController:self];
if (!reply)
[self showErrorMessage];
// else
// [self showOkMessage:@"Email changed"];
}
-(IBAction)editingChange:(UITextField*)sender{
changef=true;
return;
if(sender ==domain || sender==mqtt) return;
NSManagedObject *matches = nil;
matches = [appDelegate.emailsArray objectAtIndex:sender.tag];
[matches setValue:sender.text forKey:@"address"];
mis=[NSString stringWithFormat:@"email?address=%@&pos=%ld",sender.text,sender.tag];//multiple arguments
// NSLog(@"send feeder %@",mis);
// int reply=[comm lsender:mis andAnswer:answer andTimeOut:2.0 vcController:self];
// if (!reply)
// [self showErrorMessage];
// else
// [self showOkMessage];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFieldRect =
[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect =
[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator =
midline - viewRect.origin.y
- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
* viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
changef=YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (IBAction)segmentedAction:(UISegmentedControl*)sender
{
changef=true;//touched now dirty
}
- (IBAction)exceptionChange:(UISwitch*)sender
{
changef=true;
NSManagedObject *matches = nil;
matches = [appDelegate.emailsArray objectAtIndex:sender.tag];
[matches setValue:[NSNumber numberWithInt:sender.isOn] forKey:@"rule"];
mis=[NSString stringWithFormat:@"changeEmail?address=%@&pos=%d&ex=%d",[matches valueForKey:@"address"],(int)sender.tag,sender.isOn];//multiple arguments
int reply=[comm lsender:mis andAnswer:NULL andTimeOut:[[[NSUserDefaults standardUserDefaults]objectForKey:@"txTimeOut"] intValue] vcController:self];
if (!reply)
[self showErrorMessage];
// else
// [self showOkMessage:@"Email changed"];
}
-(void) checkLogin
{
if (!appDelegate.passwordf)
{
// NSLog(@"Need to get password again");
[self performSegueWithIdentifier:@"getPassword" sender:self];
}
}
-(IBAction)transportMode:(id)sender
{
// if(![mqtt.text isEqualToString:@""])
// {
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:(int)notis.selectedSegmentIndex] forKey:@"transport"];
[appDelegate.workingBFF setValue:[NSNumber numberWithInt:(int)notis.selectedSegmentIndex] forKey:@"bffLimbo"];
[[NSUserDefaults standardUserDefaults] synchronize];
changef=YES;
// }
}
-(void)viewDidLoad
{
int lnotis;
[super viewDidLoad];
comm=[httpVC new];
mqtt.isMandatory=NO;
domain.isMandatory=NO;
IFTTT.isMandatory=NO;
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSNumber *passw=[[NSUserDefaults standardUserDefaults]objectForKey:@"password"];
if (passw.integerValue>0)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkLogin)
name:UIApplicationWillEnterForegroundNotification object:nil];
answer=nil;
answer=[NSMutableString string];
[self workingIcon];
changef=false;
mqtt.text=[appDelegate.workingBFF valueForKey:@"bffMQTT"];// update server name
domain.text=[appDelegate.workingBFF valueForKey:@"bffDomain"];// domain name for Internet Access
IFTTT.text=@"feediot.co.nf";
NSNumber *transport=[appDelegate.workingBFF valueForKey:@"bffLimbo"];
oldtrans=transport.integerValue;
// [appDelegate.workingBFF setValue:[NSNumber numberWithInt:(int)notis.selectedSegmentIndex] forKey:@"bffLimbo"];
[notis setSelectedSegmentIndex:(long)transport.longValue];
/*
NSMutableIndexSet *mutableIndexSet = [[NSMutableIndexSet alloc] init];
for (int a=0;a<7;a++)
if (lnotis & (1<<a))
[mutableIndexSet addIndex:a];
[ notis setSelectedSegmentIndexes:mutableIndexSet];
*/
// if (IFTTT.text.length==0)
// [notis setEnabled:NO forSegmentAtIndex:2];
exception.on=lnotis & 0x8;
}
- (void)viewWillDisappear:(BOOL)animated { //Is used as a Save Options if anything was changed Instead of Buttons
[super viewWillDisappear:animated];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
if(appDelegate.emailsArray.count>0)
{
[context deleteObject:(NSManagedObject*)[appDelegate.emailsArray objectAtIndex:indexPath.row]];
[appDelegate.emailsArray removeObjectAtIndex:indexPath.row];
NSMutableSet *eset=[appDelegate.workingBFF mutableSetValueForKey:@"correos"];
// if (eset && eset.count>0)
// [eset removeObject:[appDelegate.emailsArray objectAtIndex:indexPath.row]];
// [appDelegate.workingBFF setValue:eset forKey:@"correos"];
NSError *error;
if(![context save:&error])
NSLog(@"Save error %@",error);
[emailTable reloadData];
mis=[NSString stringWithFormat:@"delEmail?pos=%d",indexPath.row];//multiple arguments
int reply=[comm lsender:mis andAnswer:NULL andTimeOut:[[[NSUserDefaults standardUserDefaults]objectForKey:@"txTimeOut"] intValue] vcController:self];
if (!reply)
[self showErrorMessage];
// else
// [self showOkMessage:@"Email Deleted"];
}
}
}
-(IBAction)editar:(UIButton *)sender {
if (!editf)
{
editab.tintColor=[UIColor redColor];
[emailTable setEditing:YES animated:YES];
}
else
{
editab.tintColor=[UIColor blueColor];
[emailTable setEditing:NO animated:YES];
}
editf=!editf;
}
-(IBAction)manageException:(id)sender
{
dirtyf=YES;
}
-(IBAction)adder:(id)sender
{
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSManagedObject *newEmail;
newEmail = [NSEntityDescription
insertNewObjectForEntityForName:@"Emails"
inManagedObjectContext:context];
[newEmail setValue:@"@change to IFTTT ID" forKey:@"address"];
[newEmail setValue:[NSNumber numberWithInt:1] forKey:@"rule"];
[newEmail setValue:[appDelegate.workingBFF valueForKey:@"bffName"] forKey:@"bffName"];
[newEmail setValue:@"Name" forKey:@"emailName"];
NSInteger randomNumber = arc4random() % (appDelegate.appColors.count -1);
[newEmail setValue:[appDelegate.appColors objectAtIndex:randomNumber] forKey:@"color"];
[appDelegate.emailsArray addObject:newEmail];
NSMutableSet *eset=[appDelegate.workingBFF mutableSetValueForKey:@"correos"];
[eset addObject:newEmail];
[appDelegate.workingBFF setValue:eset forKey:@"correos"];
[emailTable reloadData];
changef=YES; //make it save data permanently
mis=[NSString stringWithFormat:@"addEmail?address=%@&exp=1&name=%@",@"gmail.com",@"Name"];//multiple arguments
int reply=[comm lsender:mis andAnswer:NULL andTimeOut:[[[NSUserDefaults standardUserDefaults]objectForKey:@"txTimeOut"] intValue] vcController:self];
if (!reply)
[self showErrorMessage];
// else
// [self showOkMessage:@"Email Added"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return appDelegate.emailsArray.count;
}
#pragma mark - UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
emailCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"servingCell"];
NSManagedObject *matches = nil;
matches = [appDelegate.emailsArray objectAtIndex:indexPath.row];
cell.emailAddress.text=[matches valueForKey:@"address"];
cell.emailName.text=[matches valueForKey:@"emailName"];
cell.emailAddress.tag=indexPath.row;
cell.rule.tag=indexPath.row;
cell.rule.on=[[matches valueForKey:@"rule"]integerValue];
cell.emailAddress.textColor=[matches valueForKey:@"color"];
cell.emailName.textColor=[matches valueForKey:@"color"];
cell.rule.onTintColor = [matches valueForKey:@"color"];
cell.rule.tintColor = [matches valueForKey:@"color"];
cell.rule.on=[[matches valueForKey:@"rule"]integerValue];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//must pass data to next seque to edit
}
@end