Skip to content

Commit

Permalink
Improved Search management
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyLB committed Jul 28, 2019
1 parent 8d636fc commit 3cbab51
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 37 deletions.
66 changes: 49 additions & 17 deletions piwigo/Albums/AlbumImagesViewController.m
Expand Up @@ -39,7 +39,7 @@
CGFloat const kRadius = 25.0;
NSString * const kPiwigoNotificationBackToDefaultAlbum = @"kPiwigoNotificationBackToDefaultAlbum";

@interface AlbumImagesViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate, UIToolbarDelegate, UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate, ImageDetailDelegate, MoveImagesDelegate, CategorySortDelegate, CategoryCollectionViewCellDelegate, AsyncImageActivityItemProviderDelegate>
@interface AlbumImagesViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate, UIToolbarDelegate, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate, ImageDetailDelegate, MoveImagesDelegate, CategorySortDelegate, CategoryCollectionViewCellDelegate, AsyncImageActivityItemProviderDelegate>

@property (nonatomic, strong) UICollectionView *imagesCollection;
@property (nonatomic, strong) AlbumData *albumData;
Expand Down Expand Up @@ -208,7 +208,7 @@ -(void)viewDidLoad
[self.searchController.searchBar setTintColor:[UIColor piwigoOrange]];
self.searchController.searchBar.showsCancelButton = NO;
self.searchController.searchBar.showsSearchResultsButton = NO;
self.searchController.searchBar.delegate = self;
self.searchController.searchBar.delegate = self; // Monitor when the search button is tapped.
self.definesPresentationContext = YES;

// Place the search bar in the navigation bar.
Expand Down Expand Up @@ -290,6 +290,14 @@ -(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

// Called before displaying SearchImagesViewController?
UIViewController *presentedViewController = [self presentedViewController];
if ([presentedViewController isKindOfClass:[UISearchController class]]) {
// Hide toolbar
[self.navigationController setToolbarHidden:YES animated:YES];
return;
}

// Set colors, fonts, etc.
[self paletteChanged];

Expand Down Expand Up @@ -345,7 +353,18 @@ -(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];

// Refresh controller
// Called after displaying SearchImagesViewController?
UIViewController *presentedViewController = [self presentedViewController];
if ([presentedViewController isKindOfClass:[UISearchController class]]) {
// Scroll to image of interest if needed
if ([self.searchController.searchResultsController isKindOfClass:[SearchImagesViewController class]]) {
SearchImagesViewController *resultsController = (SearchImagesViewController *)self.searchController.searchResultsController;
[resultsController scrollToHighlightedCell];
}
return;
}

// Refresh controller
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
refreshControl.backgroundColor = [UIColor piwigoBackgroundColor];
refreshControl.tintColor = [UIColor piwigoOrange];
Expand All @@ -364,9 +383,8 @@ -(void)viewDidAppear:(BOOL)animated
}

// Should we scroll to image of interest?
// NSLog(@"••• Starting with %ld images", (long)[self.imagesCollection numberOfItemsInSection:1]);
if ((self.categoryId != 0) && ([self.albumData.images count] > 0) &&
([self.imageOfInterest compare:[NSIndexPath indexPathForItem:0 inSection:1]] != NSOrderedSame)) {
// NSLog(@"••• Starting with %ld images", (long)[self.imagesCollection numberOfItemsInSection:1]);
if ((self.categoryId != 0) && ([self.albumData.images count] > 0) && (self.imageOfInterest.item != 0)) {

// Not the root album, album contains images and thumbnail of interest is not the first one
// => Scroll and highlight cell of interest
Expand Down Expand Up @@ -438,9 +456,8 @@ -(void)viewDidAppear:(BOOL)animated

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {

// Apply effect on cell of lastly previewed image
if ((self.categoryId != 0) && ([self.albumData.images count] > 0) &&
([self.imageOfInterest compare:[NSIndexPath indexPathForItem:0 inSection:1]] != NSOrderedSame)) {
// Highlight image which is now visible
if ((self.categoryId != 0) && ([self.albumData.images count] > 0) && (self.imageOfInterest.item != 0)) {
// NSLog(@"=> Did end scrolling with %ld images", (long)[self.imagesCollection numberOfItemsInSection:1]);
[self highlightCell];
}
Expand Down Expand Up @@ -470,7 +487,6 @@ -(void)highlightCell
// Apply effect only when returning from image preview mode
self.imageOfInterest = [NSIndexPath indexPathForItem:0 inSection:1];
}

}

-(void)viewWillDisappear:(BOOL)animated
Expand Down Expand Up @@ -2204,6 +2220,17 @@ -(void)showErrorWithTitle:(NSString *)title andMessage:(NSString *)message

#pragma mark - UISearchControllerDelegate

- (void)willPresentSearchController:(UISearchController *)searchController
{
// Unregister category data updates
[[NSNotificationCenter defaultCenter] removeObserver:self name:kPiwigoNotificationCategoryDataUpdated object:nil];
}

- (void)didDismissSearchController:(UISearchController *)searchController
{
// Register category data updates
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(categoriesUpdated) name:kPiwigoNotificationCategoryDataUpdated object:nil];
}


#pragma mark - UISearchResultsUpdating
Expand All @@ -2213,15 +2240,20 @@ -(void)updateSearchResultsForSearchController:(UISearchController *)searchContro
// Query string
NSString *searchString = [self.searchController.searchBar text];

// Initialise search cache
PiwigoAlbumData *searchAlbum = [[PiwigoAlbumData alloc] initSearchAlbumForQuery:searchString];
[[CategoriesData sharedInstance] updateCategories:@[searchAlbum]];

// Resfresh image collection for new query
// Resfresh image collection for new query only
if ([searchController.searchResultsController isKindOfClass:[SearchImagesViewController class]]) {
SearchImagesViewController *resultsController = (SearchImagesViewController *)searchController.searchResultsController;
resultsController.searchQuery = searchString;
[resultsController searchAndLoadImages];

if (![resultsController.searchQuery isEqualToString:searchString] || !searchString.length) {

// Initialise search cache
PiwigoAlbumData *searchAlbum = [[PiwigoAlbumData alloc] initSearchAlbumForQuery:searchString];
[[CategoriesData sharedInstance] updateCategories:@[searchAlbum]];

// Resfresh image collection
resultsController.searchQuery = searchString;
[resultsController searchAndLoadImages];
}
}
}

Expand Down
1 change: 0 additions & 1 deletion piwigo/Albums/SearchImagesViewController.h
Expand Up @@ -11,7 +11,6 @@
@interface SearchImagesViewController : UIViewController

@property (nonatomic, strong) NSString *searchQuery;
@property (nonatomic, strong) UICollectionView *imagesCollection;

-(void)searchAndLoadImages;

Expand Down
25 changes: 6 additions & 19 deletions piwigo/Albums/SearchImagesViewController.m
Expand Up @@ -10,7 +10,6 @@
#import "AlbumService.h"
#import "AppDelegate.h"
#import "CategoriesData.h"
#import "CategoryCollectionViewCell.h"
#import "CategoryHeaderReusableView.h"
#import "ImageCollectionViewCell.h"
#import "ImageDetailViewController.h"
Expand All @@ -21,19 +20,13 @@

@interface SearchImagesViewController () <UICollectionViewDelegate, UICollectionViewDataSource, ImageDetailDelegate>

@property (nonatomic, strong) UICollectionView *imagesCollection;
@property (nonatomic, strong) AlbumData *albumData;
@property (nonatomic, strong) NSIndexPath *imageOfInterest;
//@property (nonatomic, strong) NSString *currentSort;
@property (nonatomic, assign) BOOL displayImageTitles;
//@property (nonatomic, strong) UIViewController *hudViewController;

@property (nonatomic, strong) UIBarButtonItem *cancelBarButton;

//@property (nonatomic, assign) BOOL isSelect;
//@property (nonatomic, assign) NSInteger totalNumberOfImages;
//@property (nonatomic, strong) NSMutableArray *selectedImageIds;
//@property (nonatomic, strong) NSMutableArray *touchedImageIds;

@property (nonatomic, assign) kPiwigoSortCategory currentSortCategory;
@property (nonatomic, strong) ImageDetailViewController *imageDetailView;

Expand All @@ -46,17 +39,12 @@ -(instancetype)init
self = [super init];
if(self)
{
self.imageOfInterest = [NSIndexPath indexPathForItem:0 inSection:1];
self.imageOfInterest = [NSIndexPath indexPathForItem:0 inSection:0];

self.albumData = [[AlbumData alloc] initWithCategoryId:kPiwigoSearchCategoryId andQuery:@""];
self.currentSortCategory = [Model sharedInstance].defaultSort;
self.displayImageTitles = [Model sharedInstance].displayImageTitles;

// Initialise selection mode
// self.isSelect = NO;
// self.touchedImageIds = [NSMutableArray new];
// self.selectedImageIds = [NSMutableArray new];

// Collection of images
self.imagesCollection = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:[UICollectionViewFlowLayout new]];
self.imagesCollection.translatesAutoresizingMaskIntoConstraints = NO;
Expand All @@ -66,7 +54,6 @@ -(instancetype)init
self.imagesCollection.delegate = self;

[self.imagesCollection registerClass:[ImageCollectionViewCell class] forCellWithReuseIdentifier:@"ImageCollectionViewCell"];
[self.imagesCollection registerClass:[CategoryCollectionViewCell class] forCellWithReuseIdentifier:@"CategoryCollectionViewCell"];
[self.imagesCollection registerClass:[CategoryHeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CategoryHeader"];
[self.imagesCollection registerClass:[NoImagesHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"NoImagesHeaderCollection"];

Expand Down Expand Up @@ -129,9 +116,6 @@ -(void)searchAndLoadImages
self.albumData.searchQuery = self.searchQuery;
[self.albumData updateImageSort:self.currentSortCategory OnCompletion:^{

// Set navigation bar buttons
// [self updateNavBar];

[self.imagesCollection reloadData];
}];
}
Expand Down Expand Up @@ -253,7 +237,10 @@ -(void)didFinishPreviewOfImageWithId:(NSInteger)imageId
index++;
}
if (index < [self.albumData.images count])
self.imageOfInterest = [NSIndexPath indexPathForItem:index inSection:1];
self.imageOfInterest = [NSIndexPath indexPathForItem:index inSection:0];

// Scroll to cell and highlight it
// [self scrollToHighlightedCell];
}

-(void)didDeleteImage:(PiwigoImageData *)image atIndex:(NSInteger)index
Expand Down

0 comments on commit 3cbab51

Please sign in to comment.