Blame client/iOS/Controllers/EditorSelectionController.m

Packit Service fa4841
/*
Packit Service fa4841
 Generic controller to select a single item from a list of options
Packit Service bb5c11
 
Packit Service fa4841
 Copyright 2013 Thincast Technologies GmbH, Author: Martin Fleisz
Packit Service bb5c11
 
Packit Service bb5c11
 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. 
Packit Service bb5c11
 If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
Packit Service fa4841
 */
Packit Service fa4841
Packit Service fa4841
#import "EditorSelectionController.h"
Packit Service fa4841
#import "ConnectionParams.h"
Packit Service fa4841
#import "OrderedDictionary.h"
Packit Service fa4841
Packit Service fa4841
@interface EditorSelectionController (Private)
Packit Service bb5c11
- (OrderedDictionary*)selectionForIndex:(int)index;
Packit Service fa4841
@end
Packit Service fa4841
Packit Service fa4841
@implementation EditorSelectionController
Packit Service fa4841
Packit Service bb5c11
- (id)initWithConnectionParams:(ConnectionParams*)params entries:(NSArray *)entries selections:(NSArray *)selections
Packit Service fa4841
{
Packit Service bb5c11
    self = [super initWithStyle:UITableViewStyleGrouped];
Packit Service bb5c11
    if (self)
Packit Service bb5c11
    {
Packit Service bb5c11
        _params = [params retain];
Packit Service bb5c11
        _entries = [entries retain];
Packit Service bb5c11
        _selections = [selections retain];
Packit Service bb5c11
        
Packit Service bb5c11
        // allocate and init current selections array
Packit Service bb5c11
        _cur_selections = [[NSMutableArray alloc] initWithCapacity:[_entries count]];        
Packit Service bb5c11
        for (int i = 0; i < [entries count]; ++i)
Packit Service bb5c11
        {
Packit Service bb5c11
            NSString* entry = [entries objectAtIndex:i];            
Packit Service bb5c11
            if([_params hasValueForKeyPath:entry])
Packit Service bb5c11
            {
Packit Service bb5c11
                NSUInteger idx = [(OrderedDictionary*)[selections objectAtIndex:i] indexForValue:[NSNumber numberWithInt:[_params intForKeyPath:entry]]];
Packit Service bb5c11
                [_cur_selections addObject:[NSNumber numberWithInt:(idx != NSNotFound ? idx : 0)]];
Packit Service bb5c11
            }
Packit Service bb5c11
            else
Packit Service bb5c11
                [_cur_selections addObject:[NSNumber numberWithInt:0]];
Packit Service bb5c11
        }
Packit Service bb5c11
    }
Packit Service bb5c11
    return self;
Packit Service fa4841
}
Packit Service fa4841
Packit Service fa4841
- (void)didReceiveMemoryWarning
Packit Service fa4841
{
Packit Service bb5c11
    // Releases the view if it doesn't have a superview.
Packit Service bb5c11
    [super didReceiveMemoryWarning];
Packit Service bb5c11
    
Packit Service bb5c11
    // Release any cached data, images, etc that aren't in use.
Packit Service bb5c11
    [_params autorelease];
Packit Service bb5c11
    [_entries autorelease];
Packit Service bb5c11
    [_selections autorelease];    
Packit Service bb5c11
    [_cur_selections autorelease];
Packit Service fa4841
}
Packit Service fa4841
Packit Service fa4841
#pragma mark - View lifecycle
Packit Service fa4841
Packit Service fa4841
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
Packit Service fa4841
{
Packit Service bb5c11
    // Return YES for supported orientations
Packit Service bb5c11
    return YES;
Packit Service fa4841
}
Packit Service fa4841
Packit Service fa4841
#pragma mark - Table view data source
Packit Service fa4841
Packit Service fa4841
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
Packit Service fa4841
{
Packit Service bb5c11
    // Return the number of sections.
Packit Service bb5c11
    return [_entries count];
Packit Service fa4841
}
Packit Service fa4841
Packit Service fa4841
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
Packit Service fa4841
{
Packit Service bb5c11
    // Return the number of rows in the section.
Packit Service bb5c11
    return [[self selectionForIndex:section] count];
Packit Service fa4841
}
Packit Service fa4841
Packit Service bb5c11
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Packit Service fa4841
{
Packit Service bb5c11
    UITableViewCell *cell = [self tableViewCellFromIdentifier:TableCellIdentifierMultiChoice];
Packit Service bb5c11
    
Packit Service bb5c11
    // get selection
Packit Service bb5c11
    OrderedDictionary* selection = [self selectionForIndex:[indexPath section]];
Packit Service bb5c11
    
Packit Service bb5c11
    // set cell properties
Packit Service fa4841
	[[cell textLabel] setText:[selection keyAtIndex:[indexPath row]]];
Packit Service bb5c11
	
Packit Service fa4841
	// set default checkmark
Packit Service bb5c11
	if([indexPath row] == [[_cur_selections objectAtIndex:[indexPath section]] intValue])
Packit Service fa4841
		[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
Packit Service fa4841
	else
Packit Service fa4841
		[cell setAccessoryType:UITableViewCellAccessoryNone];
Packit Service bb5c11
	
Packit Service bb5c11
    return cell;
Packit Service fa4841
}
Packit Service fa4841
Packit Service fa4841
#pragma mark - Table view delegate
Packit Service fa4841
Packit Service fa4841
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Packit Service fa4841
{
Packit Service fa4841
	// has selection change?
Packit Service bb5c11
    int cur_selection = [[_cur_selections objectAtIndex:[indexPath section]] intValue];
Packit Service bb5c11
	if([indexPath row] != cur_selection)
Packit Service fa4841
	{
Packit Service fa4841
		[tableView deselectRowAtIndexPath:indexPath animated:NO];
Packit Service bb5c11
		
Packit Service bb5c11
		NSIndexPath* oldIndexPath = [NSIndexPath indexPathForRow:cur_selection inSection:[indexPath section]];
Packit Service bb5c11
        
Packit Service fa4841
		// clear old checkmark
Packit Service bb5c11
		UITableViewCell* old_sel_cell = [tableView cellForRowAtIndexPath:oldIndexPath];
Packit Service fa4841
		old_sel_cell.accessoryType = UITableViewCellAccessoryNone;
Packit Service bb5c11
        
Packit Service fa4841
		// set new checkmark
Packit Service bb5c11
		UITableViewCell* new_sel_cell = [tableView cellForRowAtIndexPath:indexPath];
Packit Service fa4841
		new_sel_cell.accessoryType = UITableViewCellAccessoryCheckmark;
Packit Service fa4841
Packit Service bb5c11
        // get value from selection dictionary
Packit Service bb5c11
        OrderedDictionary* dict = [self selectionForIndex:[indexPath section]];
Packit Service bb5c11
        int sel_value = [[dict valueForKey:[dict keyAtIndex:[indexPath row]]] intValue];
Packit Service fa4841
Packit Service fa4841
		// update selection index and params value
Packit Service bb5c11
        [_cur_selections replaceObjectAtIndex:[indexPath section] withObject:[NSNumber numberWithInt:[indexPath row]]];
Packit Service bb5c11
        [_params setInt:sel_value forKeyPath:[_entries objectAtIndex:[indexPath section]]];
Packit Service bb5c11
    }    
Packit Service fa4841
}
Packit Service fa4841
Packit Service fa4841
#pragma mark - Convenience functions
Packit Service fa4841
Packit Service bb5c11
- (OrderedDictionary*)selectionForIndex:(int)index
Packit Service fa4841
{
Packit Service bb5c11
    return (OrderedDictionary*)[_selections objectAtIndex:index];
Packit Service fa4841
}
Packit Service fa4841
Packit Service fa4841
@end