-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebtDetailsViewController.m
executable file
·383 lines (306 loc) · 16.9 KB
/
DebtDetailsViewController.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
//
// UIViewController+DebtDetailsViewController.m
// RePay
//
// Created by Philip Olsson on 2014-12-08.
// Copyright (c) 2014 Philip Olsson. All rights reserved.
//
#import "DebtDetailsViewController.h"
#import <Parse/Parse.h>
#import <ParseFacebookUtils/PFFacebookUtils.h>
#import "Debt.h"
@interface DebtDetailsViewController ()
@end
@implementation DebtDetailsViewController
@synthesize debts = _debts;
@synthesize activityIndicator = _activityIndicator;
NSIndexPath *deleteIndex;
- (void)viewDidLoad {
self.navigationItem.title = @"Skulder";
//Set up buttons in the navigation bar
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
[infoButton addTarget:self action:@selector(showInfoView) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Skulder" style:self.navigationItem.backBarButtonItem.style target:nil action:nil];
_activityIndicator.hidden = YES;
/* NSDictionary *data = @{
@"alert": @"James commented on your photo!",
};*/
/*
PFQuery *pushQuery = [PFInstallation query];
[pushQuery whereKey:@"deviceType" equalTo:@"ios"];
// Send push notification to query
[PFPush sendPushMessageToQueryInBackground:pushQuery
withMessage:@"Hello World!"];*/
selectedRow = -1;
if (_debts == nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Ooops"
message:@"Något gick fel"
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"Dismiss", nil];
[alert show];
[self.navigationController popViewControllerAnimated:YES];
}else{
NSString* name;
NSInteger index = 0;
if([[_debts objectAtIndex:0] count] == 0){
index = 1;
}
//self.view.backgroundColor = [UIColor grayColor];
//_debtDetailsTableView.backgroundColor = [UIColor lightGrayColor];
self.view.backgroundColor = _debtDetailsTableView.backgroundColor;
//_debtDetailsTableView.backgroundColor = self.view.backgroundColor;
//NSLog( NSStringFromClass( [[_debts objectAtIndex:0]class] ));
//Find out the name to put as text in the cell
//If the debt objects toFbId string is not equal to the current user fbId we know that the text in the cell should be toName
//NSLog(@"Värde på index: %ld",(long)index);
if (![[[[_debts objectAtIndex:index] objectAtIndex:0] toFbId] isEqualToString:[[PFUser currentUser] objectForKey:@"fbId"]]) {
name = [[[_debts objectAtIndex:index] objectAtIndex:0] toName];
}else{ //Else we know that the text in the cell should be fromName
name = [[[_debts objectAtIndex:index] objectAtIndex:0] fromName];
}
_nameLabel.text = name;
//Calculate the differance in debts
[self calculateAmount];
[_debtDetailsTableView reloadData];
}
}
#pragma mark table view methods
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [[_debts objectAtIndex:0] count] + [[_debts objectAtIndex:1] count];
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = @"UITableViewDetailsCell";
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
// dequeue a table view cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID
forIndexPath:indexPath];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
}
UILabel *amount = (UILabel *)[cell viewWithTag:3];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yy-MM-dd"];
if(tableView == _debtDetailsTableView){
if (indexPath.row < [[_debts objectAtIndex:0] count]) {
//NSLog(@"indexPath.row : %ld",(long)indexPath.row);
NSDate *d = [[[_debts objectAtIndex:0] objectAtIndex:indexPath.row] createdAt];
//date.text = [formatter stringFromDate:d];
//message.text = [[[_debts objectAtIndex:0] objectAtIndex:indexPath.row] message];
cell.textLabel.text = [formatter stringFromDate:d];
cell.detailTextLabel.text = [[[_debts objectAtIndex:0] objectAtIndex:indexPath.row] message];
if ([cell.detailTextLabel.text isEqual:@""]) {
cell.detailTextLabel.text = @" ";
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
amount.text = [[[[_debts objectAtIndex:0] objectAtIndex:indexPath.row] amount] stringValue];
// amount.textColor = [UIColor colorWithRed:0.0f green:0.85f blue:0.0f alpha:1.0f];
//if the dept not is confirmed, set the text color to gray
if (![[[_debts objectAtIndex:0] objectAtIndex:indexPath.row] approved]) {
//date.textColor = [UIColor grayColor];
//message.textColor = [UIColor grayColor];
cell.textLabel.textColor = [UIColor grayColor];
cell.detailTextLabel.textColor = [UIColor grayColor];
amount.textColor = [UIColor grayColor];
}
else{
cell.textLabel.textColor = [UIColor colorWithRed:77.0/255.0 green:175.0/255.0 blue:231.0/255.0 alpha:1];
cell.detailTextLabel.textColor = [UIColor colorWithRed:77.0/255.0 green:175.0/255.0 blue:231.0/255.0 alpha:1];
amount.textColor = [UIColor colorWithRed:77.0/255.0 green:175.0/255.0 blue:231.0/255.0 alpha:1]; //[UIColor blackColor];
}
}else{
//NSLog(@"indexPath.row : %ld",(long)indexPath.row);
NSDate *d = [[[_debts objectAtIndex:1] objectAtIndex:(indexPath.row-[[_debts objectAtIndex:0] count])] createdAt];
// date.text = [formatter stringFromDate:d];
cell.textLabel.text = [formatter stringFromDate:d];
cell.detailTextLabel.text =[[[_debts objectAtIndex:1] objectAtIndex:(indexPath.row - [[_debts objectAtIndex:0] count])] message];
if ([cell.detailTextLabel.text isEqual:@""]) {
cell.detailTextLabel.text = @" ";
}
NSString *n = @"-";
amount.text = [n stringByAppendingString:[[[[_debts objectAtIndex:1] objectAtIndex:(indexPath.row - [[_debts objectAtIndex:0] count])] amount] stringValue]];
amount.textColor = [UIColor redColor];
//if the dept not is confirmed, set the text color to gray
if (![[[_debts objectAtIndex:1] objectAtIndex:(indexPath.row - [[_debts objectAtIndex:0] count])] approved]) {
//date.textColor = [UIColor grayColor];
//message.textColor = [UIColor grayColor];
cell.textLabel.textColor = [UIColor grayColor];
cell.detailTextLabel.textColor = [UIColor grayColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
//cell.backgroundColor =[UIColor cyanColor];
}
else{ //approved debts
//cell.backgroundColor = [UIColor yellowColor];
cell.textLabel.textColor = [UIColor colorWithRed:77.0/255.0 green:175.0/255.0 blue:231.0/255.0 alpha:1];
cell.detailTextLabel.textColor = [UIColor colorWithRed:77.0/255.0 green:175.0/255.0 blue:231.0/255.0 alpha:1];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
}
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//NSLog(@"TRYCKER PÅ CELL: %d", indexPath.row);
NSInteger index = indexPath.row - [[_debts objectAtIndex:0] count];
if ((indexPath.row >= [[_debts objectAtIndex:0] count]) && ![[[_debts objectAtIndex:1] objectAtIndex:index] approved]) {
UIAlertView *confirmDebtAlert = [[UIAlertView alloc] initWithTitle:@"Verifiera skuld"
message:@"Vill du godkänna skulden?"
delegate:self
cancelButtonTitle:@"Avbryt"
otherButtonTitles:@"Godkänn", nil];
selectedRow = indexPath.row;
/*NSLog(@"Index path . row %ld: ", (long)indexPath.row);
NSLog(@"Index %ld: ", (long)index);
NSLog(@"Antal element här: %ld", [[_debts objectAtIndex:1] count]);
NSLog(@"CHECKSUMMA: %@", [[[_debts objectAtIndex:1] objectAtIndex:index] amount]);
*/
[confirmDebtAlert show];
}
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
deleteIndex = indexPath;
UIAlertView *deleteDebt = [[UIAlertView alloc] initWithTitle:@"Ta bort skuld"
message:@"Vill du verkligen ta bort skulden?"
delegate:self
cancelButtonTitle:@"Avbryt"
otherButtonTitles:@"Ta bort", nil];
[deleteDebt show];
}
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"Ta bort";
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
if (indexPath.row < [[_debts objectAtIndex:0] count]) {
return YES;
}
return NO;
}
- (void) deleteDebt {
_activityIndicator.hidden = NO;
[_activityIndicator startAnimating];
_debtDetailsTableView.userInteractionEnabled = NO;
if (deleteIndex.row < [[_debts objectAtIndex:0] count]) {
//NSLog(@"Detta object:%@",[[[_debts objectAtIndex:0] objectAtIndex:deleteIndex.row] toName]);
NSString *objId = [[[_debts objectAtIndex:0] objectAtIndex:deleteIndex.row] objectId];
PFObject *object = [PFObject objectWithoutDataWithClassName:@"Debts"
objectId:objId];
[object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {//succeeded to delete the object in the data base
[[_debts objectAtIndex:0] removeObjectAtIndex:deleteIndex.row];
[_debtDetailsTableView deleteRowsAtIndexPaths:@[deleteIndex] withRowAnimation:UITableViewRowAnimationFade];
[self calculateAmount];
}
else { //failed to delete object in data base
UIAlertView *deleteErr = [[UIAlertView alloc]
initWithTitle:@"Fel uppstod" message:@"Ett fel uppstod, kontrollera din internet anslutning eller försök igen senare." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[deleteErr show];
}
_debtDetailsTableView.userInteractionEnabled = YES;
_activityIndicator.hidden = YES;
[_activityIndicator stopAnimating];
}];
}else{
NSString *objId = [[[_debts objectAtIndex:1] objectAtIndex:(deleteIndex.row - [[_debts objectAtIndex:0] count])] objectId];
PFObject *object = [PFObject objectWithoutDataWithClassName:@"Debts"
objectId:objId];
[object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
[[_debts objectAtIndex:1] removeObjectAtIndex:(deleteIndex.row - [[_debts objectAtIndex:0] count])];
[_debtDetailsTableView deleteRowsAtIndexPaths:@[deleteIndex] withRowAnimation:UITableViewRowAnimationFade];
[self calculateAmount];
}
else {
UIAlertView *deleteErr = [[UIAlertView alloc]
initWithTitle:@"Fel uppstod" message:@"Ett fel uppstod, kontrollera din internet anslutning eller försök igen senare." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[deleteErr show];
}
_activityIndicator.hidden = YES;
[_activityIndicator stopAnimating];
}];
}
}
- (void) calculateAmount{
NSNumber* amount = @(0);
for (int i = 0; i < [[_debts objectAtIndex:0] count]; i++) {
if ([[[_debts objectAtIndex:0] objectAtIndex:i] approved]) {
// NSLog(@"Amount %@", [[[_debts objectAtIndex:0] objectAtIndex:i] amount] );
amount = [NSNumber numberWithFloat:([[[[_debts objectAtIndex:0] objectAtIndex:i] amount] floatValue] + [amount floatValue])];
}
}
for (int i = 0; i < [[_debts objectAtIndex:1] count]; i++) {
//NSLog(@"Amount %@", [[[_debts objectAtIndex:1] objectAtIndex:i] amount]);
if ([[[_debts objectAtIndex:1] objectAtIndex:i] approved]) {
amount = [NSNumber numberWithFloat:([amount floatValue]-[[[[_debts objectAtIndex:1] objectAtIndex:i] amount] floatValue]) ];
}
}
_differanceLabel.text = [amount stringValue];
}
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([alertView.title isEqual:@"Verifiera skuld"]) {
if (buttonIndex == 0) { //Avbryt (Cancel)
selectedRow = -1;
}
else if(buttonIndex == 1){ //Godkänn (approve)
[self approveDebt];
}
}
else if ([alertView.title isEqualToString:@"Ta bort skuld"]){
if (buttonIndex == 0) { //Avbryt (Cancel)
}
else if(buttonIndex == 1){ //Godkänn (approve)
[self deleteDebt];
}
}
}
-(void) approveDebt{
if (selectedRow != -1) {
NSInteger index = selectedRow - [[_debts objectAtIndex:0] count];
PFQuery *query = [PFQuery queryWithClassName:@"Debts"];
Debt *d = [[_debts objectAtIndex:1] objectAtIndex:index];
// Retrieve the object by id
_activityIndicator.hidden = NO;
[_activityIndicator startAnimating];
[query getObjectInBackgroundWithId:d.objectId block:^(PFObject *object, NSError *error) {
if (!error) { //No error when loading the specified debt from the data base
object[@"approved"] = @YES;
//Now save the debt to the data base
[object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
//Succeeded to save the debt
if (succeeded) {
_activityIndicator.hidden = YES;
[_activityIndicator stopAnimating];
d.approved = YES;
[_debtDetailsTableView reloadData];
[self calculateAmount];
}
else { //Failed to save the debt to the database.
_activityIndicator.hidden = YES;
[_activityIndicator stopAnimating];
UIAlertView *confirm = [[UIAlertView alloc]
initWithTitle:@"Fel uppstod" message:@"Ett fel uppstod, kontrollera din internet anslutning eller försök igen senare." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[confirm show];
}
}];
}else {
_activityIndicator.hidden = YES;
[_activityIndicator stopAnimating];
UIAlertView *confirm = [[UIAlertView alloc]
initWithTitle:@"Fel uppstod" message:@"Ett fel uppstod, kontrollera din internet anslutning eller försök igen senare." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[confirm show];
}
}];
}
selectedRow = -1;
}
-(IBAction)showInfoView {
[self performSegueWithIdentifier:@"toInfoDebtDetails" sender:self];
}
@end