Files
FreeRDP/client/iOS/Controllers/EditorSelectionController.m

144 lines
4.4 KiB
Mathematica
Raw Permalink Normal View History

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