CatNiP prefinal
Sähköinen nuottikirja, HY-TKTKL-OHTUPROJ KESÄ11
|
00001 00011 #import "IMSLPDelegateDataSource.h" 00012 #import "CatNiPViewController.h" 00013 #import "IMSLPQueryHelper.h" 00014 #import "ScoreData.h" 00015 #import "CompositionData.h" 00016 00017 @implementation IMSLPDelegateDataSource 00018 00019 @synthesize master; 00020 @synthesize composers; 00021 @synthesize compositions; 00022 00024 -(id)initWithMaster:(CatNiPViewController *)m 00025 { 00026 if ((self = [super init])) 00027 { 00028 self.master = m; 00029 self.composers = [[NSMutableArray alloc] init]; 00030 self.compositions = [[NSMutableArray 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 00049 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 00050 { 00051 return 1; 00052 } 00053 00063 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 00064 { 00065 if (tableView == self.master.libraryTableViewLeft) return [self.composers count]; 00066 if (tableView == self.master.libraryTableViewRight) return [self.compositions count]; 00067 return 0; 00068 } 00069 00078 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 00079 { 00080 if (tableView == self.master.libraryTableViewLeft) return [self cellForRowAtLeftTable: indexPath]; 00081 if (tableView == self.master.libraryTableViewRight) return [self cellForRowAtRightTable: indexPath]; 00082 else return nil; 00083 } 00084 00094 - (UITableViewCell *)cellForRowAtLeftTable:(NSIndexPath *)indexPath 00095 { 00096 UITableViewCell *cell = [self.master tableView:self.master.libraryTableViewLeft createCellWithIdentifier:@"IMSLPTableLCell"]; 00097 cell.textLabel.text = [self.composers objectAtIndex:[indexPath row]]; 00098 cell.accessoryView = nil; 00099 return cell; 00100 } 00101 00110 - (UITableViewCell *)cellForRowAtRightTable: (NSIndexPath *) indexPath 00111 { 00112 UITableViewCell *cell = [self.master tableView:self.master.libraryTableViewLeft createCellWithIdentifier:@"IMSLPTableRCell"]; 00113 cell.textLabel.text = [self.compositions objectAtIndex:[indexPath row]]; 00114 cell.accessoryView = nil; 00115 return cell; 00116 } 00117 00122 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 00123 { 00124 return; 00125 } 00126 00133 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 00134 { 00135 return NO; 00136 } 00137 00144 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 00145 { 00146 return NO; 00147 } 00148 00153 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 00154 { 00155 return; 00156 } 00157 00158 #pragma mark - UITableViewDelegate protocol 00159 /************ UITableViewDelegate protocol's functions **************/ 00160 /* */ 00161 /* */ 00162 /* For more information about these check framework's API. */ 00163 /********************************************************************/ 00164 00165 00176 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 00177 { 00178 NSString *t = @""; 00179 if (tableView == self.master.libraryTableViewLeft) t = [self.composers objectAtIndex:[indexPath row]]; 00180 if (tableView ==self. master.libraryTableViewRight) t = [self.compositions objectAtIndex:[indexPath row]]; 00181 NSInteger height = (([t length] / 34) * 17) + 50; // refactor maybeh? 00182 return height; 00183 } 00184 00193 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 00194 { 00195 if (tableView == self.master.libraryTableViewLeft) [self didSelectRowAtLeftTable:indexPath]; 00196 if (tableView == self.master.libraryTableViewRight)[self didSelectRowAtRightTable:indexPath]; 00197 } 00198 00205 - (void)didSelectRowAtLeftTable:(NSIndexPath *)indexPath 00206 { 00207 [master showViewWithAnimation:master.libraryViewRight]; 00208 00209 NSString *cat = [@"Category:" stringByAppendingString:[self.composers objectAtIndex:indexPath.row]]; 00210 [IMSLPQueryHelper readIMSLPCategory:cat reportTo:self.master withMethod:@selector(imslpCompositionsReport:)]; 00211 [self.master.libraryTableViewRight setEditing:NO animated:YES]; 00212 [self.master.libraryTableViewRight reloadData]; 00213 } 00214 00222 - (void)didSelectRowAtRightTable:(NSIndexPath *)indexPath 00223 { 00224 NSString *selectedComposer = [self.composers objectAtIndex:[[self.master.libraryTableViewLeft indexPathForSelectedRow] row]]; 00225 NSString *selectedComposition = [self.compositions objectAtIndex:[[self.master.libraryTableViewRight indexPathForSelectedRow] row]]; 00226 00227 if (selectedComposition != nil) 00228 { 00229 // download information about all scores for this composition 00230 [IMSLPQueryHelper readIMSLPPage:selectedComposition composerName:selectedComposer reportTo:self.master withMethod:@selector(imslpPageReport:)]; 00231 } 00232 } 00233 00243 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section 00244 { 00245 return 350; 00246 } 00247 00257 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section 00258 { 00259 UIView *footer = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 350)] autorelease]; 00260 footer.alpha = 0.0; // This is mandatory. Otherwise cell rows "behind" footer view won't be selectable. 00261 return footer; 00262 } 00263 00272 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 00273 { 00274 return 40; 00275 } 00276 00277 00284 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 00285 { 00286 UILabel *header = [[[UILabel alloc] init] autorelease]; 00287 if (tableView == self.master.libraryTableViewLeft) 00288 header.text = @"IMSLP COMPOSERS"; 00289 if (tableView == self.master.libraryTableViewRight) 00290 { 00291 NSInteger i = [[self.master.libraryTableViewLeft indexPathForSelectedRow] row]; 00292 if (i >= [self.composers count]) header.text = @""; 00293 else header.text = [self.composers objectAtIndex:[[self.master.libraryTableViewLeft indexPathForSelectedRow] row]]; 00294 } 00295 header.frame = CGRectMake(0, 0, 200, 40); 00296 return header; 00297 } 00298 00300 -(void)changeLibraryTypeToIMSLP 00301 { 00302 self.master.libraryTableViewLeft.delegate = self; 00303 self.master.libraryTableViewLeft.dataSource = self; 00304 self.master.libraryTableViewRight.delegate = self; 00305 self.master.libraryTableViewRight.dataSource = self; 00306 00307 [self.master.libraryTableViewLeft setEditing:NO animated:YES]; 00308 [self.master.libraryTableViewRight setEditing:NO animated:YES]; 00309 00310 if ([self.composers count] == 0) // IMSLP downloading is not finished yet 00311 { 00312 // clear table contents 00313 [self.master.libraryTableViewLeft reloadData]; 00314 // and tell user something is happening on the background and wait for imslpComposerReport to reload table. 00315 [self.master.libraryActivityIndicatorLeft startAnimating]; 00316 } 00317 else 00318 { 00319 [self.master.libraryActivityIndicatorLeft stopAnimating]; // double check that indicator is not showing 00320 //[self.master.libraryDataTableLeft addObjectsFromArray:self.composers]; 00321 [self.master.libraryTableViewLeft reloadData]; 00322 [self.master.libraryTableViewRight reloadData]; 00323 } 00324 } 00325 00326 @end