Revision: 18556
          http://bibdesk.svn.sourceforge.net/bibdesk/?rev=18556&view=rev
Author:   ununnilium
Date:     2012-03-15 23:31:33 +0000 (Thu, 15 Mar 2012)
Log Message:
-----------
Add basic search functionality

Modified Paths:
--------------
    trunk/bibdesk_ios/BDSKMasterViewController.m
    trunk/bibdesk_ios/BDSKPubTableViewController.h
    trunk/bibdesk_ios/BDSKPubTableViewController.m
    trunk/bibdesk_ios/en.lproj/MainStoryboard_iPad.storyboard
    trunk/bibdesk_ios/en.lproj/MainStoryboard_iPhone.storyboard

Modified: trunk/bibdesk_ios/BDSKMasterViewController.m
===================================================================
--- trunk/bibdesk_ios/BDSKMasterViewController.m        2012-03-15 10:53:14 UTC 
(rev 18555)
+++ trunk/bibdesk_ios/BDSKMasterViewController.m        2012-03-15 23:31:33 UTC 
(rev 18556)
@@ -234,11 +234,14 @@
         
         [document openWithCompletionHandler:^(BOOL success) {            
             if (success) {
-                [viewController setDocument:document];
+                dispatch_async(dispatch_get_main_queue(), ^{
+                    viewController.document = document;
+                    [document release];
+                });
             } else {
                 NSLog(@"Couldn't Open Document: %@", file.fullPath);
+                [document release];
             }
-            [document release];
         }];
         
     } else if ([[segue identifier] isEqualToString:@"allPDFFiles"]) {

Modified: trunk/bibdesk_ios/BDSKPubTableViewController.h
===================================================================
--- trunk/bibdesk_ios/BDSKPubTableViewController.h      2012-03-15 10:53:14 UTC 
(rev 18555)
+++ trunk/bibdesk_ios/BDSKPubTableViewController.h      2012-03-15 23:31:33 UTC 
(rev 18556)
@@ -40,7 +40,7 @@
 
 @class BibItem;
 
-@interface BDSKPubTableViewController : UITableViewController
+@interface BDSKPubTableViewController : UITableViewController 
<UISearchDisplayDelegate, UISearchBarDelegate>
 
 @property (nonatomic, retain) NSArray *bibItems;
 

Modified: trunk/bibdesk_ios/BDSKPubTableViewController.m
===================================================================
--- trunk/bibdesk_ios/BDSKPubTableViewController.m      2012-03-15 10:53:14 UTC 
(rev 18555)
+++ trunk/bibdesk_ios/BDSKPubTableViewController.m      2012-03-15 23:31:33 UTC 
(rev 18556)
@@ -48,9 +48,24 @@
 #import "NSString_BDSKExtensions.h"
 #import "NSArray_BDSKExtensions.h"
 
+BDSKLocalFile *LocalFileForBibItem(BibItem *bibItem) {
+
+    BDSKDropboxStore *dropboxStore = [BDSKDropboxStore sharedStore];
+    
+    BDSKLocalFile *localFile = nil;
+    for (BDSKLinkedFile *file in bibItem.localFiles) {
+        if ((localFile = [dropboxStore.pdfFilePaths 
objectForKey:file.relativePath])) {
+            break;
+        }
+    }
+    
+    return localFile;
+}
+
 @interface BDSKPubTableViewController () {
 
-    NSMutableArray *_pdfFiles;
+    NSMutableArray *_filteredBibItems;
+    BOOL _firstAppearance;
 }
 
 @end
@@ -71,12 +86,13 @@
 - (void)awakeFromNib
 {
     self.bibItems = [NSArray array];
-    _pdfFiles = nil;
+    _filteredBibItems = [[NSMutableArray alloc] init];
+    _firstAppearance = YES;
 }
 
 - (void)dealloc {
 
-    [_pdfFiles release];
+    [_filteredBibItems release];
     [super dealloc];
 }
 
@@ -98,6 +114,16 @@
     // e.g. self.myOutlet = nil;
 }
 
