Blame client/iOS/Controllers/EditorSelectionController.m

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