FreeRDP
Loading...
Searching...
No Matches
EditorSelectionController.m
1/*
2 Generic controller to select a single item from a list of options
3
4 Copyright 2013 Thincast Technologies GmbH, Author: Martin Fleisz
5
6 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7 If a copy of the MPL was not distributed with this file, You can obtain one at
8 http://mozilla.org/MPL/2.0/.
9 */
10
11#import "EditorSelectionController.h"
12#import "ConnectionParams.h"
13#import "OrderedDictionary.h"
14
15@interface EditorSelectionController (Private)
16- (OrderedDictionary *)selectionForIndex:(NSInteger)index;
17@end
18
19@implementation EditorSelectionController
20
21- (id)initWithConnectionParams:(ConnectionParams *)params
22 entries:(NSArray *)entries
23 selections:(NSArray *)selections
24{
25 self = [super initWithStyle:UITableViewStyleGrouped];
26 if (self)
27 {
28 _params = [params retain];
29 _entries = [entries retain];
30 _selections = [selections retain];
31
32 // allocate and init current selections array
33 _cur_selections = [[NSMutableArray alloc] initWithCapacity:[_entries count]];
34 for (int i = 0; i < [entries count]; ++i)
35 {
36 NSString *entry = [entries objectAtIndex:i];
37 if ([_params hasValueForKeyPath:entry])
38 {
39 NSUInteger idx = [(OrderedDictionary *)[selections objectAtIndex:i]
40 indexForValue:[NSNumber numberWithInt:[_params intForKeyPath:entry]]];
41 [_cur_selections
42 addObject:[NSNumber numberWithInteger:(idx != NSNotFound ? idx : 0)]];
43 }
44 else
45 [_cur_selections addObject:[NSNumber numberWithInt:0]];
46 }
47 }
48 return self;
49}
50
51- (void)didReceiveMemoryWarning
52{
53 // Releases the view if it doesn't have a superview.
54 [super didReceiveMemoryWarning];
55
56 // Release any cached data, images, etc that aren't in use.
57 [_params autorelease];
58 [_entries autorelease];
59 [_selections autorelease];
60 [_cur_selections autorelease];
61}
62
63#pragma mark - View lifecycle
64
65- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
66{
67 // Return YES for supported orientations
68 return YES;
69}
70
71#pragma mark - Table view data source
72
73- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
74{
75 // Return the number of sections.
76 return [_entries count];
77}
78
79- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
80{
81 // Return the number of rows in the section.
82 return [[self selectionForIndex:section] count];
83}
84
85- (UITableViewCell *)tableView:(UITableView *)tableView
86 cellForRowAtIndexPath:(NSIndexPath *)indexPath
87{
88 UITableViewCell *cell = [self tableViewCellFromIdentifier:TableCellIdentifierMultiChoice];
89
90 // get selection
91 OrderedDictionary *selection = [self selectionForIndex:[indexPath section]];
92
93 // set cell properties
94 [[cell textLabel] setText:[selection keyAtIndex:[indexPath row]]];
95
96 // set default checkmark
97 if ([indexPath row] == [[_cur_selections objectAtIndex:[indexPath section]] intValue])
98 [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
99 else
100 [cell setAccessoryType:UITableViewCellAccessoryNone];
101
102 return cell;
103}
104
105#pragma mark - Table view delegate
106
107- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
108{
109 // has selection change?
110 int cur_selection = [[_cur_selections objectAtIndex:[indexPath section]] intValue];
111 if ([indexPath row] != cur_selection)
112 {
113 [tableView deselectRowAtIndexPath:indexPath animated:NO];
114
115 NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:cur_selection
116 inSection:[indexPath section]];
117
118 // clear old checkmark
119 UITableViewCell *old_sel_cell = [tableView cellForRowAtIndexPath:oldIndexPath];
120 old_sel_cell.accessoryType = UITableViewCellAccessoryNone;
121
122 // set new checkmark
123 UITableViewCell *new_sel_cell = [tableView cellForRowAtIndexPath:indexPath];
124 new_sel_cell.accessoryType = UITableViewCellAccessoryCheckmark;
125
126 // get value from selection dictionary
127 OrderedDictionary *dict = [self selectionForIndex:[indexPath section]];
128 int sel_value = [[dict valueForKey:[dict keyAtIndex:[indexPath row]]] intValue];
129
130 // update selection index and params value
131 [_cur_selections replaceObjectAtIndex:[indexPath section]
132 withObject:[NSNumber numberWithInteger:[indexPath row]]];
133 [_params setInt:sel_value forKeyPath:[_entries objectAtIndex:[indexPath section]]];
134 }
135}
136
137#pragma mark - Convenience functions
138
139- (OrderedDictionary *)selectionForIndex:(NSInteger)index
140{
141 return (OrderedDictionary *)[_selections objectAtIndex:index];
142}
143
144@end