Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
805 | - | 1 | // /////////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) 2010, Frank Blumenberg |
||
3 | // |
||
4 | // See License.txt for complete licensing and attribution information. |
||
5 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
||
6 | // of this software and associated documentation files (the "Software"), to deal |
||
7 | // in the Software without restriction, including without limitation the rights |
||
8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||
9 | // copies of the Software, and to permit persons to whom the Software is |
||
10 | // furnished to do so, subject to the following conditions: |
||
11 | // |
||
12 | // The above copyright notice and this permission notice shall be included in all |
||
13 | // copies or substantial portions of the Software. |
||
14 | // |
||
15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||
18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||
19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||
20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
||
21 | // THE SOFTWARE. |
||
22 | // |
||
23 | // /////////////////////////////////////////////////////////////////////////////// |
||
24 | |||
25 | #import "MainViewController.h" |
||
26 | #import "MKHost.h" |
||
27 | #import "GradientButton.h" |
||
28 | |||
29 | #import "MKConnectionController.h" |
||
30 | #import "MKDataConstants.h" |
||
31 | |||
32 | |||
33 | @implementation MainViewController |
||
34 | |||
35 | @synthesize host=_host; |
||
36 | |||
37 | #pragma mark - |
||
38 | |||
39 | - (id)initWithHost:(MKHost*)theHost { |
||
40 | |||
41 | if ((self = [super initWithFile:@"Main"])) { |
||
42 | self.title = theHost.name; |
||
43 | self.host = theHost; |
||
44 | super.dataSource = nil; |
||
45 | |||
46 | connectionState=MKConnectionStateDisconnected; |
||
47 | } |
||
48 | |||
49 | return self; |
||
50 | } |
||
51 | |||
52 | - (void)dealloc { |
||
53 | |||
54 | self.host = nil; |
||
55 | [_host release]; |
||
56 | [super dealloc]; |
||
57 | } |
||
58 | |||
59 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
||
60 | #pragma mark - |
||
61 | |||
62 | - (void)viewDidLoad { |
||
63 | |||
64 | [super viewDidLoad]; |
||
65 | |||
66 | //Create an instance of activity indicator view |
||
67 | UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)]; |
||
68 | //set the initial property |
||
69 | [activityIndicator stopAnimating]; |
||
70 | [activityIndicator hidesWhenStopped]; |
||
71 | |||
72 | //Create an instance of Bar button item with custome view which is of activity indicator |
||
73 | UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator]; |
||
74 | |||
75 | //Set the bar button the navigation bar |
||
76 | [self navigationItem].rightBarButtonItem = barButton; |
||
77 | |||
78 | //Memory clean up |
||
79 | [activityIndicator release]; |
||
80 | [barButton release]; |
||
81 | |||
82 | } |
||
83 | |||
84 | - (void)viewWillAppear:(BOOL)animated { |
||
85 | |||
86 | NSNotificationCenter* nc=[NSNotificationCenter defaultCenter]; |
||
87 | [nc addObserver:self |
||
88 | selector:@selector(connectionRequestDidSucceed:) |
||
89 | name:MKConnectedNotification |
||
90 | object:nil]; |
||
91 | |||
92 | [nc addObserver:self |
||
93 | selector:@selector(disconnected:) |
||
94 | name:MKDisconnectedNotification |
||
95 | object:nil]; |
||
96 | |||
97 | [nc addObserver:self |
||
98 | selector:@selector(connectionRequestDidFail:) |
||
99 | name:MKDisconnectErrorNotification |
||
100 | object:nil]; |
||
101 | |||
102 | [nc addObserver:self |
||
103 | selector:@selector(versionResponse:) |
||
104 | name:MKVersionNotification |
||
105 | object:nil]; |
||
106 | |||
107 | [self.navigationController setToolbarHidden:NO animated:NO]; |
||
108 | } |
||
109 | |||
110 | - (void)viewDidAppear:(BOOL)animated { |
||
111 | if( ![[MKConnectionController sharedMKConnectionController] isRunning]) { |
||
112 | |||
113 | self.settingsTableView.userInteractionEnabled=NO; |
||
114 | [(UIActivityIndicatorView *)[self navigationItem].rightBarButtonItem.customView startAnimating]; |
||
115 | [[MKConnectionController sharedMKConnectionController] start:self.host]; |
||
116 | |||
117 | } |
||
118 | else { |
||
119 | [[MKConnectionController sharedMKConnectionController] activateNaviCtrl]; |
||
120 | } |
||
121 | |||
122 | [self.settingsTableView deselectRowAtIndexPath:[self.settingsTableView indexPathForSelectedRow] animated:YES]; |
||
123 | } |
||
124 | |||
125 | -(void) viewWillDisappear:(BOOL)animated { |
||
126 | NSNotificationCenter* nc=[NSNotificationCenter defaultCenter]; |
||
127 | [nc removeObserver:self]; |
||
128 | } |
||
129 | |||
130 | - (void)viewDidUnload { |
||
131 | [[MKConnectionController sharedMKConnectionController] stop]; |
||
132 | } |
||
133 | |||
134 | /* |
||
135 | // Override to allow orientations other than the default portrait orientation. |
||
136 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
||
137 | // Return YES for supported orientations |
||
138 | return (interfaceOrientation == UIInterfaceOrientationPortrait); |
||
139 | } |
||
140 | */ |
||
141 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
||
142 | |||
143 | |||
144 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
||
145 | #pragma mark - |
||
146 | #pragma mark Table view data source |
||
147 | |||
148 | // Customize the number of sections in the table view. |
||
149 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
||
150 | |||
151 | if(connectionState==MKConnectionStateConnected ) |
||
152 | return [super numberOfSectionsInTableView:tableView]; |
||
153 | |||
154 | return 1; |
||
155 | } |
||
156 | |||
157 | // Customize the number of rows in the table view. |
||
158 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
||
159 | |||
160 | DLog(@"%d state %d",section,connectionState); |
||
161 | |||
162 | if(connectionState==MKConnectionStateConnected ) |
||
163 | return [super tableView:tableView numberOfRowsInSection:section]; |
||
164 | |||
165 | return 1; |
||
166 | } |
||
167 | |||
168 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
||
169 | |||
170 | DLog(@"%@ state %d",indexPath,connectionState); |
||
171 | |||
172 | if(connectionState==MKConnectionStateConnected ) |
||
173 | return [super tableView:tableView cellForRowAtIndexPath:indexPath]; |
||
174 | |||
175 | static NSString *CellIdentifier = @"MKConnectionCell"; |
||
176 | |||
177 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
||
178 | if (cell == nil) { |
||
179 | cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
||
180 | } |
||
181 | |||
182 | cell.textLabel.text = NSLocalizedString(@"Connecting…",@"Text connecting label"); |
||
183 | cell.selectionStyle = UITableViewCellSelectionStyleNone; |
||
184 | cell.accessoryType = UITableViewCellAccessoryNone; |
||
185 | |||
186 | return cell; |
||
187 | } |
||
188 | |||
189 | ////////////////////////////////////////////////////////////////////////////////////////////// |
||
190 | #pragma mark - |
||
191 | #pragma mark Connection status actions (Statemachine) |
||
192 | |||
193 | - (void)userDidDisconnect; |
||
194 | { |
||
195 | [[MKConnectionController sharedMKConnectionController] stop]; |
||
196 | } |
||
197 | |||
198 | - (void)userDidCancelLastConnectRequest; |
||
199 | { |
||
200 | [[MKConnectionController sharedMKConnectionController] stop]; |
||
201 | connectionState=MKConnectionStateDisconnected; |
||
202 | [self.settingsTableView reloadData]; |
||
203 | } |
||
204 | |||
205 | - (void)connectionRequestDidFail:(NSNotification *)aNotification; |
||
206 | { |
||
207 | NSError* err = [[aNotification userInfo] objectForKey:@"error"]; |
||
208 | |||
209 | UIAlertView *alert = [[UIAlertView alloc] |
||
210 | initWithTitle:NSLocalizedString(@"Server error", @"Server error") |
||
211 | message:[err localizedDescription] |
||
212 | delegate:self |
||
213 | cancelButtonTitle:@"Ok" |
||
214 | otherButtonTitles:nil]; |
||
215 | [alert show]; |
||
216 | [alert release]; |
||
217 | |||
218 | connectionState=MKConnectionStateDisconnected; |
||
219 | [self.navigationController popToRootViewControllerAnimated:YES]; |
||
220 | } |
||
221 | |||
222 | - (void)connectionRequestDidSucceed:(NSNotification *)aNotification; |
||
223 | { |
||
224 | DLog(@"Got connected"); |
||
225 | self.settingsTableView.userInteractionEnabled=YES; |
||
226 | |||
227 | connectionState=MKConnectionStateConnected; |
||
228 | |||
229 | [self.settingsTableView beginUpdates]; |
||
230 | [self.settingsTableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade]; |
||
231 | [self.settingsTableView endUpdates]; |
||
232 | |||
233 | [(UIActivityIndicatorView *)[self navigationItem].rightBarButtonItem.customView stopAnimating]; |
||
234 | |||
235 | [[MKConnectionController sharedMKConnectionController] activateNaviCtrl]; |
||
236 | } |
||
237 | |||
238 | - (void)disconnected:(NSNotification *)aNotification; |
||
239 | { |
||
240 | connectionState=MKConnectionStateDisconnected; |
||
241 | [self.navigationController popToRootViewControllerAnimated:YES]; |
||
242 | } |
||
243 | |||
244 | - (void)versionResponse:(NSNotification *)aNotification; |
||
245 | { |
||
246 | // self.versionString = [[MKConnectionController sharedMKConnectionController]longVersionForAddress:MKAddressFC]; |
||
247 | // [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:CONNECT_SECTIONID] withRowAnimation:UITableViewRowAnimationNone]; |
||
248 | } |
||
249 | |||
250 | ////////////////////////////////////////////////////////////////////////////////////////////// |
||
251 | #pragma mark - |
||
252 | |||
253 | @end |