+- (void)viewWillAppear:(BOOL)animated {
+
+    [super viewWillAppear:animated];
+
+    if (_firstAppearance) {
+        self.tableView.contentOffset = CGPointMake(0, 
self.searchDisplayController.searchBar.bounds.size.height);
+        _firstAppearance = NO;
+    }
+}
+
 - 
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
     if ([[UIDevice currentDevice] userInterfaceIdiom] == 
UIUserInterfaceIdiomPhone) {
@@ -109,6 +135,15 @@
 
 #pragma mark - Table view data source
 
+- (BibItem *)tableView:(UITableView *)tableView 
bibItemForRowAtIndexPath:(NSIndexPath *)indexPath {
+
+    if (tableView == self.searchDisplayController.searchResultsTableView) {
+        return [_filteredBibItems objectAtIndex:indexPath.row];
+    }
+    
+   return [bibItems objectAtIndex:indexPath.row];
+}
+
 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
     // Return the number of sections.
@@ -117,6 +152,10 @@
 
 - (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section
 {
+    if (tableView == self.searchDisplayController.searchResultsTableView) {
+        return [_filteredBibItems count];
+    }
+    
     // Return the number of rows in the section.
     return self.bibItems.count;
 }
@@ -124,9 +163,9 @@
 - (UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
     static NSString *CellIdentifier = @"Cell";
-    UITableViewCell *cell = [tableView 
dequeueReusableCellWithIdentifier:CellIdentifier];
+    UITableViewCell *cell = [self.tableView 
dequeueReusableCellWithIdentifier:CellIdentifier];
     
-    BibItem *bibItem = [bibItems objectAtIndex:indexPath.row];
+    BibItem *bibItem = [self tableView:tableView 
bibItemForRowAtIndexPath:indexPath];
     
     cell.textLabel.text = bibItem.title;
     
@@ -143,7 +182,7 @@
     
     //NSLog(@"File Count: %i", bibItem.files.count);
     
-    if (((BDSKLocalFile *)[_pdfFiles objectAtIndex:indexPath.row]).path) {
+    if (LocalFileForBibItem(bibItem)) {
         //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
         cell.accessoryView = [[[UIImageView alloc] initWithImage:[UIImage 
imageNamed:@"pdf.png"]] autorelease];
         cell.selectionStyle = UITableViewCellSelectionStyleBlue;
@@ -199,18 +238,20 @@
 
 - (NSIndexPath *)tableView:(UITableView *)tableView 
willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 
-    BDSKLocalFile *pdfFile = [_pdfFiles objectAtIndex:indexPath.row];
+    BibItem *bibItem = [self tableView:tableView 
bibItemForRowAtIndexPath:indexPath];
 
-    if (pdfFile.path) return indexPath;
+    if (LocalFileForBibItem(bibItem)) return indexPath;
     
     return nil;
 }
 
 - (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
-    BDSKLocalFile *pdfFile = [_pdfFiles objectAtIndex:indexPath.row];
+    BibItem *bibItem = [self tableView:tableView 
bibItemForRowAtIndexPath:indexPath];
 
-    if (pdfFile.path) {
+    BDSKLocalFile *pdfFile = LocalFileForBibItem(bibItem);
+
+    if (pdfFile) {
         if (self.splitViewController) {
             UINavigationController *navigationController = 
[self.splitViewController.viewControllers objectAtIndex:1];
             BDSKDetailViewController *viewController = 
(BDSKDetailViewController *)[navigationController.viewControllers 
objectAtIndex:0];
@@ -224,8 +265,15 @@
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
     if ([[segue identifier] isEqualToString:@"showPDF"]) {
-        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
-        BDSKLocalFile *pdfFile = [_pdfFiles objectAtIndex:indexPath.row];
+        UITableView *tableView;
+        if (self.searchDisplayController.active) {
+            tableView = self.searchDisplayController.searchResultsTableView;
+        } else {
+            tableView = self.tableView;
+        }
+        NSIndexPath *indexPath = [tableView indexPathForSelectedRow];
+        BibItem *bibItem = [self tableView:tableView 
bibItemForRowAtIndexPath:indexPath];
+        BDSKLocalFile *pdfFile = LocalFileForBibItem(bibItem);
         [[segue destinationViewController] setDisplayedFile:pdfFile];
     }
 }
@@ -237,27 +285,67 @@
         BDSKTableSortDescriptor *sortDescriptor = [BDSKTableSortDescriptor 
tableSortDescriptorForIdentifier:BDSKPubDateString ascending:NO];
         bibItems = [[newBibItems 
sortedArrayUsingMergesortWithDescriptors:[NSArray 
arrayWithObject:sortDescriptor]] retain];
         
-        [_pdfFiles release];
-        _pdfFiles = [[NSMutableArray alloc] init];
-        
-        BDSKDropboxStore *dropboxStore = [BDSKDropboxStore sharedStore];
-        for (BibItem *bibItem in newBibItems) {
-            BDSKLocalFile *localFile = nil;
-            for (BDSKLinkedFile *file in bibItem.files) {
-                //NSLog(@"Relative Path: %@", file.relativePath);
-                if ((localFile = [dropboxStore.pdfFilePaths 
objectForKey:file.relativePath])) {
-                    break;
-                }
-            }
-            if (localFile) {
-                [_pdfFiles addObject:localFile];
-            } else {
-                [_pdfFiles addObject:[[[BDSKLocalFile alloc] 
initWithDropboxPath:nil lastModifiedDate:nil totalByets:0] autorelease]];
-            }
-        }
-        
         [self.tableView reloadData];
     }
 }
 
+#pragma mark -
+#pragma mark Content Filtering
+
+- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
+{
+       /*
+        Update the filtered array based on the search text and scope.
+        */
+       
+       [_filteredBibItems removeAllObjects]; // First clear the filtered array.
+       
+       /*
+        Search the main list for products whose type matches the scope (if 
selected) and whose name matches searchText; add items that match to the 
filtered array.
+        */
+       for (BibItem *bibItem in bibItems) {
+               NSString *bibString = [bibItem allFieldsString];
+        if ([bibString rangeOfString:searchText 
options:NSCaseInsensitiveSearch].location != NSNotFound) {
+            [_filteredBibItems addObject:bibItem];
+        }
+       }
+}
+
+#pragma mark -
+#pragma mark UISearchDisplayController Delegate Methods
+
+- (BOOL)searchDisplayController:(UISearchDisplayController *)controller 
shouldReloadTableForSearchString:(NSString *)searchString
+{
+    [self filterContentForSearchText:searchString scope:
+                       [[self.searchDisplayController.searchBar 
scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar 
selectedScopeButtonIndex]]];
+    
+    // Return YES to cause the search result table view to be reloaded.
+    return YES;
+}
+
+- (BOOL)searchDisplayController:(UISearchDisplayController *)controller 
shouldReloadTableForSearchScope:(NSInteger)searchOption
+{
+    [self filterContentForSearchText:[self.searchDisplayController.searchBar 
text] scope:
+                       [[self.searchDisplayController.searchBar 
scopeButtonTitles] objectAtIndex:searchOption]];
+    
+    // Return YES to cause the search result table view to be reloaded.
+    return YES;
+}
+
+- (void)searchDisplayController:(UISearchDisplayController *)controller 
didLoadSearchResultsTableView:(UITableView *)tableView {
+
+    tableView.rowHeight = self.tableView.rowHeight;
+}
+
+- (void)searchDisplayController:(UISearchDisplayController *)controller 
willHideSearchResultsTableView:(UITableView *)tableView {
+
+    NSIndexPath *indexPath = tableView.indexPathForSelectedRow;
+    if (indexPath) {
+        BibItem *bibItem = [_filteredBibItems objectAtIndex:indexPath.row];
+        NSUInteger row = [self.bibItems indexOfObject:bibItem];
+        NSIndexPath *indexPathToSelect = [NSIndexPath indexPathForRow:row 
inSection:0];
+        [self.tableView selectRowAtIndexPath:indexPathToSelect animated:NO 
scrollPosition:UITableViewScrollPositionMiddle];
+    }
+}
+
 @end

Modified: trunk/bibdesk_ios/en.lproj/MainStoryboard_iPad.storyboard
===================================================================
--- trunk/bibdesk_ios/en.lproj/MainStoryboard_iPad.storyboard   2012-03-15 
10:53:14 UTC (rev 18555)
+++ trunk/bibdesk_ios/en.lproj/MainStoryboard_iPad.storyboard   2012-03-15 
23:31:33 UTC (rev 18556)
@@ -215,9 +215,17 @@
                         <autoresizingMask key="autoresizingMask" 
widthSizable="YES" heightSizable="YES"/>
                         <color key="backgroundColor" white="1" alpha="1" 
colorSpace="calibratedWhite"/>
                         <simulatedOrientationMetrics 
key="simulatedOrientationMetrics" orientation="landscapeRight"/>
+                        <searchBar key="tableHeaderView" contentMode="redraw" 
id="sKD-58-ag3">
+                            <rect key="frame" x="0.0" y="0.0" width="320" 
height="44"/>
+                            <autoresizingMask key="autoresizingMask" 
widthSizable="YES" flexibleMaxY="YES"/>
+                            <textInputTraits key="textInputTraits"/>
+                            <connections>
+                                <outlet property="delegate" 
destination="2gb-PK-n1a" id="K9m-bu-80X"/>
+                            </connections>
+                        </searchBar>
                         <prototypes>
                             <tableViewCell contentMode="scaleToFill" 
selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" 
indentationWidth="0.0" reuseIdentifier="Cell" textLabel="bEX-x8-Hg0" 
detailTextLabel="SeP-mR-qYq" style="IBUITableViewCellStyleSubtitle" 
id="kmj-Oz-7wa">
-                                <rect key="frame" x="0.0" y="22" width="320" 
height="88"/>
+                                <rect key="frame" x="0.0" y="66" width="320" 
height="88"/>
                                 <autoresizingMask key="autoresizingMask"/>
                                 <view key="contentView" opaque="NO" 
clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
                                     <rect key="frame" x="0.0" y="0.0" 
width="320" height="87"/>
@@ -249,7 +257,19 @@
                     </tableView>
                     <navigationItem key="navigationItem" id="rUu-Dx-H3l"/>
                     <simulatedStatusBarMetrics key="simulatedStatusBarMetrics" 
statusBarStyle="blackTranslucent"/>
+                    <connections>
+                        <outlet property="searchDisplayController" 
destination="D0Q-bw-c5D" id="ooJ-BV-yZp"/>
+                    </connections>
                 </tableViewController>
+                <searchDisplayController id="D0Q-bw-c5D">
+                    <connections>
+                        <outlet property="delegate" destination="2gb-PK-n1a" 
id="wjU-03-0UK"/>
+                        <outlet property="searchBar" destination="sKD-58-ag3" 
id="G32-OC-w3d"/>
+                        <outlet property="searchContentsController" 
destination="2gb-PK-n1a" id="u5q-dC-DOU"/>
+                        <outlet property="searchResultsDataSource" 
destination="2gb-PK-n1a" id="gJZ-Jb-ync"/>
+                        <outlet property="searchResultsDelegate" 
destination="2gb-PK-n1a" id="30s-nK-oQF"/>
+                    </connections>
+                </searchDisplayController>
             </objects>
             <point key="canvasLocation" x="1841" y="-1115"/>
         </scene>

Modified: trunk/bibdesk_ios/en.lproj/MainStoryboard_iPhone.storyboard
===================================================================
--- trunk/bibdesk_ios/en.lproj/MainStoryboard_iPhone.storyboard 2012-03-15 
10:53:14 UTC (rev 18555)
+++ trunk/bibdesk_ios/en.lproj/MainStoryboard_iPhone.storyboard 2012-03-15 
23:31:33 UTC (rev 18556)
@@ -199,9 +199,17 @@
                         <rect key="frame" x="0.0" y="64" width="320" 
height="416"/>
                         <autoresizingMask key="autoresizingMask" 
widthSizable="YES" heightSizable="YES"/>
                         <color key="backgroundColor" white="1" alpha="1" 
colorSpace="calibratedWhite"/>
+                        <searchBar key="tableHeaderView" contentMode="redraw" 
id="diD-xh-LCP">
+                            <rect key="frame" x="0.0" y="0.0" width="320" 
height="44"/>
+                            <autoresizingMask key="autoresizingMask" 
widthSizable="YES" flexibleMaxY="YES"/>
+                            <textInputTraits key="textInputTraits"/>
+                            <connections>
+                                <outlet property="delegate" 
destination="tQb-MC-Rtt" id="XHG-Y4-cfS"/>
+                            </connections>
+                        </searchBar>
                         <prototypes>
                             <tableViewCell contentMode="scaleToFill" 
selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" 
indentationWidth="0.0" reuseIdentifier="Cell" textLabel="Xko-Qp-Cza" 
detailTextLabel="n5N-zP-AWW" style="IBUITableViewCellStyleSubtitle" 
id="8fE-n5-LJI">
-                                <rect key="frame" x="0.0" y="22" width="320" 
height="88"/>
+                                <rect key="frame" x="0.0" y="66" width="320" 
height="88"/>
                                 <autoresizingMask key="autoresizingMask"/>
                                 <view key="contentView" opaque="NO" 
clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
                                     <rect key="frame" x="0.0" y="0.0" 
width="320" height="87"/>
@@ -234,9 +242,19 @@
                     <navigationItem key="navigationItem" id="tbm-oy-BVS"/>
                     <simulatedStatusBarMetrics 
key="simulatedStatusBarMetrics"/>
                     <connections>
+                        <outlet property="searchDisplayController" 
destination="d8n-6V-obc" id="mjD-nK-jeP"/>
                         <segue destination="21" kind="push" 
identifier="showPDF" id="CnH-em-F7k"/>
                     </connections>
                 </tableViewController>
+                <searchDisplayController id="d8n-6V-obc">
+                    <connections>
+                        <outlet property="delegate" destination="tQb-MC-Rtt" 
id="KQj-lI-RgP"/>
+                        <outlet property="searchBar" destination="diD-xh-LCP" 
id="myz-fW-OL4"/>
+                        <outlet property="searchContentsController" 
destination="tQb-MC-Rtt" id="XvU-YT-QwG"/>
+                        <outlet property="searchResultsDataSource" 
destination="tQb-MC-Rtt" id="Bx4-FI-g33"/>
+                        <outlet property="searchResultsDelegate" 
destination="tQb-MC-Rtt" id="04u-rh-12A"/>
+                    </connections>
+                </searchDisplayController>
             </objects>
             <point key="canvasLocation" x="1433" y="-198"/>
         </scene>

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here 
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
Bibdesk-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to