FreeRDP
Loading...
Searching...
No Matches
CredentialsInputController.m
1/*
2 Credentials input controller
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 "CredentialsInputController.h"
12#import "RDPSession.h"
13
15- (void)updateScrollViewContentSize;
16
17- (UIView *)activeCredentialField;
18- (void)setKeyboardOverlap:(CGFloat)overlap notification:(NSNotification *)notification;
19@end
20
21@implementation CredentialsInputController
22
23- (id)initWithNibName:(NSString *)nibNameOrNil
24 bundle:(NSBundle *)nibBundleOrNil
25 session:(RDPSession *)session
26 params:(NSMutableDictionary *)params
27{
28 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
29 if (self)
30 {
31 _session = session;
32 _params = params;
33 [self setModalPresentationStyle:UIModalPresentationFormSheet];
34
35 [[NSNotificationCenter defaultCenter] addObserver:self
36 selector:@selector(keyboardWillShow:)
37 name:UIKeyboardWillShowNotification
38 object:nil];
39 [[NSNotificationCenter defaultCenter] addObserver:self
40 selector:@selector(keyboardWillHide:)
41 name:UIKeyboardWillHideNotification
42 object:nil];
43 }
44 return self;
45}
46
47- (void)viewDidLoad
48{
49 [super viewDidLoad];
50
51 // set localized strings
52 [_lbl_message
53 setText:NSLocalizedString(
54 @"Please provide the missing user information in order to proceed and login.",
55 @"Credentials input view message")];
56 [_textfield_username
57 setPlaceholder:NSLocalizedString(@"Username", @"Credentials Input Username hint")];
58 [_textfield_password
59 setPlaceholder:NSLocalizedString(@"Password", @"Credentials Input Password hint")];
60 [_textfield_domain
61 setPlaceholder:NSLocalizedString(@"Domain", @"Credentials Input Domain hint")];
62 [_btn_login setTitle:NSLocalizedString(@"Login", @"Login Button")
63 forState:UIControlStateNormal];
64 [_btn_cancel setTitle:NSLocalizedString(@"Cancel", @"Cancel Button")
65 forState:UIControlStateNormal];
66
67 [_scroll_view setKeyboardDismissMode:UIScrollViewKeyboardDismissModeInteractive];
68 [self updateScrollViewContentSize];
69
70 // set params in the view
71 [_textfield_username setText:[_params valueForKey:@"username"]];
72 [_textfield_password setText:[_params valueForKey:@"password"]];
73 [_textfield_domain setText:[_params valueForKey:@"domain"]];
74}
75
76- (void)viewDidLayoutSubviews
77{
78 [super viewDidLayoutSubviews];
79 [self updateScrollViewContentSize];
80}
81
82- (void)viewDidUnload
83{
84 [super viewDidUnload];
85}
86
87- (void)viewDidDisappear:(BOOL)animated
88{
89 [super viewDidDisappear:animated];
90 // set signal
91 [[_session uiRequestCompleted] signal];
92}
93
94- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
95{
96 return YES;
97}
98
99- (void)dealloc
100{
101 [[NSNotificationCenter defaultCenter] removeObserver:self];
102 [super dealloc];
103}
104
105#pragma mark -
106#pragma mark iOS Keyboard Notification Handlers
107
108- (void)keyboardWillShow:(NSNotification *)notification
109{
110 CGRect keyboardScreenFrame =
111 [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
112 CGRect keyboardViewFrame = [[self view] convertRect:keyboardScreenFrame fromView:nil];
113 CGRect overlapRect = CGRectIntersection([[self view] bounds], keyboardViewFrame);
114 CGFloat overlap = CGRectIsNull(overlapRect) ? 0.0f : CGRectGetHeight(overlapRect);
115 [self setKeyboardOverlap:overlap notification:notification];
116}
117
118- (void)keyboardWillHide:(NSNotification *)notification
119{
120 [self setKeyboardOverlap:0.0f notification:notification];
121}
122
123- (void)updateScrollViewContentSize
124{
125 CGFloat contentBottom = 0.0f;
126 for (UIView *subview in [_scroll_view subviews])
127 contentBottom = MAX(contentBottom, CGRectGetMaxY([subview frame]));
128
129 CGSize contentSize = [_scroll_view bounds].size;
130 contentSize.height = MAX(contentSize.height, contentBottom + 20.0f);
131 [_scroll_view setContentSize:contentSize];
132}
133
134- (UIView *)activeCredentialField
135{
136 if ([_textfield_username isFirstResponder])
137 return _textfield_username;
138 if ([_textfield_password isFirstResponder])
139 return _textfield_password;
140 if ([_textfield_domain isFirstResponder])
141 return _textfield_domain;
142 return nil;
143}
144
145- (void)setKeyboardOverlap:(CGFloat)overlap notification:(NSNotification *)notification
146{
147 NSTimeInterval duration =
148 [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
149 UIViewAnimationCurve curve = (UIViewAnimationCurve)[
150 [[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
151 UIViewAnimationOptions options =
152 UIViewAnimationOptionBeginFromCurrentState | (UIViewAnimationOptions)(curve << 16);
153
154 [UIView animateWithDuration:duration
155 delay:0.0
156 options:options
157 animations:^{
158 UIEdgeInsets insets = [_scroll_view contentInset];
159 insets.bottom = overlap;
160 [_scroll_view setContentInset:insets];
161 [_scroll_view setScrollIndicatorInsets:insets];
162
163 UIView *activeField = [self activeCredentialField];
164 if (activeField)
165 {
166 CGRect visibleRect = [activeField convertRect:[activeField bounds]
167 toView:_scroll_view];
168 visibleRect = CGRectInset(visibleRect, -12.0f, -16.0f);
169 [_scroll_view scrollRectToVisible:visibleRect animated:NO];
170 }
171 }
172 completion:nil];
173}
174
175#pragma mark - Action handlers
176
177- (IBAction)loginPressed:(id)sender
178{
179 // read input back in
180 [_params setValue:[_textfield_username text] forKey:@"username"];
181 [_params setValue:[_textfield_password text] forKey:@"password"];
182 [_params setValue:[_textfield_domain text] forKey:@"domain"];
183 [_params setValue:[NSNumber numberWithBool:YES] forKey:@"result"];
184
185 // dismiss controller
186 [self dismissModalViewControllerAnimated:YES];
187}
188
189- (IBAction)cancelPressed:(id)sender
190{
191 [_params setValue:[NSNumber numberWithBool:NO] forKey:@"result"];
192
193 // dismiss controller
194 [self dismissModalViewControllerAnimated:YES];
195}
196
197@end