-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvoicesTableViewController.m
148 lines (117 loc) · 4.95 KB
/
InvoicesTableViewController.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
//
// InvoicesTableViewController.m
// TrackandBill_iOSversion
//
// Created by William Seaman on 2/15/15.
// Copyright (c) 2015 Loudsoftware. All rights reserved.
//
#import "InvoicesTableViewController.h"
#import "AppDelegate.h"
#import "Model.h"
#import "InvoiceTableViewCell.h"
#import "Invoice+CoreDataClass.h"
#import "InvoiceTableViewController.h"
#define kTableRowHeight 80
@interface InvoicesTableViewController (){
AppDelegate* _app;
NSManagedObjectContext* _context;
NSMutableArray* _data;
NSDateFormatter* _df;
}
@end
@implementation InvoicesTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Set the title of the navigation item
[[self navigationItem] setTitle:NSLocalizedString(@"invoices", nil)];
[self.navigationController.navigationBar setBarTintColor:[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:NavBarImage]]];
_df = [[NSDateFormatter alloc] init];
[_df setDateFormat:@"MM/dd/yyyy"];
_app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
_context = _app.persistentContainer.viewContext;
}
- (void)viewWillAppear:(BOOL)animated {
[self fetchData];
}
- (void)fetchData {
// Fetch data from persistent data store;
_data = [Model dataForEntity:@"Invoice" andSortKey:@"number"];
[[self tableView] reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _data.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return kTableRowHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"InvoiceCell";
InvoiceTableViewCell *cell = (InvoiceTableViewCell *)[tableView
dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"InvoiceTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
Invoice* inv = (Invoice*)[_data objectAtIndex:indexPath.row];
[cell.invoiceNumber setText:[NSString stringWithFormat:@"#%lld", inv.number]];
[cell.invoiceDate setText:[_df stringFromDate:inv.date]];
[cell.invoiceClient setText:inv.client_name];
[cell.invoiceProject setText:inv.project_name];
return cell;
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath
*)indexPath {
// Return NO if you do not 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 {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
UIAlertController *alert = [UIAlertController
alertControllerWithTitle:NSLocalizedString(@"really_delete_title", nil)
message:NSLocalizedString(@"really_delete_message", nil)
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *remove = [UIAlertAction
actionWithTitle:NSLocalizedString(@"yes", nil)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
//remove invoice
[_context deleteObject:[_data objectAtIndex:indexPath.row]];
[_app saveContext];
[self fetchData];
[alert dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:NSLocalizedString(@"cancel", nil)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[alert dismissViewControllerAnimated:YES completion:nil];
//do nothing
}];
[alert addAction:remove];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Invoice* inv = (Invoice*)[_data objectAtIndex:indexPath.row];
InvoiceTableViewController *invoiceVC = [[InvoiceTableViewController alloc] initWithNibName:@"InvoiceTableViewController" bundle:nil];
invoiceVC.selectedProject = inv.projects;
invoiceVC.isArchive = YES;
[self.navigationController pushViewController:invoiceVC animated:YES];
}
@end