Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // NewUserViewController.m
- // MyPharmacist
- //
- // Created by Matthew Faluotico on 2/18/15.
- // Copyright (c) 2015 MyPharmacy. All rights reserved.
- //
- #import "ModifyUserViewController.h"
- #import "UIImageView+Letters.h"
- #import "PrescriptionCell.h"
- #import "ArchiveViewController.h"
- #import <MessageUI/MessageUI.h>
- #import <POP/pop.h>
- @interface ModifyUserViewController () <UITextFieldDelegate, MFMailComposeViewControllerDelegate>
- @property NSDateFormatter *format;
- @end
- NSInteger selph = 50; // names this 'selph' because I was foolish and tried to name it 'self'
- NSInteger fname = 51;
- NSInteger lname = 52;
- NSInteger relation = 53;
- NSInteger height = 54;
- NSInteger weight = 55;
- NSInteger allergy = 56;
- NSInteger birthday = 57;
- NSInteger pharm = 58;
- NSInteger pharmPhone = 59;
- NSInteger pharmAddress = 60;
- NSInteger pharmCity = 61;
- NSInteger doctor = 62;
- @implementation ModifyUserViewController
- - (void)viewDidAppear:(BOOL)animated {
- //Check if user provided, if yes loads detail, if no loads startFresh
- self.tableView.allowsSelection = YES;
- if (self.user) {
- [self load];
- } else {
- [self startFresh];
- }
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- // sets the title based on the user
- self.format = [[NSDateFormatter alloc] init];
- self.format.dateFormat = @"MMM, dd, yyyy";
- self.title = @"User Setup";
- self.tableView.tableFooterView = [UIView new];
- if (self.isMainUser) {
- UIBarButtonItem *save = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(save)];
- self.navigationItem.rightBarButtonItem = save;
- } else {
- self.navigationItem.hidesBackButton = YES;
- UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(cancel)];
- self.navigationItem.leftBarButtonItem = cancel;
- UIBarButtonItem *edit = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(edit)];
- self.navigationItem.rightBarButtonItem = edit;
- }
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #pragma mark - Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return 5;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- switch (section) {
- case 0:
- return 1;
- break;
- case 1:
- return 3;
- break;
- case 2:
- return 5;
- break;
- case 3:
- return 1;
- case 4:
- return 2;
- default:
- return 2;
- }
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- // USER SELFIE INFORMATION
- if (indexPath.section == 0) {
- UserSelfieCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserSelfieCell reusableCellName]];
- if (self.user) {
- [cell.selfie setImageWithString:[NSString stringWithFormat:@"%@ %@", self.user.fname, self.user.lname] color:[MyColors colorForKey:kMCTertiary]];
- } else {
- [cell.selfie setImageWithString:@"M P" color:[MyColors colorForKey:@"scarlet"]];
- }
- cell.selfie.tag = selph;
- cell.fname.text = self.user.fname;
- cell.fname.tag = fname;
- cell.fname.delegate = self;
- cell.lname.text = self.user.lname;
- cell.lname.tag = lname;
- cell.lname.delegate = self;
- cell.relation.tag = relation;
- cell.relation.text = self.user.relation;
- return cell;
- // USER BIO (HEIGHT, WEIGHT)
- } else if (indexPath.section == 1) {
- switch (indexPath.row) {
- case 0: {
- UserInputCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserInputCell reusableCellName]];
- NSString *date = [self.format stringFromDate:self.user.birthday];
- [cell asBirthdayCell];
- cell.valueField.text = date;
- cell.unitLabel.text = @"Birthday";
- cell.unitLabel.textColor = [UIColor blackColor];
- cell.valueField.tag = birthday;
- return cell;
- break;
- }
- case 1: {
- UserInputUnitCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserInputUnitCell reusableCellName]];
- cell.valueField.text = (self.user.height) ? [NSString stringWithFormat:@"%@ in", self.user.height] : nil;
- cell.valueField.placeholder = @"inches";
- cell.unitLabel.text = @"Height";
- cell.valueField.tag = height;
- cell.valueField.suffix = @" in";
- return cell;
- break;
- }
- case 2: {
- UserInputUnitCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserInputUnitCell reusableCellName]];
- cell.valueField.text = (self.user.weight) ? [NSString stringWithFormat:@"%@ lbs", self.user.weight] : nil;
- cell.valueField.placeholder = @"lbs";
- cell.unitLabel.text = @"Weight";
- cell.valueField.tag = weight;
- cell.valueField.suffix = @" lbs";
- return cell;
- break;
- }
- default: {
- UserInputCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserInputCell reusableCellName]];
- return cell;
- break;
- }
- }
- // USER PHARMACY AND DOCTOR INFORMATION
- } else if (indexPath.section == 2){
- // Pharmacist name
- if (indexPath.row == 0) {
- UserInputCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserInputCell reusableCellName]];
- cell.unitLabel.text = @"Pharmacy";
- cell.valueField.keyboardType = UIKeyboardTypeDefault;
- cell.valueField.tag = pharm;
- cell.valueField.placeholder = @"John Smith";
- return cell;
- // Pharmacy address
- } else if (indexPath.row == 1){
- UserInputCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserInputCell reusableCellName]];
- cell.unitLabel.text = @"Address";
- cell.valueField.tag = pharmAddress;
- cell.valueField.keyboardType = UIKeyboardTypeDefault;
- cell.valueField.placeholder = @"1140 High St.";
- return cell;
- // pharmacy city
- } else if (indexPath.row == 2) {
- UserInputCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserInputCell reusableCellName]];
- cell.unitLabel.text = @"City";
- cell.valueField.tag = pharmCity;
- cell.valueField.keyboardType = UIKeyboardTypeDefault;
- cell.valueField.placeholder = @"Columbus";
- return cell;
- // Pharmacy Phone
- } else if (indexPath.row == 3) {
- UserInputCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserInputCell reusableCellName]];
- cell.unitLabel.text = @"Phone";
- cell.valueField.tag = pharmPhone;
- cell.valueField.keyboardType = UIKeyboardTypeNumberPad;
- cell.valueField.placeholder = @"614 132 4324";
- return cell;
- } else {
- UserInputCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserInputCell reusableCellName]];
- cell.unitLabel.text = @"Doctor";
- cell.valueField.tag = pharmPhone;
- cell.valueField.keyboardType = UIKeyboardTypeDefault;
- cell.valueField.placeholder = @"Smith";
- return cell;
- }
- } else if (indexPath.section == 3) {
- MedicationNotesCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[MedicationNotesCell reusableCellName]];
- cell.notesLabel.text = @"Allergies";
- cell.notesField.text = @"List allergies";
- return cell;
- } else {
- if (indexPath.row == 0) {
- UserInputCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserInputCell reusableCellName]];
- cell.unitLabel.text = @"Archived Medication";
- cell.unitLabel.textColor = [MyColors colorForKey:@"scarlet"];
- cell.valueField.placeholder = @"";
- cell.valueField.userInteractionEnabled = NO;
- [cell.valueField removeFromSuperview];
- if (self.user) {
- cell.selectionStyle = UITableViewCellSelectionStyleBlue;
- } else {
- cell.selectionStyle = UITableViewCellEditingStyleNone;
- }
- return cell;
- } else {
- UserInputCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[UserInputCell reusableCellName]];
- cell.unitLabel.text = @"Email information";
- cell.unitLabel.textColor = [UIColor blackColor];
- cell.valueField.placeholder = @"";
- cell.valueField.userInteractionEnabled = NO;
- if (self.user) {
- cell.selectionStyle = UITableViewCellSelectionStyleBlue;
- } else {
- cell.selectionStyle = UITableViewCellEditingStyleNone;
- }
- return cell;
- }
- }
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- NSLog(@"%@", indexPath);
- if (indexPath.section == 4 && indexPath.row == 0) {
- [self performSegueWithIdentifier:@"Archive" sender:self];
- } else if (indexPath.section == 4 && indexPath.row == 1) {
- [self shareUser];
- }
- }
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- if ([[segue identifier] isEqualToString:@"Archive"]) {
- ArchiveViewController *destination = (ArchiveViewController*)[segue destinationViewController];
- [destination setUser:self.user];
- }
- }
- #pragma Mark - loading and editing
- - (void) startFresh {
- UIBarButtonItem *save = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];
- self.navigationItem.rightBarButtonItem = save;
- }
- - (void) load {
- for (UITextField *field in [self getAllTextFields]) {
- field.enabled = NO;
- }
- }
- - (void) edit {
- for (UITextField *field in [self getAllTextFields]) {
- field.enabled = YES;
- POPBasicAnimation *textColor = [POPBasicAnimation animationWithPropertyNamed:kPOPLabelTextColor];
- POPSpringAnimation *scale = [POPSpringAnimation animationWithPropertyNamed:kPOPViewScaleXY];
- scale.toValue = [NSValue valueWithCGSize:CGSizeMake(1.1, 1.1)];;
- textColor.toValue = [MyColors colorForKey:kMCPrimary];
- [textColor setCompletionBlock:^(POPAnimation *a, BOOL t) {
- POPBasicAnimation *textColorBack = [POPBasicAnimation animationWithPropertyNamed:kPOPLabelTextColor];
- POPSpringAnimation *scaleBack = [POPSpringAnimation animationWithPropertyNamed:kPOPViewScaleXY];
- scaleBack.toValue = [NSValue valueWithCGSize:CGSizeMake(1, 1)];;
- textColorBack.toValue = [MyColors colorForKey:kColorGray];
- [field pop_addAnimation:scaleBack forKey:@"scaleBack"];
- [field pop_addAnimation:textColorBack forKey:@"textBack"];
- }];
- [field pop_addAnimation:scale forKey:@"scale"];
- [field pop_addAnimation:textColor forKey:@"color"];
- }
- UIBarButtonItem *save = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];
- self.navigationItem.rightBarButtonItem = save;
- }
- - (NSArray *) getAllTextFields {
- return @[[self.view viewWithTag: fname],
- [self.view viewWithTag: lname],
- [self.view viewWithTag: birthday],
- [self.view viewWithTag: height],
- [self.view viewWithTag: relation],
- [self.view viewWithTag: weight]];
- }
- #pragma mark - Saving and undwing
- - (void) updateImage {
- UITextField * fnameField = (UITextField*)[self.view viewWithTag: fname];
- UITextField * lnameField = (UITextField*)[self.view viewWithTag: lname];
- UIImageView * imgView = (UIImageView*)[self.view viewWithTag:selph];
- NSString* fname = fnameField.text;
- NSString* lname = lnameField.text;
- [imgView setImageWithString:[NSString stringWithFormat:@"%@ %@", fname, lname] color:[MyColors colorForKey:kMCTertiary]];
- }
- - (void) cancel {
- [self unwind];
- }
- - (void) save {
- UITextField * fnameField = (UITextField*)[self.view viewWithTag: fname];
- UITextField * lnameField = (UITextField*)[self.view viewWithTag: lname];
- UITextField * relationField = (UITextField*)[self.view viewWithTag: relation];
- UITextField * allergiesField = (UITextField*)[self.view viewWithTag:allergy];
- UITextField *pharmField = (UITextField*)[self.view viewWithTag: pharm];
- UITextField *pharmPhoneField = (UITextField*)[self.view viewWithTag: pharmPhone];
- UITextField *pharmAddressField = (UITextField*)[self.view viewWithTag: pharmAddress];
- UITextField *pharmCityField = (UITextField*)[self.view viewWithTag: pharmCity];
- UITextField *doctorField = (UITextField*)[self.view viewWithTag: doctor];
- UITextFieldWithSuffix * heightField = (UITextFieldWithSuffix*)[self.view viewWithTag: height];
- UITextFieldWithSuffix * weightField = (UITextFieldWithSuffix*)[self.view viewWithTag: weight];
- UITextField * birthdayField = (UITextField*)[self.view viewWithTag: birthday];
- // number formatter: converts String to NSNUmber
- NSNumberFormatter *numFormatt = [[NSNumberFormatter alloc] init];
- numFormatt.numberStyle = NSNumberFormatterDecimalStyle;
- NSString* fname = fnameField.text;
- NSString* lname = lnameField.text;
- NSString * relation = relationField.text;
- NSString * allegies = allergiesField.text;
- NSDate *birthday = [self.format dateFromString: birthdayField.text];
- NSNumber *height = [numFormatt numberFromString:[heightField getText]];
- NSNumber *weight = [numFormatt numberFromString:[weightField getText]];
- NSString * pharmFieldValue = pharmField.text;
- NSString * pharmPhoneFieldValue = pharmPhoneField.text;
- NSString * pharmAddressFieldValue = pharmAddressField.text;
- NSString * pharmCityFieldValue = pharmCityField.text;
- NSString * doctorFieldValue = doctorField.text;
- NSManagedObjectContext *context = [DataManager getContext];
- if (!self.user) {
- self.user = [NSEntityDescription
- insertNewObjectForEntityForName:[User entityName]
- inManagedObjectContext:context];
- }
- self.user.relation = relation;
- self.user.fname = fname;
- self.user.lname = lname;
- self.user.birthday = birthday;
- self.user.height = height;
- self.user.weight = weight;
- self.user.pharmacist = pharmFieldValue;
- self.user.pharmacistPhone = pharmPhoneFieldValue;
- self.user.pharmacistAddress = pharmAddressFieldValue;
- self.user.pharmacistCity = pharmCityFieldValue;
- self.user.doctor = doctorFieldValue;
- self.user.isMainUser = (self.isMainUser || self.user.isMainUser)? [NSNumber numberWithBool:YES] : [NSNumber numberWithBool:NO];
- self.user.allergies = allegies;
- NSError *err;
- if([context save:&err]) {
- NSLog(@"User saved to database with information:");
- } else {
- NSLog(@"Error saving user");
- }
- [self unwind];
- }
- - (void) unwind {
- if (self.isMainUser) {
- [[NSNotificationCenter defaultCenter]
- postNotificationName:@"reloadForUser"
- object:self.user];
- }
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- - (void) registerNibs {
- // load nibs for custom cells
- [UserInputCell loadFromNibForTableView:self.tableView];
- [UserSelfieCell loadFromNibForTableView:self.tableView];
- [UserInputUnitCell loadFromNibForTableView:self.tableView];
- [MedicationNotesCell loadFromNibForTableView:self.tableView];
- }
- #pragma mark - Delegates
- - (void) textFieldDidEndEditing:(UITextField *)textField {
- [self updateImage];
- }
- #pragma mark - Mail Composer
- - (void) shareUser {
- NSString *subject = [NSString stringWithFormat:@"Medication informatin for %@ %@", self.user.fname, self.user.lname];
- NSString *body = [UIFactory shareStringForUser:self.user];
- MFMailComposeViewController *mailcomposer = [[MFMailComposeViewController alloc] init];
- mailcomposer.mailComposeDelegate = self;
- [mailcomposer setSubject: subject];
- [mailcomposer setMessageBody: body isHTML:NO];
- [self presentViewController:mailcomposer animated:YES completion:nil];
- }
- - (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement