source: gs2-extensions/iOS-1.x/trunk/QueryView.mm@ 32238

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

Original version of Greenstone2 app for iPhone/iPod-touch

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