Blame client/iOS/Controllers/ScreenSelectionController.m

Packit 1fb8d4
/*
Packit 1fb8d4
 controller for screen settings selection
Packit 1fb8d4
 
Packit 1fb8d4
 Copyright 2013 Thincast Technologies GmbH, Author: Martin Fleisz
Packit 1fb8d4
 
Packit 1fb8d4
 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. 
Packit 1fb8d4
 If a copy of the MPL was not distributed with this file, You can obtain one at 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 1fb8d4
-(NSString*)keyPathForKey:(NSString*)key;
Packit 1fb8d4
@end
Packit 1fb8d4
Packit 1fb8d4
@implementation ScreenSelectionController
Packit 1fb8d4
Packit 1fb8d4
- (id)initWithConnectionParams:(ConnectionParams*)params
Packit 1fb8d4
{
Packit 1fb8d4
    return [self initWithConnectionParams:params keyPath:nil];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (id)initWithConnectionParams:(ConnectionParams*)params keyPath:(NSString*)keyPath
Packit 1fb8d4
{
Packit 1fb8d4
    self = [super initWithStyle:UITableViewStyleGrouped];
Packit 1fb8d4
    if (self)
Packit 1fb8d4
    {
Packit 1fb8d4
        _params = [params retain];
Packit 1fb8d4
        _keyPath = (keyPath != nil ? [keyPath retain] : nil);
Packit 1fb8d4
 
Packit 1fb8d4
        _color_options = (OrderedDictionary*)[SelectionForColorSetting() retain];
Packit 1fb8d4
        _resolution_modes = [ResolutionModes() retain];
Packit 1fb8d4
        
Packit 1fb8d4
        // init current selections
Packit 1fb8d4
        NSUInteger idx = [_color_options indexForValue:[NSNumber numberWithInt:[_params intForKeyPath:[self keyPathForKey:@"colors"]]]];
Packit 1fb8d4
        _selection_color = (idx != NSNotFound) ? idx : 0;
Packit 1fb8d4
Packit 1fb8d4
        idx = [_resolution_modes indexOfObject:ScreenResolutionDescription([_params intForKeyPath:[self keyPathForKey:@"screen_resolution_type"]],
Packit 1fb8d4
                                    [_params intForKeyPath:[self keyPathForKey:@"width"]],
Packit 1fb8d4
                                    [_params intForKeyPath:[self keyPathForKey:@"height"]])];
Packit 1fb8d4
        _selection_resolution = (idx != NSNotFound) ? idx : 0;
Packit 1fb8d4
    }
Packit 1fb8d4
    return self;    
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)dealloc
Packit 1fb8d4
{
Packit 1fb8d4
    [super dealloc];
Packit 1fb8d4
    [_params autorelease];
Packit 1fb8d4
    [_keyPath autorelease];
Packit 1fb8d4
    [_color_options autorelease];
Packit 1fb8d4
    [_resolution_modes autorelease];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
-(NSString*)keyPathForKey:(NSString*)key
Packit 1fb8d4
{
Packit 1fb8d4
    if (_keyPath)
Packit 1fb8d4
        return [_keyPath stringByAppendingFormat:@".%@", key];
Packit 1fb8d4
    return key;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#pragma mark - View lifecycle
Packit 1fb8d4
Packit 1fb8d4
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
Packit 1fb8d4
{
Packit 1fb8d4
    // Return YES for supported orientations
Packit 1fb8d4
    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 1fb8d4
    return 2;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
Packit 1fb8d4
{
Packit 1fb8d4
    // Return the number of rows in the section.
Packit 1fb8d4
    if (section == 0)
Packit 1fb8d4
        return [_color_options count];
Packit 1fb8d4
    return [_resolution_modes count] + 2; // +2 for custom width/height input fields
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Packit 1fb8d4
{
Packit 1fb8d4
    UITableViewCell *cell = nil;
Packit 1fb8d4
    switch ([indexPath section])
Packit 1fb8d4
    {
Packit 1fb8d4
        case 0:
Packit 1fb8d4
            cell = [self tableViewCellFromIdentifier:TableCellIdentifierMultiChoice];
Packit 1fb8d4
            [[cell textLabel] setText:[_color_options keyAtIndex:[indexPath row]]];        
Packit 1fb8d4
            break;
Packit 1fb8d4
            
Packit 1fb8d4
        case 1:
Packit 1fb8d4
            if ([indexPath row] < [_resolution_modes count])
Packit 1fb8d4
            {
Packit 1fb8d4
                cell = [self tableViewCellFromIdentifier:TableCellIdentifierMultiChoice];   
Packit 1fb8d4
                [[cell textLabel] setText:[_resolution_modes objectAtIndex:[indexPath row]]];        
Packit 1fb8d4
            }
Packit 1fb8d4
            else
Packit 1fb8d4
                cell = [self tableViewCellFromIdentifier:TableCellIdentifierText];
Packit 1fb8d4
            break;
Packit 1fb8d4
            
Packit 1fb8d4
        default:
Packit 1fb8d4
            break;
Packit 1fb8d4
    }
Packit 1fb8d4
    
Packit 1fb8d4
    if ([indexPath section] == 1)
Packit 1fb8d4
    {
Packit 1fb8d4
        BOOL enabled = ([_params intForKeyPath:[self keyPathForKey:@"screen_resolution_type"]] == TSXScreenOptionCustom);
Packit 1fb8d4
        if ([indexPath row] == [_resolution_modes count])
Packit 1fb8d4
        {
Packit 1fb8d4
            int value = [_params intForKeyPath:[self keyPathForKey:@"width"]];
Packit 1fb8d4
            EditTextTableViewCell* textCell = (EditTextTableViewCell*)cell;
Packit 1fb8d4
            [[textCell label] setText:NSLocalizedString(@"Width", @"Custom Screen Width")];
Packit 1fb8d4
            [[textCell textfield] setText:[NSString stringWithFormat:@"%d", value ? value : 800]];
Packit 1fb8d4
            [[textCell textfield] setKeyboardType:UIKeyboardTypeNumberPad];
Packit 1fb8d4
            [[textCell label] setEnabled:enabled];
Packit 1fb8d4
            [[textCell textfield] setEnabled:enabled];
Packit 1fb8d4
            [[textCell textfield] setTag:1];
Packit 1fb8d4
        }
Packit 1fb8d4
        else if ([indexPath row] == ([_resolution_modes count] + 1))
Packit 1fb8d4
        {
Packit 1fb8d4
            int value = [_params intForKeyPath:[self keyPathForKey:@"height"]];
Packit 1fb8d4
            EditTextTableViewCell* textCell = (EditTextTableViewCell*)cell;
Packit 1fb8d4
            [[textCell label] setText:NSLocalizedString(@"Height", @"Custom Screen Height")];
Packit 1fb8d4
            [[textCell textfield] setText:[NSString stringWithFormat:@"%d", value ? value : 600]];
Packit 1fb8d4
            [[textCell textfield] setKeyboardType:UIKeyboardTypeNumberPad];
Packit 1fb8d4
            [[textCell label] setEnabled:enabled];
Packit 1fb8d4
            [[textCell textfield] setEnabled:enabled];
Packit 1fb8d4
            [[textCell textfield] setTag:2];
Packit 1fb8d4
        }
Packit 1fb8d4
    }
Packit 1fb8d4
	
Packit 1fb8d4
	// set default checkmark
Packit 1fb8d4
	if([indexPath row] == ([indexPath section] == 0 ? _selection_color : _selection_resolution))
Packit 1fb8d4
		[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
Packit 1fb8d4
	else
Packit 1fb8d4
		[cell setAccessoryType:UITableViewCellAccessoryNone];
Packit 1fb8d4
	
Packit 1fb8d4
    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
    // custom widht/height cells are not selectable
Packit 1fb8d4
    if ([indexPath section] == 1 && [indexPath row] >= [_resolution_modes count])
Packit 1fb8d4
        return;
Packit 1fb8d4
    
Packit 1fb8d4
	// has selection change?
Packit 1fb8d4
    int cur_selection = ([indexPath section] == 0 ? _selection_color : _selection_resolution);
Packit 1fb8d4
	if([indexPath row] != cur_selection)
Packit 1fb8d4
	{
Packit 1fb8d4
		[tableView deselectRowAtIndexPath:indexPath animated:NO];
Packit 1fb8d4
		
Packit 1fb8d4
		NSIndexPath* oldIndexPath = [NSIndexPath indexPathForRow:cur_selection inSection:[indexPath section]];
Packit 1fb8d4
        
Packit 1fb8d4
		// clear old checkmark
Packit 1fb8d4
		UITableViewCell* old_sel_cell = [tableView cellForRowAtIndexPath:oldIndexPath];
Packit 1fb8d4
		old_sel_cell.accessoryType = UITableViewCellAccessoryNone;
Packit 1fb8d4
        
Packit 1fb8d4
		// set new checkmark
Packit 1fb8d4
		UITableViewCell* new_sel_cell = [tableView cellForRowAtIndexPath:indexPath];
Packit 1fb8d4
		new_sel_cell.accessoryType = UITableViewCellAccessoryCheckmark;
Packit 1fb8d4
        
Packit 1fb8d4
        if ([indexPath section] == 0)
Packit 1fb8d4
        {
Packit 1fb8d4
            // get value from color dictionary
Packit 1fb8d4
            int sel_value = [[_color_options valueForKey:[_color_options keyAtIndex:[indexPath row]]] intValue];
Packit 1fb8d4
            
Packit 1fb8d4
            // update selection index and params value
Packit 1fb8d4
            [_params setInt:sel_value forKeyPath:[self keyPathForKey:@"colors"]];
Packit 1fb8d4
            _selection_color = [indexPath row];
Packit 1fb8d4
        }
Packit 1fb8d4
        else
Packit 1fb8d4
        {
Packit 1fb8d4
            // update selection index and params value
Packit 1fb8d4
            int width, height;
Packit 1fb8d4
            TSXScreenOptions mode;
Packit 1fb8d4
            ScanScreenResolution([_resolution_modes objectAtIndex:[indexPath row]], &width, &height, &mode);
Packit 1fb8d4
            [_params setInt:mode forKeyPath:[self keyPathForKey:@"screen_resolution_type"]];
Packit 1fb8d4
            if (mode != TSXScreenOptionCustom)
Packit 1fb8d4
            {
Packit 1fb8d4
                [_params setInt:width forKeyPath:[self keyPathForKey:@"width"]];
Packit 1fb8d4
                [_params setInt:height forKeyPath:[self keyPathForKey:@"height"]];                
Packit 1fb8d4
            }
Packit 1fb8d4
            _selection_resolution = [indexPath row];
Packit 1fb8d4
Packit 1fb8d4
            // refresh width/height edit fields if custom selection changed
Packit 1fb8d4
            NSArray* indexPaths = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:[_resolution_modes count] inSection:1], 
Packit 1fb8d4
                                                            [NSIndexPath indexPathForRow:([_resolution_modes count] + 1) inSection:1], nil];
Packit 1fb8d4
            [[self tableView] reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
Packit 1fb8d4
        }
Packit 1fb8d4
    }    
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#pragma mark -
Packit 1fb8d4
#pragma mark Text Field delegate
Packit 1fb8d4
Packit 1fb8d4
- (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 1fb8d4
   
Packit 1fb8d4
	switch([textField tag])
Packit 1fb8d4
	{            
Packit 1fb8d4
            // update resolution settings (and check for invalid input)
Packit 1fb8d4
		case 1:
Packit 1fb8d4
            if ([[textField text] intValue] < 640) [textField setText:@"640"];
Packit 1fb8d4
			[_params setInt:[[textField text] intValue] forKeyPath:[self keyPathForKey:@"width"]];
Packit 1fb8d4
			break;
Packit 1fb8d4
            
Packit 1fb8d4
		case 2:
Packit 1fb8d4
            if ([[textField text] intValue] < 480) [textField setText:@"480"];
Packit 1fb8d4
			[_params setInt:[[textField text] intValue] forKeyPath:[self keyPathForKey:@"height"]];
Packit 1fb8d4
			break;
Packit 1fb8d4
                        
Packit 1fb8d4
		default:
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
	return YES;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
@end