CatNiP prefinal
Sähköinen nuottikirja, HY-TKTKL-OHTUPROJ KESÄ11
/Users/awniemel/Notepad-SVN/svn/trunk/CatNiP/CatNiP/LocalDelegateDataSource.m
Go to the documentation of this file.
00001 
00010 #define LIBRARYTYPE self.master.libraryType.selectedSegmentIndex 
00011 
00012 #import "LocalDelegateDataSource.h"
00013 #import "CatNiPViewController.h"
00014 
00015 @implementation LocalDelegateDataSource
00016 
00017 @synthesize master;
00018 @synthesize composers;
00019 @synthesize compositions;
00020 @synthesize allLocalScores;
00021 @synthesize composerDictionary;
00022 
00024 -(id)initWithMaster:(CatNiPViewController *)m 
00025 {
00026     if ((self = [super init])) 
00027     {
00028         self.master = m;
00029         self.composers = [[NSArray alloc] init];
00030         self.compositions = [[NSArray alloc] init];
00031     }
00032     return self;
00033 }
00034 
00035 #pragma mark - UITableViewDataSource protocol
00036 /*********** UITableViewDataSource protocol's functions *************/
00037 /*                                                                  */
00038 /*                                                                  */
00039 /*  For more information about these check framework's API.         */
00040 /********************************************************************/
00041 
00042 
00043 
00051 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
00052 {
00053        return 1;
00054 }
00055 
00064 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
00065 {
00066     if (tableView == self.master.libraryTableViewLeft)  return [self.composers count];
00067     if (tableView == self.master.libraryTableViewRight) return [self.compositions count];
00068     return 0;
00069 }
00070 
00081 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
00082 {    
00083     if (tableView == self.master.libraryTableViewLeft)  return [self createTableCellLeft: indexPath];    
00084     if (tableView == self.master.libraryTableViewRight) return [self createTableCellRight: indexPath];        
00085     else return nil;
00086 }
00087 
00096 - (UITableViewCell *)createTableCellLeft:(NSIndexPath *)indexPath 
00097 {
00098     UITableViewCell *cell = [self.master tableView:self.master.libraryTableViewLeft createCellWithIdentifier:@"localTableLCell"];
00099     cell.textLabel.text = [self.composers objectAtIndex:[indexPath row]];
00100     cell.editingAccessoryView = nil;
00101     cell.accessoryView = nil;
00102     return cell;
00103 }
00104 
00113 - (UITableViewCell *)createTableCellRight: (NSIndexPath *) indexPath  
00114 {
00115     UITableViewCell *cell = [self.master tableView:self.master.libraryTableViewRight createCellWithIdentifier:@"localTableRCell"];   
00116     cell.textLabel.text = [[self.compositions objectAtIndex:[indexPath row]] compositionName];
00117     cell.imageView.frame = CGRectMake(0, 0, 0, 0);
00118     cell.accessoryView = nil;
00119     return cell;
00120 }
00121 
00126 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
00127 {
00128     return;
00129 }
00130 
00137 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
00138 {
00139     return NO;
00140 }
00141 
00146 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
00147 {
00148     return YES;
00149 }
00150 
00156 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
00157 {
00158     if (editingStyle == UITableViewCellEditingStyleDelete) // show alert popup for delete confirmation
00159     {
00160         if (tableView == self.master.libraryTableViewLeft) 
00161         {
00162             NSString *composer = [self.composers objectAtIndex:indexPath.row];
00163             if ([composer compare:self.master.currentScore.composer] == 0)
00164             {
00165                 UIAlertView *a = [[[UIAlertView alloc] initWithTitle:@"Operation can't be completed" message:@"Score that is currently showing can't be deleted" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
00166                 [a show];
00167             
00168             }
00169             else 
00170             {
00171                 NSString *title = @"Are you sure you want to delete composer?";
00172                 UIAlertView *confirm = [[[UIAlertView alloc] initWithTitle:title message:composer delegate:self cancelButtonTitle:@"Oops!" otherButtonTitles:@"Yes, sir!", nil] autorelease];
00173                 [confirm show];
00174             }
00175         }
00176         if (tableView == self.master.libraryTableViewRight) 
00177         {
00178             CompositionData *cd = [self.compositions objectAtIndex:indexPath.row];
00179             if ([cd.composerName compare:self.master.currentScore.composer] == 0 && [cd.compositionName compare:self.master.currentScore.composition] == 0)
00180             {
00181                 UIAlertView *a = [[[UIAlertView alloc] initWithTitle:@"Operation can't be completed" message:@"Score that is currently showing can't be deleted" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
00182                 [a show];
00183                 
00184             }
00185             else 
00186             {    
00187                 NSString *title = @"Are you sure you want to delete composition?";
00188                 NSString *message = [[self.compositions objectAtIndex:indexPath.row] compositionName];
00189                 UIAlertView *confirm = [[[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"Oops!" otherButtonTitles:@"Yes, sir!", nil] autorelease];
00190                 [confirm show];
00191             }
00192         }
00193     }
00194     if (editingStyle == UITableViewCellEditingStyleInsert)
00195     {
00196         // nothing to be done
00197     }
00198 }
00199 
00200 #pragma mark - UITableViewDelegate protocol
00201 /************ UITableViewDelegate protocol's functions **************/
00202 /*                                                                  */
00203 /*                                                                  */
00204 /*  For more information about these check framework's API.         */
00205 /********************************************************************/
00206 
00207 
00218 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
00219 {
00220     NSString *t = @"";
00221     if (tableView == self.master.libraryTableViewLeft) t = [self.composers objectAtIndex:[indexPath row]];  
00222     if (tableView == self.master.libraryTableViewRight) t = [[self.compositions objectAtIndex:[indexPath row]] compositionName];  
00223     NSInteger height = (([t length] / 34) * 17) + 50;
00224     return height;
00225 }
00226 
00235 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
00236 {
00237     if (tableView == self.master.libraryTableViewLeft) [self didSelectRowAtLeftTable:indexPath];
00238     if (tableView == self.master.libraryTableViewRight)[self didSelectRowAtRightTable:indexPath];
00239 }
00240 
00247 - (void)didSelectRowAtLeftTable:(NSIndexPath *)indexPath 
00248 {
00249     [self.master showViewWithAnimation:self.master.libraryViewRight];
00250     
00251     NSString *selectedComposer = [self.composers objectAtIndex:indexPath.row];
00252     NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"composerName" ascending:YES] autorelease];
00253     NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
00254     [self setCompositions:[[self.composerDictionary valueForKey:selectedComposer] sortedArrayUsingDescriptors:sortDescriptors]];
00255     [self.master.libraryTableViewRight reloadData];    
00256 
00257 }
00258 
00265 - (void)didSelectRowAtRightTable:(NSIndexPath *)indexPath 
00266 {
00267      CompositionData *selectedComposition = [self.compositions objectAtIndex:indexPath.row];
00268     
00269     if (selectedComposition != nil)
00270     {   
00271         [self.master openScorePopover:selectedComposition editable:YES];
00272     }
00273 }
00274 
00284 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section 
00285 {
00286     return 350;
00287 }
00288 
00298 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section 
00299 {
00300     UIView *footer = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 350)] autorelease];
00301     footer.alpha = 0.0; // This is mandatory. Otherwise cell rows "behind" footer view won't be selectable.
00302     return footer;
00303 }
00304 
00311 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
00312 {
00313     return 40;
00314 }
00315 
00322 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
00323 { 
00324     UILabel *header = [[[UILabel alloc] init] autorelease];  
00325     if (tableView == self.master.libraryTableViewLeft) 
00326     {
00327        header.text = @"LOCAL COMPOSERS";
00328     }
00329     
00330     if (tableView == self.master.libraryTableViewRight)
00331     {
00332         if ([self.composers count] > [[self.master.libraryTableViewLeft indexPathForSelectedRow] row]) 
00333         {
00334             NSString *selectedComposer = [self.composers objectAtIndex:[[self.master.libraryTableViewLeft indexPathForSelectedRow] row]];
00335             header.text = selectedComposer;
00336         }
00337     }
00338     header.frame = CGRectMake(0, 0, 200, 40);
00339     return header;
00340 }
00341 
00350 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
00351 {
00352     return UITableViewCellEditingStyleDelete;
00353 }
00354 
00355 /*****************************************************/
00356 /*            CUSTOM FUNCTIONS                       */
00357 /*****************************************************/
00358 
00360 -(void)changeLibraryTypeToLocal 
00361 {
00362     self.composerDictionary = [LocalDataHandler createComposerDictionary:allLocalScores];
00363     [self setComposers:[[composerDictionary allKeys] sortedArrayUsingSelector:@selector(compare:)]];
00364     NSLog(@"Composers count %d", [self.composers count]);
00365     
00366     self.master.libraryTableViewLeft.delegate = self;
00367     self.master.libraryTableViewLeft.dataSource = self;
00368     self.master.libraryTableViewRight.delegate = self;
00369     self.master.libraryTableViewRight.dataSource = self;
00370     
00371     [self.master.libraryTableViewLeft setEditing:NO animated:YES];
00372     [self.master.libraryTableViewRight setEditing:NO animated:YES];
00373     
00374     [self.master.libraryActivityIndicatorLeft stopAnimating];
00375     [self.master.libraryTableViewRight reloadData];
00376     [self.master.libraryTableViewLeft reloadData];   
00377 }
00378 
00381 -(void)updateTables 
00382 {
00383     self.composerDictionary = [LocalDataHandler createComposerDictionary:allLocalScores];
00384     [self setComposers:[[composerDictionary allKeys] sortedArrayUsingSelector:@selector(compare:)]];
00385     
00386     if ([[self.master.libraryTableViewLeft indexPathForSelectedRow] row] < [self.composers count])
00387     {
00388         NSString *selectedComposer = [self.composers objectAtIndex:[[self.master.libraryTableViewLeft indexPathForSelectedRow] row]];
00389         NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"composerName" ascending:YES] autorelease];
00390         NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
00391         [self setCompositions:[[self.composerDictionary valueForKey:selectedComposer] sortedArrayUsingDescriptors:sortDescriptors]];
00392     }
00393     else 
00394     {
00395         [self setCompositions:NULL];
00396     }
00397     [self.master.libraryTableViewRight reloadData];
00398     [self.master.libraryTableViewLeft reloadData]; 
00399 }
00400 
00405 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
00406 {
00407     if (alertView.title == @"Are you sure you want to delete composer?") 
00408     {
00409         if (buttonIndex == 0) // cancel button clicked, do nothing
00410             NSLog(@"Cancel button clicked");
00411         if  (buttonIndex == 1) // ok button clicked, remove composers all compositions and scores from local memory
00412         {
00413             // if any of the composer's compositions is in currently viewed playlist stop viewing playlist.
00414             NSArray *composersCompositions = [self.composerDictionary objectForKey:alertView.message];
00415             if ([LocalDataHandler compositionDataArray:composersCompositions hasScoreWithPdfInScoreArray:self.master.currentPlaylist.scorelist]) 
00416                 self.master.currentPlaylist = nil;
00417             
00418             // remove and delete it and update user view afterwards
00419             [LocalDataHandler removeAndDeleteCompositionDataArray:composersCompositions fromScoreArray:self.allLocalScores andDictionary:self.composerDictionary andPlaylistArray:self.master.playlistsDelegateAndDataSource.playlists];
00420             [LocalDataHandler saveScoresToDefaultFile:self.allLocalScores];
00421             [LocalDataHandler savePlaylistsToDefaultFile:self.master.playlistsDelegateAndDataSource.playlists];
00422             [self updateTables];
00423         }
00424     }
00425     if (alertView.title == @"Are you sure you want to delete composition?") 
00426     {
00427         if (buttonIndex == 0) // cancel button clicked, do nothing 
00428             NSLog(@"Cancel button clicked");
00429         if  (buttonIndex == 1) // ok button clicked, remove all scores for the composition
00430         { 
00431             CompositionData *compositionToDelete;
00432             for (CompositionData *cd in compositions)
00433             {
00434                 if ([cd.compositionName compare:alertView.message] == 0)
00435                     compositionToDelete = cd;
00436             }
00437             if (compositionToDelete != NULL) 
00438             {
00439                 // if any of the composition's scores is in currently viewed playlist stop viewing playlist.
00440                 if ([LocalDataHandler compositionDataArray:[[NSArray alloc] initWithObjects:compositionToDelete, nil]hasScoreWithPdfInScoreArray:self.master.currentPlaylist.scorelist]) 
00441                     self.master.currentPlaylist = nil;
00442                 
00443                 // remove and delete it and update user view afterwards
00444                 [LocalDataHandler removeAndDeleteScoreDataArray:[NSArray arrayWithArray:compositionToDelete.musicScores] fromScoreArray:self.allLocalScores andDictionary:self.composerDictionary andPlaylistArray:self.master.playlistsDelegateAndDataSource.playlists];
00445                 [LocalDataHandler saveScoresToDefaultFile:self.allLocalScores];
00446                 [LocalDataHandler savePlaylistsToDefaultFile:self.master.playlistsDelegateAndDataSource.playlists];
00447             }
00448             
00449             [self updateTables];
00450         }
00451     }
00452 
00453 }
00454 
00455 @end
 All Classes Files Functions Variables Enumerations Enumerator Properties Defines