CatNiP prefinal
Sähköinen nuottikirja, HY-TKTKL-OHTUPROJ KESÄ11
|
00001 00009 #import "PlaylistsDelegateDataSource.h" 00010 #import "CatNiPViewController.h" 00011 00012 @implementation PlaylistsDelegateDataSource 00013 00014 @synthesize master; 00015 @synthesize playlists; 00016 @synthesize currentPlaylist; 00017 @synthesize playlistName; 00018 00020 -(id)init { 00021 if((self = [super init])) { 00022 self.playlists = [[NSMutableArray alloc] init]; 00023 self.currentPlaylist = nil; 00024 } 00025 return self; 00026 } 00027 00032 -(id)initWithMaster:(CatNiPViewController *)m 00033 { 00034 if ((self = [self init])) 00035 { 00036 self.master = m; 00037 } 00038 return self; 00039 } 00040 00041 #pragma mark - UITableViewDataSource protocol 00042 /*********** UITableViewDataSource protocol's functions *************/ 00043 /* */ 00044 /* */ 00045 /* For more information about these check framework's API. */ 00046 /********************************************************************/ 00047 00048 00049 00057 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 00058 { 00059 return 1; 00060 } 00061 00070 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 00071 { 00072 if (tableView == self.master.libraryTableViewLeft) 00073 { 00074 return [self.playlists count] +1; 00075 } 00076 if (tableView == self.master.libraryTableViewRight) 00077 { 00078 if(self.currentPlaylist) { 00079 return [self.currentPlaylist.scorelist count]; 00080 } 00081 } 00082 return 0; 00083 } 00084 00095 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 00096 { 00097 if (tableView == self.master.libraryTableViewLeft) return [self createTableCellLeft: indexPath]; 00098 if (tableView == self.master.libraryTableViewRight) return [self createTableCellRight: indexPath]; 00099 else return nil; 00100 } 00101 00110 - (UITableViewCell *)createTableCellLeft:(NSIndexPath *)indexPath 00111 { 00112 UITableViewCell *cell = [self.master tableView:master.libraryTableViewLeft createCellWithIdentifier:@"playlistTableLCell"]; 00113 00114 if (indexPath.row == 0) { 00115 cell.textLabel.text =@"CREATE NEW PLAYLIST"; 00116 cell.imageView.image = [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"10-medical.png"]]; 00117 if(cell.accessoryView) { 00118 cell.accessoryView = nil; 00119 } 00120 /* 00121 [button setImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"06-magnify.png"]] forState:UIControlStateNormal]; 00122 [button setImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"06-magnify.png"]] forState:UIControlStateSelected]; 00123 */ 00124 } 00125 else 00126 { 00127 // The Create new playlist row shouldn't have accessory views or a custom content view, so we set them up here 00128 cell.textLabel.text =[[self.playlists objectAtIndex:(indexPath.row -1)] name]; 00129 //[self populateContentView:cell forPlaylist:indexPath]; 00130 cell.accessoryView = [self createAccessoryViewForPlaylist:indexPath]; 00131 cell.editingAccessoryView = cell.accessoryView; 00132 if(cell.imageView) { 00133 cell.imageView.image = nil; 00134 } 00135 00136 00137 } 00138 return cell; 00139 } 00140 00149 - (UITableViewCell *)createTableCellRight:(NSIndexPath *)indexPath 00150 { 00151 UITableViewCell *cell = [self.master tableView:self.master.libraryTableViewRight createCellWithIdentifier:@"playlistTableRCell"]; 00152 if(self.currentPlaylist) { 00153 LocalScoreData *score = [self.currentPlaylist.scorelist objectAtIndex:[indexPath row]]; 00154 cell.textLabel.text = [[score composition] stringByAppendingFormat:@" - %@", [score description]]; 00155 cell.showsReorderControl = YES; 00156 } 00157 return cell; 00158 } 00159 00172 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { 00173 00174 if (tableView == self.master.libraryTableViewLeft) return; // playlists are kept in order with sorting 00175 if (tableView == self.master.libraryTableViewRight) 00176 { 00177 if(!self.currentPlaylist) { 00178 return; // Since we don't have a current playlist we shouldn't really be ordering stuff either. 00179 } 00180 if (fromIndexPath.row >= [self.currentPlaylist.scorelist count] || toIndexPath.row >= [self.currentPlaylist.scorelist count]) 00181 { 00182 [self.master.libraryTableViewRight reloadData]; 00183 return; 00184 } 00185 else 00186 { 00187 // swap scores in right playlist 00188 //NSInteger i = [[self.master.libraryTableViewLeft indexPathForSelectedRow] row] -1; // first one is "create new playlist" 00189 Playlist *pl = self.currentPlaylist; 00190 if(self.master.currentPlaylist == pl) { 00191 // we are editing the playlist that is currently open, which means we can't trust the index-based navigation 00192 // any more. So stop being in the playlist. 00193 self.master.currentPlaylist = nil; 00194 } 00195 NSObject *temp = [[[self.currentPlaylist scorelist] objectAtIndex:fromIndexPath.row] retain]; 00196 [[pl scorelist] removeObjectAtIndex:fromIndexPath.row]; 00197 [[pl scorelist] insertObject:temp atIndex:toIndexPath.row]; 00198 [temp release]; 00199 [self.master.libraryTableViewRight reloadData]; 00200 [LocalDataHandler savePlaylistsToDefaultFile:self.playlists]; 00201 } 00202 } 00203 } 00204 00216 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 00217 { 00218 if (tableView == self.master.libraryTableViewRight) 00219 { 00220 if (indexPath.row < [self.currentPlaylist.scorelist count]) return YES; 00221 } 00222 00223 return NO; 00224 } 00225 00236 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 00237 { 00238 return YES; 00239 } 00240 00246 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 00247 { 00248 if (tableView == self.master.libraryTableViewRight) 00249 { 00250 if (editingStyle == UITableViewCellEditingStyleDelete) 00251 { 00252 NSLog(@"Deleting cell at (%d, %d)", indexPath.section, indexPath.row); 00253 [[self.currentPlaylist scorelist] removeObjectAtIndex:indexPath.row]; 00254 [self.master.libraryTableViewRight reloadData]; 00255 [LocalDataHandler savePlaylistsToDefaultFile:self.playlists]; 00256 00257 } 00258 if (editingStyle == UITableViewCellEditingStyleInsert) 00259 { 00260 // no cells with this editing style 00261 } 00262 } 00263 if (tableView == self.master.libraryTableViewLeft) 00264 { 00265 if (editingStyle == UITableViewCellEditingStyleDelete) 00266 { 00267 NSLog(@"Deleting cell at (%d, %d)", indexPath.section, indexPath.row); 00268 Playlist *bye = [self.playlists objectAtIndex:indexPath.row-1]; 00269 if(self.currentPlaylist == bye) { 00270 [self.master.libraryTableViewRight reloadData]; 00271 [self.master hideViewWithAnimation:self.master.libraryViewRight]; 00272 self.currentPlaylist = nil; 00273 } 00274 [self.playlists removeObjectAtIndex:indexPath.row-1]; 00275 [self.master.libraryTableViewLeft reloadData]; 00276 [LocalDataHandler savePlaylistsToDefaultFile:self.playlists]; 00277 00278 } 00279 if (editingStyle == UITableViewCellEditingStyleInsert) 00280 { 00282 } 00283 } 00284 } 00285 00286 #pragma mark - UITableViewDelegate protocol 00287 /************ UITableViewDelegate protocol's functions **************/ 00288 /* */ 00289 /* */ 00290 /* For more information about these check framework's API. */ 00291 /********************************************************************/ 00292 00293 00306 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 00307 { 00308 NSString *t = @""; 00309 if (tableView == self.master.libraryTableViewLeft) t = @"PLAYLISTS"; 00310 if (tableView == self.master.libraryTableViewRight) 00311 { 00312 // if(!self.currentPlaylist) { 00313 // t = @"No data nooo"; 00314 // } 00315 LocalScoreData *score = [self.currentPlaylist.scorelist objectAtIndex:[indexPath row]]; 00316 t = [[score composition] stringByAppendingFormat:@" - %@", [score description]]; 00317 } 00318 00319 NSInteger height = (([t length] / 34) * 17) + 50; 00320 return height; 00321 } 00322 00333 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 00334 { 00335 if (tableView == self.master.libraryTableViewLeft) [self didSelectRowAtLeftTable:indexPath]; 00336 if (tableView == self.master.libraryTableViewRight)[self didSelectRowAtRightTable:indexPath]; 00337 } 00338 00344 - (void)didSelectRowAtLeftTable:(NSIndexPath *)indexPath 00345 { 00346 if (indexPath.row == 0) 00347 { 00348 Playlist* newPlaylist = [[Playlist alloc] init]; 00349 newPlaylist.name = @"New Playlist"; 00350 NSMutableArray *temp = [[NSMutableArray alloc] initWithArray:self.playlists]; 00351 [temp addObject:newPlaylist]; 00352 self.playlists = temp; 00353 [temp release]; 00354 [self.master.libraryTableViewLeft reloadData]; 00355 NSIndexPath* newSelected = [NSIndexPath indexPathForRow:[self.playlists count] inSection:indexPath.section]; 00356 [self.master.libraryTableViewLeft selectRowAtIndexPath:newSelected animated:YES scrollPosition:UITableViewScrollPositionMiddle]; 00357 [self setPlaylistViewContents:newSelected]; 00358 [LocalDataHandler savePlaylistsToDefaultFile:self.playlists]; 00359 [newPlaylist release]; 00360 } 00361 else // dummy implementation 00362 { 00363 self.currentPlaylist = [self.playlists objectAtIndex:[indexPath row]-1]; 00364 [self.master showViewWithAnimation:self.master.libraryViewRight]; 00365 self.master.currentPlaylist = self.currentPlaylist; 00366 } 00367 } 00368 00377 - (void)didSelectRowAtRightTable:(NSIndexPath *)indexPath 00378 { 00379 if(!self.currentPlaylist) { 00380 return; 00381 } 00382 NSInteger i = [[self.master.libraryTableViewRight indexPathForSelectedRow] row]; 00383 if (i >= [self.currentPlaylist.scorelist count]) return; 00384 00385 LocalScoreData *selectedScore = [self.currentPlaylist.scorelist objectAtIndex:i]; 00386 UITableViewCell *selectedCell = [self.master.libraryTableViewRight cellForRowAtIndexPath:indexPath]; 00387 00388 if (selectedScore != nil) 00389 { 00390 [self.master openDetailPopoverFromRect:selectedCell.frame inView:self.master.libraryTableViewRight withScore:selectedScore ofComposer:selectedScore.composer andComposition:selectedScore.composition]; 00391 } 00392 00393 } 00394 00406 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section 00407 { 00408 return 350; 00409 } 00410 00420 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section 00421 { 00422 UIView *footer = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 350)] autorelease]; 00423 footer.alpha = 0.0; // This is mandatory. Otherwise cell rows "behind" footer view won't be selectable. 00424 return footer; 00425 } 00426 00437 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 00438 { 00439 return 40; 00440 } 00441 00442 00451 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 00452 { 00453 if (tableView == self.master.libraryTableViewLeft) 00454 { 00455 UILabel *header = [[[UILabel alloc] init] autorelease]; 00456 header.text = @"PLAYLISTS"; 00457 header.frame = CGRectMake(0, 0, 200, 40); 00458 return header; 00459 00460 } 00461 else if (tableView == self.master.libraryTableViewRight) 00462 { 00463 if (self.currentPlaylist) { 00464 //header.text = self.currentPlaylist.name; 00465 CGRect fieldFrame = CGRectMake(0, 0, 200, 40); 00466 playlistName = [[[ValueTextField alloc] initWithFrame:fieldFrame] autorelease]; 00467 playlistName.delegate = self; 00468 playlistName.fieldValue = self.currentPlaylist; 00469 playlistName.text = self.currentPlaylist.name; 00470 playlistName.font = self.master.defaultFont; 00471 playlistName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 00472 playlistName.borderStyle = UITextBorderStyleRoundedRect; 00473 //playlistName.fieldValue = indexPath; 00474 return playlistName; 00475 } 00476 } 00477 return nil; 00478 } 00479 00487 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 00488 { 00489 if (tableView == self.master.libraryTableViewLeft) 00490 { 00491 if (indexPath.row == 0) 00492 { 00493 return UITableViewCellEditingStyleNone; 00494 } 00495 return UITableViewCellEditingStyleDelete; 00496 } 00497 if (tableView == self.master.libraryTableViewRight) 00498 { 00499 return UITableViewCellEditingStyleDelete; 00500 } 00501 return UITableViewCellEditingStyleNone; 00502 } 00503 00510 - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath 00511 { 00512 if (tableView == self.master.libraryTableViewLeft) 00513 return @"Delete"; 00514 if (tableView == self.master.libraryTableViewRight) 00515 return @"Remove"; 00516 return @"Delete"; 00517 } 00518 00519 /*****************************************************/ 00520 /* CUSTOM FUNCTIONS */ 00521 /*****************************************************/ 00522 00524 -(void)changeLibraryTypeToPlaylists 00525 { 00526 self.master.libraryTableViewLeft.delegate = self; 00527 self.master.libraryTableViewLeft.dataSource = self; 00528 self.master.libraryTableViewRight.delegate = self; 00529 self.master.libraryTableViewRight.dataSource = self; 00530 00531 [self.master.libraryTableViewLeft setEditing:NO animated:NO]; 00532 [self.master.libraryTableViewRight setEditing:YES animated:YES]; 00533 00534 [self.master.libraryActivityIndicatorLeft stopAnimating]; 00535 [self.master.libraryTableViewLeft selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop]; 00536 [self.master.libraryTableViewRight reloadData]; 00537 [self.master.libraryTableViewLeft reloadData]; 00538 } 00539 00540 00548 -(UIView *)createAccessoryViewForPlaylist:(NSIndexPath *)indexPath { 00549 UIView *av = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 70, 70)] autorelease]; 00550 00551 ValueButton* button = [[[ValueButton alloc] initWithValue:indexPath] autorelease]; 00552 button.frame = CGRectMake(10, 10, 50, 50); 00553 00554 [button setImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"157-wrench.png"]] forState:UIControlStateNormal]; 00555 [button setImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"157-wrench.png"]] forState:UIControlStateSelected]; 00556 [button addTarget:self action:@selector(playlistActivateClicked:) forControlEvents:UIControlEventTouchUpInside]; 00557 [av addSubview:button]; 00558 return av; 00559 00560 } 00568 -(void)playlistActivateClicked:(id)button { 00569 NSIndexPath* indexPath = (NSIndexPath*)[button buttonValue]; 00570 [self.master showViewWithAnimation:self.master.libraryViewRight]; 00571 [self setPlaylistViewContents:indexPath]; 00572 //Playlist* selectedPlaylist = [self.playlists objectAtIndex:[indexPath row]-1]; 00573 //NSLog(@"You selected playlist %@",[selectedPlaylist name]); 00574 } 00580 -(void)setPlaylistViewContents:(NSIndexPath *)indexPath { 00581 self.currentPlaylist = [self.playlists objectAtIndex:(indexPath.row -1)]; 00582 00583 // make rows draggable and editable 00584 [self.master.libraryTableViewRight setEditing:YES animated:YES]; 00585 [self.master.libraryTableViewRight reloadData]; 00586 } 00587 00592 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { 00593 return YES; 00594 } 00595 00600 -(void)textFieldDidEndEditing:(UITextField *)textField { 00601 //NSLog(@"Ymm."); 00602 ValueTextField *field = (ValueTextField*)textField; 00603 if (field.fieldValue == self.currentPlaylist) 00604 { 00605 Playlist* editedPlaylist = self.currentPlaylist; 00606 editedPlaylist.name = field.text; 00607 [self.master.libraryTableViewLeft reloadData]; 00608 [LocalDataHandler savePlaylistsToDefaultFile:self.playlists]; 00609 } 00610 } 00615 -(BOOL)textFieldShouldReturn:(UITextField *)textField { 00616 [textField resignFirstResponder]; 00617 return NO; 00618 } 00619 00620 @end