Blame client/iOS/Controllers/ScreenSelectionController.m

Packit 1fb8d4
/*
Packit 1fb8d4
 controller for screen settings selection
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 "ScreenSelectionController.h"
Packit 1fb8d4
#import "Utils.h"
Packit 1fb8d4
#import "OrderedDictionary.h"
Packit 1fb8d4
#import "ConnectionParams.h"
Packit 1fb8d4
Packit 1fb8d4
@interface ScreenSelectionController (Private)
Packit Service 5a9772
- (NSString *)keyPathForKey:(NSString *)key;
Packit 1fb8d4
@end
Packit 1fb8d4
Packit 1fb8d4
@implementation ScreenSelectionController
Packit 1fb8d4
Packit Service 5a9772
- (id)initWithConnectionParams:(ConnectionParams *)params
Packit 1fb8d4
{
Packit Service 5a9772
	return [self initWithConnectionParams:params keyPath:nil];
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
- (id)initWithConnectionParams:(ConnectionParams *)params keyPath:(NSString *)keyPath
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
		_keyPath = (keyPath != nil ? [keyPath retain] : nil);
Packit Service 5a9772
Packit Service 5a9772
		_color_options = (OrderedDictionary *)[SelectionForColorSetting() retain];
Packit Service 5a9772
		_resolution_modes = [ResolutionModes() retain];
Packit Service 5a9772
Packit Service 5a9772
		// init current selections
Packit Service 5a9772
		NSUInteger idx = [_color_options
Packit Service 5a9772
		    indexForValue:[NSNumber
Packit Service 5a9772
		                      numberWithInt:[_params
Packit Service 5a9772
		                                        intForKeyPath:[self keyPathForKey:@"colors"]]]];
Packit Service 5a9772
		_selection_color = (idx != NSNotFound) ? idx : 0;
Packit Service 5a9772
Packit Service 5a9772
		idx = [_resolution_modes
Packit Service 5a9772
		    indexOfObject:ScreenResolutionDescription(
Packit Service 5a9772
		                      [_params
Packit Service 5a9772
		                          intForKeyPath:[self keyPathForKey:@"screen_resolution_type"]],
Packit Service 5a9772
		                      [_params intForKeyPath:[self keyPathForKey:@"width"]],
Packit Service 5a9772
		                      [_params intForKeyPath:[self keyPathForKey:@"height"]])];
Packit Service 5a9772
		_selection_resolution = (idx != NSNotFound) ? idx : 0;
Packit Service 5a9772
	}
Packit Service 5a9772
	return self;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)dealloc
Packit 1fb8d4
{
Packit Service 5a9772
	[super dealloc];
Packit Service 5a9772
	[_params autorelease];
Packit Service 5a9772
	[_keyPath autorelease];
Packit Service 5a9772
	[_color_options autorelease];
Packit Service 5a9772
	[_resolution_modes autorelease];
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
- (NSString *)keyPathForKey:(NSString *)key
Packit 1fb8d4
{
Packit Service 5a9772
	if (_keyPath)
Packit Service 5a9772
		return [_keyPath stringByAppendingFormat:@".%@", key];
Packit Service 5a9772
	return key;
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 2;
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
	if (section == 0)
Packit Service 5a9772
		return [_color_options count];
Packit Service 5a9772
	return [_resolution_modes count] + 2; // +2 for custom width/height input fields
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
- (UITableViewCell *)tableView:(UITableView *)tableView
Packit Service 5a9772
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
Packit 1fb8d4
{
Packit Service 5a9772
	UITableViewCell *cell = nil;
Packit Service 5a9772
	switch ([indexPath section])
Packit Service 5a9772
	{
Packit Service 5a9772
		case 0:
Packit Service 5a9772
			cell = [self tableViewCellFromIdentifier:TableCellIdentifierMultiChoice];
Packit Service 5a9772
			[[cell textLabel] setText:[_color_options keyAtIndex:[indexPath row]]];
Packit Service 5a9772
			break;
Packit Service 5a9772
Packit Service 5a9772
		case 1:
Packit Service 5a9772
			if ([indexPath row] < [_resolution_modes count])
Packit Service 5a9772
			{
Packit Service 5a9772
				cell = [self tableViewCellFromIdentifier:TableCellIdentifierMultiChoice];
Packit Service 5a9772
				[[cell textLabel] setText:[_resolution_modes objectAtIndex:[indexPath row]]];
Packit Service 5a9772
			}
Packit Service 5a9772
			else
Packit Service 5a9772
				cell = [self tableViewCellFromIdentifier:TableCellIdentifierText];
Packit Service 5a9772
			break;
Packit Service 5a9772
Packit Service 5a9772
		default:
Packit Service 5a9772
			break;
Packit Service 5a9772
	}
Packit Service 5a9772
Packit Service 5a9772
	if ([indexPath section] == 1)
Packit Service 5a9772
	{
Packit Service 5a9772
		BOOL enabled = ([_params intForKeyPath:[self keyPathForKey:@"screen_resolution_type"]] ==
Packit Service 5a9772
		                TSXScreenOptionCustom);
Packit Service 5a9772
		if ([indexPath row] == [_resolution_modes count])
Packit Service 5a9772
		{
Packit Service 5a9772
			int value = [_params intForKeyPath:[self keyPathForKey:@"width"]];
Packit Service 5a9772
			EditTextTableViewCell *textCell = (EditTextTableViewCell *)cell;
Packit Service 5a9772
			[[textCell label] setText:NSLocalizedString(@"Width", @"Custom Screen Width")];
Packit Service 5a9772
			[[textCell textfield] setText:[NSString stringWithFormat:@"%d", value ? value : 800]];
Packit Service 5a9772
			[[textCell textfield] setKeyboardType:UIKeyboardTypeNumberPad];
Packit Service 5a9772
			[[textCell label] setEnabled:enabled];
Packit Service 5a9772
			[[textCell textfield] setEnabled:enabled];
Packit Service 5a9772
			[[textCell textfield] setTag:1];
Packit Service 5a9772
		}
Packit Service 5a9772
		else if ([indexPath row] == ([_resolution_modes count] + 1))
Packit Service 5a9772
		{
Packit Service 5a9772
			int value = [_params intForKeyPath:[self keyPathForKey:@"height"]];
Packit Service 5a9772
			EditTextTableViewCell *textCell = (EditTextTableViewCell *)cell;
Packit Service 5a9772
			[[textCell label] setText:NSLocalizedString(@"Height", @"Custom Screen Height")];
Packit Service 5a9772
			[[textCell textfield] setText:[NSString stringWithFormat:@"%d", value ? value : 600]];
Packit Service 5a9772
			[[textCell textfield] setKeyboardType:UIKeyboardTypeNumberPad];
Packit Service 5a9772
			[[textCell label] setEnabled:enabled];
Packit Service 5a9772
			[[textCell textfield] setEnabled:enabled];
Packit Service 5a9772
			[[textCell textfield] setTag:2];
Packit Service 5a9772
		}
Packit Service 5a9772
	}
Packit Service 5a9772
Packit 1fb8d4
	// set default checkmark
Packit Service 5a9772
	if ([indexPath row] == ([indexPath section] == 0 ? _selection_color : _selection_resolution))
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 Service 5a9772
	// custom widht/height cells are not selectable
Packit Service 5a9772
	if ([indexPath section] == 1 && [indexPath row] >= [_resolution_modes count])
Packit Service 5a9772
		return;
Packit Service 5a9772
Packit 1fb8d4
	// has selection change?
Packit Service 5a9772
	int cur_selection = ([indexPath section] == 0 ? _selection_color : _selection_resolution);
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 Service 5a9772
Packit Service 5a9772
		if ([indexPath section] == 0)
Packit Service 5a9772
		{
Packit Service 5a9772
			// get value from color dictionary
Packit Service 5a9772
			int sel_value =
Packit Service 5a9772
			    [[_color_options valueForKey:[_color_options keyAtIndex:[indexPath row]]] intValue];
Packit Service 5a9772
Packit Service 5a9772
			// update selection index and params value
Packit Service 5a9772
			[_params setInt:sel_value forKeyPath:[self keyPathForKey:@"colors"]];
Packit Service 5a9772
			_selection_color = [indexPath row];
Packit Service 5a9772
		}
Packit Service 5a9772
		else
Packit Service 5a9772
		{
Packit Service 5a9772
			// update selection index and params value
Packit Service 5a9772
			int width, height;
Packit Service 5a9772
			TSXScreenOptions mode;
Packit Service 5a9772
			ScanScreenResolution([_resolution_modes objectAtIndex:[indexPath row]], &width, &height,
Packit Service 5a9772
			                     &mode);
Packit Service 5a9772
			[_params setInt:mode forKeyPath:[self keyPathForKey:@"screen_resolution_type"]];
Packit Service 5a9772
			if (mode != TSXScreenOptionCustom)
Packit Service 5a9772
			{
Packit Service 5a9772
				[_params setInt:width forKeyPath:[self keyPathForKey:@"width"]];
Packit Service 5a9772
				[_params setInt:height forKeyPath:[self keyPathForKey:@"height"]];
Packit Service 5a9772
			}
Packit Service 5a9772
			_selection_resolution = [indexPath row];
Packit Service 5a9772
Packit Service 5a9772
			// refresh width/height edit fields if custom selection changed
Packit Service 5a9772
			NSArray *indexPaths = [NSArray
Packit Service 5a9772
			    arrayWithObjects:[NSIndexPath indexPathForRow:[_resolution_modes count]
Packit Service 5a9772
			                                        inSection:1],
Packit Service 5a9772
			                     [NSIndexPath indexPathForRow:([_resolution_modes count] + 1)
Packit Service 5a9772
			                                        inSection:1],
Packit Service 5a9772
			                     nil];
Packit Service 5a9772
			[[self tableView] reloadRowsAtIndexPaths:indexPaths
Packit Service 5a9772
			                        withRowAnimation:UITableViewRowAnimationNone];
Packit Service 5a9772
		}
Packit Service 5a9772
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#pragma mark -
Packit 1fb8d4
#pragma mark Text Field delegate
Packit 1fb8d4
Packit Service 5a9772
- (BOOL)textFieldShouldReturn:(UITextField *)textField
Packit 1fb8d4
{
Packit 1fb8d4
	[textField resignFirstResponder];
Packit 1fb8d4
	return NO;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
Packit 1fb8d4
{
Packit Service 5a9772
Packit Service 5a9772
	switch ([textField tag])
Packit Service 5a9772
	{
Packit Service 5a9772
			// update resolution settings (and check for invalid input)
Packit 1fb8d4
		case 1:
Packit Service 5a9772
			if ([[textField text] intValue] < 640)
Packit Service 5a9772
				[textField setText:@"640"];
Packit 1fb8d4
			[_params setInt:[[textField text] intValue] forKeyPath:[self keyPathForKey:@"width"]];
Packit 1fb8d4
			break;
Packit Service 5a9772
Packit 1fb8d4
		case 2:
Packit Service 5a9772
			if ([[textField text] intValue] < 480)
Packit Service 5a9772
				[textField setText:@"480"];
Packit 1fb8d4
			[_params setInt:[[textField text] intValue] forKeyPath:[self keyPathForKey:@"height"]];
Packit 1fb8d4
			break;
Packit Service 5a9772
Packit 1fb8d4
		default:
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
	return YES;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
@end