Advertisement
Guest User

Untitled

a guest
May 30th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.94 KB | None | 0 0
  1. //
  2. // NewUserViewController.m
  3. // MyPharmacist
  4. //
  5. // Created by Matthew Faluotico on 2/18/15.
  6. // Copyright (c) 2015 MyPharmacy. All rights reserved.
  7. //
  8.  
  9. #import "ModifyUserViewController.h"
  10. #import "UIImageView+Letters.h"
  11. #import "PrescriptionCell.h"
  12. #import "ArchiveViewController.h"
  13. #import <MessageUI/MessageUI.h>
  14. #import <POP/pop.h>
  15.  
  16. @interface ModifyUserViewController () <UITextFieldDelegate, MFMailComposeViewControllerDelegate>
  17. @property NSDateFormatter *format;
  18. @end
  19.  
  20. NSInteger selph = 50; // names this 'selph' because I was foolish and tried to name it 'self'
  21. NSInteger fname = 51;
  22. NSInteger lname = 52;
  23. NSInteger relation = 53;
  24. NSInteger height = 54;
  25. NSInteger weight = 55;
  26. NSInteger allergy = 56;
  27. NSInteger birthday = 57;
  28. NSInteger pharm = 58;
  29. NSInteger pharmPhone = 59;
  30. NSInteger pharmAddress = 60;
  31. NSInteger pharmCity = 61;
  32. NSInteger doctor = 62;
  33.  
  34. @implementation ModifyUserViewController
  35.  
  36. - (void)viewDidAppear:(BOOL)animated {
  37. //Check if user provided, if yes loads detail, if no loads startFresh
  38.  
  39. self.tableView.allowsSelection = YES;
  40.  
  41. if (self.user) {
  42. [self load];
  43. } else {
  44. [self startFresh];
  45. }
  46.  
  47. }
  48.  
  49. - (void)viewDidLoad {
  50. [super viewDidLoad];
  51. // sets the title based on the user
  52.  
  53. self.format = [[NSDateFormatter alloc] init];
  54. self.format.dateFormat = @"MMM, dd, yyyy";
  55.  
  56. self.title = @"User Setup";
  57.  
  58. self.tableView.tableFooterView = [UIView new];
  59.  
  60. if (self.isMainUser) {
  61. UIBarButtonItem *save = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(save)];
  62. self.navigationItem.rightBarButtonItem = save;
  63. } else {
  64. self.navigationItem.hidesBackButton = YES;
  65. UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(cancel)];
  66. self.navigationItem.leftBarButtonItem = cancel;
  67. UIBarButtonItem *edit = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(edit)];
  68. self.navigationItem.rightBarButtonItem = edit;
  69.  
  70. }
  71. }
  72.  
  73. - (void)didReceiveMemoryWarning {
  74. [super didReceiveMemoryWarning];
  75. // Dispose of any resources that can be recreated.
  76. }
  77.  
  78. #pragma mark - Table view data source
  79.  
  80. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  81. return 5;
  82. }
  83.  
  84. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  85.  
  86. switch (section) {
  87. case 0:
  88. return 1;
  89. break;
  90. case 1:
  91. return 3;
  92. break;
  93. case 2:
  94. return 5;
  95. break;
  96. case 3:
  97. return 1;
  98. case 4:
  99. return 2;
  100. default:
  101. return 2;
  102. }
  103. }
  104.  
  105. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  106.  
  107. // USER SELFIE INFORMATION
  108. if (indexPath.section == 0) {
  109.  
  110. UserSelfieCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserSelfieCell reusableCellName]];
  111.  
  112. if (self.user) {
  113. [cell.selfie setImageWithString:[NSString stringWithFormat:@"%@ %@", self.user.fname, self.user.lname] color:[MyColors colorForKey:kMCTertiary]];
  114. } else {
  115. [cell.selfie setImageWithString:@"M P" color:[MyColors colorForKey:@"scarlet"]];
  116. }
  117.  
  118. cell.selfie.tag = selph;
  119. cell.fname.text = self.user.fname;
  120. cell.fname.tag = fname;
  121. cell.fname.delegate = self;
  122. cell.lname.text = self.user.lname;
  123. cell.lname.tag = lname;
  124. cell.lname.delegate = self;
  125. cell.relation.tag = relation;
  126. cell.relation.text = self.user.relation;
  127. return cell;
  128.  
  129. // USER BIO (HEIGHT, WEIGHT)
  130. } else if (indexPath.section == 1) {
  131.  
  132. switch (indexPath.row) {
  133. case 0: {
  134.  
  135. UserInputCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserInputCell reusableCellName]];
  136.  
  137. NSString *date = [self.format stringFromDate:self.user.birthday];
  138. [cell asBirthdayCell];
  139. cell.valueField.text = date;
  140. cell.unitLabel.text = @"Birthday";
  141. cell.unitLabel.textColor = [UIColor blackColor];
  142. cell.valueField.tag = birthday;
  143.  
  144. return cell;
  145. break;
  146. }
  147. case 1: {
  148. UserInputUnitCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserInputUnitCell reusableCellName]];
  149. cell.valueField.text = (self.user.height) ? [NSString stringWithFormat:@"%@ in", self.user.height] : nil;
  150. cell.valueField.placeholder = @"inches";
  151. cell.unitLabel.text = @"Height";
  152. cell.valueField.tag = height;
  153. cell.valueField.suffix = @" in";
  154. return cell;
  155. break;
  156. }
  157. case 2: {
  158. UserInputUnitCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserInputUnitCell reusableCellName]];
  159. cell.valueField.text = (self.user.weight) ? [NSString stringWithFormat:@"%@ lbs", self.user.weight] : nil;
  160. cell.valueField.placeholder = @"lbs";
  161. cell.unitLabel.text = @"Weight";
  162. cell.valueField.tag = weight;
  163. cell.valueField.suffix = @" lbs";
  164. return cell;
  165. break;
  166. }
  167. default: {
  168. UserInputCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserInputCell reusableCellName]];
  169. return cell;
  170. break;
  171. }
  172. }
  173.  
  174. // USER PHARMACY AND DOCTOR INFORMATION
  175. } else if (indexPath.section == 2){
  176. // Pharmacist name
  177. if (indexPath.row == 0) {
  178. UserInputCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserInputCell reusableCellName]];
  179. cell.unitLabel.text = @"Pharmacy";
  180. cell.valueField.keyboardType = UIKeyboardTypeDefault;
  181. cell.valueField.tag = pharm;
  182. cell.valueField.placeholder = @"John Smith";
  183. return cell;
  184. // Pharmacy address
  185. } else if (indexPath.row == 1){
  186. UserInputCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserInputCell reusableCellName]];
  187. cell.unitLabel.text = @"Address";
  188. cell.valueField.tag = pharmAddress;
  189. cell.valueField.keyboardType = UIKeyboardTypeDefault;
  190. cell.valueField.placeholder = @"1140 High St.";
  191. return cell;
  192. // pharmacy city
  193. } else if (indexPath.row == 2) {
  194. UserInputCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserInputCell reusableCellName]];
  195. cell.unitLabel.text = @"City";
  196. cell.valueField.tag = pharmCity;
  197. cell.valueField.keyboardType = UIKeyboardTypeDefault;
  198. cell.valueField.placeholder = @"Columbus";
  199. return cell;
  200. // Pharmacy Phone
  201. } else if (indexPath.row == 3) {
  202. UserInputCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserInputCell reusableCellName]];
  203. cell.unitLabel.text = @"Phone";
  204. cell.valueField.tag = pharmPhone;
  205. cell.valueField.keyboardType = UIKeyboardTypeNumberPad;
  206. cell.valueField.placeholder = @"614 132 4324";
  207. return cell;
  208. } else {
  209. UserInputCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserInputCell reusableCellName]];
  210. cell.unitLabel.text = @"Doctor";
  211. cell.valueField.tag = pharmPhone;
  212. cell.valueField.keyboardType = UIKeyboardTypeDefault;
  213. cell.valueField.placeholder = @"Smith";
  214. return cell;
  215. }
  216. } else if (indexPath.section == 3) {
  217. MedicationNotesCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[MedicationNotesCell reusableCellName]];
  218. cell.notesLabel.text = @"Allergies";
  219. cell.notesField.text = @"List allergies";
  220. return cell;
  221. } else {
  222.  
  223. if (indexPath.row == 0) {
  224. UserInputCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserInputCell reusableCellName]];
  225. cell.unitLabel.text = @"Archived Medication";
  226. cell.unitLabel.textColor = [MyColors colorForKey:@"scarlet"];
  227. cell.valueField.placeholder = @"";
  228. cell.valueField.userInteractionEnabled = NO;
  229. [cell.valueField removeFromSuperview];
  230.  
  231. if (self.user) {
  232. cell.selectionStyle = UITableViewCellSelectionStyleBlue;
  233. } else {
  234. cell.selectionStyle = UITableViewCellEditingStyleNone;
  235. }
  236.  
  237. return cell;
  238.  
  239. } else {
  240. UserInputCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserInputCell reusableCellName]];
  241. cell.unitLabel.text = @"Email information";
  242. cell.unitLabel.textColor = [UIColor blackColor];
  243. cell.valueField.placeholder = @"";
  244. cell.valueField.userInteractionEnabled = NO;
  245.  
  246. if (self.user) {
  247. cell.selectionStyle = UITableViewCellSelectionStyleBlue;
  248. } else {
  249. cell.selectionStyle = UITableViewCellEditingStyleNone;
  250. }
  251.  
  252. return cell;
  253.  
  254. }
  255. }
  256. }
  257.  
  258. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  259. NSLog(@"%@", indexPath);
  260.  
  261. if (indexPath.section == 4 && indexPath.row == 0) {
  262. [self performSegueWithIdentifier:@"Archive" sender:self];
  263. } else if (indexPath.section == 4 && indexPath.row == 1) {
  264. [self shareUser];
  265. }
  266. }
  267.  
  268. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  269. if ([[segue identifier] isEqualToString:@"Archive"]) {
  270. ArchiveViewController *destination = (ArchiveViewController*)[segue destinationViewController];
  271. [destination setUser:self.user];
  272. }
  273. }
  274.  
  275. #pragma Mark - loading and editing
  276.  
  277. - (void) startFresh {
  278. UIBarButtonItem *save = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];
  279. self.navigationItem.rightBarButtonItem = save;
  280. }
  281.  
  282. - (void) load {
  283.  
  284. for (UITextField *field in [self getAllTextFields]) {
  285. field.enabled = NO;
  286. }
  287. }
  288.  
  289. - (void) edit {
  290.  
  291. for (UITextField *field in [self getAllTextFields]) {
  292. field.enabled = YES;
  293.  
  294. POPBasicAnimation *textColor = [POPBasicAnimation animationWithPropertyNamed:kPOPLabelTextColor];
  295. POPSpringAnimation *scale = [POPSpringAnimation animationWithPropertyNamed:kPOPViewScaleXY];
  296. scale.toValue = [NSValue valueWithCGSize:CGSizeMake(1.1, 1.1)];;
  297. textColor.toValue = [MyColors colorForKey:kMCPrimary];
  298.  
  299. [textColor setCompletionBlock:^(POPAnimation *a, BOOL t) {
  300. POPBasicAnimation *textColorBack = [POPBasicAnimation animationWithPropertyNamed:kPOPLabelTextColor];
  301. POPSpringAnimation *scaleBack = [POPSpringAnimation animationWithPropertyNamed:kPOPViewScaleXY];
  302. scaleBack.toValue = [NSValue valueWithCGSize:CGSizeMake(1, 1)];;
  303. textColorBack.toValue = [MyColors colorForKey:kColorGray];
  304.  
  305. [field pop_addAnimation:scaleBack forKey:@"scaleBack"];
  306. [field pop_addAnimation:textColorBack forKey:@"textBack"];
  307. }];
  308.  
  309. [field pop_addAnimation:scale forKey:@"scale"];
  310. [field pop_addAnimation:textColor forKey:@"color"];
  311. }
  312.  
  313. UIBarButtonItem *save = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];
  314. self.navigationItem.rightBarButtonItem = save;
  315. }
  316.  
  317. - (NSArray *) getAllTextFields {
  318.  
  319. return @[[self.view viewWithTag: fname],
  320. [self.view viewWithTag: lname],
  321. [self.view viewWithTag: birthday],
  322. [self.view viewWithTag: height],
  323. [self.view viewWithTag: relation],
  324. [self.view viewWithTag: weight]];
  325. }
  326.  
  327. #pragma mark - Saving and undwing
  328.  
  329. - (void) updateImage {
  330. UITextField * fnameField = (UITextField*)[self.view viewWithTag: fname];
  331. UITextField * lnameField = (UITextField*)[self.view viewWithTag: lname];
  332. UIImageView * imgView = (UIImageView*)[self.view viewWithTag:selph];
  333. NSString* fname = fnameField.text;
  334. NSString* lname = lnameField.text;
  335.  
  336.  
  337. [imgView setImageWithString:[NSString stringWithFormat:@"%@ %@", fname, lname] color:[MyColors colorForKey:kMCTertiary]];
  338.  
  339. }
  340.  
  341. - (void) cancel {
  342. [self unwind];
  343. }
  344.  
  345. - (void) save {
  346.  
  347. UITextField * fnameField = (UITextField*)[self.view viewWithTag: fname];
  348. UITextField * lnameField = (UITextField*)[self.view viewWithTag: lname];
  349. UITextField * relationField = (UITextField*)[self.view viewWithTag: relation];
  350. UITextField * allergiesField = (UITextField*)[self.view viewWithTag:allergy];
  351.  
  352. UITextField *pharmField = (UITextField*)[self.view viewWithTag: pharm];
  353. UITextField *pharmPhoneField = (UITextField*)[self.view viewWithTag: pharmPhone];
  354. UITextField *pharmAddressField = (UITextField*)[self.view viewWithTag: pharmAddress];
  355. UITextField *pharmCityField = (UITextField*)[self.view viewWithTag: pharmCity];
  356. UITextField *doctorField = (UITextField*)[self.view viewWithTag: doctor];
  357.  
  358. UITextFieldWithSuffix * heightField = (UITextFieldWithSuffix*)[self.view viewWithTag: height];
  359. UITextFieldWithSuffix * weightField = (UITextFieldWithSuffix*)[self.view viewWithTag: weight];
  360. UITextField * birthdayField = (UITextField*)[self.view viewWithTag: birthday];
  361.  
  362. // number formatter: converts String to NSNUmber
  363. NSNumberFormatter *numFormatt = [[NSNumberFormatter alloc] init];
  364. numFormatt.numberStyle = NSNumberFormatterDecimalStyle;
  365.  
  366. NSString* fname = fnameField.text;
  367. NSString* lname = lnameField.text;
  368. NSString * relation = relationField.text;
  369. NSString * allegies = allergiesField.text;
  370. NSDate *birthday = [self.format dateFromString: birthdayField.text];
  371. NSNumber *height = [numFormatt numberFromString:[heightField getText]];
  372. NSNumber *weight = [numFormatt numberFromString:[weightField getText]];
  373.  
  374. NSString * pharmFieldValue = pharmField.text;
  375. NSString * pharmPhoneFieldValue = pharmPhoneField.text;
  376. NSString * pharmAddressFieldValue = pharmAddressField.text;
  377. NSString * pharmCityFieldValue = pharmCityField.text;
  378. NSString * doctorFieldValue = doctorField.text;
  379.  
  380. NSManagedObjectContext *context = [DataManager getContext];
  381.  
  382. if (!self.user) {
  383. self.user = [NSEntityDescription
  384. insertNewObjectForEntityForName:[User entityName]
  385. inManagedObjectContext:context];
  386. }
  387.  
  388. self.user.relation = relation;
  389. self.user.fname = fname;
  390. self.user.lname = lname;
  391. self.user.birthday = birthday;
  392. self.user.height = height;
  393. self.user.weight = weight;
  394. self.user.pharmacist = pharmFieldValue;
  395. self.user.pharmacistPhone = pharmPhoneFieldValue;
  396. self.user.pharmacistAddress = pharmAddressFieldValue;
  397. self.user.pharmacistCity = pharmCityFieldValue;
  398. self.user.doctor = doctorFieldValue;
  399.  
  400. self.user.isMainUser = (self.isMainUser || self.user.isMainUser)? [NSNumber numberWithBool:YES] : [NSNumber numberWithBool:NO];
  401. self.user.allergies = allegies;
  402.  
  403. NSError *err;
  404. if([context save:&err]) {
  405. NSLog(@"User saved to database with information:");
  406. } else {
  407. NSLog(@"Error saving user");
  408. }
  409.  
  410. [self unwind];
  411. }
  412.  
  413. - (void) unwind {
  414.  
  415. if (self.isMainUser) {
  416. [[NSNotificationCenter defaultCenter]
  417. postNotificationName:@"reloadForUser"
  418. object:self.user];
  419. }
  420.  
  421.  
  422. [self dismissViewControllerAnimated:YES completion:nil];
  423. }
  424.  
  425. - (void) registerNibs {
  426. // load nibs for custom cells
  427. [UserInputCell loadFromNibForTableView:self.tableView];
  428. [UserSelfieCell loadFromNibForTableView:self.tableView];
  429. [UserInputUnitCell loadFromNibForTableView:self.tableView];
  430. [MedicationNotesCell loadFromNibForTableView:self.tableView];
  431. }
  432.  
  433. #pragma mark - Delegates
  434.  
  435. - (void) textFieldDidEndEditing:(UITextField *)textField {
  436. [self updateImage];
  437. }
  438.  
  439. #pragma mark - Mail Composer
  440.  
  441. - (void) shareUser {
  442.  
  443. NSString *subject = [NSString stringWithFormat:@"Medication informatin for %@ %@", self.user.fname, self.user.lname];
  444. NSString *body = [UIFactory shareStringForUser:self.user];
  445.  
  446. MFMailComposeViewController *mailcomposer = [[MFMailComposeViewController alloc] init];
  447. mailcomposer.mailComposeDelegate = self;
  448. [mailcomposer setSubject: subject];
  449. [mailcomposer setMessageBody: body isHTML:NO];
  450. [self presentViewController:mailcomposer animated:YES completion:nil];
  451. }
  452.  
  453. - (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
  454. [self dismissViewControllerAnimated:YES completion:nil];
  455. }
  456.  
  457.  
  458.  
  459. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement