-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBBMainViewController.m
145 lines (121 loc) · 5.2 KB
/
BBMainViewController.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
//
// BBMainViewController.m
// iCare2
//
// Created by billy bray on 8/25/14.
// Copyright (c) 2014 Spartan Systems Inc. All rights reserved.
//
#import "BBMainViewController.h"
#import "Practitioner.h"
#import "Patient.h"
#import "BPBAppDelegate.H"
#import "LoginViewController.h"
#import "BBPatientFormsViewController.h"
#import "BBNewPatientViewController.h"
#import "EditUserViewController.h"
@interface BBMainViewController () <UITableViewDataSource, UITableViewDelegate, LoginViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UITableView *patientsTableView;
@property BOOL isLoggedIn;
@property NSString *userID;
@property NSArray *patientsArray;
@property (nonatomic, strong) Practitioner *practitioner;
@property (nonatomic, strong) NSMutableArray *headerArray;
@end
@implementation BBMainViewController
-(void)viewDidLoad
{
// CAGradientLayer *gradient = [CAGradientLayer layer];
// gradient.frame = self.view.bounds;
// gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor colorWithRed:91.0/255.0 green:196.0/255.0 blue:105.0/255.0 alpha:1.0] CGColor], nil];
// [self.view.layer insertSublayer:gradient atIndex:0];
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (!self.isLoggedIn) {
LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
loginViewController.delegate = self;
[self presentViewController:loginViewController animated:YES completion:nil];
} else {
[self refreshData];
}
}
-(void)setLoggedIn:(BOOL)loggedIn withPractionerID:(Practitioner *)practitioner
{
self.isLoggedIn = loggedIn;
self.practitioner = practitioner;
}
-(void)refreshData
{
self.patientsArray = [self.practitioner.patients allObjects];
[self.patientsTableView reloadData];
}
#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
{
if ([[segue identifier] isEqualToString:@"NewPatientSegue"]) {
BBNewPatientViewController *vc = [segue destinationViewController];
vc.practitioner = self.practitioner;
vc.mainViewController = self;
}
if ([[segue identifier] isEqualToString:@"PatientFormsSegue"]) {
BBPatientFormsViewController *vc = [segue destinationViewController];
CGPoint cellPosition = [sender convertPoint:CGPointZero toView:self.patientsTableView];
NSIndexPath *indexPath = [self.patientsTableView indexPathForRowAtPoint:cellPosition];
Patient *p = [[self.headerArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
NSLog(@"Patient name:%@", p.firstName);
vc.patient = p;
}
if ([[segue identifier] isEqualToString:@"Edit User Segue"]) {
EditUserViewController *vc = [segue destinationViewController];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
vc.modalPresentationStyle = UIModalPresentationFormSheet;
vc.practitioner = self.practitioner;
}
}
#pragma mark - TableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if ([self.practitioner.patients count] == 0){
return 0;
}
self.headerArray = [[NSMutableArray alloc] init];
NSMutableArray *sortedPatients = [[NSMutableArray alloc] initWithArray:[self.practitioner.patients allObjects]];
sortedPatients = (NSMutableArray*)[sortedPatients sortedArrayUsingComparator: ^(Patient *p1, Patient *p2) {
return [p1.lastName compare:p2.lastName options:NSCaseInsensitiveSearch];
}];
int j=0;
[self.headerArray addObject:[[NSMutableArray alloc] init]];
for (int i = 0; i < [sortedPatients count]; i++) {
Patient *currentPatient = [sortedPatients objectAtIndex:i];
[[self.headerArray objectAtIndex:j] addObject:currentPatient];
if ( i < [sortedPatients count] - 1 ){
Patient *nextPatient = [sortedPatients objectAtIndex:i+1];
if ([currentPatient.lastName characterAtIndex:0] !=
[nextPatient.lastName characterAtIndex:0]){
j++;
[self.headerArray addObject:[[NSMutableArray alloc] init]];
}
}
}
return self.headerArray.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
Patient *p = [[self.headerArray objectAtIndex:section] objectAtIndex:0];
return [NSString stringWithFormat:@"%c", [p.lastName characterAtIndex:0]];
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self.headerArray objectAtIndex:section] count];
}
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Patient *p = [[self.headerArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PatientCell"];
UILabel *nameLabel = (UILabel*)[cell.contentView viewWithTag:1];
[nameLabel setText:[NSString stringWithFormat:@"%@, %@", p.lastName, p.firstName]];
return cell;
}
@end