Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ScheduleView.m
- // varrdle_v2
- //
- // Created by Paragon Software on 3/31/14.
- // Copyright (c) 2014 Paragon Software. All rights reserved.
- // CHanged : 2014.04.09 06.30pm
- #import "ScheduleView.h"
- #import "VCPerson.h"
- #import "cellForDatePickCell.h"
- #import "jsonpaser.h"
- #import "LoginView.h"
- static NSDictionary* requestsF_date;
- static NSString* date;
- #define kDatePickerTag 1
- #define kTitleKey @"title"
- #define kDateKey @"date"
- static NSString *kPersonCellID = @"personCell";
- static NSString *kDatePickerCellID = @"datePickerCell";
- static NSString *kSegueIdentifier = @"addPerson";
- static NSString *kOtherCellIdentifier = @"otherCell";
- @interface ScheduleView ()
- //@property (weak, nonatomic) IBOutlet UITableViewCell *datePickerCell;
- //@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;
- @property (strong, nonatomic) NSIndexPath *datePickerIndexPath;
- @property (assign) NSInteger pickerCellRowHeight;
- @property (strong, nonatomic) NSDateFormatter *dateFormatter;
- @property (strong, nonatomic) NSMutableArray *persons;
- @property (strong, nonatomic) NSMutableArray *dataArray;
- - (IBAction)dateChanged:(UIDatePicker *)sender;
- @end
- @implementation ScheduleView
- +(NSString*)getDate{
- return date;
- }
- +(NSDictionary*)getRequestForDate{
- return requestsF_date;
- }
- - (id)initWithStyle:(UITableViewStyle)style
- {
- self = [super initWithStyle:style];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //Initialize the dataArray
- _dataArray = [[NSMutableArray alloc] init];
- //First section data
- NSArray *firstItemsArray = [[NSArray alloc] initWithObjects:@"Item 1", @"Item 2", @"Item 3", nil];
- NSDictionary *firstItemsArrayDict = [NSDictionary dictionaryWithObject:firstItemsArray forKey:@"data"];
- [_dataArray addObject:firstItemsArrayDict];
- //Second section data
- NSArray *secondItemsArray = [[NSArray alloc] initWithObjects:@"Item 4", @"Item 5", @"Item 6", @"Last Item", nil];
- NSDictionary *secondItemsArrayDict = [NSDictionary dictionaryWithObject:secondItemsArray forKey:@"data"];
- [_dataArray addObject:secondItemsArrayDict];
- UITableViewCell *pickerViewCellToCheck = [self.tableView dequeueReusableCellWithIdentifier:kDatePickerCellID];
- self.pickerCellRowHeight = pickerViewCellToCheck.frame.size.height;
- // NSMutableDictionary *itemOne = [@{ kTitleKey : @"Tap a cell to change its date:" } mutableCopy];
- // NSMutableDictionary *itemTwo = [@{ kTitleKey : @"Start Date",
- // kDateKey : [NSDate date] } mutableCopy];
- //
- //self.persons = @[itemOne, itemTwo];
- [self createFakeData];
- [self createDateFormatter];
- if (!self.requestsForDate) {
- self.requestsForDate = [[NSDictionary alloc]init];
- }
- // Uncomment the following line to preserve selection between presentations.
- // self.clearsSelectionOnViewWillAppear = NO;
- // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
- // self.navigationItem.rightBarButtonItem = self.editButtonItem;
- }
- - (void)createDateFormatter {
- self.dateFormatter = [[NSDateFormatter alloc] init];
- [self.dateFormatter setDateStyle:NSDateFormatterShortStyle];
- [self.dateFormatter setTimeStyle:NSDateFormatterNoStyle];
- }
- - (void)createFakeData {
- VCPerson *p1 = [[VCPerson alloc] initWithName:@"Select Date"
- dateOfBirth:[NSDate dateWithTimeIntervalSince1970:632448000]
- placeOfBirth:@"London"];
- // VCPerson *p2 = [[VCPerson alloc] initWithName:@"Jane Andersen" dateOfBirth:[NSDate dateWithTimeIntervalSince1970:123456789] placeOfBirth:@"San Francisco"];
- if (!self.persons){
- self.persons = [[NSMutableArray alloc] init];
- }
- [self.persons addObject:p1];
- // [self.persons addObject:p2];
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #pragma mark - Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- NSDictionary* reqFdate= [ScheduleView getRequestForDate];
- if (reqFdate.count == 0 ) {
- NSInteger numberOfRows = [self.persons count];
- if ([self datePickerIsShown]){
- numberOfRows++;
- }
- NSLog(@"NO OF ROWS in IF %d",numberOfRows);
- return numberOfRows;
- }
- else{
- NSLog(@"NO OF ROWS %d",reqFdate.count + 1);
- return reqFdate.count + 1;
- }
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- UITableViewCell *cell;
- NSDate *today = [NSDate date];
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- // display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
- [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
- NSString *currentTime = [dateFormatter stringFromDate:today];
- NSDate *date=[dateFormatter dateFromString:currentTime];
- if(indexPath.row==0){
- VCPerson *person = self.persons[0];
- cell = [self createPersonCell:person];
- }
- else if ([self datePickerIsShown] && (self.datePickerIndexPath.row == 1)){
- // VCPerson *person = self.persons[indexPath.row -1];
- cell = [self createPickerCell:date];
- }
- else{
- cellForDatePickCell *cell = (cellForDatePickCell*)[self.tableView dequeueReusableCellWithIdentifier:kOtherCellIdentifier];
- cell.delegate_Dtepick = self;
- return cell;
- }
- if(indexPath.section!=0 && tapfirstCell) {
- UITableViewCell *cell = (UITableViewCell*)[self.tableView dequeueReusableCellWithIdentifier:kOtherCellIdentifier forIndexPath:indexPath];
- //cell.delegate_Dtepick = self;
- NSDictionary *dictionary = [_dataArray objectAtIndex:indexPath.section];
- NSArray *array = [dictionary objectForKey:@"data"];
- NSString *cellValue = [array objectAtIndex:indexPath.row];
- cell.textLabel.text =cellValue;
- }
- return cell;
- /*
- if ([self datePickerIsShown] && (self.datePickerIndexPath.row == indexPath.row)){
- // VCPerson *person = self.persons[indexPath.row -1];
- cell = [self createPickerCell:date];
- }
- else {
- // cellForDatePickCell* cell;
- VCPerson *person = self.persons[indexPath.row];
- cell = [self createPersonCell:person];
- //cell = [self createCustomCell:indexPath];
- }
- return cell;
- */
- }
- - (cellForDatePickCell *)createCustomCell:(NSIndexPath*)indexpath {
- cellForDatePickCell *cell = (cellForDatePickCell*)[self.tableView dequeueReusableCellWithIdentifier:kOtherCellIdentifier];
- cell.delegate_Dtepick = self;
- return cell;
- }
- - (UITableViewCell *)createPersonCell:(VCPerson *)person {
- UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:kPersonCellID];
- cell.textLabel.text = person.name;
- cell.detailTextLabel.text = [self.dateFormatter stringFromDate:person.dateOfBirth];
- self.dateSelected = cell.detailTextLabel.text;
- /* NSString* selectedDate = self.dateSelected;
- NSString* personID =[LoginView getPersonID];
- NSDictionary* parms = [NSDictionary dictionaryWithObjectsAndKeys:personID,@"caregiverPersonId",selectedDate,@"selectedDate", nil];
- jsonpaser* jp = [[jsonpaser alloc]init];
- [jp getWebServiceResponce:@"http://qa.vardle.com/Mobile/WebServices/AppointmentService.asmx/GetAppointments" :parms success:^(NSDictionary *responseObject)
- {
- requestsF_date = responseObject;
- NSLog(@"RESPONSEFORDATE :%@",requestsF_date);
- }];
- NSLog(@"DATE :%@",selectedDate);
- */
- return cell;
- }
- - (UITableViewCell *)createPickerCell:(NSDate *)date {
- UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:kDatePickerCellID];
- UIDatePicker *targetedDatePicker = (UIDatePicker *)[cell viewWithTag:kDatePickerTag];
- [targetedDatePicker setDate:date animated:NO];
- return cell;
- }
- - (IBAction)dateChanged:(UIDatePicker *)sender {
- NSIndexPath *parentCellIndexPath = nil;
- if ([self datePickerIsShown]){
- parentCellIndexPath = [NSIndexPath indexPathForRow:self.datePickerIndexPath.row - 1 inSection:0];
- }else {
- return;
- }
- // cellForDatePickCell *cell = (cellForDatePickCell*)[self.tableView dequeueReusableCellWithIdentifier:kOtherCellIdentifier];
- // cell.delegate_Dtepick = self;
- UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:parentCellIndexPath];
- VCPerson *person = self.persons[parentCellIndexPath.row];
- person.dateOfBirth = sender.date;
- cell.detailTextLabel.text = [self.dateFormatter stringFromDate:sender.date];
- //set the date for the gloabal date variable to access for the web service call
- date = cell.detailTextLabel.text;
- NSLog(@"changed");
- }
- -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- CGFloat rowHeight = self.tableView.rowHeight;
- if ([self datePickerIsShown] && (self.datePickerIndexPath.row == indexPath.row)){
- rowHeight = self.pickerCellRowHeight;
- }
- return rowHeight;
- }
- - (BOOL)datePickerIsShown {
- return self.datePickerIndexPath != nil;
- }
- -(NSMutableArray*)getIndexPaths{
- int countOfRows = [ScheduleView getRequestForDate].count;
- NSMutableArray* indexes = [[NSMutableArray alloc]init];
- for (int row=0; row<=countOfRows; row++) {
- NSIndexPath* index = [NSIndexPath indexPathForRow:row inSection:0];
- [indexes addObject:index];
- }
- NSLog(@"OLD ARRAY : %@",indexes);
- [indexes removeObjectAtIndex:0];
- NSLog(@"NEW ARRAY : %@",indexes);
- return indexes;
- }
- -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- /*
- if (tapfirstCell==false) {
- tapfirstCell = true;
- }
- else{
- tapfirstCell = false;
- }
- */
- [self.tableView beginUpdates];
- int countOfRowss = [ScheduleView getRequestForDate].count;
- if(countOfRowss>0)
- {
- NSArray* indexpathsArry = [self getIndexPaths];
- [self.tableView deleteRowsAtIndexPaths:indexpathsArry withRowAnimation:YES];
- requestsF_date = nil;
- }
- if ([self datePickerIsShown] && (self.datePickerIndexPath.row - 1 == indexPath.row)){
- [self hideExistingPicker];
- // [self.tableView reloadData];
- //[self viewDidLoad];
- //call the service and take the results
- NSString* selecteDate = [ScheduleView getDate];
- NSString* prsonID =[LoginView getPersonID];
- NSDictionary* parms = [NSDictionary dictionaryWithObjectsAndKeys:prsonID,@"caregiverPersonId",selecteDate,@"selectedDate", nil];
- jsonpaser* jp = [[jsonpaser alloc]init];
- [jp getWebServiceResponce:@"http://qa.vardle.com/Mobile/WebServices/AppointmentService.asmx/GetAppointments" :parms success:^(NSDictionary *responseObject)
- {
- requestsF_date = responseObject;
- NSLog(@"RESPONSEFORDATE_IN DIDSELECT :%@",requestsF_date);
- NSArray* indexpaths = [self getIndexPaths];
- NSLog(@"indexPATHS %@",indexpaths);
- [self.tableView reloadData];
- }];
- // cellForDatePickCell *cell = (cellForDatePickCell*)[self.tableView dequeueReusableCellWithIdentifier:kOtherCellIdentifier forIndexPath:indexPath];
- // cell.delegate_Dtepick = self;
- //tapfirstCell = true;
- /*
- cellForDatePickCell *cell=(cellForDatePickCell*)[tableView cellForRowAtIndexPath:indexPath];
- if(![cell.textLabel.text isEqualToString:@"5/23/14"])
- {
- return;
- }
- */
- }else {
- //--
- NSIndexPath *newPickerIndexPath = [self calculateIndexPathForNewPicker:indexPath];
- if ([self datePickerIsShown]){
- [self hideExistingPicker];
- }
- [self showNewPickerAtIndex:newPickerIndexPath];
- self.datePickerIndexPath = [NSIndexPath indexPathForRow:newPickerIndexPath.row + 1 inSection:0];
- NSLog(@"DATEPICKERINDEX %@",self.datePickerIndexPath);
- //self.datePickerIndexPath = nil;
- }
- // this is the line where a row deleted
- [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
- [self.tableView endUpdates];
- }
- bool tapfirstCell = false;
- - (void)hideExistingPicker {
- self.datePickerIndexPath = nil;
- [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.datePickerIndexPath.row inSection:0]]
- withRowAnimation:UITableViewRowAnimationFade];
- }
- - (NSIndexPath *)calculateIndexPathForNewPicker:(NSIndexPath *)selectedIndexPath {
- NSIndexPath *newIndexPath;
- if (([self datePickerIsShown]) && (self.datePickerIndexPath.row < selectedIndexPath.row)){
- newIndexPath = [NSIndexPath indexPathForRow:selectedIndexPath.row - 1 inSection:0];
- }else {
- newIndexPath = [NSIndexPath indexPathForRow:selectedIndexPath.row inSection:0];
- }
- return newIndexPath;
- }
- - (void)showNewPickerAtIndex:(NSIndexPath *)indexPath {
- NSArray *indexPaths = @[[NSIndexPath indexPathForRow:indexPath.row + 1 inSection:0]];
- [self.tableView insertRowsAtIndexPaths:indexPaths
- withRowAnimation:UITableViewRowAnimationFade];
- }
- /*
- // Override to support conditional editing of the table view.
- - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
- {
- // Return NO if you do not want the specified item to be editable.
- return YES;
- }
- */
- /*
- // Override to support editing the table view.
- - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (editingStyle == UITableViewCellEditingStyleDelete) {
- // Delete the row from the data source
- [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
- }
- else if (editingStyle == UITableViewCellEditingStyleInsert) {
- // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
- }
- }
- */
- /*
- // Override to support rearranging the table view.
- - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
- {
- }
- */
- /*
- // Override to support conditional rearranging of the table view.
- - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
- {
- // Return NO if you do not want the item to be re-orderable.
- return YES;
- }
- */
- /*
- #pragma mark - Navigation
- // In a story board-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
- {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement