Advertisement
Guest User

Untitled

a guest
Apr 11th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ScheduleView.m
  3. //  varrdle_v2
  4. //
  5. //  Created by Paragon Software on 3/31/14.
  6. //  Copyright (c) 2014 Paragon Software. All rights reserved.
  7. //  CHanged : 2014.04.09 06.30pm
  8.  
  9. #import "ScheduleView.h"
  10. #import "VCPerson.h"
  11. #import "cellForDatePickCell.h"
  12. #import "jsonpaser.h"
  13. #import "LoginView.h"
  14.  
  15.  
  16.  
  17. static NSDictionary* requestsF_date;
  18. static NSString* date;
  19.  
  20.  
  21.  
  22. #define kDatePickerTag 1
  23.  
  24. #define kTitleKey       @"title"
  25. #define kDateKey        @"date"
  26. static NSString *kPersonCellID = @"personCell";
  27. static NSString *kDatePickerCellID = @"datePickerCell";
  28. static NSString *kSegueIdentifier = @"addPerson";
  29. static NSString *kOtherCellIdentifier = @"otherCell";
  30. @interface ScheduleView ()
  31.  
  32. //@property (weak, nonatomic) IBOutlet UITableViewCell *datePickerCell;
  33. //@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;
  34. @property (strong, nonatomic) NSIndexPath *datePickerIndexPath;
  35. @property (assign) NSInteger pickerCellRowHeight;
  36. @property (strong, nonatomic) NSDateFormatter *dateFormatter;
  37. @property (strong, nonatomic) NSMutableArray *persons;
  38. @property (strong, nonatomic) NSMutableArray *dataArray;
  39.  
  40.  
  41.  
  42. - (IBAction)dateChanged:(UIDatePicker *)sender;
  43. @end
  44.  
  45. @implementation ScheduleView
  46.  
  47.  
  48.  
  49. +(NSString*)getDate{
  50.     return date;
  51. }
  52.  
  53. +(NSDictionary*)getRequestForDate{
  54.     return  requestsF_date;
  55. }
  56.  
  57. - (id)initWithStyle:(UITableViewStyle)style
  58. {
  59.     self = [super initWithStyle:style];
  60.     if (self) {
  61.         // Custom initialization
  62.     }
  63.     return self;
  64. }
  65.  
  66. - (void)viewDidLoad
  67. {
  68.     [super viewDidLoad];
  69.     //Initialize the dataArray
  70.     _dataArray = [[NSMutableArray alloc] init];
  71.    
  72.     //First section data
  73.     NSArray *firstItemsArray = [[NSArray alloc] initWithObjects:@"Item 1", @"Item 2", @"Item 3", nil];
  74.     NSDictionary *firstItemsArrayDict = [NSDictionary dictionaryWithObject:firstItemsArray forKey:@"data"];
  75.     [_dataArray addObject:firstItemsArrayDict];
  76.    
  77.     //Second section data
  78.     NSArray *secondItemsArray = [[NSArray alloc] initWithObjects:@"Item 4", @"Item 5", @"Item 6", @"Last Item", nil];
  79.     NSDictionary *secondItemsArrayDict = [NSDictionary dictionaryWithObject:secondItemsArray forKey:@"data"];
  80.     [_dataArray addObject:secondItemsArrayDict];
  81.    
  82.    
  83.    
  84.     UITableViewCell *pickerViewCellToCheck = [self.tableView dequeueReusableCellWithIdentifier:kDatePickerCellID];
  85.     self.pickerCellRowHeight = pickerViewCellToCheck.frame.size.height;
  86.    
  87.    
  88.    
  89.   //  NSMutableDictionary *itemOne = [@{ kTitleKey : @"Tap a cell to change its date:" } mutableCopy];
  90.   //  NSMutableDictionary *itemTwo = [@{ kTitleKey : @"Start Date",
  91.                                       // kDateKey : [NSDate date] } mutableCopy];
  92.    //
  93.     //self.persons = @[itemOne, itemTwo];
  94.    
  95.     [self createFakeData];
  96.     [self createDateFormatter];
  97.    
  98.    
  99.    
  100.     if (!self.requestsForDate) {
  101.         self.requestsForDate = [[NSDictionary alloc]init];
  102.     }
  103.    
  104.    
  105.  
  106.     // Uncomment the following line to preserve selection between presentations.
  107.     // self.clearsSelectionOnViewWillAppear = NO;
  108.  
  109.     // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
  110.     // self.navigationItem.rightBarButtonItem = self.editButtonItem;
  111. }
  112. - (void)createDateFormatter {
  113.    
  114.     self.dateFormatter = [[NSDateFormatter alloc] init];
  115.    
  116.     [self.dateFormatter setDateStyle:NSDateFormatterShortStyle];
  117.    
  118.     [self.dateFormatter setTimeStyle:NSDateFormatterNoStyle];
  119.    
  120. }
  121. - (void)createFakeData {
  122.    
  123.     VCPerson *p1 = [[VCPerson alloc] initWithName:@"Select Date"
  124.                                       dateOfBirth:[NSDate dateWithTimeIntervalSince1970:632448000]
  125.                                      placeOfBirth:@"London"];
  126.    
  127.    
  128.    // VCPerson *p2 = [[VCPerson alloc] initWithName:@"Jane Andersen" dateOfBirth:[NSDate dateWithTimeIntervalSince1970:123456789] placeOfBirth:@"San Francisco"];
  129.    
  130.     if (!self.persons){
  131.        
  132.         self.persons = [[NSMutableArray alloc] init];
  133.        
  134.     }
  135.    
  136.     [self.persons addObject:p1];
  137.    // [self.persons addObject:p2];
  138. }
  139. - (void)didReceiveMemoryWarning
  140. {
  141.     [super didReceiveMemoryWarning];
  142.     // Dispose of any resources that can be recreated.
  143. }
  144.  
  145. #pragma mark - Table view data source
  146.  
  147. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  148.    
  149.     return 1;
  150. }
  151.  
  152.  
  153. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  154.    
  155.    NSDictionary* reqFdate= [ScheduleView getRequestForDate];
  156.    
  157.     if (reqFdate.count == 0 ) {
  158.         NSInteger numberOfRows = [self.persons count];
  159.        
  160.         if ([self datePickerIsShown]){
  161.            
  162.             numberOfRows++;
  163.            
  164.         }
  165.         NSLog(@"NO OF ROWS in IF %d",numberOfRows);
  166.         return numberOfRows;
  167.     }
  168.     else{
  169.         NSLog(@"NO OF ROWS %d",reqFdate.count + 1);
  170.         return reqFdate.count + 1;
  171.     }
  172.    
  173.    
  174. }
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  190. {
  191.    
  192.        
  193.         UITableViewCell *cell;
  194.        
  195.         NSDate *today = [NSDate date];
  196.         NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  197.         // display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
  198.         [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
  199.         NSString *currentTime = [dateFormatter stringFromDate:today];
  200.         NSDate *date=[dateFormatter dateFromString:currentTime];
  201.    
  202.    
  203.  
  204.         if(indexPath.row==0){
  205.             VCPerson *person = self.persons[0];
  206.            
  207.             cell = [self createPersonCell:person];
  208.            
  209.         }
  210.      
  211.    
  212.    
  213.         else if ([self datePickerIsShown] && (self.datePickerIndexPath.row == 1)){
  214.            
  215.             // VCPerson *person = self.persons[indexPath.row -1];
  216.            
  217.            
  218.            
  219.             cell = [self createPickerCell:date];
  220.            
  221.         }
  222.        
  223.         else{
  224.            
  225.             cellForDatePickCell *cell = (cellForDatePickCell*)[self.tableView dequeueReusableCellWithIdentifier:kOtherCellIdentifier];
  226.             cell.delegate_Dtepick = self;
  227.            
  228.             return cell;
  229.  
  230.         }
  231.        
  232.        
  233.         if(indexPath.section!=0 && tapfirstCell) {
  234.            
  235.             UITableViewCell *cell = (UITableViewCell*)[self.tableView dequeueReusableCellWithIdentifier:kOtherCellIdentifier forIndexPath:indexPath];
  236.             //cell.delegate_Dtepick = self;
  237.             NSDictionary *dictionary = [_dataArray objectAtIndex:indexPath.section];
  238.             NSArray *array = [dictionary objectForKey:@"data"];
  239.             NSString *cellValue = [array objectAtIndex:indexPath.row];
  240.             cell.textLabel.text =cellValue;
  241.            
  242.         }
  243.        
  244.         return cell;
  245.  
  246.  
  247.  
  248.    
  249.        /*
  250.         if ([self datePickerIsShown] && (self.datePickerIndexPath.row == indexPath.row)){
  251.            
  252.             // VCPerson *person = self.persons[indexPath.row -1];
  253.            
  254.            
  255.            
  256.             cell = [self createPickerCell:date];
  257.            
  258.         }
  259.        
  260.        
  261.        
  262.         else {
  263.            
  264.            // cellForDatePickCell* cell;
  265.             VCPerson *person = self.persons[indexPath.row];
  266.            
  267.             cell = [self createPersonCell:person];
  268.             //cell = [self createCustomCell:indexPath];
  269.         }
  270.        
  271.         return cell;
  272.  
  273.    
  274. */
  275.        
  276.  
  277.      
  278.    
  279. }
  280.  
  281. - (cellForDatePickCell *)createCustomCell:(NSIndexPath*)indexpath {
  282.    
  283.      cellForDatePickCell *cell = (cellForDatePickCell*)[self.tableView dequeueReusableCellWithIdentifier:kOtherCellIdentifier];
  284.      cell.delegate_Dtepick = self;
  285.    
  286.    
  287.    
  288.    
  289.     return cell;
  290.    
  291. }
  292.  
  293. - (UITableViewCell *)createPersonCell:(VCPerson *)person {
  294.    
  295.     UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:kPersonCellID];
  296.    
  297.     cell.textLabel.text = person.name;
  298.    
  299.     cell.detailTextLabel.text = [self.dateFormatter stringFromDate:person.dateOfBirth];
  300.     self.dateSelected = cell.detailTextLabel.text;
  301.    
  302.   /*  NSString* selectedDate = self.dateSelected;
  303.    
  304.     NSString* personID =[LoginView getPersonID];
  305.    
  306.     NSDictionary* parms = [NSDictionary dictionaryWithObjectsAndKeys:personID,@"caregiverPersonId",selectedDate,@"selectedDate", nil];
  307.    
  308.     jsonpaser* jp = [[jsonpaser alloc]init];
  309.    
  310.     [jp getWebServiceResponce:@"http://qa.vardle.com/Mobile/WebServices/AppointmentService.asmx/GetAppointments" :parms success:^(NSDictionary *responseObject)
  311.     {
  312.        
  313.    
  314.      
  315.         requestsF_date = responseObject;
  316.         NSLog(@"RESPONSEFORDATE :%@",requestsF_date);
  317.    
  318.     }];
  319.    
  320.     NSLog(@"DATE :%@",selectedDate);
  321.    */
  322.     return cell;
  323.    
  324. }
  325.  
  326. - (UITableViewCell *)createPickerCell:(NSDate *)date {
  327.    
  328.     UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:kDatePickerCellID];
  329.    
  330.     UIDatePicker *targetedDatePicker = (UIDatePicker *)[cell viewWithTag:kDatePickerTag];
  331.    
  332.     [targetedDatePicker setDate:date animated:NO];
  333.    
  334.     return cell;
  335. }
  336.  
  337. - (IBAction)dateChanged:(UIDatePicker *)sender {
  338.    
  339.     NSIndexPath *parentCellIndexPath = nil;
  340.    
  341.     if ([self datePickerIsShown]){
  342.        
  343.         parentCellIndexPath = [NSIndexPath indexPathForRow:self.datePickerIndexPath.row - 1 inSection:0];
  344.        
  345.     }else {
  346.        
  347.         return;
  348.     }
  349.    
  350.    
  351.    
  352.    // cellForDatePickCell *cell = (cellForDatePickCell*)[self.tableView dequeueReusableCellWithIdentifier:kOtherCellIdentifier];
  353.    // cell.delegate_Dtepick = self;
  354.    
  355.     UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:parentCellIndexPath];
  356.     VCPerson *person = self.persons[parentCellIndexPath.row];
  357.     person.dateOfBirth = sender.date;
  358.    
  359.     cell.detailTextLabel.text = [self.dateFormatter stringFromDate:sender.date];
  360.    
  361.    
  362.    
  363.     //set the date for the gloabal date variable to access for the web service call
  364.     date = cell.detailTextLabel.text;
  365.    
  366.     NSLog(@"changed");
  367.    
  368.    
  369.    
  370.    
  371.    
  372.    
  373. }
  374.  
  375. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  376.    
  377.     CGFloat rowHeight = self.tableView.rowHeight;
  378.    
  379.     if ([self datePickerIsShown] && (self.datePickerIndexPath.row == indexPath.row)){
  380.        
  381.         rowHeight = self.pickerCellRowHeight;
  382.        
  383.     }
  384.    
  385.     return rowHeight;
  386. }
  387.  
  388. - (BOOL)datePickerIsShown {
  389.    
  390.     return self.datePickerIndexPath != nil;
  391. }
  392.  
  393.  
  394. -(NSMutableArray*)getIndexPaths{
  395.    
  396.     int countOfRows = [ScheduleView getRequestForDate].count;
  397.     NSMutableArray* indexes = [[NSMutableArray alloc]init];
  398.     for (int row=0; row<=countOfRows; row++) {
  399.        
  400.         NSIndexPath* index = [NSIndexPath indexPathForRow:row inSection:0];
  401.         [indexes addObject:index];
  402.     }
  403.  
  404.     NSLog(@"OLD ARRAY : %@",indexes);
  405.     [indexes removeObjectAtIndex:0];
  406.     NSLog(@"NEW ARRAY : %@",indexes);
  407.     return  indexes;
  408.    
  409. }
  410.  
  411. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  412.    
  413.  
  414.    
  415.   /*
  416.     if (tapfirstCell==false) {
  417.         tapfirstCell = true;
  418.     }
  419.     else{
  420.         tapfirstCell = false;
  421.     }
  422.    */
  423.    
  424.     [self.tableView beginUpdates];
  425.    
  426.     int countOfRowss = [ScheduleView getRequestForDate].count;
  427.     if(countOfRowss>0)
  428.     {
  429.         NSArray* indexpathsArry = [self getIndexPaths];
  430.        
  431.         [self.tableView deleteRowsAtIndexPaths:indexpathsArry withRowAnimation:YES];
  432.         requestsF_date = nil;
  433.        
  434.     }
  435.    
  436.     if ([self datePickerIsShown] && (self.datePickerIndexPath.row - 1 == indexPath.row)){
  437.        
  438.         [self hideExistingPicker];
  439.        // [self.tableView reloadData];
  440.         //[self viewDidLoad];
  441.        
  442.        
  443.        
  444.         //call the service and take the results
  445.        
  446.        
  447.        
  448.        
  449.        
  450.         NSString* selecteDate = [ScheduleView getDate];
  451.        
  452.         NSString* prsonID =[LoginView getPersonID];
  453.        
  454.         NSDictionary* parms = [NSDictionary dictionaryWithObjectsAndKeys:prsonID,@"caregiverPersonId",selecteDate,@"selectedDate", nil];
  455.        
  456.         jsonpaser* jp = [[jsonpaser alloc]init];
  457.        
  458.         [jp getWebServiceResponce:@"http://qa.vardle.com/Mobile/WebServices/AppointmentService.asmx/GetAppointments" :parms success:^(NSDictionary *responseObject)
  459.          {
  460.              
  461.              
  462.              
  463.              requestsF_date = responseObject;
  464.              NSLog(@"RESPONSEFORDATE_IN DIDSELECT :%@",requestsF_date);
  465.              NSArray* indexpaths = [self getIndexPaths];
  466.              NSLog(@"indexPATHS %@",indexpaths);
  467.              [self.tableView reloadData];
  468.              
  469.          }];
  470.  
  471.        
  472.        
  473.        
  474.        
  475.        
  476.         // cellForDatePickCell *cell = (cellForDatePickCell*)[self.tableView dequeueReusableCellWithIdentifier:kOtherCellIdentifier forIndexPath:indexPath];
  477.         // cell.delegate_Dtepick = self;
  478.         //tapfirstCell = true;
  479.        /*
  480.        cellForDatePickCell *cell=(cellForDatePickCell*)[tableView cellForRowAtIndexPath:indexPath];
  481.         if(![cell.textLabel.text isEqualToString:@"5/23/14"])
  482.         {
  483.             return;
  484.         }
  485.       */
  486.        
  487.                
  488.     }else {
  489.        
  490.         //--
  491.        
  492.         NSIndexPath *newPickerIndexPath = [self calculateIndexPathForNewPicker:indexPath];
  493.        
  494.         if ([self datePickerIsShown]){
  495.            
  496.             [self hideExistingPicker];
  497.            
  498.            
  499.            
  500.         }
  501.        
  502.         [self showNewPickerAtIndex:newPickerIndexPath];
  503.        
  504.        
  505.            
  506.             self.datePickerIndexPath = [NSIndexPath indexPathForRow:newPickerIndexPath.row + 1 inSection:0];
  507.             NSLog(@"DATEPICKERINDEX %@",self.datePickerIndexPath);
  508.        
  509.        
  510.         //self.datePickerIndexPath = nil;
  511.        
  512.        
  513.        
  514.     }
  515.    
  516.     // this is the line where a row deleted
  517.     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  518.    
  519.     [self.tableView endUpdates];
  520.    
  521. }
  522.  
  523. bool tapfirstCell = false;
  524.  
  525. - (void)hideExistingPicker {
  526.      self.datePickerIndexPath = nil;
  527.     [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.datePickerIndexPath.row inSection:0]]
  528.                           withRowAnimation:UITableViewRowAnimationFade];
  529.    
  530.    
  531.    
  532. }
  533.  
  534.  
  535. - (NSIndexPath *)calculateIndexPathForNewPicker:(NSIndexPath *)selectedIndexPath {
  536.    
  537.     NSIndexPath *newIndexPath;
  538.    
  539.     if (([self datePickerIsShown]) && (self.datePickerIndexPath.row < selectedIndexPath.row)){
  540.        
  541.         newIndexPath = [NSIndexPath indexPathForRow:selectedIndexPath.row - 1 inSection:0];
  542.        
  543.     }else {
  544.        
  545.         newIndexPath = [NSIndexPath indexPathForRow:selectedIndexPath.row  inSection:0];
  546.        
  547.     }
  548.    
  549.     return newIndexPath;
  550. }
  551.  
  552. - (void)showNewPickerAtIndex:(NSIndexPath *)indexPath {
  553.    
  554.     NSArray *indexPaths = @[[NSIndexPath indexPathForRow:indexPath.row + 1 inSection:0]];
  555.    
  556.     [self.tableView insertRowsAtIndexPaths:indexPaths
  557.                           withRowAnimation:UITableViewRowAnimationFade];
  558. }
  559.  
  560.  
  561. /*
  562. // Override to support conditional editing of the table view.
  563. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  564. {
  565.     // Return NO if you do not want the specified item to be editable.
  566.     return YES;
  567. }
  568. */
  569.  
  570. /*
  571. // Override to support editing the table view.
  572. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  573. {
  574.     if (editingStyle == UITableViewCellEditingStyleDelete) {
  575.         // Delete the row from the data source
  576.         [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  577.     }  
  578.     else if (editingStyle == UITableViewCellEditingStyleInsert) {
  579.         // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
  580.     }  
  581. }
  582. */
  583.  
  584. /*
  585. // Override to support rearranging the table view.
  586. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
  587. {
  588. }
  589. */
  590.  
  591. /*
  592. // Override to support conditional rearranging of the table view.
  593. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
  594. {
  595.     // Return NO if you do not want the item to be re-orderable.
  596.     return YES;
  597. }
  598. */
  599.  
  600. /*
  601. #pragma mark - Navigation
  602.  
  603. // In a story board-based application, you will often want to do a little preparation before navigation
  604. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  605. {
  606.     // Get the new view controller using [segue destinationViewController].
  607.     // Pass the selected object to the new view controller.
  608. }
  609.  
  610.  */
  611.  
  612. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement