Bronze Challenge Solution


#1

Well, this one went pretty quickly. SImply unhid the UIToolBar, added two items plus some flexspace bar items, a variable to hold the numberOfSongsToFetch, and a method for changing numberOfSongsToFetch. All in ListViewController.

ListViewController.m

-(void)changeNumberOfSongsToFetch:(UIBarButtonItem *)bbi
{
    if ([[bbi title] isEqualToString:@"More Songs"])
    {
        numberofSongsToFetch++;
    }
    else if ([[bbi title] isEqualToString:@"Less Songs"])
    {
        numberofSongsToFetch--;
    }
    
    [self fetchEntries];
}


-(void)fetchEntries
{
    //Get the titleview and create an activityview
    UIView *currentTitleView = [[self navigationItem] titleView];
    
    UIActivityIndicatorView *aiView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    
    [[self navigationItem] setTitleView:aiView];
    [aiView startAnimating];
    
    
    void(^completionBlock)(RSSChannel *obj, NSError *err) = ^(RSSChannel *obj, NSError *err)
    {
        [[self navigationItem] setTitleView:currentTitleView];
        if (!err)
        {
            channel = obj;
            [[self tableView] reloadData];
        }
        else
        {
            NSString *errorString = [[NSString alloc] initWithFormat:@"Fetch failed: %@", [err localizedDescription]];
            
            UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error"
                                                         message:errorString
                                                        delegate:nil
                                                cancelButtonTitle:@"OK"
                                               otherButtonTitles:nil];
            [av show];
        }
    };
    
    if (rssType ==ListViewControllerRSSTypeBNR)
    {
        [[BNRFeedStore sharedStore] fetchRSSFeedWithCompletion:completionBlock];
        
        [[self navigationController] setToolbarHidden:YES animated:YES];
    
    }
    else if (rssType == ListViewControllerRSSTypeApple)
    {
        [[BNRFeedStore sharedStore] fetchTopSongs:numberofSongsToFetch withCompletion:completionBlock];
        
        [[self navigationController] setToolbarHidden:NO animated:YES];
        UIToolbar *tb = [[self navigationController] toolbar];
        UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                                   target:nil
                                                                                   action:nil];

        UIBarButtonItem *addSong = [[UIBarButtonItem alloc] initWithTitle:@"More Songs"
                                                                    style:UIBarButtonItemStyleBordered
                                                                   target:self
                                                                   action:@selector(changeNumberOfSongsToFetch:)];
        UIBarButtonItem *subtractSong = [[UIBarButtonItem alloc] initWithTitle:@"Less Songs"
                                                                         style:UIBarButtonItemStyleBordered
                                                                        target:self
                                                                        action:@selector(changeNumberOfSongsToFetch:)];
        NSArray *tbitems = [NSArray arrayWithObjects:flexSpace, addSong, subtractSong, flexSpace,nil];
        [tb setItems:tbitems];

    }
}

@end