FreeRDP
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:(int)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 addObject:[NSNumber numberWithInt:(idx != NSNotFound ? idx : 0)]];
42  }
43  else
44  [_cur_selections addObject:[NSNumber numberWithInt:0]];
45  }
46  }
47  return self;
48 }
49 
50 - (void)didReceiveMemoryWarning
51 {
52  // Releases the view if it doesn't have a superview.
53  [super didReceiveMemoryWarning];
54 
55  // Release any cached data, images, etc that aren't in use.
56  [_params autorelease];
57  [_entries autorelease];
58  [_selections autorelease];
59  [_cur_selections autorelease];
60 }
61 
62 #pragma mark - View lifecycle
63 
64 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
65 {
66  // Return YES for supported orientations
67  return YES;
68 }
69 
70 #pragma mark - Table view data source
71 
72 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
73 {
74  // Return the number of sections.
75  return [_entries count];
76 }
77 
78 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
79 {
80  // Return the number of rows in the section.
81  return [[self selectionForIndex:section] count];
82 }
83 
84 - (UITableViewCell *)tableView:(UITableView *)tableView
85  cellForRowAtIndexPath:(NSIndexPath *)indexPath
86 {
87  UITableViewCell *cell = [self tableViewCellFromIdentifier:TableCellIdentifierMultiChoice];
88 
89  // get selection
90  OrderedDictionary *selection = [self selectionForIndex:[indexPath section]];
91 
92  // set cell properties
93  [[cell textLabel] setText:[selection keyAtIndex:[indexPath row]]];
94 
95  // set default checkmark
96  if ([indexPath row] == [[_cur_selections objectAtIndex:[indexPath section]] intValue])
97  [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
98  else
99  [cell setAccessoryType:UITableViewCellAccessoryNone];
100 
101  return cell;
102 }
103 
104 #pragma mark - Table view delegate
105 
106 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
107 {
108  // has selection change?
109  int cur_selection = [[_cur_selections objectAtIndex:[indexPath section]] intValue];
110  if ([indexPath row] != cur_selection)
111  {
112  [tableView deselectRowAtIndexPath:indexPath animated:NO];
113 
114  NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:cur_selection
115  inSection:[indexPath section]];
116 
117  // clear old checkmark
118  UITableViewCell *old_sel_cell = [tableView cellForRowAtIndexPath:oldIndexPath];
119  old_sel_cell.accessoryType = UITableViewCellAccessoryNone;
120 
121  // set new checkmark
122  UITableViewCell *new_sel_cell = [tableView cellForRowAtIndexPath:indexPath];
123  new_sel_cell.accessoryType = UITableViewCellAccessoryCheckmark;
124 
125  // get value from selection dictionary
126  OrderedDictionary *dict = [self selectionForIndex:[indexPath section]];
127  int sel_value = [[dict valueForKey:[dict keyAtIndex:[indexPath row]]] intValue];
128 
129  // update selection index and params value
130  [_cur_selections replaceObjectAtIndex:[indexPath section]
131  withObject:[NSNumber numberWithInt:[indexPath row]]];
132  [_params setInt:sel_value forKeyPath:[_entries objectAtIndex:[indexPath section]]];
133  }
134 }
135 
136 #pragma mark - Convenience functions
137 
138 - (OrderedDictionary *)selectionForIndex:(int)index
139 {
140  return (OrderedDictionary *)[_selections objectAtIndex:index];
141 }
142 
143 @end