-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddDrugsViewController.m
155 lines (117 loc) · 5.74 KB
/
AddDrugsViewController.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
//
// AddDrugsViewController.m
// FinalProject
//
// Created by Xun on 12/4/16.
// Copyright © 2016 Xun. All rights reserved.
//
#import "AddDrugsViewController.h"
#import "AppDelegate.h"
@interface AddDrugsViewController ()
@end
@implementation AddDrugsViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)pickPhoto:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if((UIButton *) sender == self.pickBtn){
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}else{
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[self presentViewController:picker animated:YES completion:nil];
}
// to set and show the image
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
[[picker presentingViewController] dismissViewControllerAnimated:YES completion:nil];
UIImage* originalImage = nil;
originalImage = [info objectForKey:UIImagePickerControllerEditedImage];
if(originalImage==nil)
{
originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
}
if(originalImage==nil)
{
originalImage = [info objectForKey:UIImagePickerControllerCropRect];
}
// to show the image
self.image.image = originalImage;
}
- (IBAction)complete:(id)sender {
NSString* drugName = self.drugNametf.text;
NSString* drugPrice = self.drugPricetf.text;
// to create drug name alert
if([drugName length] == 0){
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Alert"
message:@"Please type a drug name"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
return;
}
// to create drug price alert
if([drugPrice length] == 0){
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Alert"
message:@"Please type a drug price"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
return;
}
// to create drug image alert
if(self.image == nil){
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Alert"
message:@"Please select an image of the drug"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
return;
}
// to add a dollar sign
drugPrice = [drugPrice stringByAppendingString:@"$"];
// Core Data
AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext* context = appDelegate.persistentContainer.viewContext;
NSManagedObject* drugEntity = [NSEntityDescription insertNewObjectForEntityForName:@"DrugEntity" inManagedObjectContext:context];
[drugEntity setValue:drugName forKey:@"name"];
[drugEntity setValue:drugPrice forKey:@"price"];
NSData *imageData = UIImagePNGRepresentation(self.image.image);
[drugEntity setValue:imageData forKey:@"image"];
NSError* error;
if(![context save:&error]){
NSLog(@"failed to add");
}
self.drugNametf.enabled = false;
self.drugPricetf.enabled = false;
self.pickBtn.enabled = false;
self.completeBtn.enabled = false;
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Success"
message:@"You have added the drug successfully"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
@end