source: gs2-extensions/iOs-3.x/trunk/Classes/QueryView.mm@ 22603

Last change on this file since 22603 was 22603, checked in by davidb, 14 years ago

Initial cut as iOs 3.x version of Greenstone2.app

File size: 11.5 KB
Line 
1#import <CoreFoundation/CoreFoundation.h>
2#import <Foundation/Foundation.h>
3#import <UIKit/CDStructures.h>
4#import <UIKit/UITextLabel.h>
5#import <UIKit/UIDateLabel.h> // For defaultFont.
6#import <UIKit/UITable.h>
7#import <UIKit/UITableCell.h>
8#import <UIKit/UITableColumn.h>
9//#import <UIKit/UISearchField.h>
10#import <UIKit/UISearchBar.h>
11#import <UIKit/UIFrameAnimation.h>
12#import <UIKit/UITransitionView.h>
13#import <UIKit/UIAnimator.h>
14
15#import "GreenstoneApplication.h"
16//#import "SimpleWebView.h"
17#import "GSResultItem.h"
18
19#import "QueryView.h"
20
21@implementation SearchTableCell
22
23// ***** is the following needed, or need to be modified?
24- (void) drawContentInRect: (struct CGRect)rect selected: (BOOL) selected
25{
26 [song_name setHighlighted: selected];
27 [artist_name setHighlighted: selected];
28 [super drawContentInRect: rect selected: selected];
29}
30
31@end
32
33
34@implementation QueryView
35
36- (id)initWithFrame:(struct CGRect)frame withParent:(UIView*)parent
37 withCollection:(NSString*)collection withTitle:(NSString*)title
38{
39 self = [super initWithFrame:frame];
40
41 _parent = parent;
42 _collection = collection;
43
44 // Create the storage array.
45 _cellResultList = [[NSMutableArray alloc] init];
46 _oidItem = [[NSMutableArray alloc] init];
47
48
49 // Create the navigation bar
50 int frame_x_dim = CGRectGetMaxX(frame)+1;
51 int frame_y_dim = CGRectGetMaxY(frame)+1;
52
53 int nbr1_x_org = 0;
54 int nbr1_y_org = 0;
55 int nbr1_x_dim = frame_x_dim;
56 int nbr1_y_dim = TOP_NAVBAR_HEIGHT;
57
58 CGRect navbar1_rect = CGRectMake(nbr1_x_org,nbr1_y_org,nbr1_x_dim,nbr1_y_dim);
59
60 UINavigationBar* navbar1 = [[UINavigationBar alloc] initWithFrame:navbar1_rect];
61 [navbar1 showLeftButton:@"Back" withStyle:1 rightButton:nil withStyle:0]; // 2 = arrow left.
62 [navbar1 setBarStyle: UIBarStyleDefault];
63 [self addSubview: navbar1];
64 [navbar1 enableAnimation];
65 [navbar1 setDelegate:self];
66
67 _navbar = navbar1;
68 [self setTitle:title];
69
70
71 // Not sure if a second nav bar is really the way to get a
72 // search box with background seamlessly merged with main nav bar
73 // Tried to make 1 navbar high enough for everything, but back
74 // button was alligned to bottom of bar
75
76 int nbr2_x_org = nbr1_x_org;
77 int nbr2_y_org = nbr1_y_dim;
78 int nbr2_x_dim = nbr1_x_dim;
79 int nbr2_y_dim = [UISearchField defaultHeight];
80
81 CGRect navbar2_rect = CGRectMake(nbr2_x_org,nbr2_y_org,nbr2_x_dim,nbr2_y_dim);
82 UINavigationBar* navbar2 = [[UINavigationBar alloc] initWithFrame:navbar2_rect];
83 [navbar2 showLeftButton:nil withStyle:0 rightButton:nil withStyle:0];
84 [navbar2 enableAnimation];
85 [navbar2 setBarStyle: UIBarStyleDefault];
86 [navbar2 setDelegate:self];
87
88 [self addSubview: navbar2];
89
90
91 // Create the search box.
92
93 int sr_x_org = 30;
94 int sr_y_org = 0;
95 int sr_x_dim = frame.size.width - (2*30);
96 int sr_y_dim = [UISearchField defaultHeight];
97
98 CGRect search_rect = CGRectMake(sr_x_org,sr_y_org,sr_x_dim,sr_y_dim);
99
100 _searchBar = [[UISearchBar alloc] initWithFrame:search_rect];
101 _searchBar.delegate = self;
102 _searchBar.showsCancelButton = NO;
103
104 //_searchBox = [[UISearchField alloc] initWithFrame:search_rect];
105 //[_searchBox setClearButtonStyle:2];
106 //[_searchBox setFont:[UIFont systemFontOfSize:14.0]];
107
108 ////[[_searchBox textTraits] setReturnKeyType: UIReturnKeySearch];
109 ////[[_searchBox textTraits] setEditingDelegate:self];
110 ////[[_searchBox textTraits] setAutoCapsType: NO];
111 ////[[_searchBox textTraits] setAutoCorrectionType:1];
112
113
114 [_searchBar setText:@""];
115 ////[_searchBox setDisplayEnabled:YES];
116
117 [_searchBar becomeFirstResponder];
118
119
120 // Create the table.
121 int tab_y_org = nbr2_y_org+nbr2_y_dim;
122 CGRect table_rect = CGRectMake(0, tab_y_org, frame.size.width, frame_y_dim - tab_y_org);
123
124 _table = [[UITable alloc] initWithFrame:table_rect];
125 [self addSubview: _table];
126
127 UITableColumn *col = [[UITableColumn alloc] initWithTitle: @"iMPDclient" identifier: @"column1" width: frame_x_dim];
128 [_table addTableColumn: col];
129 [_table setDelegate: self];
130 [_table setDataSource: self];
131 [_table setSeparatorStyle:1];
132 [_table setRowHeight:42.0f];
133
134
135 // Create the keyboard
136 int kb_x_org = 0;
137 int kb_y_org = 410 - [UIKeyboard defaultSize].height;
138 int kb_x_dim = frame.size.width; // ****? what about when landscape
139 int kb_y_dim = [UIKeyboard defaultSize].height;
140 CGRect kb_rect = CGRectMake(kb_x_org,kb_y_org,kb_x_dim,kb_y_dim);
141
142 //_keyboard = [[UIKeyboard alloc] initWithFrame:kb_rect];
143
144 //[self addSubview: _keyboard];
145 //_keyboardVisible = YES;
146
147 [navbar2 addSubview:_searchBar];
148
149 return self;
150}
151
152-(void) setTitle:(NSString*)title
153{
154 UINavigationItem* navtitle = [UINavigationItem alloc];
155 [_navbar pushNavigationItem: [navtitle initWithTitle:title]];
156}
157
158
159- (void)performQuery:(NSString *)searchtext
160{
161 GreenstoneAPI* greenstoneAPI = [GreenstoneApplication greenstoneAPI];
162 NSMutableArray* resultlist = greenstoneAPI->query(_collection,searchtext);
163
164 [_cellResultList removeAllObjects];
165 [_oidItem removeAllObjects];
166
167 if (resultlist != nil) {
168 NSEnumerator* resultlist_enumerator = [resultlist objectEnumerator];
169 GSResultItem* resultitem;
170 while (resultitem = [resultlist_enumerator nextObject]) {
171
172 SearchTableCell* cell = [[SearchTableCell alloc] init];
173
174 NSString* resultoid = [resultitem oid];
175
176 NSString* resulttitle = [resultitem title];
177 if (resulttitle == nil) {
178 // doesn't have a valid title
179 resulttitle = [[NSString alloc] initWithString:@"Unknown Title"];
180 }
181
182 NSString* resulturl = [resultitem srcurl];
183 if (resulturl == nil) { // **** don't need to do this check now
184 // doesn't have a valid srcurl => set as the empty string
185 resulturl = [[NSString alloc] initWithString:@""];
186 }
187
188 [cell setTitle:resulttitle];
189 [_cellResultList addObject:cell];
190 [_oidItem addObject:resultitem];
191
192 [cell release];
193 }
194 [resultlist release];
195 }
196
197 // Update the table contents.
198 [_table reloadData];
199}
200
201
202
203//***********
204//* Delegates
205//***********
206
207-(void) navigationBar:(UINavigationBar*)navbar buttonClicked:(int)button
208{
209 switch (button)
210 {
211 case 1: // left
212 [GreenstoneApplication setInCollection:nil];
213 [GreenstoneApplication transitionView:UITransitionBothShiftRight
214 fromView:self toView:_parent];
215 break;
216
217 default:
218 NSLog(@"Unrecognised navbar button = %d",button);
219 break;
220 }
221}
222
223
224- (void)tableRowSelected:(NSNotification*)notification
225{
226 UITable* table = [notification object];
227 int rowId = [table selectedRow];
228
229 SearchTableCell* pCell = [table cellAtRow:rowId column:0];
230 [pCell setSelected:FALSE withFade:TRUE];
231
232 GSResultItem* oidItem = [_oidItem objectAtIndex:rowId];
233
234 NSString* oid = [oidItem oid];
235 NSString* srcurl = [oidItem srcurl];
236
237 NSURL *url = [NSURL alloc];
238
239 if (srcurl == nil) {
240
241 NSTimer *timer = [ NSTimer scheduledTimerWithTimeInterval: 0.3
242 target: self
243 selector: @selector(asynchronousLoad:)
244 userInfo: oid
245 repeats: NO ];
246 url = [url initWithString:@"file:///var/gs2-runtime-svn/ext/iOS3.x/loading.html"]; // ****
247
248 /* *****
249 GreenstoneAPI* greenstoneAPI = [GreenstoneApplication greenstoneAPI];
250 NSString* doctext = greenstoneAPI->getResolvedDocument(_collection,oid);
251 [doctext writeToFile:@"/tmp/gsdoc.html" atomically:NO encoding:NSUTF8StringEncoding error:nil];
252
253 // make sure file is writable by all
254 // => allows for switching between running from command line and Springboard
255 system("chmod a+rw /tmp/gsdoc.html");
256
257 url = [url initWithString:@"file:///tmp/gsdoc.html"];
258 */
259
260 }
261 else {
262 NSString* fullurlstr = [[NSString alloc] initWithFormat:@"file://%@",srcurl];
263 url = [url initWithString:fullurlstr];
264 }
265
266 int transStyle = UITransitionBothShiftLeft;
267 [GreenstoneApplication showWebView:transStyle fromView:self withURL:url];
268
269}
270
271-(void) asynchronousLoad:(NSTimer*)timer
272{
273 // **** This is identical to one in BrowseCascadeMenu
274 NSString* childGID = [timer userInfo];
275
276 GreenstoneAPI* greenstoneAPI = [GreenstoneApplication greenstoneAPI];
277 NSString* doctext = greenstoneAPI->getResolvedDocument(_collection,childGID);
278
279 [doctext writeToFile:@"/tmp/gsdoc.html" atomically:NO encoding:NSUTF8StringEncoding error:nil];
280 NSURL* url = [[NSURL alloc] initWithString:@"file:///tmp/gsdoc.html"];
281 system("chmod a+rw /tmp/gsdoc.html");
282
283 [GreenstoneApplication showWebView:0 fromView:self withURL:url];
284}
285
286
287- (int) numberOfRowsInTable: (UITable *)table
288{
289 return [_cellResultList count];
290}
291
292
293- (UITableCell *) table: (UITable *)table cellForRow: (int)row column: (int)col
294{
295 return [_cellResultList objectAtIndex:row];
296}
297
298- (UITableCell *) table: (UITable *)table cellForRow: (int)row column: (int)col reusing: (BOOL) reusing
299{
300 return [self table: table cellForRow: row column: col];
301}
302
303
304- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
305 NSString* query = [_searchBar text];
306
307 [self performQuery:query];
308}
309
310
311
312- (void)keyboardInputChangedSelection:(id)whatisthis
313{
314 // when i tap the search field, hide if keyboard is already there, or show if it isn't
315 if (!_keyboardVisible)
316 [self ShowKeyboard];
317}
318
319-(void)keyboardInput:(id)k shouldInsertText:(id)i isMarkedText:(int)b
320{
321 if ([i characterAtIndex:0] == 0xA) {
322 [self keyboardReturnPressed];
323
324 NSString* query = [_searchBar text];
325 [self performQuery:query];
326 }
327}
328
329- (void)scrollerWillStartDragging:(id)whatisthis
330{
331 // hide keyboard when start scrolling
332 if (_keyboardVisible)
333 [self HideKeyboard];
334}
335
336// --- KEYBOARD METHODS -----------------------------------------------
337
338- (void)keyboardReturnPressed
339{
340 [self HideKeyboard];
341}
342
343- (void)ShowKeyboard
344{
345 _keyboardVisible = YES;
346
347 CGRect startFrame;
348 CGRect endFrame;
349 NSMutableArray *animations = [[NSMutableArray alloc] init];
350
351 endFrame = CGRectMake(0.0f, 410 - [UIKeyboard defaultSize].height, 320, [UIKeyboard defaultSize].height);
352 startFrame = endFrame;
353 startFrame.origin.y = 245.0 + 216.;
354
355 [_keyboard setFrame:startFrame];
356
357 UIFrameAnimation *keyboardAnimation = [[UIFrameAnimation alloc] initWithTarget:_keyboard];
358 [keyboardAnimation setStartFrame:startFrame];
359 [keyboardAnimation setEndFrame:endFrame];
360 [keyboardAnimation setSignificantRectFields:2];
361 [keyboardAnimation setDelegate:self];
362 [animations addObject:keyboardAnimation];
363 [keyboardAnimation release];
364
365 [[UIAnimator sharedAnimator] addAnimations:animations withDuration:.5 start:YES];
366}
367
368- (void)HideKeyboard
369{
370 _keyboardVisible = NO;
371
372 CGRect startFrame;
373 CGRect endFrame;
374 NSMutableArray *animations = [[NSMutableArray alloc] init];
375
376 startFrame = CGRectMake(0, 410 - [UIKeyboard defaultSize].height, 320, [UIKeyboard defaultSize].height);
377 endFrame = startFrame;
378 endFrame.origin.y = 245.0 + 216.;
379
380 [_keyboard setFrame:startFrame];
381
382 UIFrameAnimation *keyboardAnimation = [[UIFrameAnimation alloc] initWithTarget:_keyboard];
383 [keyboardAnimation setStartFrame:startFrame];
384 [keyboardAnimation setEndFrame:endFrame];
385 [keyboardAnimation setSignificantRectFields:2];
386 [keyboardAnimation setDelegate:self];
387 [animations addObject:keyboardAnimation];
388 [keyboardAnimation release];
389
390 [[UIAnimator sharedAnimator] addAnimations:animations withDuration:.5 start:YES];
391}
392
393
394-(void) dealloc
395{
396 [_oidItem release];
397
398 [_collection release];
399
400 [super dealloc];
401}
402
403@end
404
Note: See TracBrowser for help on using the repository browser.