Discussion Forums  >  Uncategorized

Replies: 9    Views: 295

manutd
Aspiring developer
Profile
Posts: 33
Reg: Apr 21, 2011
California
2,280
01/11/12 06:23 PM (14 years ago)

Found Solution: Search to pull information to the app from remotely hosted SQL database

Hi Friends, We have solution to the requirement to pull information from backend server hosting SQL database for a given search string match. We rewrote and customized Search functionality in buzztouch. mail me at venkat.dabbara at gmail for details.
 
hacerapps
buzztouch Evangelist
Profile
Posts: 542
Reg: Jan 30, 2011
usa
11,170
like
01/11/12 07:16 PM (14 years ago)
why not posted here
 
manutd
Aspiring developer
Profile
Posts: 33
Reg: Apr 21, 2011
California
2,280
like
01/12/12 01:23 AM (14 years ago)
Ok let me try to summarize the changes first. Then i will try to cut and paste the code to see if makes sense. Current behavior of search is to display all the screens created within buzztouch application and when search string is entered, it matches the screens present within the app and displays it. First thing would be to bypass/comment out this part of the code and rewrite the logic to fetch JSON data from the backend to display in the below table view. Now what we want to do here is, when we get to the search screen, we can either have a blank screen with no values in the table view below or you can have an URL pointing to your backend to return JSON values to be displayed in the table view below. I call this as landing page when no search entry is entered. When user enters a Search String, we need to form a search URL with the search string appended to it and call the download logic to download the information from the search URL. Note that the backend server logic return a JSON in response to the search URL. This now needs to be displayed in the table view below search box. Did i make sense? I tried my best to explain this...Sorry if it wasn't clear.... I can send the modified search.h and search.c files with the details what i mentioned above.
 
Annonymous
Profile
01/12/12 01:58 AM (14 years ago)
Ok let me try to summarize the changes first. Then i will try to cut and paste the code to see if makes sense. Current behavior of search is to display all the screens created within buzztouch application and when search string is entered, it matches the screens present within the app and displays it. First thing would be to bypass/comment out this part of the code and rewrite the logic to fetch JSON data from the backend to display in the below table view. Now what we want to do here is, when we get to the search screen, we can either have a blank screen with no values in the table view below or you can have an URL pointing to your backend to return JSON values to be displayed in the table view below. I call this as landing page when no search entry is entered. When user enters a Search String, we need to form a search URL with the search string appended to it and call the download logic to download the information from the search URL. Note that the backend server logic return a JSON in response to the search URL. This now needs to be displayed in the table view below search box. Did i make sense? I tried my best to explain this...Sorry if it wasn't clear.... I can send the modified search.h and search.c files with the details what i mentioned above.
 
ThomasB
Lost but trying
Profile
Posts: 162
Reg: Jun 23, 2011
Palo Alto
3,270
like
01/12/12 10:55 AM (14 years ago)
Very interesting
 
ByFred
Code is Art
Profile
Posts: 51
Reg: Aug 26, 2011
New York
510
like
01/12/12 11:05 AM (14 years ago)
Post the changed code here? I really like custom code
 
Glitz521
I hate code!
Profile
Posts: 2
Reg: Jan 12, 2012
location unknow...
20
like
01/12/12 05:52 PM (14 years ago)
subscribe
 
manutd
Aspiring developer
Profile
Posts: 33
Reg: Apr 21, 2011
California
2,280
like
01/18/12 12:55 AM (14 years ago)
Sorry for the delayed post. My buzztouch login had some problem. Here you go. BT_screen_menusearch.h modifications. #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> #import BT_viewController.h #import BT_item.h @interface BT_screen_menuSearch : BT_viewController <BT_downloadFileDelegate, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>{ NSMutableArray *menuItems; NSMutableArray *filteredMenuItems; NSMutableArray *displayMenuItems; UITableView *myTableView; int didInit; UITextField *searchBox; NSTimer *searchTimer; BOOL isSearching; #ifndef VENKAT_DEBUG BT_downloader *downloader; NSString *saveAsFileName; NSString *searchUrl; BOOL isLoading; #endif } @property (nonatomic, retain) NSMutableArray *menuItems; @property (nonatomic, retain) NSMutableArray *filteredMenuItems; @property (nonatomic, retain) NSMutableArray *displayMenuItems; @property (nonatomic, retain) UITableView *myTableView; @property (nonatomic) int didInit; @property (nonatomic, retain) UITextField *searchBox; @property (nonatomic, retain) NSTimer *searchTimer; @property (nonatomic) BOOL isSearching; #ifndef VENKAT_DEBUG @property (nonatomic, retain) NSString *saveAsFileName; @property (nonatomic, retain) NSString *searchUrl; @property (nonatomic, retain) BT_downloader *downloader; @property (nonatomic) BOOL isLoading; #endif -(void)layoutScreen; -(void)textFieldDidChange:(id)sender; #ifndef VENKAT_DEBUG -(void)loadData; -(void)downloadData; -(void)parseScreenData:(NSString *)theData; -(void)checkIsLoading; #endif @end
 
