-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceSave.m
More file actions
189 lines (140 loc) · 6.36 KB
/
ServiceSave.m
File metadata and controls
189 lines (140 loc) · 6.36 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
//
// ServiceSave.m
// Family Farm
//
// Created by Axel Trajano on 1/25/14.
// Copyright (c) 2014 Axel S. Trajano. All rights reserved.
//
#import "ServiceSave.h"
#import "PaymentType.h"
@interface ServiceSave ()
@property (strong, nonatomic) NSManagedObjectContext * context;
@property (copy) CallBack callback;
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *baseLandField;
@property (weak, nonatomic) IBOutlet UITextField *baseCostField;
@property (weak, nonatomic) IBOutlet UITextField *workerPercentageField;
@property (weak, nonatomic) IBOutlet UITextField *unitOfLandField;
@property (weak, nonatomic) IBOutlet UIButton *unitOfCostField;
@property (strong, nonatomic) PaymentType * paymentType;
@property (strong, nonatomic) NSArray * paymentTypes;
@property (strong, nonatomic) NSMutableArray * listOfCostTypes;
@property (strong, nonatomic) UIActionSheet * listUnitOfCost;
- (IBAction)saveService:(UIBarButtonItem *)sender;
- (IBAction)popUnitOfCost:(UIButton *)sender;
@end
@implementation ServiceSave
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_context = [[[UIApplication sharedApplication] delegate] performSelector:@selector(managedObjectContext) withObject:nil];
NSEntityDescription * entity = [NSEntityDescription entityForName:[[PaymentType class] description] inManagedObjectContext:_context];
NSFetchRequest * fetchRequest = [NSFetchRequest new];
[fetchRequest setEntity:entity];
_paymentTypes = [_context executeFetchRequest:fetchRequest error:nil];
if (_service) {
[_nameField setText:_service.name];
[_baseLandField setText:[_service.baseLandArea stringValue]];
[_unitOfLandField setText:[_service unitOfLand]];
[_baseCostField setText:[_service.baseCost stringValue]];
[_unitOfCostField setTitle:[_service unitOfCost] forState:UIControlStateNormal];
[_workerPercentageField setText:[_service.defaultPercentage stringValue]];
} else {
_paymentType = [_paymentTypes objectAtIndex:0];
[_unitOfCostField setTitle:_paymentType.name forState:UIControlStateNormal];
}
_listOfCostTypes = [NSMutableArray new];
_listUnitOfCost = [[UIActionSheet alloc] initWithTitle:@"Unit:" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
for (PaymentType * paymentType in _paymentTypes) {
[_listOfCostTypes addObject:paymentType.name];
[_listUnitOfCost addButtonWithTitle:paymentType.name];
}
[_listOfCostTypes addObject:@"Cancel"];
[_listUnitOfCost addButtonWithTitle:@"Cancel"];
[_listUnitOfCost setCancelButtonIndex:_listOfCostTypes.count];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)saveService:(UIBarButtonItem *)sender {
[self resignFirstResponder];
if (!_service) {
_service = [NSEntityDescription insertNewObjectForEntityForName:[[Service class] description]inManagedObjectContext:_context];
}
NSString * name = [_nameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
UIAlertView * alert;
NSMutableString * message = [NSMutableString new];
if ([_nameField isEmptyOnTrimWhiteSpaceAndNewLineCharacters:YES]) {
[message appendString:@"Name"];
}
if ([_baseLandField isEmptyOnTrimWhiteSpaceAndNewLineCharacters:YES]) {
[message appendString:@"\nBase Hectare"];
}
if ([_baseCostField isEmptyOnTrimWhiteSpaceAndNewLineCharacters:YES]) {
[message appendString:@"\nBase Cost"];
}
if ([_unitOfLandField isEmptyOnTrimWhiteSpaceAndNewLineCharacters:YES]) {
[message appendString:@"\nUnit Of Land"];
}
if ([_workerPercentageField isEmptyOnTrimWhiteSpaceAndNewLineCharacters:YES]) {
[message appendString:@"\nWorker Percentage"];
}
if (message.length > 1) {
alert = [[UIAlertView alloc] initWithTitle:@"Please provide the following:" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
} else {
NSNumber * baseLandArea = [NSNumber numberWithDouble:[_baseLandField.text doubleValue]];
NSDecimalNumber * baseCost = [NSDecimalNumber decimalNumberWithString:_baseCostField.text];
NSNumber * defaultPercentage = [NSNumber numberWithDouble:[_workerPercentageField.text doubleValue]];
[_service setName:name];
[_service setBaseLandArea:baseLandArea];
[_service setUnitOfLand:[_unitOfLandField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
[_service setBaseCost:baseCost];
[_service setUnitOfCost:[[_unitOfCostField titleLabel] text]];
[_service setDefaultPercentage:defaultPercentage];
NSError * error;
if([_context save:&error]){
if (_callback) {
_callback();
}
}else{
NSLog(@"Error : %@", error);
}
[self.navigationController popViewControllerAnimated:YES];
}
}
- (IBAction)popUnitOfCost:(UIButton *)sender {
if (![_listUnitOfCost isVisible]) {
[_listUnitOfCost showFromRect:sender.bounds inView:sender animated:YES];
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
[_unitOfCostField setTitle:[[_paymentTypes objectAtIndex:buttonIndex] name] forState:UIControlStateNormal];
}
- (void)callback:(CallBack)callback {
self.callback = callback;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == _nameField) {
[_baseLandField becomeFirstResponder];
} else if (textField == _baseLandField) {
[_baseCostField becomeFirstResponder];
} else if (textField == _baseLandField) {
[_workerPercentageField becomeFirstResponder];
} else {
[textField resignFirstResponder];
}
return YES;
}
@end