Today's Menu  Portugal
journal and plan nutrition
T1HealthKitViewController.m
Go to the documentation of this file.
1 //
2 // T1HealthKitViewController.m
3 // TodaysMenu
4 //
5 // Created by Don Zeek on 11/23/16.
6 // Copyright © 2016 net.dzeek.y2015.ios.portfolio. All rights reserved.
7 //
8 
10 #import "Z5DataController.h"
11 
12 @interface T1HealthKitViewController ()
13 @property (nonatomic, strong) NSDateFormatter *mDateFormatter;
14 
15 @end
16 
17 @implementation T1HealthKitViewController
18 
19 NSMutableArray<NSDictionary *> *componentsAuthorityStatus;
20 NSMutableArray<NSDictionary *> *updatedAuthorityStatus;
21 NSMutableSet *needAuthorizationSet;
22 
23 - (void)viewDidLoad {
24  [super viewDidLoad];
25  // Do any additional setup after loading the view.
26  Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
27 
28  _mDateFormatter = [[NSDateFormatter alloc] init];
29  [_mDateFormatter setDateStyle:NSDateFormatterMediumStyle];
30  [_mDateFormatter setTimeStyle:NSDateFormatterNoStyle];
31 
32  // fetch, scale, and add a subview background image
33  UIImage *backgrdImage = [UIImage imageNamed:@"HealthKit.png"];
34  CGSize photoSize = [backgrdImage size];
35  CGRect totalFrame = self.view.frame;
36  float resizeRatio = photoSize.width / totalFrame.size.width;
37  UIImage *resizedImage = [UIImage imageWithCGImage:[backgrdImage CGImage]
38  scale:backgrdImage.scale * resizeRatio
39  orientation:(backgrdImage.imageOrientation) ];
40  UIImageView *backgrd = [[UIImageView alloc] initWithImage:resizedImage];
41  [self.view addSubview:backgrd];
42  // end - fetch, scale, and add a subview background image
43 
44  // data source and delegate connect
45  self.tableView.delegate = self;
46  self.tableView.dataSource = self;
47 
48  componentsAuthorityStatus = [[NSMutableArray alloc] init];
49  needAuthorizationSet = [[NSMutableSet alloc] init];
50  Boolean needsAdditionalAuthority = NO;
51  for (NSString *param in [localData establishNutritionParameters]) {
52  HKQuantityType *rowQuantityType = [HKObjectType quantityTypeForIdentifier:param];
53  [needAuthorizationSet addObject:rowQuantityType];
54  needsAdditionalAuthority = YES;
55 
56  NSString *shortName = [param substringFromIndex:31];
57  NSDictionary *authorityEntry = [[NSDictionary alloc] initWithObjectsAndKeys:shortName, [Z5LocalDataController componentNameKey], nil];
58  [componentsAuthorityStatus addObject:authorityEntry];
59  }
60  /*
61  * these lines cool well and try to get approval, however it keeps all future
62  * requests leaving the user picking the way through 'Health' app
63  - if (needsAdditionalAuthority) {
64  - [localData getAddedAuthorizationToShare:needAuthorizationSet forDelegate:self];
65  - } */
66 
67  for (NSString *param in [localData establishNutritionParameters]) {
68 
69  HKQuantityType *rowQuantityType = [HKObjectType quantityTypeForIdentifier:param];
70 
71  // get new authority info
72  if (rowQuantityType) {
73  NSNumber *shareOK = [localData checkTypeAuthorization:rowQuantityType];
74  NSLog(@"healthKitView.viewDidLoad: param: %@ shareOk: %@", param, shareOK);
75  }
76  }
77  [self.tableView reloadData];
78 }
79 
80 #pragma mark - UITableViewDataSource
81 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
82 {
83  Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
84  updatedAuthorityStatus = [localData annotateAuthority:componentsAuthorityStatus];
85  /*
86  * the updated-authority-status is an array of dictionaries with good authority
87  * status inside. Count how many different authority conditions there are.
88  */
89  NSArray *authorityConditionArray = [self authorityConditionSections];
90  NSInteger nSections = [authorityConditionArray count];
91  NSLog(@"healthKitView.numberOfSectionsInTableView: nSections: %ld", (long)nSections);
92  return nSections;
93 }
94 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
95 {
96  NSLog(@"healthKitView.numberOfSectionsInTableView");
97  NSDictionary *sectionDictionary = [[self authorityConditionSections] objectAtIndex:section];
98 
99  NSArray *sectionComponentCount = [sectionDictionary objectForKey:@"HKAuthority-status-types"];
100  NSInteger sectionCount = [sectionComponentCount count];
101  NSLog(@"healthKitView.numberOfSectionsInTableView: sectionCount: %ld", (long)sectionCount);
102  return sectionCount;
103 }
104 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
105 {
106 
107  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"health-kit-categories"];
108  if (cell == nil) {
109  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"health-kit-categories"];
110  // cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
111  }
112 
113  NSString *caption;
114  NSString *detailCaption;
115  NSDictionary *sectionDictionary = [[self authorityConditionSections] objectAtIndex:[indexPath section]];
116  NSArray *sectionItems = [sectionDictionary objectForKey:@"HKAuthority-status-types"];
117  if ([indexPath row] < [sectionItems count]) {
118  // caption = [sectionItems objectAtIndex:[indexPath row]];
119  // detailCaption = @"detail";
120 
121  NSDictionary *rowInfo = [sectionItems objectAtIndex:[indexPath row]];
122  caption = [rowInfo objectForKey:@"component-name"];
123  NSDate *authorityDate = [rowInfo objectForKey:@"component-authority-date"];
124  detailCaption = [_mDateFormatter stringFromDate:authorityDate];
125 
126  }
127 
128  [Z5DataController labelizeCell:cell withText:caption inColor:[UIColor brownColor] andDetail:detailCaption];
129 
130  return cell;
131 }
132 
133 // without this, runs
134 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
135 {
136  NSDictionary *sectionDictionary = [[self authorityConditionSections] objectAtIndex:section];
137  NSString *sectionName = [sectionDictionary objectForKey:@"HKAuthority-status-ident"];
138 
139  return sectionName;
140 }
141 #pragma mark - UITableVIewDelegate
142 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
143 {
144  // Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
145 
146  NSLog(@"HealthKitVIew.didSelectRow: section: %ld row: %ld\nset: %@", (long)[indexPath section], (long)[indexPath row], needAuthorizationSet);
147 
148  // modify the following to get only the component identified with the row
149  // this pops up the healthkei screen if any component (?) is unquestioned
150  // [localData getAddedAuthorizationToShare:needAuthorizationSet forDelegate:self];
151 
152 }
153 #pragma mark - Z5HealthConnectionProtocol
154 - (void) healthQuantityTypeData: (HKQuantityType *)quantityType success: (Boolean) flag
155 {
156 
157 }
159 {
160 
161 }
162 
169 - (NSArray<NSDictionary *> *)authorityConditionSections
170 {
171  int notDetermined = 0;
172  int itemDenied = 0;
173  int itemAuthorized = 0;
174  NSMutableArray<NSMutableDictionary *> *notDeterminedTypes = [[NSMutableArray alloc] init];
175  NSMutableArray<NSMutableDictionary *> *deniedTypes = [[NSMutableArray alloc] init];
176  NSMutableArray<NSMutableDictionary *> *allowedTypes = [[NSMutableArray alloc] init];
177  // AT THIS POINT I NEED THE LAST CHANGE TO THE COMPONENT AUTHORITY
178  for (NSDictionary *compo in updatedAuthorityStatus) {
179  NSNumber *authorityData = [compo objectForKey:[Z5LocalDataController componentAuthorityKey]];
180  NSString *nameData = [compo objectForKey:[Z5LocalDataController componentNameKey]];
181  NSDate *dateData = [compo objectForKey:[Z5LocalDataController componentAuthorityDateKey]];
182 
183  // in lieu of a string 'name', compose a dictionary with 'name' and 'date'
184  NSMutableDictionary *componentData = [[NSMutableDictionary alloc] init];
185  [componentData setObject:nameData forKey:@"component-name"];
186  [componentData setObject:dateData forKey:@"component-authority-date"];
187  // END - in lieu of a string 'name', compose a dictionary with 'name' and 'date'
188  HKAuthorizationStatus status = [authorityData integerValue];
189  switch (status) {
190  case HKAuthorizationStatusNotDetermined:
191  // [notDeterminedTypes addObject:nameData];
192  [notDeterminedTypes addObject:componentData];
193  notDetermined++;
194  break;
195  case HKAuthorizationStatusSharingDenied:
196  // [deniedTypes addObject:nameData];
197  [deniedTypes addObject:componentData];
198  itemDenied++;
199  break;
200  case HKAuthorizationStatusSharingAuthorized:
201  // [allowedTypes addObject:nameData];
202  [allowedTypes addObject:componentData];
203  itemAuthorized++;
204  break;
205  } // end switch
206  }
207  NSMutableArray<NSDictionary *> *ret = [[NSMutableArray alloc] initWithCapacity:3];
208  NSNumber *statusNumb;
209  if (0 < notDetermined)
210  {
211  statusNumb = [NSNumber numberWithInt:HKAuthorizationStatusNotDetermined];
212  // statusCount = [NSNumber numberWithInt:notDetermined];
213  NSArray *statusTypes = [[NSArray alloc] initWithArray:notDeterminedTypes];
214  NSDictionary *notDeterminedElement = [[NSDictionary alloc] initWithObjectsAndKeys:@"not deteermined", @"HKAuthority-status-ident", statusTypes, @"HKAuthority-status-types", nil];
215  [ret addObject:notDeterminedElement];
216  }
217  if (0 < itemDenied)
218  {
219  statusNumb = [NSNumber numberWithInt:HKAuthorizationStatusSharingDenied];
220  // statusCount = [NSNumber numberWithInt:notDetermined];
221  NSArray *statusTypes = [[NSArray alloc] initWithArray:deniedTypes];
222  NSDictionary *sharingDeniedElement = [[NSDictionary alloc] initWithObjectsAndKeys:@"denied", @"HKAuthority-status-ident", statusTypes, @"HKAuthority-status-types", nil];
223  [ret addObject:sharingDeniedElement];
224  }
225  if (0 < itemAuthorized)
226  {
227  statusNumb = [NSNumber numberWithInt:HKAuthorizationStatusSharingAuthorized];
228  // statusCount = [NSNumber numberWithInt:notDetermined];
229  NSArray *statusTypes = [[NSArray alloc] initWithArray:allowedTypes];
230  NSDictionary *sharingApprovedElement = [[NSDictionary alloc] initWithObjectsAndKeys:@"authorized", @"HKAuthority-status-ident", statusTypes, @"HKAuthority-status-types", nil];
231  [ret addObject:sharingApprovedElement];
232  }
233  return ret;
234 }
235 
236 
237 
239  [super didReceiveMemoryWarning];
240  // Dispose of any resources that can be recreated.
241 }
242 
243 /*
244 #pragma mark - Navigation
245 
246 // In a storyboard-based application, you will often want to do a little preparation before navigation
247 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
248  // Get the new view controller using [segue destinationViewController].
249  // Pass the selected object to the new view controller.
250 }
251 */
252 
253 @end
NSMutableSet * needAuthorizationSet
NSArray< NSDictionary *> * authorityConditionSections()
instancetype sharedInstance()
NSMutableArray< NSDictionary * > * componentsAuthorityStatus
NSDateFormatter * mDateFormatter
NSMutableArray< NSDictionary * > * updatedAuthorityStatus
NSNumber * checkTypeAuthorization:(HKQuantityType *quantityType)
Singleton interface to both core and remote data sources.
NSMutableArray * annotateAuthority:(NSArray *componentArray)
void labelizeCell:withText:inColor:andDetail:(UITableViewCell *targCell, [withText] NSString *txt, [inColor] UIColor *color, [andDetail] NSString *detailTxt)