manutd
Aspiring developer
Profile
Posts: 33
Reg: Apr 21, 2011
California
2,280
like
01/18/12 12:57 AM (14 years ago)
BT_screen_menusearch.c modifications #import <UIKit/UIKit.h> #import <Foundation/Foundation.h> #import JSON.h #import BT_fileManager.h #import BT_color.h #import demotestapp_appDelegate.h #import BT_strings.h #import BT_viewUtilities.h #import BT_item.h #import BT_debugger.h #import BT_viewControllerManager.h #import BT_cell_menuList.h #import BT_screen_menuSearch.h @implementation BT_screen_menuSearch @synthesize menuItems, filteredMenuItems, displayMenuItems, myTableView, didInit; @synthesize searchBox, searchTimer, isSearching; #ifndef VENKAT_DEBUG @synthesize saveAsFileName, downloader, isLoading, searchUrl; #endif //viewDidLoad -(void)viewDidLoad{ [BT_debugger showIt:self:@viewDidLoad]; [super viewDidLoad]; //init screen properties [self setDidInit:0]; [self setIsSearching:FALSE]; //////////////////////////////////////////////////////////////////////////////////////// //build the table that holds the menu items. self.myTableView = [BT_viewUtilities getTableViewForScreen:[self screenData]]; self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [self.myTableView setDataSource:self]; [self.myTableView setDelegate:self]; [self.view addSubview:myTableView]; //prevent scrolling? if([[BT_strings getStyleValueForScreen:self.screenData:@preventAllScrolling:@] isEqualToString:@1]){ [self.myTableView setScrollEnabled:FALSE]; } //create adView? if([[BT_strings getJsonPropertyValue:self.screenData.jsonVars:@includeAds:@0] isEqualToString:@1]){ [self createAdBannerView]; } } //view will appear -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [BT_debugger showIt:self:@viewWillAppear]; //flag this as the current screen demotestapp_appDelegate *appDelegate = (demotestapp_appDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.rootApp.currentScreenData = self.screenData; //setup navigation bar and background [BT_viewUtilities configureBackgroundAndNavBar:self:[self screenData]]; //custom view for title so we can insert a search bar UIView *titleView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 235, 40)] autorelease]; [titleView setTag:88]; titleView.backgroundColor = [UIColor clearColor]; //left view for search box UIImageView *searchImgView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@search.png]] autorelease]; //search hint... NSString *searchHint = NSLocalizedString(@search, search...); if(![[BT_strings getStyleValueForScreen:self.screenData:@searchHint:@] isEqualToString:@]){ searchHint = [BT_strings getStyleValueForScreen:self.screenData:@searchHint:@]; } //if we already searched reset the search hint (happens when coming back to this screen) NSString *searchText = @; if([[BT_strings getPrefString:@currentSearchValue] length] > 0){ searchText = [BT_strings getPrefString:@currentSearchValue]; } //searchBar in title view searchBox = [[[UITextField alloc] initWithFrame:CGRectMake(0, 5, 210, 30)] autorelease]; searchBox.clearButtonMode = UITextFieldViewModeAlways; searchBox.tag = 199; searchBox.text = searchText; searchBox.placeholder = searchHint; searchBox.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; searchBox.textAlignment = UITextAlignmentLeft; searchBox.textColor = [UIColor blackColor]; searchBox.font = [UIFont systemFontOfSize:14]; searchBox.keyboardType = UIKeyboardTypeDefault; searchBox.borderStyle = UITextBorderStyleRoundedRect; searchBox.autocorrectionType = UITextAutocorrectionTypeNo; searchBox.autocapitalizationType = UITextAutocapitalizationTypeNone; searchBox.keyboardAppearance = UIKeyboardAppearanceAlert; searchBox.leftViewMode = UITextFieldViewModeAlways; searchBox.returnKeyType = UIReturnKeyDone; searchBox.leftView = searchImgView; searchBox.delegate = self; [searchBox addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; [titleView addSubview:searchBox]; self.navigationItem.titleView = titleView; //if we have not yet inited data.. if(self.didInit == 0){ [self performSelector:(@selector(loadData)) withObject:nil afterDelay:0.1]; } #ifndef VENKAT_DEBUG [self.screenData.jsonVars setValue:@ENTER URL INCASE YOU WANT A LANDING PAGE IN THE SEARCH SCREEN forKey:@dataURL]; #endif //show adView? if([[BT_strings getJsonPropertyValue:self.screenData.jsonVars:@includeAds:@0] isEqualToString:@1]){ [self showHideAdView]; } } #ifdef VENKAT_DEBUG //load data -(void)loadData{ [BT_debugger showIt:self:@loadData]; //prevent interaction during operation [myTableView setScrollEnabled:FALSE]; [myTableView setAllowsSelection:FALSE]; //appDelegate demotestapp_appDelegate *appDelegate = (demotestapp_appDelegate *)[[UIApplication sharedApplication] delegate]; //init the items array self.menuItems = [[NSMutableArray alloc] init]; self.filteredMenuItems = [[NSMutableArray alloc] init]; self.displayMenuItems = [[NSMutableArray alloc] init]; //loop through every screen in the applications list of screens for(int i = 0; i < [[appDelegate.rootApp screens] count]; i++){ BT_item *thisScreen = (BT_item *)[[appDelegate.rootApp screens] objectAtIndex:i]; //do we include this screen in the search results? BOOL includeInResults = TRUE; NSString *menuTitle = @; //must have navigation bar text if(![thisScreen.jsonVars objectForKey:@navBarTitleText]){ includeInResults = FALSE; }else{ if([[thisScreen.jsonVars objectForKey:@navBarTitleText] length] < 1){ includeInResults = FALSE; }else{ menuTitle = [thisScreen.jsonVars objectForKey:@navBarTitleText]; } } //if we did not have a nav-bar title text (like call us, launch native app, etc), use the nickname if(!includeInResults){ if([thisScreen.jsonVars objectForKey:@itemNickname]){ menuTitle = [thisScreen.jsonVars objectForKey:@itemNickname]; includeInResults = TRUE; } } //must not be hidden from search results if([thisScreen.jsonVars objectForKey:@hideFromSearch]){ if([[thisScreen.jsonVars objectForKey:@hideFromSearch] isEqualToString:@1]){ includeInResults = FALSE; } } //if this is a splash screen, ignore... if([[thisScreen itemType] isEqualToString:@BT_screen_splash]){ includeInResults = FALSE; } //if this is a search menu, ingore... if([[thisScreen itemType] isEqualToString:@BT_screen_menuSearch]){ includeInResults = FALSE; } //if we are including it... if(includeInResults){ BT_item *thisMenuItemData = [[BT_item alloc] init]; [thisMenuItemData setItemId:@unused in this context]; [thisMenuItemData setItemType:@BT_menuItem]; [thisMenuItemData setSortableColumnValue:menuTitle]; NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: [thisScreen itemId], @loadScreenWithItemId, menuTitle, @titleText, @, @descriptionText, nil]; [thisMenuItemData setJsonVars:dict]; [self.menuItems addObject:thisMenuItemData]; //clean up [thisMenuItemData release]; } //sort list alphabetical NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@sortableColumnValue ascending:YES] autorelease]; NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; [self.menuItems sortUsingDescriptors:sortDescriptors]; }//end for... //layout screen [self layoutScreen]; //flag as inited [self setDidInit:1]; } #else //load data -(void)loadData{ [BT_debugger showIt:self:@loadData]; self.isLoading = TRUE; //prevent interaction during operation [myTableView setScrollEnabled:FALSE]; [myTableView setAllowsSelection:FALSE]; Screen Data scenarios -------------------------------- a) No dataURL is provided in the screen data - use the info configured in the app's configuration file b) A dataURL is provided, download now if we don't have a cache, else, download on refresh. */ self.saveAsFileName = [NSString stringWithFormat:@screenData_%@.txt, [screenData itemId]]; //do we have a URL? BOOL haveURL = FALSE; if([[BT_strings getJsonPropertyValue:screenData.jsonVars:@dataURL:@] length] > 10){ haveURL = TRUE; } //start by filling the list from the configuration file, use these if we can't get anything from a URL if([[self.screenData jsonVars] objectForKey:@childItems]){ //init the items array self.menuItems = [[NSMutableArray alloc] init]; NSArray *tmpMenuItems = [[self.screenData jsonVars] objectForKey:@childItems]; for(NSDictionary *tmpMenuItem in tmpMenuItems){ BT_item *thisMenuItem = [[BT_item alloc] init]; thisMenuItem.itemId = [tmpMenuItem objectForKey:@itemId]; thisMenuItem.itemType = [tmpMenuItem objectForKey:@itemType]; thisMenuItem.jsonVars = tmpMenuItem; [self.menuItems addObject:thisMenuItem]; [thisMenuItem release]; } } //if we have a URL, fetch.. if(haveURL){ #if 0 //VENKAT_DEBUG //look for a previously cached version of this screens data... if([BT_fileManager doesLocalFileExist:[self saveAsFileName]]){ [BT_debugger showIt:self:@parsing cached version of screen data]; NSString *staleData = [BT_fileManager readTextFileFromCacheWithEncoding:[self saveAsFileName]:-1]; [self parseScreenData:staleData]; }else{ [BT_debugger showIt:self:@no cached version of this screens data available.]; [self downloadData]; } #else [BT_debugger showIt:self:@no cached version of this screens data available.]; [self downloadData]; #endif }else{ //show the child items in the config data [BT_debugger showIt:self:@using locations from the app's configuration file.]; [self layoutScreen]; } } #endif //build screen -(void)layoutScreen{ [BT_debugger showIt:self:@layoutScreen]; NSLog(@VENKAT layout screen menu_search is %d,self.menuItems.count); //if we did not have any menu items... if(self.menuItems.count < 1){ for(int i = 0; i < 5; i++){ //create a dictionary for an empty BT_item NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: @, @titleText, @, @descriptionText, @none, @rowAccessoryType, nil]; //create a menu item from the data BT_item *thisMenuItemData = [[BT_item alloc] init]; [thisMenuItemData setJsonVars:dict]; [thisMenuItemData setItemId:@]; [thisMenuItemData setItemType:@BT_menuItem]; [self.menuItems addObject:thisMenuItemData]; [thisMenuItemData release]; } } //assign display menu items displayMenuItems = menuItems; //trigger the search (needed if we have a previously entered value) [self textFieldDidChange:NULL]; //enable interaction again (unless owner turned it off) if([[BT_strings getStyleValueForScreen:self.screenData:@preventAllScrolling:@] isEqualToString:@1]){ [self.myTableView setScrollEnabled:FALSE]; }else{ [myTableView setScrollEnabled:TRUE]; } [myTableView setAllowsSelection:TRUE]; //reload table [self.myTableView reloadData]; } ////////////////////////////////////////////////////////////// //UITableView delegate methods - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } // number of rows - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.displayMenuItems count]; } //table view cells - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @Cell; BT_cell_menuList *cell = (BT_cell_menuList *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil){ //init our custom cell cell = [[[BT_cell_menuList alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } //this menu item BT_item *thisMenuItemData = [self.displayMenuItems objectAtIndex:indexPath.row]; [cell setTheMenuItemData:thisMenuItemData]; [cell setTheParentMenuScreenData:[self screenData]]; [cell configureCell]; //custom background view. Must be done here so we can retain the round corners if this is a round table //this method refers to this screen's listRowBackgroundColor and it's position in the tap. Top and //bottom rows may need to be rounded if this is screen uses listStyle:round [cell setBackgroundView:[BT_viewUtilities getCellBackgroundForListRow:[self screenData]:indexPath:[self.menuItems count]]]; //return return cell; } //on row select - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [BT_debugger showIt:self:@didSelectRowAtIndexPath]; //pass this menu item to the tapForMenuItem method BT_item *thisMenuItem = [self.displayMenuItems objectAtIndex:indexPath.row]; //appDelegate demotestapp_appDelegate *appDelegate = (demotestapp_appDelegate *)[[UIApplication sharedApplication] delegate]; //get the itemId of the screen to load NSString *loadScreenItemId = [BT_strings getJsonPropertyValue:thisMenuItem.jsonVars:@loadScreenWithItemId:@]; //bail if load screen = none if([loadScreenItemId isEqualToString:@none]){ return; } //get the nickname of the screen to load NSString *loadScreenNickname = [BT_strings getJsonPropertyValue:thisMenuItem.jsonVars:@loadScreenWithNickname:@]; //use item id if we had it... BT_item *screenObjectToLoad = nil; if([loadScreenItemId length] > 1){ screenObjectToLoad = [appDelegate.rootApp getScreenDataByItemId:loadScreenItemId]; }else{ if([loadScreenNickname length] > 1){ screenObjectToLoad = [appDelegate.rootApp getScreenDataByNickname:loadScreenNickname]; } } //if we still don't have a screen to load, check for a loadScreenObject if(screenObjectToLoad == nil){ if([thisMenuItem.jsonVars objectForKey:@loadScreenObject]){ screenObjectToLoad = [[BT_item alloc] init]; [screenObjectToLoad setItemId:[[thisMenuItem.jsonVars objectForKey:@loadScreenObject] objectForKey:@itemId]]; [screenObjectToLoad setItemNickname:[[thisMenuItem.jsonVars objectForKey:@loadScreenObject] objectForKey:@itemNickname]]; [screenObjectToLoad setItemType:[[thisMenuItem.jsonVars objectForKey:@loadScreenObject] objectForKey:@itemType]]; [screenObjectToLoad setJsonVars:[thisMenuItem.jsonVars objectForKey:@loadScreenObject]]; } } //load next screen [BT_viewControllerManager handleTapToLoadScreen:[self screenData]:thisMenuItem:screenObjectToLoad]; } //on accessory view tap (details arrow tap) - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{ [BT_debugger showIt:self:@didSelectRowAtIndexPath]; //pass this menu item to the tapForMenuItem method BT_item *thisMenuItem = [self.displayMenuItems objectAtIndex:indexPath.row]; //appDelegate demotestapp_appDelegate *appDelegate = (demotestapp_appDelegate *)[[UIApplication sharedApplication] delegate]; //get the itemId of the screen to load NSString *loadScreenItemId = [BT_strings getJsonPropertyValue:thisMenuItem.jsonVars:@loadScreenWithItemId:@]; //use the id of the screen we want to load to get it's data object from the app BT_item *screenObjectToLoad = [appDelegate.rootApp getScreenDataByItemId:loadScreenItemId]; //BT_viewControllerManager will launch the next screen [BT_viewControllerManager handleTapToLoadScreen:[self screenData]:thisMenuItem:screenObjectToLoad]; } ////////////////////////////////////////////// //search text delegate methods.. //begin edit - (void)textFieldDidBeginEditing:(UITextField *)textField{ NSLog(@textFieldDidBeginEditing); } //end edit -(void)textFieldDidEndEditing:(UITextField *)textField{ //NSLog(@textFieldDidEndEditing); //remember the searched value [BT_strings setPrefString:@currentSearchValue:[searchBox text]]; NSLog(@VENKAT textFieldDidEndEditing currentSearchValue is %@,[searchBox text]); #ifndef VENKAT_DEBUG VENKAT:: At this point the user has finished entering the search string * Need to form the URL and get the JSON string with the possible matches */ // This appends the search string to the search URL here. self.searchUrl = [NSString stringWithFormat:@HTTP://SEARCH URL#=#%@#, [searchBox text]]; [self.screenData.jsonVars setValue:self.searchUrl forKey:@dataURL]; NSLog(@VENKAT textFieldDidEndEditing searchUrl is %@,self.searchUrl); [BT_debugger showIt:self:@no cached version of this screens data available.]; [self downloadData]; #endif } //should begin editing -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ NSLog(@textFieldShouldBeginEditing); //deselect previously selected row NSIndexPath* selection = [myTableView indexPathForSelectedRow]; if(selection){ [myTableView deselectRowAtIndexPath:selection animated:NO]; } return YES; } //should end editing -(BOOL)textFieldShouldEndEditing:(UITextField *)textField{ NSLog(@textFieldShouldEndEditing); return YES; } //return tapped -(BOOL)textFieldShouldReturn:(UITextField *)textField{ NSLog(@textFieldShouldReturn); [self.searchBox resignFirstResponder]; return TRUE; } //should clear -(BOOL)textFieldShouldClear:(UITextField *)textField{ NSLog(@textFieldShouldClear); //forward event to textFieldDidChange [self textFieldDidChange:NULL]; return YES; } //text field changed -(void)textFieldDidChange:(id)sender{ NSLog(@textFieldDidChange); #if 0 // VENKAT_DEBUG //remove filtered items [filteredMenuItems removeAllObjects]; //remember the searched value for next time (in case we leave the screen and come back) [BT_strings setPrefString:@currentSearchValue:[searchBox text]]; NSLog(@VENKAT textFieldDidChange currentSearchValue is %@,[searchBox text]); //search on every character... if([searchBox.text length] > 0){ //loop through items in menu data... for(int i = 0; i < [self.menuItems count]; i++){ BT_item *thisItem = [self.menuItems objectAtIndex:i]; NSRange r = [[thisItem.jsonVars objectForKey:@titleText] rangeOfString:searchBox.text options:NSCaseInsensitiveSearch]; if(r.location != NSNotFound){ if(r.location == 0){ [self.filteredMenuItems addObject:thisItem]; } } } } //show the filtered list if searching... if([searchBox.text length] > 0){ displayMenuItems = self.filteredMenuItems; }else{ displayMenuItems = self.menuItems; } //reload [myTableView reloadData]; #endif } #ifndef VENKAT_DEBUG //download data -(void)downloadData{ [BT_debugger showIt:self:[NSString stringWithFormat:@downloading screen data from: %@, [BT_strings getJsonPropertyValue:screenData.jsonVars:@dataURL:@]]]; //flag this as the current screen demotestapp_appDelegate *appDelegate = (demotestapp_appDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.rootApp.currentScreenData = self.screenData; //prevent interaction during operation [myTableView setScrollEnabled:FALSE]; [myTableView setAllowsSelection:FALSE]; //show progress [self showProgress]; NSString *tmpURL = @; if([[BT_strings getJsonPropertyValue:screenData.jsonVars:@dataURL:@] length] > 3){ //merge url variables tmpURL = [BT_strings getJsonPropertyValue:screenData.jsonVars:@dataURL:@]; ///merge possible variables in URL NSString *useURL = [BT_strings mergeBTVariablesInString:tmpURL]; NSString *escapedUrl = [useURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //fire downloader to fetch and results downloader = [[BT_downloader alloc] init]; [downloader setSaveAsFileName:[self saveAsFileName]]; [downloader setSaveAsFileType:@text]; [downloader setUrlString:escapedUrl]; [downloader setDelegate:self]; [downloader downloadFile]; } } //parse screen data -(void)parseScreenData:(NSString *)theData{ [BT_debugger showIt:self:@parseScreenData]; //prevent interaction during operation [myTableView setScrollEnabled:FALSE]; [myTableView setAllowsSelection:FALSE]; @try { //arrays for screenData self.menuItems = [[NSMutableArray alloc] init]; //create dictionary from the JSON string SBJsonParser *parser = [SBJsonParser new]; id jsonData = [parser objectWithString:theData]; if(!jsonData){ [BT_debugger showIt:self:[NSString stringWithFormat:@ERROR parsing JSON: %@, parser.errorTrace]]; [self showAlert:NSLocalizedString(@errorTitle,@~ Error ~):NSLocalizedString(@appParseError, @There was a problem parsing some configuration data. Please make sure that it is well-formed):0]; [BT_fileManager deleteFile:[self saveAsFileName]]; }else{ if([jsonData objectForKey:@childItems]){ NSArray *tmpMenuItems = [jsonData objectForKey:@childItems]; for(NSDictionary *tmpMenuItem in tmpMenuItems){ BT_item *thisMenuItem = [[BT_item alloc] init]; thisMenuItem.itemId = [tmpMenuItem objectForKey:@itemId]; thisMenuItem.itemType = [tmpMenuItem objectForKey:@itemType]; thisMenuItem.jsonVars = tmpMenuItem; [self.menuItems addObject:thisMenuItem]; [thisMenuItem release]; } } //layout screen [self layoutScreen]; } }@catch (NSException * e) { //delete bogus data, show alert [BT_fileManager deleteFile:[self saveAsFileName]]; [self showAlert:NSLocalizedString(@errorTitle,@~ Error ~):NSLocalizedString(@appParseError, @There was a problem parsing some configuration data. Please make sure that it is well-formed):0]; [BT_debugger showIt:self:[NSString stringWithFormat:@error parsing screen data: %@, e]]; } } //allows us to check to see if we pulled-down to refresh -(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{ [self checkIsLoading]; } -(void)checkIsLoading{ if(isLoading){ return; }else{ //how far down did we pull? double down = myTableView.contentOffset.y; if(down <= -65){ if([[BT_strings getJsonPropertyValue:screenData.jsonVars:@dataURL:@1] length] > 3){ [self downloadData]; } } } } ////////////////////////////////////////////////////////////////////////////////////////////////// //downloader delegate methods -(void)downloadFileStarted:(NSString *)message{ [BT_debugger showIt:self:[NSString stringWithFormat:@downloadFileStarted: %@, message]]; } -(void)downloadFileInProgress:(NSString *)message{ [BT_debugger showIt:self:[NSString stringWithFormat:@downloadFileInProgress: %@, message]]; if(progressView != nil){ UILabel *tmpLabel = (UILabel *)[progressView.subviews objectAtIndex:2]; [tmpLabel setText:message]; } } -(void)downloadFileCompleted:(NSString *)message{ [BT_debugger showIt:self:[NSString stringWithFormat:@downloadFileCompleted: %@, message]]; [self hideProgress]; //if message contains error, look for previously cached data... if([message rangeOfString:@ERROR options:NSCaseInsensitiveSearch].location != NSNotFound){ [BT_debugger showIt:self:[NSString stringWithFormat:@download error: There was a problem downloading data from the internet.%@, message]]; //NSLog(@Message: %@, message); //show alert [self showAlert:nil:NSLocalizedString(@downloadError, @There was a problem downloading some data. Check your internet connection then try again.):0]; //show local data if it exists if([BT_fileManager doesLocalFileExist:[self saveAsFileName]]){ //use stale data if we have it NSString *staleData = [BT_fileManager readTextFileFromCacheWithEncoding:self.saveAsFileName:-1]; [BT_debugger showIt:self:[NSString stringWithFormat:@building screen from stale configuration data: @, [self saveAsFileName]]]; [self parseScreenData:staleData]; }else{ [BT_debugger showIt:self:[NSString stringWithFormat:@There is no local data availalbe for this screen?%@, @]]; //if we have items... else.. show alert if(self.menuItems.count > 0){ [self layoutScreen]; } } }else{ //parse previously saved data if([BT_fileManager doesLocalFileExist:[self saveAsFileName]]){ [BT_debugger showIt:self:[NSString stringWithFormat:@parsing downloaded screen data.%@, @]]; NSString *downloadedData = [BT_fileManager readTextFileFromCacheWithEncoding:[self saveAsFileName]:-1]; [self parseScreenData:downloadedData]; }else{ [BT_debugger showIt:self:[NSString stringWithFormat:@Error caching downloaded file: %@, [self saveAsFileName]]]; [self layoutScreen]; //show alert [self showAlert:nil:NSLocalizedString(@appDownloadError, @There was a problem saving some data downloaded from the internet.):0]; } } } #endif //dealloc -(void)dealloc{ [screenData release]; screenData = nil; [progressView release]; progressView = nil; [menuItems release]; menuItems = nil; [myTableView release]; myTableView = nil; [filteredMenuItems release]; filteredMenuItems = nil; [displayMenuItems release]; displayMenuItems = nil; [searchBox release]; searchBox = nil; [searchTimer release]; searchTimer = nil; [super dealloc]; } @end
 
manutd
Aspiring developer
Profile
Posts: 33
Reg: Apr 21, 2011
California
2,280
like
01/18/12 12:58 AM (14 years ago)
Custom code is under #ifndef VENKAT_DEBUG
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.