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