diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..ebf952d --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,2 @@ +github: timoliver +custom: https://tim.dev/paypal diff --git a/TODocumentPickerViewController.podspec b/TODocumentPickerViewController.podspec index 1ce567c..0f315d0 100644 --- a/TODocumentPickerViewController.podspec +++ b/TODocumentPickerViewController.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = 'TODocumentPickerViewController' - spec.version = '0.2.1' + spec.version = '0.2.2' spec.platform = :ios, '9.0' spec.license = { :type => 'MIT', :file => 'LICENSE' } spec.homepage = 'https://github.com/TimOliver/TODocumentPickerViewController' diff --git a/TODocumentPickerViewController/Models/TODocumentPickerConfiguration.h b/TODocumentPickerViewController/Models/TODocumentPickerConfiguration.h index 75bedf5..d5c64f0 100644 --- a/TODocumentPickerViewController/Models/TODocumentPickerConfiguration.h +++ b/TODocumentPickerViewController/Models/TODocumentPickerConfiguration.h @@ -37,6 +37,12 @@ NS_ASSUME_NONNULL_BEGIN /* Whether this controller shows and manages the navigation controller toolbar (Default is YES) */ @property (nonatomic, assign) BOOL showToolbar; +/* When not in 'Select' mode, the bar button items on the left hand side of the toolbar */ +@property (nonatomic, strong) NSArray *toolbarLeftItems; + +/* When not in 'Select' mode, the bar button items on the right hand side of the toolbar */ +@property (nonatomic, strong) NSArray *toolbarRightItems; + /* File formats that may be selected by this controller. (If nil, all files may be selected) */ @property (nonatomic, strong, nullable) NSArray *allowedFileExtensions; diff --git a/TODocumentPickerViewController/Models/TODocumentPickerItemManager.m b/TODocumentPickerViewController/Models/TODocumentPickerItemManager.m index deaa4c9..2a62a4b 100644 --- a/TODocumentPickerViewController/Models/TODocumentPickerItemManager.m +++ b/TODocumentPickerViewController/Models/TODocumentPickerItemManager.m @@ -102,7 +102,7 @@ - (NSArray *)sortedItemsArrayWithArray:(NSArray *)items - (NSArray *)filteredItemsWithItems:(NSArray *)items searchString:(NSString *)searchString { NSMutableArray *filteredItems = [NSMutableArray array]; - [items enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(TODocumentPickerItem *item, NSUInteger i, BOOL *stop) { + [items enumerateObjectsUsingBlock:^(TODocumentPickerItem *item, NSUInteger i, BOOL *stop) { if ([item.fileName rangeOfString:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)].location != NSNotFound) { [filteredItems addObject:item]; } diff --git a/TODocumentPickerViewController/TODocumentPickerViewController.m b/TODocumentPickerViewController/TODocumentPickerViewController.m index f06eec7..fa32d4b 100644 --- a/TODocumentPickerViewController/TODocumentPickerViewController.m +++ b/TODocumentPickerViewController/TODocumentPickerViewController.m @@ -151,7 +151,6 @@ - (void)viewDidLoad self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.rowHeight = 64.0f; - self.tableView.cellLayoutMarginsFollowReadableWidth = NO; self.tableView.sectionIndexBackgroundColor = self.view.backgroundColor; self.tableView.allowsMultipleSelectionDuringEditing = YES; @@ -195,9 +194,6 @@ - (void)viewDidLoad /* Set-up Select All button */ self.selectAllButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Select All", @"") style:UIBarButtonItemStylePlain target:self action:@selector(selectAllButtonTapped)]; - - /* Set-up the toolbar */ - [self configureToolbar]; } - (void)configureToolbar @@ -229,16 +225,48 @@ - (void)configureToolbar UIBarButtonItem *spaceItemRight = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *labelItem = [[UIBarButtonItem alloc] initWithCustomView:self.toolBarLabel]; - /* Toolbar button elements */ + /* Toolbar button elements when not in 'Select' mode */ if (self.nonEditingToolbarItems == nil) { - if (self.doneButton == nil) { - self.doneButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", nil) - style:UIBarButtonItemStyleDone - target:self - action:@selector(doneButtonTapped)]; + + NSMutableArray *barItems = [NSMutableArray array]; + + /* Configure left side of toolbar */ + if (self.configuration.toolbarLeftItems) { + [barItems addObjectsFromArray:self.configuration.toolbarLeftItems]; } - - self.nonEditingToolbarItems = @[self.doneButton, spaceItemLeft, labelItem, spaceItemRight]; + else { + /* Don't add a 'Done' button if we're not being presented modally */ + if (self.presentingViewController) { + if (self.doneButton == nil) { + self.doneButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", nil) + style:UIBarButtonItemStyleDone + target:self + action:@selector(doneButtonTapped)]; + } + + [barItems addObject:self.doneButton]; + } + } + + // Add the flexible element to center the label if there are less than 2 bar items on the left side + if (barItems.count < 2) { + [barItems addObject:spaceItemLeft]; + } + + // Add the label + [barItems addObject:labelItem]; + + // Add spacing if there's less than 2 items on the right side + if (self.configuration.toolbarRightItems.count < 2) { + [barItems addObject:spaceItemRight]; + } + + // Add right items + if (self.configuration.toolbarRightItems) { + [barItems addObjectsFromArray:self.configuration.toolbarRightItems]; + } + + self.nonEditingToolbarItems = [NSArray arrayWithArray:barItems]; } /* Set up editing buttons */ @@ -262,15 +290,17 @@ - (void)configureToolbar - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; - - /* Configure the sizing and insetting of the header now that the table view will be ready */ - UIEdgeInsets headerInsets = UIEdgeInsetsZero; - headerInsets.left = self.tableView.separatorInset.left; - headerInsets.right = self.tableView.separatorInset.left; - headerInsets.top = 10.0f; - headerInsets.bottom = 10.0f; - self.headerView.layoutMargins = headerInsets; - [self.headerView sizeToFit]; + + if (self.dataSource == nil) { + NSException *exception = [NSException exceptionWithName:NSInternalInconsistencyException + reason:@"TODocumentPickerViewController: A data source must be set before the view controller is presented" + userInfo:nil]; + [exception raise]; + return; + } + + /* Set-up the toolbar */ + [self configureToolbar]; /* Add the header view to the table view */ UIView *tableHeaderView = [[UIView alloc] initWithFrame:self.headerView.bounds]; @@ -309,6 +339,15 @@ - (void)viewDidLayoutSubviews [self resetTableViewInitialOffset]; self.viewInitiallyLaidOut = YES; } + + /* Configure the sizing and insetting of the header now that the table view will be ready */ + UIEdgeInsets headerInsets = UIEdgeInsetsZero; + headerInsets.left = self.tableView.separatorInset.left; + headerInsets.right = self.tableView.separatorInset.left; + headerInsets.top = 10.0f; + headerInsets.bottom = 10.0f; + self.headerView.layoutMargins = headerInsets; + [self.headerView sizeToFit]; } - (void)resetAfterInitialItemLoad @@ -576,6 +615,8 @@ - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)ce // Check if we already have an icon for it if (self.configuration.fileIcons[fileExtension]) { return; } + UIColor *tintColor = self.view.tintColor; + // Kick off a new thread to render the new icon __weak typeof(self) weakSelf = self; dispatch_async(self.mediaQueue, ^{ @@ -586,7 +627,7 @@ - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)ce } if (fileIcon == nil) { - fileIcon = [UIImage TO_documentPickerDefaultFileIconWithExtension:fileExtension tintColor:weakSelf.view.tintColor style:self.configuration.style]; + fileIcon = [UIImage TO_documentPickerDefaultFileIconWithExtension:fileExtension tintColor:tintColor style:self.configuration.style]; } dispatch_async(dispatch_get_main_queue(), ^{ @@ -674,6 +715,7 @@ - (void)showFeedbackLabelIfNeeded self.feedbackLabel.font = [UIFont systemFontOfSize:16.0f]; self.feedbackLabel.textColor = [UIColor colorWithWhite:0.8f alpha:1.0f]; self.feedbackLabel.textAlignment = NSTextAlignmentCenter; + self.feedbackLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; [self.tableView addSubview:self.feedbackLabel]; } @@ -823,7 +865,7 @@ - (BOOL)allCellsSelected - (id)dataSource { - if (_dataSource == nil) { + if (_dataSource == nil && self != self.rootViewController) { return self.rootViewController.dataSource; } diff --git a/TODocumentPickerViewController/Views/TODocumentPickerHeaderView.m b/TODocumentPickerViewController/Views/TODocumentPickerHeaderView.m index 3e1cece..da517cf 100644 --- a/TODocumentPickerViewController/Views/TODocumentPickerHeaderView.m +++ b/TODocumentPickerViewController/Views/TODocumentPickerHeaderView.m @@ -24,6 +24,7 @@ #import "TOSearchBar.h" static const CGFloat kTODocumentPickerHeaderViewPadding = 5.0f; +static const CGFloat kTODocumentPickerHeaderMaxWidth = 640.0f; @interface TODocumentPickerHeaderView () @@ -114,16 +115,16 @@ - (void)layoutSubviews // Layout the child views if (self.searchBar) { frame = self.searchBar.frame; - frame.origin.x = self.layoutMargins.left; - frame.size.width = self.bounds.size.width - (self.layoutMargins.left + self.layoutMargins.right); + frame.size.width = MIN(kTODocumentPickerHeaderMaxWidth, self.bounds.size.width - (self.layoutMargins.left + self.layoutMargins.right)); frame.size.height = 44.0f; frame.origin.y = self.layoutMargins.top; + frame.origin.x = CGRectGetMidX(self.bounds) - (frame.size.width * 0.5f); self.searchBar.frame = CGRectIntegral(frame); } frame = self.sortControl.frame; - frame.origin.x = self.layoutMargins.left; - frame.size.width = self.bounds.size.width - (self.layoutMargins.left + self.layoutMargins.right); + frame.size.width = MIN(kTODocumentPickerHeaderMaxWidth, self.bounds.size.width - (self.layoutMargins.left + self.layoutMargins.right)); + frame.origin.x = CGRectGetMidX(self.bounds) - (frame.size.width * 0.5f); frame.size.height = 33.0f; if (self.searchBar) { frame.origin.y = CGRectGetMaxY(self.searchBar.frame) + kTODocumentPickerHeaderViewPadding; @@ -135,6 +136,12 @@ - (void)layoutSubviews self.sortControl.frame = CGRectIntegral(frame); } +- (void)layoutMarginsDidChange +{ + [super layoutMarginsDidChange]; + [self setNeedsLayout]; +} + #pragma mark - Search Bar Delegate - - (void)searchBar:(TOSearchBar *)searchBar textDidChange:(NSString *)searchText { diff --git a/TODocumentPickerViewControllerExample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/TODocumentPickerViewControllerExample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/TODocumentPickerViewControllerExample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + +