-
Notifications
You must be signed in to change notification settings - Fork 0
/
BPDetailViewController.m
258 lines (194 loc) · 7.8 KB
/
BPDetailViewController.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
//
// BPDetailViewController.m
// BPTracker
//
// Created by Robert Saccone on 4/27/14.
// Copyright (c) 2017 Robert Saccone. All rights reserved.
//
#import "BPDetailViewController.h"
#import "BloodPressureReading.h"
#import "BPReadingDetailViewController.h"
#import "BPReadingSelectionViewController.h"
@interface BPDetailViewController () <BPReadingDetailViewControllerDelegate>
- (void)addCompletionBlockToDictionary:(ReadingDismissedBlock)readingDismissedBlock forViewController:(BPReadingDetailViewController *)bpReadingDetailViewCtrlr;
- (ReadingDismissedBlock)removeCompletionBlockFromDictionary:(BPReadingDetailViewController *)bpReadingDetailViewCtrlr;
@property(nonatomic, assign) BOOL editingNewReading;
@property(nonatomic, strong) NSMutableDictionary *viewCtrlrToCompletionBlocks;
@end
@implementation BPDetailViewController
@synthesize currBPReadingDetailViewController;
@synthesize currBPReading;
@synthesize delegate;
- (id)init
{
self = [super initWithNibName:@"BPDetailViewController" bundle:nil];
if (self)
{
// Custom initialization
_editingNewReading = NO;
_viewCtrlrToCompletionBlocks = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - ReadingDismissedBlock Management
- (void)addCompletionBlockToDictionary:(ReadingDismissedBlock)readingDismissedBlock forViewController:(BPReadingDetailViewController *)bpReadingDetailViewCtrlr
{
ReadingDismissedBlock copy = [readingDismissedBlock copy];
[[self viewCtrlrToCompletionBlocks] setObject:copy forKey:[NSValue valueWithNonretainedObject:bpReadingDetailViewCtrlr]];
}
- (ReadingDismissedBlock)removeCompletionBlockFromDictionary:(BPReadingDetailViewController *)bpReadingDetailViewCtrlr
{
NSValue *key = [NSValue valueWithNonretainedObject:bpReadingDetailViewCtrlr];
ReadingDismissedBlock completionBlock = self.viewCtrlrToCompletionBlocks[key];
if (completionBlock != nil)
{
[self.viewCtrlrToCompletionBlocks removeObjectForKey:key];
}
return completionBlock;
}
#pragma mark - BPReadingSlectionViewController methods
- (void)selectedReading:(BloodPressureReading *)bpReading
completion:(ReadingDismissedBlock)readingDismissed;
{
BPReadingDetailViewController *bpReadingDetailViewCtrlr =
[[BPReadingDetailViewController alloc] init:bpReading newReading:NO setDefaultsFromReading:YES viewControllerDelegate:self];
if (readingDismissed != nil)
{
[self addCompletionBlockToDictionary:readingDismissed forViewController:bpReadingDetailViewCtrlr];
}
[bpReadingDetailViewCtrlr setHidesBottomBarWhenPushed:YES];
// Pop off the top leve reading, if any.
[self popTopLevelReading];
[self.navigationController pushViewController:bpReadingDetailViewCtrlr animated:NO];
}
- (void)editNewReading:(BloodPressureReading *)bpReading
completion:(ReadingDismissedBlock)readingDismissed;
{
if (!_editingNewReading && (bpReading != nil))
{
BPReadingDetailViewController *bpReadingDetailViewCtrlr =
[[BPReadingDetailViewController alloc] init:bpReading newReading:YES setDefaultsFromReading:NO viewControllerDelegate:self];
[bpReadingDetailViewCtrlr setHidesBottomBarWhenPushed:YES];
if (readingDismissed != nil)
{
[self addCompletionBlockToDictionary:readingDismissed forViewController:bpReadingDetailViewCtrlr];
}
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:bpReadingDetailViewCtrlr];
UINavigationController *topLevelNavigationController = self.navigationController;
topLevelNavigationController.definesPresentationContext = YES;
navController.modalPresentationStyle = UIModalPresentationCurrentContext; //UIModalPresentationPageSheet;
[topLevelNavigationController presentViewController:navController animated:YES completion:NULL];
self.editingNewReading = YES;
}
}
- (void)popTopLevelReading
{
BPReadingDetailViewController *topBPReadingDetailVC = self.currBPReadingDetailViewController;
if (topBPReadingDetailVC != nil)
{
// If working on editing a new reading than cancel edit mode.
// This will ensure that the readingDismissedBlock for the
// view controller will be cleaned up correctly.
if (topBPReadingDetailVC.newReading)
{
// Canceling edit mode will take care of cleaning up the
// blockentry in the
[topBPReadingDetailVC cancelEditMode];
}
else
{
(void)[self removeCompletionBlockFromDictionary:topBPReadingDetailVC];
}
}
[self.navigationController popToRootViewControllerAnimated:NO];
}
#pragma mark - BPReadingDetailViewControllerDegate implementation
- (void)modeChanged:(BOOL) editing
{
id<BPReadingSelectionViewControllerDelegate> del = self.delegate;
if (del != nil)
{
[del modeChanged:editing];
}
}
- (BOOL)doneUpdatingBloodPressureReading:(BPReadingDetailViewController *)viewController
bloodPressureReading:(BloodPressureReading *)reading
saved:(BOOL)saved
newReading:(BOOL)newReading
{
ReadingDismissedBlock readingDismissed = [self removeCompletionBlockFromDictionary:viewController];
if (readingDismissed != nil)
{
// Invoke the block.
readingDismissed(saved);
}
if (newReading)
{
[self dismissViewControllerAnimated:YES completion:NULL];
self.editingNewReading = NO;
return YES;
}
return NO;
}
#pragma mark - currBPReading Property
- (BloodPressureReading *)currBPReading
{
BPReadingDetailViewController *currDetailController = self.currBPReadingDetailViewController;
if (currDetailController != nil)
{
return currDetailController.bloodPressureReading;
}
return nil;
}
- (BPReadingDetailViewController *)currBPReadingDetailViewController
{
UIViewController *topVC = [self.navigationController topViewController];
if ((topVC != nil) && ([topVC isKindOfClass:[BPReadingDetailViewController class]]))
{
return (BPReadingDetailViewController *)topVC;
}
return nil;
}
#pragma mark - UISplitViewControllerDelegate
- (void)splitViewController:(UISplitViewController *)svc
popoverController:(UIPopoverController *)pc
willPresentViewController:(UIViewController *)aViewController
{
}
- (BOOL)splitViewController:(UISplitViewController *)svc
shouldHideViewController:(UIViewController *)vc
inOrientation:(UIInterfaceOrientation)orientation
{
return NO;
}
- (void)splitViewController:(UISplitViewController *)svc
willHideViewController:(UIViewController *)aViewController
withBarButtonItem:(UIBarButtonItem *)barButtonItem
forPopoverController:(UIPopoverController *)pc
{
barButtonItem.title = @"Readings";
// Place the bar button on the left side of the nav. item
self.navigationItem.leftBarButtonItem = barButtonItem;
}
- (void)splitViewController:(UISplitViewController *)svc
willShowViewController:(UIViewController *)aViewController
invalidatingBarButtonItem:(UIBarButtonItem *)button
{
// Remove the bar button item from the navigation item.
// Double check that it is correct even though we know it is.
if (button == self.navigationItem.leftBarButtonItem)
{
self.navigationItem.leftBarButtonItem = nil;
}
}
@end